@@ -18,6 +18,7 @@ package cni
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"net"
22
23
"testing"
23
24
@@ -292,21 +293,19 @@ func TestLibCNIType120(t *testing.T) {
292
293
// Get the default CNI config
293
294
l := defaultCNIConfig ()
294
295
// Create a fake cni config directory and file
295
- cniDir , confDir := makeFakeCNIConfig (t )
296
+ cniDir , confDir := buildFakeConfig (t )
296
297
defer tearDownCNIConfig (t , cniDir )
297
298
l .pluginConfDir = confDir
298
299
// Set the minimum network count as 2 for this test
299
300
l .networkCount = 2
300
- err := l .Load (WithAllConf )
301
- assert .NoError (t , err )
302
-
303
- err = l .Status ()
301
+ err := l .Load (WithLoNetwork , WithDefaultConf )
304
302
assert .NoError (t , err )
305
303
306
304
mockCNI := & MockCNI {}
305
+ l .cniConfig = mockCNI
307
306
l .networks [0 ].cni = mockCNI
308
307
l .networks [1 ].cni = mockCNI
309
- ipv4 , err := types .ParseCIDR ("10.0 .0.1/24 " )
308
+ ipv4 , err := types .ParseCIDR ("10.88 .0.1/16 " )
310
309
assert .NoError (t , err )
311
310
expectedRT := & cnilibrary.RuntimeConf {
312
311
ContainerID : "container-id1" ,
@@ -315,59 +314,65 @@ func TestLibCNIType120(t *testing.T) {
315
314
Args : [][2 ]string (nil ),
316
315
CapabilityArgs : map [string ]interface {}{},
317
316
}
318
- mockCNI .On ("AddNetworkList" , l .networks [0 ].config , expectedRT ).Return (& types100.Result {
319
- CNIVersion : "1.1.0" ,
320
- Interfaces : []* types100.Interface {
317
+
318
+ loRT := & cnilibrary.RuntimeConf {
319
+ ContainerID : "container-id1" ,
320
+ NetNS : "/proc/12345/ns/net" ,
321
+ IfName : "lo" ,
322
+ Args : [][2 ]string (nil ),
323
+ CapabilityArgs : map [string ]interface {}{},
324
+ }
325
+
326
+ // mock for loopback
327
+ mockCNI .On ("GetStatusNetworkList" , l .networks [0 ].config ).Return (nil )
328
+ mockCNI .On ("AddNetworkList" , l .networks [0 ].config , loRT ).Return (& types040.Result {
329
+ CNIVersion : "0.3.1" ,
330
+ Interfaces : []* types040.Interface {
321
331
{
322
- Name : "eth0 " ,
332
+ Name : "lo " ,
323
333
},
324
334
},
325
- IPs : []* types100 .IPConfig {
335
+ IPs : []* types040 .IPConfig {
326
336
{
327
- Interface : types100 .Int (0 ),
328
- Address : * ipv4 ,
329
- Gateway : net .ParseIP ("10.0.0.255" ),
337
+ Interface : types040 .Int (0 ),
338
+ Address : net.IPNet {
339
+ IP : net .ParseIP ("127.0.0.1" ),
340
+ },
330
341
},
331
342
},
332
343
}, nil )
333
- mockCNI .On ("DelNetworkList" , l .networks [0 ].config , expectedRT ).Return (nil )
334
- mockCNI .On ("CheckNetworkList" , l .networks [0 ].config , expectedRT ).Return (nil )
335
- ipv4 , err = types .ParseCIDR ("10.0.0.2/24" )
336
- assert .NoError (t , err )
337
- l .networks [1 ].cni = mockCNI
338
- expectedRT = & cnilibrary.RuntimeConf {
339
- ContainerID : "container-id1" ,
340
- NetNS : "/proc/12345/ns/net" ,
341
- IfName : "eth1" ,
342
- Args : [][2 ]string (nil ),
343
- CapabilityArgs : map [string ]interface {}{},
344
- }
344
+ mockCNI .On ("DelNetworkList" , l .networks [0 ].config , loRT ).Return (nil )
345
+ mockCNI .On ("CheckNetworkList" , l .networks [0 ].config , loRT ).Return (nil )
346
+
347
+ // mock for primary cni
348
+ mockCNI .On ("GetStatusNetworkList" , l .networks [1 ].config ).Return (nil )
345
349
mockCNI .On ("AddNetworkList" , l .networks [1 ].config , expectedRT ).Return (& types100.Result {
346
350
CNIVersion : "1.1.0" ,
347
351
Interfaces : []* types100.Interface {
348
352
{
349
- Name : "eth1 " ,
353
+ Name : "eth0 " ,
350
354
},
351
355
},
352
356
IPs : []* types100.IPConfig {
353
357
{
354
358
Interface : types100 .Int (0 ),
355
359
Address : * ipv4 ,
356
- Gateway : net .ParseIP ("10.0 .0.2 " ),
360
+ Gateway : net .ParseIP ("10.88 .0.1 " ),
357
361
},
358
362
},
359
363
}, nil )
360
364
mockCNI .On ("DelNetworkList" , l .networks [1 ].config , expectedRT ).Return (nil )
361
365
mockCNI .On ("CheckNetworkList" , l .networks [1 ].config , expectedRT ).Return (nil )
362
366
ctx := context .Background ()
367
+
368
+ err = l .Status ()
369
+ assert .NoError (t , err )
370
+
363
371
r , err := l .Setup (ctx , "container-id1" , "/proc/12345/ns/net" )
364
372
assert .NoError (t , err )
365
373
assert .Contains (t , r .Interfaces , "eth0" )
366
374
assert .NotNil (t , r .Interfaces ["eth0" ].IPConfigs )
367
- assert .Equal (t , r .Interfaces ["eth0" ].IPConfigs [0 ].IP .String (), "10.0.0.1" )
368
- assert .Contains (t , r .Interfaces , "eth1" )
369
- assert .NotNil (t , r .Interfaces ["eth1" ].IPConfigs )
370
- assert .Equal (t , r .Interfaces ["eth1" ].IPConfigs [0 ].IP .String (), "10.0.0.2" )
375
+ assert .Equal (t , r .Interfaces ["eth0" ].IPConfigs [0 ].IP .String (), "10.88.0.1" )
371
376
372
377
err = l .Check (ctx , "container-id1" , "/proc/12345/ns/net" )
373
378
assert .NoError (t , err )
@@ -376,6 +381,30 @@ func TestLibCNIType120(t *testing.T) {
376
381
assert .NoError (t , err )
377
382
}
378
383
384
+ func TestLibCNIType120FailStatus (t * testing.T ) {
385
+ // Get the default CNI config
386
+ l := defaultCNIConfig ()
387
+ // Create a fake cni config directory and file
388
+ cniDir , confDir := buildFakeConfig (t )
389
+ defer tearDownCNIConfig (t , cniDir )
390
+ l .pluginConfDir = confDir
391
+ // Set the minimum network count as 2 for this test
392
+ l .networkCount = 2
393
+ err := l .Load (WithLoNetwork , WithDefaultConf )
394
+ assert .NoError (t , err )
395
+
396
+ mockCNI := & MockCNI {}
397
+ l .cniConfig = mockCNI
398
+ l .networks [0 ].cni = mockCNI
399
+ l .networks [1 ].cni = mockCNI
400
+
401
+ mockCNI .On ("GetStatusNetworkList" , l .networks [0 ].config ).Return (nil )
402
+ mockCNI .On ("GetStatusNetworkList" , l .networks [1 ].config ).Return (errors .New ("no ip addresses" ))
403
+ l .cniConfig = mockCNI
404
+ err = l .Status ()
405
+ assert .Error (t , err )
406
+ }
407
+
379
408
type MockCNI struct {
380
409
mock.Mock
381
410
}
0 commit comments