def test_create_vm(self): lm = LogManager('./test-logs') ptm = PhysicalTopologyManager(root_dir=os.path.dirname(os.path.abspath(__file__)) + '/../..', log_manager=lm) root_cfg = HostDef('root', bridges={'br0': BridgeDef('br0', ip_addresses=[IP('10.0.0.240')])}, interfaces={'cmp1eth0': InterfaceDef('cmp1eth0', linked_bridge='br0')}) cmp1_cfg = HostDef('cmp1', interfaces={'eth0': InterfaceDef('eth0', ip_addresses=[IP('10.0.0.8')])}) cmp1_icfg= ImplementationDef('cmp1', 'PTM.ComputeHost', id='1') root_icfg = ImplementationDef('cmp1', 'PTM.RootHost') root = RootHost('root', ptm) cmp1 = ComputeHost(cmp1_cfg.name, ptm) log = lm.add_file_logger('test.log', 'test') root.set_logger(log) cmp1.set_logger(log) # Now configure the host with the definition and impl configs root.config_from_ptc_def(root_cfg, root_icfg) cmp1.config_from_ptc_def(cmp1_cfg, cmp1_icfg) root.link_interface(root.interfaces['cmp1eth0'], cmp1, cmp1.interfaces['eth0']) cmp1.create() cmp1.boot() root.net_up() cmp1.net_up() root.net_finalize() cmp1.net_finalize() vm1 = cmp1.create_vm("vm1") vm1.create_interface('eth0', ip_list=['10.1.1.2']) self.assertTrue('eth0' in vm1.interfaces) vm1.create() vm1.boot() vm1.net_up() vm1.net_finalize() self.assertTrue(LinuxCLI().grep_cmd('ip netns exec vm1 ip l', 'eth0')) vm1.net_down() vm1.shutdown() vm1.remove() cmp1.net_down() root.net_down() cmp1.shutdown() root.shutdown() cmp1.remove()
def test_veth_connection_between_two_hosts(self): lm = LogManager('./test-logs') ptm = PhysicalTopologyManager(root_dir=os.path.dirname(os.path.abspath(__file__)) + '/../..', log_manager=lm) h1cfg = HostDef('test1', interfaces={'testi': InterfaceDef('testi', ['192.168.1.1'])}) i1cfg = ImplementationDef('test', 'RootHost') h2cfg = HostDef('test2', interfaces={'testp': InterfaceDef('testp', ['192.168.1.3'])}) i2cfg = ImplementationDef('test', 'NetNSHost') # Host should act the same regardless of using NS or base OS h1 = RootHost(h1cfg.name, ptm) h2 = NetNSHost(h2cfg.name, ptm) log = lm.add_file_logger('test.log', 'test') h1.set_logger(log) h2.set_logger(log) # Now configure the host with the definition and impl configs h1.config_from_ptc_def(h1cfg, i1cfg) h2.config_from_ptc_def(h2cfg, i2cfg) h1.link_interface(h1.interfaces['testi'], h2, h2.interfaces['testp']) h1.create() h2.create() h1.boot() h2.boot() h1.net_up() h2.net_up() h1.net_finalize() h2.net_finalize() self.assertTrue(h1.cli.grep_cmd('ip l', 'testi')) # Linked interface should start and get peered self.assertTrue(h2.cli.grep_cmd('ip l', 'testp')) # Linked interface should start and get peered self.assertTrue(h1.cli.grep_cmd('ip a | grep testi', '192.168.1.1')) self.assertTrue(h2.cli.grep_cmd('ip a | grep testp', '192.168.1.3')) h1.net_down() h2.net_down() h2.shutdown() h2.remove() self.assertFalse(h1.cli.grep_cmd('ip netns', 'test2')) h1.shutdown() h1.remove()
class VMHostTest(unittest.TestCase): def setUp(self): lm = LogManager('./test-logs') ptm = PhysicalTopologyManager(root_dir=os.path.dirname(os.path.abspath(__file__)) + '/../..', log_manager=lm) self.hypervisor = None self.root_host = None root_hostcfg = HostDef('root', interfaces={'hveth0': InterfaceDef('hveth0')}) root_host_implcfg = ImplementationDef('test', 'RootHost') hypervisorcfg = HostDef('hv', interfaces={'eth0': InterfaceDef('eth0', [IP('192.168.1.3')])}) hypervisor_implcfg = ImplementationDef('test', 'ComputeHost') self.root_host = RootHost(root_hostcfg.name, ptm) self.hypervisor = ComputeHost(hypervisorcfg.name, ptm) log = lm.add_file_logger('test.log', 'test') self.root_host.set_logger(log) self.hypervisor.set_logger(log) # Now configure the host with the definition and impl configs self.root_host.config_from_ptc_def(root_hostcfg, root_host_implcfg) self.hypervisor.config_from_ptc_def(hypervisorcfg, hypervisor_implcfg) self.root_host.link_interface(self.root_host.interfaces['hveth0'], self.hypervisor, self.hypervisor.interfaces['eth0']) self.root_host.create() self.hypervisor.create() self.root_host.boot() self.hypervisor.boot() self.root_host.net_up() self.hypervisor.net_up() self.root_host.net_finalize() self.hypervisor.net_finalize() def dtest_create_vm(self): vm_host = self.hypervisor.create_vm('test_vm') self.assertIs(vm_host, self.hypervisor.get_vm('test_vm')) vm_host.create() vm_host.boot() vm_host.net_up() vm_host.net_finalize() vm_host.shutdown() vm_host.remove() def dtest_create_vm_interface(self): vm_host = self.hypervisor.create_vm('test_vm') self.assertIs(vm_host, self.hypervisor.get_vm('test_vm')) new_if = vm_host.create_interface('eth0', ip_list=[IP('10.50.50.3')]) self.assertTrue(vm_host.cli.grep_cmd('ip l', 'eth0')) self.assertTrue(self.hypervisor.cli.grep_cmd('ip l', 'test_vmeth0')) vm_host.shutdown() vm_host.remove() def test_packet_communication(self): vm_host1 = self.hypervisor.create_vm('test_vm1') try: vm_host1.create_interface('eth0', ip_list=[IP('10.50.50.3')]) vm_host1.start_capture('lo', save_dump_file=True, save_dump_filename='tcp.vmhost.out') ping_ret = vm_host1.ping('10.50.50.3') vm_host1.send_tcp_packet(iface='lo', dest_ip='10.50.50.3', source_port=6015, dest_port=6055) ret1 = vm_host1.capture_packets('lo', count=1, timeout=5) ret2 = vm_host1.capture_packets('lo', count=1, timeout=5) vm_host1.stop_capture('lo') self.assertTrue(ping_ret) self.assertEquals(1, len(ret1)) finally: vm_host1.shutdown() vm_host1.remove() def tearDown(self): self.root_host.net_down() self.hypervisor.net_down() self.hypervisor.shutdown() self.root_host.shutdown() self.hypervisor.remove() self.root_host.remove() LinuxCLI().cmd('ip netns del test_vm') LinuxCLI().rm('tcp.vmhost.out')
def test_startup(self): lm = LogManager('./test-logs') ptm = PhysicalTopologyManager(root_dir=os.path.dirname(os.path.abspath(__file__)) + '/../..', log_manager=lm) root_cfg = HostDef('root', bridges={'br0': BridgeDef('br0', ip_addresses=[IP('10.0.0.240')])}, interfaces={'edge1eth0': InterfaceDef('edge1eth0', linked_bridge='br0')}) edge1_cfg = HostDef('edge1', interfaces={'eth0': InterfaceDef('eth0', ip_addresses=[IP('10.0.0.2')])}) edge1_icfg= ImplementationDef('edge1', 'PTM.RouterHost', id='1') root_icfg = ImplementationDef('edge1', 'PTM.RootHost') root = RootHost('root', ptm) edge1 = RouterHost(edge1_cfg.name, ptm) log = lm.add_file_logger('test.log', 'test') root.set_logger(log) edge1.set_logger(log) # Now configure the host with the definition and impl configs root.config_from_ptc_def(root_cfg, root_icfg) edge1.config_from_ptc_def(edge1_cfg, edge1_icfg) root.link_interface(root.interfaces['edge1eth0'], edge1, edge1.interfaces['eth0']) ptm.hosts_by_name['root'] = root ptm.hosts_by_name['edge1'] = edge1 ptm.host_by_start_order.append(root) ptm.host_by_start_order.append(edge1) root.create() edge1.create() root.boot() edge1.boot() root.net_up() edge1.net_up() root.net_finalize() edge1.net_finalize() edge1.prepare_config() start_process = ptm.unshare_control('start', edge1) stdout, stderr = start_process.communicate() start_process.poll() print("Host control process output: ") print stdout print("Host control process error output: ") print stderr if start_process.returncode != 0: raise SubprocessFailedException('Host control start failed with: ' + str(start_process.returncode)) edge1.wait_for_process_start() bgpd_pid = LinuxCLI().read_from_file('/run/quagga.1/bgpd.pid').rstrip() zebra_pid = LinuxCLI().read_from_file('/run/quagga.1/zebra.pid').rstrip() self.assertTrue(bgpd_pid in LinuxCLI().get_running_pids()) self.assertTrue(zebra_pid in LinuxCLI().get_running_pids()) stop_process = ptm.unshare_control('stop', edge1) stdout, stderr = stop_process.communicate() stop_process.poll() print("Host control process output: ") print stdout print("Host control process error output: ") print stderr if stop_process.returncode != 0: raise SubprocessFailedException('Host control start failed with: ' + str(stop_process.returncode)) edge1.wait_for_process_stop() time.sleep(1) self.assertFalse(bgpd_pid in LinuxCLI().get_running_pids()) root.net_down() edge1.net_down() root.shutdown() edge1.shutdown() root.remove() edge1.remove()