/
utils.go
440 lines (377 loc) · 13.8 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/contiv/netplugin/core"
"github.com/contiv/netplugin/drivers"
"github.com/contiv/netplugin/netmaster/intent"
u "github.com/contiv/netplugin/utils"
log "github.com/Sirupsen/logrus"
)
const (
examplesDir = "/src/github.com/contiv/netplugin/examples/"
)
// OkToCleanup tests if a testbed cleanup should be performed.
func OkToCleanup(testFailed bool) bool {
// don't cleanup if stop-on-error is set
if os.Getenv("CONTIV_SOE") != "" && testFailed {
return false
}
return true
}
// StopOnError stops the test and panics if CONTIV_SOE env-variable is set and test has failed
func StopOnError(testFailed bool) {
if os.Getenv("CONTIV_SOE") != "" && testFailed {
panic("Stopping tests as stop on error was set. Please check test logs to determine the actual failure. The system is left in same state for debugging.")
}
}
// ConfigCleanupCommon performs common cleanup after each test
func ConfigCleanupCommon(t *testing.T, nodes []TestbedNode) {
if !OkToCleanup(t.Failed()) {
return
}
for _, node := range nodes {
cmdStr := "sh -c 'sudo $GOSRC/github.com/contiv/netplugin/scripts/cleanup'"
output, err := node.RunCommandWithOutput(cmdStr)
if err != nil {
t.Errorf("Failed to cleanup the left over test case state. Error: %s\nCmd: %q\nOutput:\n%s\n",
err, cmdStr, output)
}
}
//XXX: remove this once netplugin is capable of handling cleanup
StopNetPlugin(t, nodes)
StopNetmaster(t, nodes[0])
}
// StopNetPlugin stops the netplugin on specified testbed nodes
func StopNetPlugin(t *testing.T, nodes []TestbedNode) {
for _, node := range nodes {
cmdStr := "sudo pkill netplugin"
node.RunCommand(cmdStr)
}
}
// StopNetmaster stops the netmaster on specified testbed node
func StopNetmaster(t *testing.T, node TestbedNode) {
cmdStr := "sudo pkill netmaster"
node.RunCommand(cmdStr)
}
// StartNetPluginWithConfig starts netplugin on specified testbed nodes with specified config
func StartNetPluginWithConfig(t *testing.T, nodes []TestbedNode, nativeInteg bool,
configStr string) {
nativeIntegStr := ""
if nativeInteg {
nativeIntegStr = "-native-integration"
}
for i, node := range nodes {
//start the netplugin
var (
cmdStr string
flagsStr string
)
if configStr != "" {
cfgFile := fmt.Sprintf("/tmp/plugin-%d.cfg", i+1)
//fill up the host-label in the passed config string format
jsonCfg := fmt.Sprintf(configStr, i+1)
jsonCfg = getEchoCompatibleStr(jsonCfg)
cmdStr := fmt.Sprintf("echo \"%s\" > %s", jsonCfg, cfgFile)
output, err := node.RunCommandWithOutput("sh -c '" + cmdStr + "'")
if err != nil {
t.Fatalf("Error '%s' creating config file\nCmd: %q\n Output : %s \n",
err, cmdStr, output)
}
flagsStr = fmt.Sprintf("-config %s %s", cfgFile, nativeIntegStr)
} else {
flagsStr = fmt.Sprintf("-host-label host%d %s", i+1, nativeIntegStr)
}
if os.Getenv("CONTIV_TESTBED") == "DIND" {
tCmd := &TestCommand{}
cmdStr = "sudo docker version | grep 'Server version' | awk '{print $3}'"
output, err := tCmd.RunWithOutput("sh", "-c", cmdStr)
if err != nil {
t.Fatalf("Failed to determine docker version. Error: %s\nCmd:%q\n Output : %s\n",
err, cmdStr, output)
}
output = []byte(strings.Trim(string(output), " \n"))
t.Logf("Docker version: %q", output)
if bytes.Compare(output, []byte("1.6.0")) > 0 {
// for docker version greater than 1.6.0 add the --force-delete-ep flag
flagsStr = fmt.Sprintf("%s --force-delete-ep=true", flagsStr)
}
cmdStr = fmt.Sprintf("netplugin %s 1>/tmp/netplugin-%s.log 2>&1",
flagsStr, time.Now().Format("15:04:05.999999999"))
} else {
cmdStr = fmt.Sprintf("sudo PATH=$PATH nohup netplugin %s 0<&- &>/tmp/netplugin-%s.log",
flagsStr, time.Now().Format("15:04:05.999999999"))
}
output, err := node.RunCommandBackground(cmdStr)
if err != nil {
t.Fatalf("Failed to launch netplugin. Error: %s\nCmd:%q\n Output : %s\n",
err, cmdStr, output)
}
}
time.Sleep(3 * time.Second)
}
// StartNetPlugin starts netplugin on specified testbed nodes
func StartNetPlugin(t *testing.T, nodes []TestbedNode, nativeInteg bool) {
StartNetPluginWithConfig(t, nodes, nativeInteg, "")
}
// StartNetmasterWithFlags starts netplugin on specified testbed nodes with specified flags
func StartNetmasterWithFlags(t *testing.T, node TestbedNode, flags map[string]string) {
var (
cmdStr string
flagsStr string
)
for k, v := range flags {
flagsStr += fmt.Sprintf("%s=%s", k, v)
}
if os.Getenv("CONTIV_TESTBED") == "DIND" {
cmdStr = fmt.Sprintf("netmaster %s 1>/tmp/netmaster.log 2>&1", flagsStr)
} else {
cmdStr = fmt.Sprintf("nohup netmaster %s 0<&- &>/tmp/netmaster.log", flagsStr)
}
output, err := node.RunCommandBackground(cmdStr)
if err != nil {
t.Fatalf("Failed to launch netplugin. Error: %s\nCmd:%q\n Output : %s\n",
err, cmdStr, output)
}
}
// StartNetmaster starts netplugin on specified testbed node
func StartNetmaster(t *testing.T, node TestbedNode) {
StartNetmasterWithFlags(t, node, map[string]string{})
}
func getEchoCompatibleStr(inStr string) string {
// replace newlines with space and "(quote) with \"(escaped quote) for
// echo to consume and produce desired json config
return strings.Replace(strings.Replace(inStr, "\n", " ", -1), "\"", "\\\"", -1)
}
func applyConfig(t *testing.T, cfgType, jsonCfg string, node TestbedNode, stateStore string) {
// replace newlines with space and "(quote) with \"(escaped quote) for
// echo to consume and produce desired json config
jsonCfg = getEchoCompatibleStr(jsonCfg)
cmdStr := fmt.Sprintf("echo \"%s\" > /tmp/netdcli.cfg", jsonCfg)
output, err := node.RunCommandWithOutput("sh -c '" + cmdStr + "'")
if err != nil {
t.Fatalf("Error '%s' creating config file\nCmd: %q\n Output : %s \n",
err, cmdStr, output)
}
cmdStr = "netdcli -" + cfgType + " /tmp/netdcli.cfg 2>&1"
if stateStore != "" {
cmdStr = "netdcli -state-store " + stateStore + " -" + cfgType + " /tmp/netdcli.cfg 2>&1"
}
output, err = node.RunCommandWithOutput(cmdStr)
if err != nil {
t.Fatalf("Failed to apply config. Error: %s\nCmd: %q\n Output : %s\n",
err, cmdStr, output)
}
}
// AddConfig issues netdcli with -add-cfg flag
func AddConfig(t *testing.T, jsonCfg string, node TestbedNode) {
applyConfig(t, "add-cfg", jsonCfg, node, "")
}
// AddConfigConsul issues netdcli with -add-cfg flag and uses consul state-store
func AddConfigConsul(t *testing.T, jsonCfg string, node TestbedNode) {
applyConfig(t, "add-cfg", jsonCfg, node, u.ConsulNameStr)
}
// DelConfig issues netdcli with -del-cfg flag
func DelConfig(t *testing.T, jsonCfg string, node TestbedNode) {
applyConfig(t, "del-cfg", jsonCfg, node, "")
}
// DelConfigConsul issues netdcli with -del-cfg flag and uses consul state-store
func DelConfigConsul(t *testing.T, jsonCfg string, node TestbedNode) {
applyConfig(t, "del-cfg", jsonCfg, node, u.ConsulNameStr)
}
// ApplyDesiredConfig issues netdcli with -cfg flag
func ApplyDesiredConfig(t *testing.T, jsonCfg string, node TestbedNode) {
applyConfig(t, "cfg", jsonCfg, node, "")
}
// ApplyDesiredConfigConsul issues netdcli with -cfg flag and uses consul state-store
func ApplyDesiredConfigConsul(t *testing.T, jsonCfg string, node TestbedNode) {
applyConfig(t, "cfg", jsonCfg, node, u.ConsulNameStr)
}
// ApplyHostBindingsConfig issues netdcli with -host-bindings-cfg flag
func ApplyHostBindingsConfig(t *testing.T, jsonCfg string, node TestbedNode) {
applyConfig(t, "host-bindings-cfg", jsonCfg, node, "")
}
// FixUpContainerUUIDs fills up UUID information in passed jsonCfg and returns host-binding configuration
func FixUpContainerUUIDs(t *testing.T, nodes []TestbedNode, jsonCfg string) (string, error) {
epBindings := []intent.ConfigEP{}
err := json.Unmarshal([]byte(jsonCfg), &epBindings)
if err != nil {
t.Fatalf("error '%s' unmarshing host bindings, data %s \n", err,
jsonCfg)
return "", err
}
// fill in as much as possible for this host; assume that the
// container name is unique across hosts
for _, node := range nodes {
for idx := range epBindings {
ep := &epBindings[idx]
if ep.AttachUUID != "" {
continue
}
attachUUID, _ := getContainerUUID(node, ep.Container)
if attachUUID != "" {
ep.AttachUUID = attachUUID
}
}
}
bytes, err := json.Marshal(epBindings)
if err != nil {
t.Fatalf("error '%s' marshaling host bindings, data %v \n",
err, epBindings)
return "", err
}
return string(bytes[:]), err
}
// FixUpInfraContainerUUIDs fills up UUID information in passed jsonCfg and returns host-binding configuration
func FixUpInfraContainerUUIDs(t *testing.T, nodes []TestbedNode, jsonCfg, infraContCfg string) (string, error) {
epBindings := []intent.ConfigEP{}
err := json.Unmarshal([]byte(jsonCfg), &epBindings)
if err != nil {
t.Fatalf("error '%s' unmarshing host bindings, data %s \n", err,
jsonCfg)
return "", err
}
infraContMap := make(map[string]string)
infraContCfg = strings.TrimSpace(infraContCfg)
infraContRecords := strings.Split(infraContCfg, "\n")
for _, infraContRecord := range infraContRecords {
fields := strings.Split(infraContRecord, ":")
if len(fields) != 2 {
t.Fatalf("error parsing the container mappings cfg '%s' rec '%s'\n",
infraContCfg, infraContRecord)
}
infraContMap[fields[0]] = fields[1]
}
// fill in as much as possible for this host; assume that the
// container name is unique across hosts
for _, node := range nodes {
for idx := range epBindings {
ep := &epBindings[idx]
if ep.AttachUUID != "" {
continue
}
infraContName, ok := infraContMap[ep.Container]
if !ok {
continue
}
attachUUID, _ := getContainerUUID(node, infraContName)
if attachUUID != "" {
ep.AttachUUID = attachUUID
}
}
}
bytes, err := json.Marshal(epBindings)
if err != nil {
t.Fatalf("error '%s' marshaling host bindings, data %v \n",
err, epBindings)
return "", err
}
return string(bytes[:]), err
}
// ConfigSetupCommon performs common configuration setup on specified testbed nodes
func ConfigSetupCommon(t *testing.T, jsonCfg string, nodes []TestbedNode) {
StartNetmaster(t, nodes[0])
StartNetPlugin(t, nodes, false)
ApplyDesiredConfig(t, jsonCfg, nodes[0])
}
// GetNetpluginConfigWithConsul returns netplugin config that uses consul state store
func GetNetpluginConfigWithConsul() string {
return `{
"drivers" : {
"network": "ovs",
"state": "consul"
},
"plugin-instance": {
"host-label": "host%d"
},
"ovs" : {
"dbip": "127.0.0.1",
"dbport": 6640
},
"consul" : {
"address": "127.0.0.1:8500"
},
"crt" : {
"type": "docker"
},
"docker" : {
"socket" : "unix:///var/run/docker.sock"
}
}`
}
// ConfigSetupCommonWithConsul performs common configuration setup on specified testbed nodes
func ConfigSetupCommonWithConsul(t *testing.T, jsonCfg string, nodes []TestbedNode) {
StartNetmasterWithFlags(t, nodes[0], map[string]string{
"--state-store": "consul"})
StartNetPluginWithConfig(t, nodes, false, GetNetpluginConfigWithConsul())
ApplyDesiredConfigConsul(t, jsonCfg, nodes[0])
}
// GetIPAddress returns IP-address information for specified endpoint
func GetIPAddress(t *testing.T, node TestbedNode, ep, stateStore string) string {
cmdStr := "netdcli -oper get -construct endpoint " + ep + " 2>&1"
if stateStore != "" {
cmdStr = "netdcli -oper get -state-store " + stateStore + " -construct endpoint " + ep + " 2>&1"
}
output, err := node.RunCommandWithOutput(cmdStr)
if err != nil || string(output) == "" {
time.Sleep(2 * time.Second)
output, err = node.RunCommandWithOutput(cmdStr)
if err != nil || output == "" {
t.Fatalf("Error getting ip for ep %s. Error: %s, Cmdstr: %s, Output: \n%s\n",
err, ep, cmdStr, output)
}
}
output = strings.Trim(string(output), "[]")
epStruct := drivers.OvsOperEndpointState{}
if err := json.Unmarshal([]byte(output), &epStruct); err != nil {
t.Fatalf("Error getting ip for ep %s. Error: %s, Cmdstr: %s, Output: \n%s\n",
err, ep, cmdStr, output)
}
return epStruct.IPAddress
}
// NetworkStateExists tests if state for specified network exists
func NetworkStateExists(node TestbedNode, network, stateStore string) error {
cmdStr := "netdcli -oper get -construct network " + network + " 2>&1"
if stateStore != "" {
cmdStr = "netdcli -state-store " + stateStore + "-oper get -construct network " + network + " 2>&1"
}
output, err := node.RunCommandWithOutput(cmdStr)
if err != nil {
return err
}
if string(output) == "" {
return core.Errorf("got null output")
}
return nil
}
// DumpNetpluginLogs prints netplugin logs from the specified testbed node
func DumpNetpluginLogs(node TestbedNode) {
cmdStr := fmt.Sprintf("sudo cat /tmp/netplugin.log")
output, err := node.RunCommandWithOutput(cmdStr)
if err == nil {
log.Debugf("logs on node %s: \n%s\n", node.GetName(), output)
}
}
// GetCfgFile returns the path string for specified file name in examples directory
func GetCfgFile(fileName string) string {
cfgDir := os.Getenv("GOPATH")
cfgDir = cfgDir + examplesDir
return cfgDir + fileName + ".json"
}