@@ -24,6 +24,7 @@ import (
24
24
"testing"
25
25
"time"
26
26
27
+ "github.com/containernetworking/cni/libcni"
27
28
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
28
29
"github.com/firecracker-microvm/firecracker-go-sdk/fctesting"
29
30
"github.com/sparrc/go-ping"
@@ -238,7 +239,15 @@ func TestNetworkInterfacesValidationFails_BothSpecified(t *testing.T) {
238
239
assert .Error (t , err , "invalid network config with both static and cni configuration did not result in validation error" )
239
240
}
240
241
241
- func TestNetworkMachineCNI (t * testing.T ) {
242
+ func TestNetworkMachineCNIWithConfFile (t * testing.T ) {
243
+ testNetworkMachineCNI (t , true )
244
+ }
245
+
246
+ func TestNetworkMachineCNIWithParsedConfig (t * testing.T ) {
247
+ testNetworkMachineCNI (t , false )
248
+ }
249
+
250
+ func testNetworkMachineCNI (t * testing.T , useConfFile bool ) {
242
251
if testing .Short () {
243
252
t .Skip ()
244
253
}
@@ -281,10 +290,23 @@ func TestNetworkMachineCNI(t *testing.T) {
281
290
]
282
291
}` , networkName )
283
292
293
+ var networkConf * libcni.NetworkConfigList
294
+
284
295
cniConfPath := filepath .Join (cniConfDir , fmt .Sprintf ("%s.conflist" , networkName ))
285
- require .NoError (t ,
286
- ioutil .WriteFile (cniConfPath , []byte (cniConf ), 0666 ), // broad permissions for tests
287
- "failed to write cni conf file" )
296
+ if useConfFile {
297
+ require .NoError (t ,
298
+ ioutil .WriteFile (cniConfPath , []byte (cniConf ), 0666 ), // broad permissions for tests
299
+ "failed to write cni conf file" )
300
+ } else {
301
+ // make sure config file doesn't exist
302
+ err := os .Remove (cniConfPath )
303
+ if err != nil && ! os .IsNotExist (err ) {
304
+ require .NoError (t , err , "failed to delete cni conf file" )
305
+ }
306
+
307
+ networkConf , err = libcni .ConfListFromBytes ([]byte (cniConf ))
308
+ require .NoError (t , err , "cni conf should parse" )
309
+ }
288
310
289
311
numVMs := 10
290
312
vmIPs := make (chan string , numVMs )
@@ -304,7 +326,7 @@ func TestNetworkMachineCNI(t *testing.T) {
304
326
ctx , cancel := context .WithCancel (context .Background ())
305
327
// NewMachine cannot be in the goroutine below, since go-openapi/runtime has a globally-shared mutable logger...
306
328
// https://github.com/go-openapi/runtime/blob/553c9d1fb273d9550562d9f76949a413af265138/client/runtime.go#L463
307
- m := newCNIMachine (t , ctx , firecrackerSockPath , rootfsPath , cniConfDir , cniCacheDir , networkName , ifName , vmID , cniBinPath )
329
+ m := newCNIMachine (t , ctx , firecrackerSockPath , rootfsPath , cniConfDir , cniCacheDir , networkName , ifName , vmID , cniBinPath , networkConf )
308
330
309
331
go func (ctx context.Context , cancel func (), m * Machine , vmID string ) {
310
332
defer vmWg .Done ()
@@ -357,12 +379,17 @@ func newCNIMachine(t *testing.T,
357
379
ifName ,
358
380
vmID string ,
359
381
cniBinPath []string ,
382
+ networkConf * libcni.NetworkConfigList ,
360
383
) * Machine {
361
384
rootfsBytes , err := ioutil .ReadFile (testRootfs )
362
385
require .NoError (t , err , "failed to read rootfs file" )
363
386
err = ioutil .WriteFile (rootfsPath , rootfsBytes , 0666 )
364
387
require .NoError (t , err , "failed to copy vm rootfs to %s" , rootfsPath )
365
388
389
+ if networkConf != nil {
390
+ networkName = ""
391
+ }
392
+
366
393
cmd := VMCommandBuilder {}.
367
394
WithSocketPath (firecrackerSockPath ).
368
395
WithBin (getFirecrackerBinaryPath ()).
@@ -386,12 +413,13 @@ func newCNIMachine(t *testing.T,
386
413
},
387
414
NetworkInterfaces : []NetworkInterface {{
388
415
CNIConfiguration : & CNIConfiguration {
389
- ConfDir : cniConfDir ,
390
- BinPath : cniBinPath ,
391
- CacheDir : cniCacheDir ,
392
- NetworkName : networkName ,
393
- IfName : ifName ,
394
- VMIfName : "eth0" ,
416
+ ConfDir : cniConfDir ,
417
+ BinPath : cniBinPath ,
418
+ CacheDir : cniCacheDir ,
419
+ NetworkName : networkName ,
420
+ NetworkConfig : networkConf ,
421
+ IfName : ifName ,
422
+ VMIfName : "eth0" ,
395
423
},
396
424
}},
397
425
VMID : vmID ,
0 commit comments