This repository was archived by the owner on Apr 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathconfigdata_test.go
488 lines (461 loc) · 21.9 KB
/
configdata_test.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright 2020 Intel Corp.
//
// 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 configdata
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/containernetworking/cni/pkg/skel"
current "github.com/containernetworking/cni/pkg/types/100"
"github.com/intel/userspace-cni-network-plugin/pkg/types"
"github.com/intel/userspace-cni-network-plugin/userspace/testdata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
)
func TestSaveRemoteConfig(t *testing.T) {
testCases := []struct {
name string
netConf *types.NetConf
kubeClientNil bool
testType string
ipResult *current.Result
brokenDir string
expJson string
expErr error
}{
{
name: "save to pod vhostuser with host mode client and NetConf name",
netConf: &types.NetConf{Name: "Simple NetConf", HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", VhostConf: types.VhostConf{Mode: "client"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"vhost":{"mode":"server"},"bridge":{}},"ipResult":{"dns":{}},"name":"Simple NetConf"}]`,
},
{
name: "save to pod vhostuser with host mode server",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", VhostConf: types.VhostConf{Mode: "server"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"vhost":{"mode":"client"},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod vhostuser with host mode client",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", VhostConf: types.VhostConf{Mode: "client"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"vhost":{"mode":"server"},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod vhostuser with no host mode",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser"}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"vhost":{"mode":"client"},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod vhostuser with both host and ContainerConf mode server",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", VhostConf: types.VhostConf{Mode: "server"}}, ContainerConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", VhostConf: types.VhostConf{Mode: "server"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"engine":"ovs-dpdk","iftype":"vhostuser","memif":{},"vhost":{"mode":"server"},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod vhostuser with ContainerConf mode server and Socketfile override",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", VhostConf: types.VhostConf{Mode: "client", Socketfile: "vhostuser-hostconf.sock"}}, ContainerConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", VhostConf: types.VhostConf{Mode: "server", Socketfile: "vhostuser-containerconf.sock"}}},
// FIXME: possible bug - Socketfile from ContainerConf is overrided by value from HostConf!
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"engine":"ovs-dpdk","iftype":"vhostuser","memif":{},"vhost":{"mode":"server","socketfile":"vhostuser-hostconf.sock"},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod without ContainerConf netType",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", NetType: "bridge", VhostConf: types.VhostConf{Mode: "client"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"vhost":{"mode":"server"},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod with ipResult and without ContainerConf netType",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", NetType: "bridge", VhostConf: types.VhostConf{Mode: "client"}}},
ipResult: ¤t.Result{Interfaces: []*current.Interface{¤t.Interface{Name: "vlan0", Mac: "fe:ed:de:ad:be:ef"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"netType":"interface","vhost":{"mode":"server"},"bridge":{}},"ipResult":{"interfaces":[{"name":"vlan0","mac":"fe:ed:de:ad:be:ef"}],"dns":{}}}]`,
},
{
name: "save to pod with ipResult and with ContainerConf netType set",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", NetType: "bridge", VhostConf: types.VhostConf{Mode: "client"}}, ContainerConf: types.UserSpaceConf{NetType: "bridge"}},
ipResult: ¤t.Result{Interfaces: []*current.Interface{¤t.Interface{Name: "vlan0", Mac: "fe:ed:de:ad:be:ef"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"netType":"bridge","vhost":{"mode":"server"},"bridge":{}},"ipResult":{"interfaces":[{"name":"vlan0","mac":"fe:ed:de:ad:be:ef"}],"dns":{}}}]`,
},
{
name: "save to pod with ContainerConf ifType set",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", NetType: "bridge", VhostConf: types.VhostConf{Mode: "client"}}, ContainerConf: types.UserSpaceConf{IfType: "interface"}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"interface","memif":{}, "vhost":{},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod with ifType memif and no host role",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "memif"}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"memif","memif":{"role":"master"},"vhost":{},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod with ifType memif and ContainerConf role master and socketfile override",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "memif", MemifConf: types.MemifConf{Role: "master", Socketfile: "memif-hostconf.sock"}}, ContainerConf: types.UserSpaceConf{IfType: "memif", MemifConf: types.MemifConf{Role: "master", Socketfile: "memif-memifconf.sock"}}},
// FIXME: possible bug - Socketfile from ContainerConf is overrided by value from HostConf!
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"memif","memif":{"role":"master","socketfile":"memif-hostconf.sock"},"vhost":{},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod with ifType memif and host role master",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "memif", MemifConf: types.MemifConf{Role: "master"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"memif","memif":{"role":"slave"},"vhost":{},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod with ifType memif and host role slave",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "memif", MemifConf: types.MemifConf{Role: "slave"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"memif","memif":{"role":"master"},"vhost":{},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod with ifType memif and host role master and mode ip",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "memif", MemifConf: types.MemifConf{Role: "master", Mode: "ip"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"memif","memif":{"role":"slave","mode":"ip"},"vhost":{},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to pod with ifType memif and ContainerConf role master and mode ethernet",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "memif", MemifConf: types.MemifConf{Role: "slave", Mode: "ip"}}, ContainerConf: types.UserSpaceConf{IfType: "memif", MemifConf: types.MemifConf{Role: "master", Mode: "ethernet"}}},
expJson: `[{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"memif","memif":{"role":"master","mode":"ethernet"},"vhost":{},"bridge":{}},"ipResult":{"dns":{}}}]`,
},
{
name: "save to file",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", NetType: "bridge", VhostConf: types.VhostConf{Mode: "client"}}},
testType: "client_nil",
expJson: `{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"vhost":{"mode":"server"},"bridge":{}},"ipResult":{"dns":{}}}`,
},
{
name: "save to file to newly created shared dir",
netConf: &types.NetConf{HostConf: types.UserSpaceConf{Engine: "ovs-dpdk", IfType: "vhostuser", NetType: "bridge", VhostConf: types.VhostConf{Mode: "client"}}},
testType: "client_nil",
brokenDir: "none",
expJson: `{"containerId":"#UUID#","ifName":"#ifName#","name":"","config":{"iftype":"vhostuser","memif":{},"vhost":{"mode":"server"},"bridge":{}},"ipResult":{"dns":{}}}`,
expErr: nil,
},
{
name: "fail to save file to broken dir",
netConf: &types.NetConf{},
testType: "client_nil",
brokenDir: "proc",
expErr: errors.New("mkdir "),
},
{
name: "fail to save to file pretending to be shared dir",
netConf: &types.NetConf{},
testType: "client_nil",
brokenDir: "file",
expErr: errors.New("stat "),
},
{
name: "fail with netconf set to nil",
netConf: nil,
testType: "netconf_nil",
expErr: errors.New("SaveRemoteConfig(): Error conf is set to: <nil>"),
},
{
name: "fail with args set to nil",
netConf: &types.NetConf{},
testType: "args_nil",
expErr: errors.New("SaveRemoteConfig(): Error args is set to: <nil>"),
},
{
name: "fail with pod set tot nil",
netConf: &types.NetConf{},
testType: "pod_nil",
expErr: errors.New("SaveRemoteConfig(): Error pod is set to: nil"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var kubeClient kubernetes.Interface
var resPod *v1.Pod
var resErr error
sharedDir, dirErr := os.MkdirTemp("/tmp", "test-configdata-")
require.NoError(t, dirErr, "Can't create temporary directory")
// remove sharedDir if needed
defer os.RemoveAll(sharedDir)
switch tc.brokenDir {
case "proc":
sharedDir = "/proc/broken_dir"
case "file":
os.RemoveAll(sharedDir)
_, err := os.Create(sharedDir)
require.NoError(t, err, "Can't create temp file")
// add trailing slash to fail directory check by os.stat()
sharedDir = sharedDir + "/"
case "none":
os.RemoveAll(sharedDir)
}
args := testdata.GetTestArgs()
pod := testdata.GetTestPod(sharedDir)
podOrig := pod.DeepCopy()
kubeClient = fake.NewSimpleClientset(pod)
tc.expJson = strings.Replace(tc.expJson, "#UUID#", args.ContainerID, -1)
tc.expJson = strings.Replace(tc.expJson, "#ifName#", args.IfName, -1)
// NOTE: ipResult set to nil is a valid case tested by several TCs already
switch tc.testType {
case "conf_nil":
resPod, resErr = SaveRemoteConfig(nil, args, kubeClient, sharedDir, pod, tc.ipResult)
case "args_nil":
resPod, resErr = SaveRemoteConfig(tc.netConf, nil, kubeClient, sharedDir, pod, tc.ipResult)
case "client_nil":
resPod, resErr = SaveRemoteConfig(tc.netConf, args, nil, sharedDir, pod, tc.ipResult)
case "pod_nil":
resPod, resErr = SaveRemoteConfig(tc.netConf, args, kubeClient, sharedDir, nil, tc.ipResult)
default:
resPod, resErr = SaveRemoteConfig(tc.netConf, args, kubeClient, sharedDir, pod, tc.ipResult)
}
var data []byte
if tc.expErr == nil {
assert.NoError(t, resErr, "Unexpected error")
if tc.testType == "client_nil" {
// data saved to the file
assert.Equal(t, podOrig, resPod, "Unexpected change of pod data")
assert.Empty(t, resPod.Annotations["userspace/configuration-data"], "Unexpected pod Annotations were found")
fileName := fmt.Sprintf("configData-%s-%s.json", args.ContainerID[:12], args.IfName)
fileName = filepath.Join(sharedDir, fileName)
require.FileExists(t, fileName, "Container data were not saved to file")
var err error
data, err = os.ReadFile(fileName)
require.NoError(t, err, "Can't read saved container data")
} else {
// data saved to resPod Annotations
assert.NotEqual(t, podOrig, resPod, "Pod data shall be modified")
require.NotEmpty(t, resPod.Annotations["userspace/configuration-data"], "Data are not saved to pod Annotations")
data = []byte(resPod.Annotations["userspace/configuration-data"])
}
assert.JSONEq(t, tc.expJson, string(data), fmt.Sprintf("Unexpected result\n%v\n", string(data)))
} else {
require.Error(t, resErr, "Unexpected result")
assert.Contains(t, resErr.Error(), tc.expErr.Error(), "Unexpected result")
}
})
}
}
func TestCleanupRemoteConfig(t *testing.T) {
testCases := []struct {
name string
dir string
expOut string
}{
{
name: "remove empty dir",
dir: "",
},
{
name: "remove dir with content",
dir: "full",
},
{
name: "fail to remove broken dir",
dir: "proc",
expOut: "unlinkat ",
},
{
name: "remove file pretending to be dir",
dir: "file",
},
{
name: "remove dir which doesn't exist",
dir: "none",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tempDir, dirErr := os.MkdirTemp("/tmp", "test-configdata-")
require.NoError(t, dirErr, "Can't create temporary directory")
// remove tempDir if needed
defer os.RemoveAll(tempDir)
switch tc.dir {
case "full":
fileName := filepath.Join(tempDir, "test-configdata.txt")
_, fileErr := os.Create(fileName)
require.NoError(t, fileErr, "Can't create temp file")
case "proc":
os.RemoveAll(tempDir)
tempDir = "/proc/meminfo"
case "file":
os.RemoveAll(tempDir)
_, err := os.Create(tempDir)
require.NoError(t, err, "Can't create temp file")
case "none":
os.RemoveAll(tempDir)
}
stdR, stdW, err := os.Pipe()
require.NoError(t, err, "Can't capture stdout")
origStdOut := os.Stdout
os.Stdout = stdW
CleanupRemoteConfig(nil, tempDir)
os.Stdout = origStdOut
stdW.Close()
var buf bytes.Buffer
_, _ = io.Copy(&buf, stdR)
if tc.expOut != "" {
assert.Contains(t, buf.String(), tc.expOut, "Unexpected standard output")
} else {
assert.NoDirExists(t, tempDir, "Dir was not removed")
assert.NoFileExists(t, tempDir, "Dir was not removed")
}
})
}
}
func TestFileCleanup(t *testing.T) {
testCases := []struct {
name string
directory string
filepath string
noiseFile bool
expErr error
}{
{
name: "remove dir and file",
directory: "#tempDir#",
filepath: "#tempDir#/test-file.txt",
},
{
name: "remove file and keep directory",
directory: "",
filepath: "#tempDir#/test-file.txt",
},
{
name: "remove file and keep directory due to other content",
directory: "#tempDir#",
filepath: "#tempDir#/test-file.txt",
noiseFile: true,
},
{
name: "remove only empty directory",
directory: "#tempDir#",
filepath: "",
},
{
name: "don't remove only directory with data",
directory: "#tempDir#",
filepath: "",
noiseFile: true,
},
{
name: "fail to remove file",
directory: "",
filepath: "/proc/meminfo",
expErr: errors.New("ERROR: Failed to delete file: remove"),
},
{
name: "fail to remove directory but remove file",
directory: "/proc",
filepath: "#tempDir#/test-file.txt",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tempDir, dirErr := os.MkdirTemp("/tmp", "test-configdata-")
require.NoError(t, dirErr, "Can't create temporary directory")
// remove tempDir if needed
defer os.RemoveAll(tempDir)
tc.directory = strings.Replace(tc.directory, "#tempDir#", tempDir, -1)
tc.filepath = strings.Replace(tc.filepath, "#tempDir#", tempDir, -1)
if tc.filepath != "" {
if _, err := os.Stat(tc.filepath); err != nil {
_, fileErr := os.Create(tc.filepath)
require.NoError(t, fileErr, "Can't create test file")
}
}
if tc.noiseFile {
noiseFile := filepath.Join(tempDir, "noise-file.txt")
_, fileErr := os.Create(noiseFile)
require.NoError(t, fileErr, "Can't create temp file")
}
err := FileCleanup(tc.directory, tc.filepath)
if tc.expErr != nil {
require.Error(t, err, "Error was expected")
assert.Contains(t, err.Error(), tc.expErr.Error(), "Unexpected error")
} else {
require.NoError(t, err, "Unexpected error")
if !(tc.directory == "" || tc.noiseFile || strings.Contains(tc.directory, "/proc")) {
assert.NoDirExists(t, tempDir, "Directory was not removed")
} else {
assert.DirExists(t, tempDir, "Directory was removed")
}
if tc.filepath != "" {
assert.NoFileExists(t, filepath.Join(tempDir, tc.filepath), "File was not removed")
}
}
})
}
}
func TestGetRemoteConfig(t *testing.T) {
testCases := []struct {
name string
annotations string
expErr error
expDir string
expResult []*InterfaceData
}{
{
name: "get config for one interface",
annotations: `userspace/mapped-dir=#tempDir# userspace/configuration-data="[{\"Name\":\"Container New Name\",\"containerId\":\"123-456-789-007\",\"ifName\":\"eth7\",\"config\":{\"iftype\":\"memif\",\"memif\":{\"role\":\"master\"}}}]"`,
expDir: "#tempDir#",
expResult: []*InterfaceData{&InterfaceData{Args: skel.CmdArgs{ContainerID: "123-456-789-007", IfName: "eth7"}, NetConf: types.NetConf{Name: "Container New Name", HostConf: types.UserSpaceConf{IfType: "memif", MemifConf: types.MemifConf{Role: "master"}}}}},
},
{
name: "get config for two interfaces",
annotations: `userspace/mapped-dir=#tempDir# userspace/configuration-data="[{\"Name\":\"Container New Name\",\"containerId\":\"123-456-789-007\",\"ifName\":\"eth7\",\"config\":{\"iftype\":\"memif\",\"memif\":{\"role\":\"master\"}}},{\"Name\":\"Container New Name\",\"containerId\":\"123-456-789-007\",\"ifName\":\"eth9\",\"config\":{\"iftype\":\"memif\",\"memif\":{\"role\":\"slave\"}}}]"`,
expDir: "#tempDir#",
expResult: []*InterfaceData{&InterfaceData{Args: skel.CmdArgs{ContainerID: "123-456-789-007", IfName: "eth7"}, NetConf: types.NetConf{Name: "Container New Name", HostConf: types.UserSpaceConf{IfType: "memif", MemifConf: types.MemifConf{Role: "master"}}}}, &InterfaceData{Args: skel.CmdArgs{ContainerID: "123-456-789-007", IfName: "eth9"}, NetConf: types.NetConf{Name: "Container New Name", HostConf: types.UserSpaceConf{IfType: "memif", MemifConf: types.MemifConf{Role: "slave"}}}}},
},
{
name: "get config for two containers",
annotations: `userspace/mapped-dir=#tempDir# userspace/configuration-data="[{\"Name\":\"init-container\",\"containerId\":\"123-456-789-007\",\"ifName\":\"eth7\",\"config\":{\"iftype\":\"memif\",\"memif\":{\"role\":\"master\"}}},{\"Name\":\"worker container\",\"containerId\":\"123-456-789-042\",\"ifName\":\"vlan0\",\"config\":{\"iftype\":\"memif\",\"memif\":{\"role\":\"slave\"}}}]"`,
expDir: "#tempDir#",
expResult: []*InterfaceData{&InterfaceData{Args: skel.CmdArgs{ContainerID: "123-456-789-007", IfName: "eth7"}, NetConf: types.NetConf{Name: "init-container", HostConf: types.UserSpaceConf{IfType: "memif", MemifConf: types.MemifConf{Role: "master"}}}}, &InterfaceData{Args: skel.CmdArgs{ContainerID: "123-456-789-042", IfName: "vlan0"}, NetConf: types.NetConf{Name: "worker container", HostConf: types.UserSpaceConf{IfType: "memif", MemifConf: types.MemifConf{Role: "slave"}}}}},
},
{
name: "fail without annotation file",
annotations: "",
expErr: errors.New("error reading "),
},
{
name: "fail without config data",
annotations: "userspace/mapped-dir=#tempDir#",
expErr: errors.New(`ERROR: "userspace/configuration-data" missing from pod annotation`),
},
{
name: "fail without mapped dir",
annotations: "userspace/no-mappedddir=#tempDir#",
expDir: "#tempDir#",
expErr: errors.New(`ERROR: "userspace/mapped-dir" missing from pod annotation`),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tempDir, dirErr := os.MkdirTemp("/tmp", "test-configdata-")
require.NoError(t, dirErr, "Can't create temporary directory")
// remove tempDir if needed
defer os.RemoveAll(tempDir)
annotFile := filepath.Join(tempDir, "annotations")
if tc.annotations != "" {
tc.annotations = strings.Replace(tc.annotations, "#tempDir#", tempDir, -1)
_ = os.WriteFile(annotFile, []byte(tc.annotations), 0644)
}
tc.expDir = strings.Replace(tc.expDir, "#tempDir#", tempDir, -1)
ifcData, mappedDir, err := GetRemoteConfig(annotFile)
if tc.expErr != nil {
require.Error(t, err, "Error was expected")
assert.Contains(t, err.Error(), tc.expErr.Error(), "Unexpected error")
} else {
require.NoError(t, err, "Unexpected error")
assert.Equal(t, tc.expDir, mappedDir, "Unexpected result")
require.NotNil(t, ifcData, "Unexpected result")
assert.Equal(t, tc.expResult, ifcData, "Unexpected result")
}
})
}
}