def test_create(self): cli = LinuxCLI() h = Host('test', None, cli, lambda name: None, lambda name: None) i = VirtualInterface('testi', host=h, ip_addr=['192.168.0.2']) i.create() deadline = datetime.datetime.now() + datetime.timedelta(seconds=5) while not cli.grep_cmd('ip l', 'testi'): if datetime.datetime.now() > deadline: self.fail("Interface on bridge failed to be added within timeout") time.sleep(1) b = Bridge('test-bridge', h, ip_addr=['192.168.0.240'], options=['stp']) b.create() time.sleep(1) # Check bridge is present self.assertTrue(h.cli.grep_cmd('brctl show | grep test-bridge | cut -f 1', 'test-bridge')) # Check STP self.assertTrue(h.cli.grep_cmd('brctl show | grep test-bridge | cut -f 4', 'yes')) b.link_interface(i) time.sleep(1) # Check interface has bridge set as link self.assertTrue(h.cli.grep_cmd('ip l | grep testi', 'test-bridge')) b.remove() i.remove() self.assertFalse(cli.grep_cmd('ip l', 'testi')) self.assertFalse(cli.grep_cmd('brctl show', 'test-bridge'))
def test_create_with_host(self): h = Host('test', None, LinuxCLI(), lambda n: None, lambda n: None) h2 = Host('test2', None, NetNSCLI('test2'), CREATENSCMD, REMOVENSCMD) h2.create() p = Interface(name='testp', host=h2, ip_addr=['10.0.0.1']) i = VirtualInterface(name='testi', host=h, ip_addr=['192.168.0.2'], far_interface=p) i.create() # should create and set peer on far host self.assertTrue(h2.cli.grep_cmd('ip l', 'testp')) self.assertTrue(LinuxCLI().grep_cmd('ip l', 'testi')) self.assertFalse(LinuxCLI().grep_cmd('ip l', 'testi.p')) i.config_addr() # the far iface should be controllable on its own just like any interface p.add_ip('192.168.1.2') p.up() self.assertTrue(h2.cli.grep_cmd('ip a | grep testp | grep inet | sed -e "s/^ *//" | cut -f 2 -d " "', '192.168.1.2')) i.remove() # should remove both interfaces self.assertFalse(h2.cli.grep_cmd('ip l', 'testp')) self.assertFalse(LinuxCLI().grep_cmd('ip l', 'testi')) h2.remove()
def test_create_no_peer(self): cli = LinuxCLI(log_cmd=True) h = Host('test', None, cli, lambda n: None, lambda n: None) i = VirtualInterface(name='testi', host=h, ip_addr=['192.168.0.2']) i.create() # should skip setting peer on host time.sleep(1) self.assertTrue(cli.grep_cmd('ip l', 'testi')) self.assertTrue(cli.grep_cmd('ip l', i.peer_name)) i.up() # should still work for near end device time.sleep(1) self.assertTrue(cli.grep_cmd('ip l | grep testi', 'UP')) i.down() time.sleep(1) self.assertFalse(cli.grep_cmd('ip l | grep testi', 'state UP')) i.remove() self.assertFalse(cli.grep_cmd('ip l', 'testi'))