github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/network/debinterfaces/activate_test.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package debinterfaces_test 5 6 // These tests verify the commands that would be executed, but using a 7 // dryrun option to the script that is executed. 8 9 import ( 10 "fmt" 11 "runtime" 12 "time" 13 14 "github.com/juju/clock" 15 "github.com/juju/clock/testclock" 16 "github.com/juju/juju/network/debinterfaces" 17 "github.com/juju/testing" 18 gc "gopkg.in/check.v1" 19 ) 20 21 type ActivationSuite struct { 22 testing.IsolationSuite 23 } 24 25 var _ = gc.Suite(&ActivationSuite{}) 26 27 func (s *ActivationSuite) SetUpSuite(c *gc.C) { 28 if runtime.GOOS == "windows" { 29 c.Skip("skipping ActivationSuite tests on windows") 30 } 31 s.IsolationSuite.SetUpSuite(c) 32 } 33 34 func (*BridgeSuite) TestActivateNonExistentDevice(c *gc.C) { 35 params := debinterfaces.ActivationParams{ 36 DryRun: true, 37 Clock: clock.WallClock, 38 Devices: map[string]string{"non-existent": "non-existent"}, 39 Filename: "testdata/TestInputSourceStanza/interfaces", 40 ReconfigureDelay: 10, 41 Timeout: 5 * time.Minute, 42 } 43 44 result, err := debinterfaces.BridgeAndActivate(params) 45 c.Assert(err, gc.IsNil) 46 c.Check(result, gc.IsNil) 47 } 48 49 func (s *BridgeSuite) TestActivateEth0(c *gc.C) { 50 filename := "testdata/TestInputSourceStanza/interfaces" 51 52 params := debinterfaces.ActivationParams{ 53 Clock: testclock.NewClock(time.Now()), 54 Devices: map[string]string{"eth0": "br-eth0", "eth1": "br-eth1"}, 55 DryRun: true, 56 Filename: filename, 57 ReconfigureDelay: 10, 58 Timeout: 5 * time.Minute, 59 } 60 result, err := debinterfaces.BridgeAndActivate(params) 61 c.Assert(err, gc.IsNil) 62 c.Check(result, gc.NotNil) 63 c.Check(result.Code, gc.Equals, 0) 64 65 expected := fmt.Sprintf(` 66 write_backup testdata/TestInputSourceStanza/interfaces.backup-%d 67 write_content testdata/TestInputSourceStanza/interfaces.new 68 ifdown --interfaces=testdata/TestInputSourceStanza/interfaces eth0 eth1 69 sleep 10 70 cp testdata/TestInputSourceStanza/interfaces.new testdata/TestInputSourceStanza/interfaces 71 ifup --interfaces=testdata/TestInputSourceStanza/interfaces -a 72 `, params.Clock.Now().Unix()) 73 c.Check(string(result.Stdout), gc.Equals, expected[1:]) 74 } 75 76 func (s *BridgeSuite) TestActivateEth0WithoutBackup(c *gc.C) { 77 filename := "testdata/TestInputSourceStanza/interfaces" 78 79 params := debinterfaces.ActivationParams{ 80 Clock: testclock.NewClock(time.Now()), 81 Devices: map[string]string{"eth0": "br-eth0", "eth1": "br-eth1"}, 82 DryRun: true, 83 Filename: filename, 84 ReconfigureDelay: 100, 85 Timeout: 5 * time.Minute, 86 } 87 88 result, err := debinterfaces.BridgeAndActivate(params) 89 c.Assert(err, gc.IsNil) 90 c.Check(result, gc.NotNil) 91 c.Check(result.Code, gc.Equals, 0) 92 93 expected := fmt.Sprintf(` 94 write_backup testdata/TestInputSourceStanza/interfaces.backup-%d 95 write_content testdata/TestInputSourceStanza/interfaces.new 96 ifdown --interfaces=testdata/TestInputSourceStanza/interfaces eth0 eth1 97 sleep 100 98 cp testdata/TestInputSourceStanza/interfaces.new testdata/TestInputSourceStanza/interfaces 99 ifup --interfaces=testdata/TestInputSourceStanza/interfaces -a 100 `, params.Clock.Now().Unix()) 101 c.Check(string(result.Stdout), gc.Equals, expected[1:]) 102 } 103 104 func (s *BridgeSuite) TestActivateWithNegativeReconfigureDelay(c *gc.C) { 105 filename := "testdata/TestInputSourceStanza/interfaces" 106 107 params := debinterfaces.ActivationParams{ 108 Clock: testclock.NewClock(time.Now()), 109 Devices: map[string]string{"eth0": "br-eth0", "eth1": "br-eth1"}, 110 DryRun: true, 111 Filename: filename, 112 ReconfigureDelay: -3, 113 Timeout: 5 * time.Minute, 114 } 115 116 result, err := debinterfaces.BridgeAndActivate(params) 117 c.Assert(err, gc.IsNil) 118 c.Check(result, gc.NotNil) 119 c.Check(result.Code, gc.Equals, 0) 120 121 expected := fmt.Sprintf(` 122 write_backup testdata/TestInputSourceStanza/interfaces.backup-%d 123 write_content testdata/TestInputSourceStanza/interfaces.new 124 ifdown --interfaces=testdata/TestInputSourceStanza/interfaces eth0 eth1 125 sleep 0 126 cp testdata/TestInputSourceStanza/interfaces.new testdata/TestInputSourceStanza/interfaces 127 ifup --interfaces=testdata/TestInputSourceStanza/interfaces -a 128 `, params.Clock.Now().Unix()) 129 c.Check(string(result.Stdout), gc.Equals, expected[1:]) 130 } 131 132 func (*BridgeSuite) TestActivateWithNoDevicesSpecified(c *gc.C) { 133 filename := "testdata/TestInputSourceStanza/interfaces" 134 135 params := debinterfaces.ActivationParams{ 136 Clock: clock.WallClock, 137 Devices: map[string]string{}, 138 DryRun: true, 139 Filename: filename, 140 } 141 142 _, err := debinterfaces.BridgeAndActivate(params) 143 c.Assert(err, gc.NotNil) 144 c.Check(err, gc.ErrorMatches, "no devices specified") 145 } 146 147 func (*BridgeSuite) TestActivateWithParsingError(c *gc.C) { 148 filename := "testdata/TestInputSourceStanzaWithErrors/interfaces" 149 150 params := debinterfaces.ActivationParams{ 151 Clock: clock.WallClock, 152 Devices: map[string]string{"eth0": "br-eth0"}, 153 DryRun: true, 154 Filename: filename, 155 } 156 157 _, err := debinterfaces.BridgeAndActivate(params) 158 c.Assert(err, gc.NotNil) 159 c.Assert(err, gc.FitsTypeOf, &debinterfaces.ParseError{}) 160 parseError := err.(*debinterfaces.ParseError) 161 c.Check(parseError, gc.DeepEquals, &debinterfaces.ParseError{ 162 Filename: "testdata/TestInputSourceStanzaWithErrors/interfaces.d/eth1.cfg", 163 Line: "iface", 164 LineNum: 2, 165 Message: "missing device name", 166 }) 167 } 168 169 func (*BridgeSuite) TestActivateWithTimeout(c *gc.C) { 170 filename := "testdata/TestInputSourceStanza/interfaces" 171 172 params := debinterfaces.ActivationParams{ 173 Clock: clock.WallClock, 174 Devices: map[string]string{"eth0": "br-eth0", "eth1": "br-eth1"}, 175 DryRun: true, 176 Filename: filename, 177 // magic value causing the bash script to sleep 178 ReconfigureDelay: 25694, 179 Timeout: 10, 180 } 181 182 _, err := debinterfaces.BridgeAndActivate(params) 183 c.Assert(err, gc.NotNil) 184 c.Check(err, gc.ErrorMatches, "bridge activation error: command cancelled") 185 } 186 187 func (*BridgeSuite) TestActivateFailure(c *gc.C) { 188 filename := "testdata/TestInputSourceStanza/interfaces" 189 190 params := debinterfaces.ActivationParams{ 191 Clock: clock.WallClock, 192 Devices: map[string]string{"eth0": "br-eth0", "eth1": "br-eth1"}, 193 DryRun: true, 194 Filename: filename, 195 // magic value causing the bash script to fail 196 ReconfigureDelay: 25695, 197 Timeout: 5 * time.Minute, 198 } 199 200 result, err := debinterfaces.BridgeAndActivate(params) 201 c.Assert(err, gc.NotNil) 202 c.Check(err, gc.ErrorMatches, "bridge activation failed: artificial failure\n") 203 c.Check(result.Code, gc.Equals, 1) 204 } 205 206 func (*BridgeSuite) TestActivateFailureShortMessage(c *gc.C) { 207 filename := "testdata/TestInputSourceStanza/interfaces" 208 209 params := debinterfaces.ActivationParams{ 210 Clock: clock.WallClock, 211 Devices: map[string]string{"eth0": "br-eth0", "eth1": "br-eth1"}, 212 DryRun: true, 213 Filename: filename, 214 // magic value causing the bash script to fail 215 ReconfigureDelay: 25696, 216 Timeout: 5 * time.Minute, 217 } 218 219 result, err := debinterfaces.BridgeAndActivate(params) 220 c.Assert(err, gc.NotNil) 221 c.Check(err, gc.ErrorMatches, "bridge activation failed, see logs for details") 222 c.Check(result.Code, gc.Equals, 1) 223 }