github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/network/bridge_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package network_test 5 6 import ( 7 "runtime" 8 "time" 9 10 "github.com/juju/clock" 11 "github.com/juju/testing" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/network" 15 ) 16 17 // A note regarding the use of clock.WallClock in these unit tests. 18 // 19 // All the tests pass 0 for a timeout, which means indefinite, and 20 // therefore no timer/clock is used. There is one test that checks for 21 // timeout and passes 0.5s as its timeout value. Because of this it's 22 // not clear why the 'testing clock' would be a better choice. 23 24 type BridgeSuite struct { 25 testing.IsolationSuite 26 } 27 28 var _ = gc.Suite(&BridgeSuite{}) 29 30 const echoArgsScript = ` 31 import sys 32 for arg in sys.argv[1:]: print(arg) 33 ` 34 35 func (s *BridgeSuite) SetUpSuite(c *gc.C) { 36 s.IsolationSuite.SetUpSuite(c) 37 if runtime.GOOS == "windows" { 38 c.Skip("skipping BridgeSuite tests on windows") 39 } 40 } 41 42 func assertENIBridgerError(c *gc.C, devices []network.DeviceToBridge, timeout time.Duration, clock clock.Clock, filename string, dryRun bool, reconfigureDelay int, expected string) { 43 bridger := network.NewEtcNetworkInterfacesBridger(clock, timeout, filename, dryRun) 44 err := bridger.Bridge(devices, reconfigureDelay) 45 c.Assert(err, gc.NotNil) 46 c.Assert(err, gc.ErrorMatches, expected) 47 } 48 49 func (*BridgeSuite) TestENIBridgerWithMissingFilenameArgument(c *gc.C) { 50 devices := []network.DeviceToBridge{ 51 { 52 DeviceName: "ens123", 53 BridgeName: "br-ens123", 54 }, 55 } 56 expected := `bridge activation error: filename and input is nil` 57 assertENIBridgerError(c, devices, 0, clock.WallClock, "", true, 0, expected) 58 } 59 60 func (*BridgeSuite) TestENIBridgerWithEmptyDeviceNamesArgument(c *gc.C) { 61 devices := []network.DeviceToBridge{} 62 expected := `bridge activation error: no devices specified` 63 assertENIBridgerError(c, devices, 0, clock.WallClock, "testdata/non-existent-filename", true, 0, expected) 64 } 65 66 func (*BridgeSuite) TestENIBridgerWithNonExistentFile(c *gc.C) { 67 devices := []network.DeviceToBridge{ 68 { 69 DeviceName: "ens123", 70 BridgeName: "br-ens123", 71 }, 72 } 73 expected := `bridge activation error: open testdata/non-existent-file: no such file or directory` 74 assertENIBridgerError(c, devices, 0, clock.WallClock, "testdata/non-existent-file", true, 0, expected) 75 } 76 77 func (*BridgeSuite) TestENIBridgerWithTimeout(c *gc.C) { 78 devices := []network.DeviceToBridge{ 79 { 80 DeviceName: "ens123", 81 BridgeName: "br-ens123", 82 }, 83 } 84 expected := "bridge activation error: bridge activation error: command cancelled" 85 // 25694 is a magic value that causes the bridging script to sleep 86 assertENIBridgerError(c, devices, 500*time.Millisecond, clock.WallClock, "testdata/interfaces", true, 25694, expected) 87 } 88 89 func (*BridgeSuite) TestENIBridgerWithDryRun(c *gc.C) { 90 devices := []network.DeviceToBridge{ 91 { 92 DeviceName: "ens123", 93 BridgeName: "br-ens123", 94 }, 95 } 96 bridger := network.NewEtcNetworkInterfacesBridger(clock.WallClock, 0, "testdata/interfaces", true) 97 err := bridger.Bridge(devices, 0) 98 c.Assert(err, gc.IsNil) 99 }