@@ -18,6 +18,7 @@ package cni
1818
1919import (
2020 "context"
21+ "errors"
2122 "net"
2223 "testing"
2324
@@ -292,21 +293,19 @@ func TestLibCNIType120(t *testing.T) {
292293 // Get the default CNI config
293294 l := defaultCNIConfig ()
294295 // Create a fake cni config directory and file
295- cniDir , confDir := makeFakeCNIConfig (t )
296+ cniDir , confDir := buildFakeConfig (t )
296297 defer tearDownCNIConfig (t , cniDir )
297298 l .pluginConfDir = confDir
298299 // Set the minimum network count as 2 for this test
299300 l .networkCount = 2
300- err := l .Load (WithAllConf )
301- assert .NoError (t , err )
302-
303- err = l .Status ()
301+ err := l .Load (WithLoNetwork , WithDefaultConf )
304302 assert .NoError (t , err )
305303
306304 mockCNI := & MockCNI {}
305+ l .cniConfig = mockCNI
307306 l .networks [0 ].cni = mockCNI
308307 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 " )
310309 assert .NoError (t , err )
311310 expectedRT := & cnilibrary.RuntimeConf {
312311 ContainerID : "container-id1" ,
@@ -315,59 +314,65 @@ func TestLibCNIType120(t *testing.T) {
315314 Args : [][2 ]string (nil ),
316315 CapabilityArgs : map [string ]interface {}{},
317316 }
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 {
321331 {
322- Name : "eth0 " ,
332+ Name : "lo " ,
323333 },
324334 },
325- IPs : []* types100 .IPConfig {
335+ IPs : []* types040 .IPConfig {
326336 {
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+ },
330341 },
331342 },
332343 }, 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 )
345349 mockCNI .On ("AddNetworkList" , l .networks [1 ].config , expectedRT ).Return (& types100.Result {
346350 CNIVersion : "1.1.0" ,
347351 Interfaces : []* types100.Interface {
348352 {
349- Name : "eth1 " ,
353+ Name : "eth0 " ,
350354 },
351355 },
352356 IPs : []* types100.IPConfig {
353357 {
354358 Interface : types100 .Int (0 ),
355359 Address : * ipv4 ,
356- Gateway : net .ParseIP ("10.0 .0.2 " ),
360+ Gateway : net .ParseIP ("10.88 .0.1 " ),
357361 },
358362 },
359363 }, nil )
360364 mockCNI .On ("DelNetworkList" , l .networks [1 ].config , expectedRT ).Return (nil )
361365 mockCNI .On ("CheckNetworkList" , l .networks [1 ].config , expectedRT ).Return (nil )
362366 ctx := context .Background ()
367+
368+ err = l .Status ()
369+ assert .NoError (t , err )
370+
363371 r , err := l .Setup (ctx , "container-id1" , "/proc/12345/ns/net" )
364372 assert .NoError (t , err )
365373 assert .Contains (t , r .Interfaces , "eth0" )
366374 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" )
371376
372377 err = l .Check (ctx , "container-id1" , "/proc/12345/ns/net" )
373378 assert .NoError (t , err )
@@ -376,6 +381,30 @@ func TestLibCNIType120(t *testing.T) {
376381 assert .NoError (t , err )
377382}
378383
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+
379408type MockCNI struct {
380409 mock.Mock
381410}
0 commit comments