@@ -12,8 +12,6 @@ import (
12
12
"sync"
13
13
"testing"
14
14
"time"
15
-
16
- "github.com/google/uuid"
17
15
)
18
16
19
17
type ClusterInput struct {
@@ -35,77 +33,41 @@ type Cluster struct {
35
33
}
36
34
37
35
type Node struct {
38
- ID string `json:"id"`
39
- Name string `json:"name"`
36
+ ID string `json:"id"`
37
+ Name string `json:"name"`
38
+ NetworkID string `json:"network_id"`
40
39
41
40
privateIP string `json:"-"`
42
41
sshEndpoint string `json:"-"`
43
42
adminConsoleURL string `json:"-"`
44
43
}
45
44
46
45
type Network struct {
47
- ID string `json:"id"`
48
- Name string `json:"name"`
46
+ ID string `json:"id"`
49
47
}
50
48
51
49
func NewCluster (in * ClusterInput ) * Cluster {
52
50
c := & Cluster {t : in .T , supportBundleNodeIndex : in .SupportBundleNodeIndex }
53
51
c .t .Cleanup (c .Destroy )
54
52
55
- c .Nodes = make ([]Node , in .Nodes )
56
-
57
- network , err := NewNetwork (in )
53
+ nodes , err := NewNodes (in )
58
54
if err != nil {
59
- in .T .Fatalf ("failed to create network: %v" , err )
60
- }
61
- c .network = network
62
-
63
- for i := range c .Nodes {
64
- node , err := NewNode (in , i , network .ID )
65
- if node != nil {
66
- c .Nodes [i ] = * node
67
- }
68
- if err != nil {
69
- in .T .Fatalf ("create node %d: %v" , i , err )
70
- }
71
- in .T .Logf ("node%d created with ID %s" , i , node .ID )
55
+ in .T .Fatalf ("failed to create nodes: %v" , err )
72
56
}
57
+ in .T .Logf ("cluster created with network ID %s" , nodes [0 ].NetworkID )
58
+ c .Nodes = nodes
59
+ c .network = & Network {ID : nodes [0 ].NetworkID }
73
60
74
61
return c
75
62
}
76
63
77
- func NewNetwork (in * ClusterInput ) (* Network , error ) {
78
- name := fmt .Sprintf ("ec-e2e-%s" , uuid .New ().String ())
79
- in .T .Logf ("creating network %s" , name )
80
-
81
- output , err := exec .Command ("replicated" , "network" , "create" , "--name" , name , "--wait" , "5m" , "-ojson" ).Output () // stderr can break json parsing
82
- if err != nil {
83
- if exitErr , ok := err .(* exec.ExitError ); ok {
84
- return nil , fmt .Errorf ("create network %s: %w: stderr: %s: stdout: %s" , name , err , string (exitErr .Stderr ), string (output ))
85
- }
86
- return nil , fmt .Errorf ("create network %s: %w: stdout: %s" , name , err , string (output ))
87
- }
88
-
89
- var networks []Network
90
- if err := json .Unmarshal (output , & networks ); err != nil {
91
- return nil , fmt .Errorf ("parse networks output: %v: %s" , err , string (output ))
92
- }
93
- if len (networks ) != 1 {
94
- return nil , fmt .Errorf ("expected 1 network, got %d" , len (networks ))
95
- }
96
- network := & networks [0 ]
97
- in .T .Logf ("Network created with ID %s" , network .ID )
98
- return network , nil
99
- }
100
-
101
- func NewNode (in * ClusterInput , index int , networkID string ) (node * Node , err error ) {
102
- nodeName := fmt .Sprintf ("node%d" , index )
103
- in .T .Logf ("creating node %s" , nodeName )
64
+ func NewNodes (in * ClusterInput ) ([]Node , error ) {
65
+ in .T .Logf ("creating %s nodes" , strconv .Itoa (in .Nodes ))
104
66
105
67
args := []string {
106
68
"vm" , "create" ,
107
- "--name" , nodeName ,
108
- "--network " , networkID ,
69
+ "--name" , "ec-test-suite" ,
70
+ "--count " , strconv . Itoa ( in . Nodes ) ,
109
71
"--wait" , "5m" ,
110
72
"-ojson" ,
111
73
}
@@ -128,54 +90,54 @@ func NewNode(in *ClusterInput, index int, networkID string) (node *Node, err err
128
90
output , err := exec .Command ("replicated" , args ... ).Output () // stderr can break json parsing
129
91
if err != nil {
130
92
if exitErr , ok := err .(* exec.ExitError ); ok {
131
- return nil , fmt .Errorf ("create node %s : %w: stderr: %s: stdout: %s" , nodeName , err , string (exitErr .Stderr ), string (output ))
93
+ return nil , fmt .Errorf ("create nodes : %w: stderr: %s: stdout: %s" , err , string (exitErr .Stderr ), string (output ))
132
94
}
133
- return nil , fmt .Errorf ("create node %s : %w: stdout: %s" , nodeName , err , string (output ))
95
+ return nil , fmt .Errorf ("create nodes : %w: stdout: %s" , err , string (output ))
134
96
}
135
97
136
98
var nodes []Node
137
99
if err := json .Unmarshal (output , & nodes ); err != nil {
138
100
return nil , fmt .Errorf ("unmarshal node: %v: %s" , err , string (output ))
139
101
}
140
- if len (nodes ) != 1 {
141
- return nil , fmt .Errorf ("expected 1 node, got %d" , len (nodes ))
142
- }
143
- node = & nodes [0 ]
144
102
145
103
// TODO (@salah): remove this once the bug is fixed in CMX
146
104
// note: the vm gets marked as ready before the services are actually running
147
105
time .Sleep (30 * time .Second )
148
106
149
- sshEndpoint , err := getSSHEndpoint (node .ID )
150
- if err != nil {
151
- return node , fmt .Errorf ("get ssh endpoint for node %s: %v" , nodeName , err )
152
- }
153
- node .sshEndpoint = sshEndpoint
107
+ for i := range nodes {
108
+ sshEndpoint , err := getSSHEndpoint (nodes [i ].ID )
109
+ if err != nil {
110
+ return nil , fmt .Errorf ("get ssh endpoint for node %s: %v" , nodes [i ].ID , err )
111
+ }
112
+ nodes [i ].sshEndpoint = sshEndpoint
154
113
155
- privateIP , err := discoverPrivateIP (* node )
156
- if err != nil {
157
- return node , fmt .Errorf ("discover node private IP: %v" , err )
158
- }
159
- node .privateIP = privateIP
114
+ privateIP , err := discoverPrivateIP (nodes [ i ] )
115
+ if err != nil {
116
+ return nil , fmt .Errorf ("discover node private IP: %v" , err )
117
+ }
118
+ nodes [ i ] .privateIP = privateIP
160
119
161
- if err := ensureAssetsDir (* node ); err != nil {
162
- return node , fmt .Errorf ("ensure assets dir on node %s: %v" , node . Name , err )
163
- }
120
+ if err := ensureAssetsDir (nodes [ i ] ); err != nil {
121
+ return nil , fmt .Errorf ("ensure assets dir on node %s: %v" , nodes [ i ]. ID , err )
122
+ }
164
123
165
- if err := copyScriptsToNode (* node ); err != nil {
166
- return node , fmt .Errorf ("copy scripts to node %s: %v" , node . Name , err )
167
- }
124
+ if err := copyScriptsToNode (nodes [ i ] ); err != nil {
125
+ return nil , fmt .Errorf ("copy scripts to node %s: %v" , nodes [ i ]. ID , err )
126
+ }
168
127
169
- if index == 0 {
170
- in .T .Logf ("exposing port 30003 on node %s" , node .Name )
171
- hostname , err := exposePort (* node , "30003" )
172
- if err != nil {
173
- return node , fmt .Errorf ("expose port: %v" , err )
128
+ if in .Nodes == 1 {
129
+ in .T .Logf ("exposing port 30003 on node %s" , nodes [i ].ID )
130
+ hostname , err := exposePort (nodes [i ], "30003" )
131
+ if err != nil {
132
+ return nil , fmt .Errorf ("expose port: %v" , err )
133
+ }
134
+ nodes [i ].adminConsoleURL = fmt .Sprintf ("http://%s" , hostname )
174
135
}
175
- node .adminConsoleURL = fmt .Sprintf ("http://%s" , hostname )
136
+
137
+ in .T .Logf ("node %d created with ID %s and private IP %s" , i , nodes [i ].ID , nodes [i ].privateIP )
176
138
}
177
139
178
- return node , nil
140
+ return nodes , nil
179
141
}
180
142
181
143
func discoverPrivateIP (node Node ) (string , error ) {
@@ -344,7 +306,7 @@ func (c *Cluster) removeNode(node Node) {
344
306
func (c * Cluster ) removeNetwork (network Network ) {
345
307
output , err := exec .Command ("replicated" , "network" , "rm" , network .ID ).CombinedOutput ()
346
308
if err != nil {
347
- c .t .Logf ("failed to destroy network %s: %v: %s" , network .Name , err , string (output ))
309
+ c .t .Logf ("failed to destroy network %s: %v: %s" , network .ID , err , string (output ))
348
310
}
349
311
}
350
312
0 commit comments