예제 #1
0
    def test_multiple(self):
        lm = LogManager()
        lm.add_format('test', logging.Formatter('%(asctime)s TEST - %(levelname)s - %(message)s'))
        logger1 = lm.add_file_logger('log_multiple.txt', format_name='test')
        logger2 = lm.add_file_logger('log_multiple2.txt', format_name='test', log_level=logging.DEBUG)

        logger1.error("Test1")
        logger1.warning("Test2")
        logger1.debug("Test3")
        logger2.error("Test1b")
        logger2.warning("Test2b")
        logger2.debug("Test3b")

        with open('log_multiple.txt', 'r') as f:
            line = f.readlines()
            self.assertEquals(len(line), 2)
            self.assertTrue(line[0].find("TEST - ERROR - Test1") != -1)
            self.assertTrue(line[1].find("TEST - WARNING - Test2") != -1)

        with open('log_multiple2.txt', 'r') as f:
            line = f.readlines()
            self.assertEquals(len(line), 3)
            self.assertTrue(line[0].find("TEST - ERROR - Test1b") != -1)
            self.assertTrue(line[1].find("TEST - WARNING - Test2b") != -1)
            self.assertTrue(line[2].find("TEST - DEBUG - Test3b") != -1)
예제 #2
0
    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()
예제 #3
0
    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()
예제 #4
0
 def test_tee(self):
     lm = LogManager()
     lm.add_format('test', logging.Formatter('%(asctime)s TEST - %(levelname)s - %(message)s'))
     logger = lm.add_tee_logger('log_tee.txt', name='tee', file_overwrite=True,
                                file_log_level=logging.DEBUG, stdout_log_level=logging.WARNING,
                                file_format_name='test', stdout_format_name='test')
     logger.error("Test1")
     logger.warning("Test2")
     logger.debug("Test3")
     with open('log_tee.txt', 'r') as f:
         line = f.readlines()
         self.assertEquals(len(line), 3)
         self.assertTrue(line[0].find("TEST - ERROR - Test1") != -1)
         self.assertTrue(line[1].find("TEST - WARNING - Test2") != -1)
         self.assertTrue(line[2].find("TEST - DEBUG - Test3") != -1)
예제 #5
0
    def test_configure(self):

        lm = LogManager('./test-logs')
        ptm = PhysicalTopologyManager(root_dir=os.path.dirname(os.path.abspath(__file__)) + '/../..', log_manager=lm)

        hcfg = HostDef('test',
                       bridges={'br0': BridgeDef('br0')},
                       interfaces={'testi': InterfaceDef('testi', ['192.168.1.2'], linked_bridge='br0')})
        icfg = ImplementationDef('test', 'NetNSHost')

        # Get the impl details and use that to instance a basic object
        h = NetNSHost(hcfg.name, ptm)
        log = lm.add_file_logger('test.log', 'test')
        h.set_logger(log)

        # Now configure the host with the definition and impl configs
        h.config_from_ptc_def(hcfg, icfg)
예제 #6
0
    def test_file(self):
        lm = LogManager()
        lm.add_format('test', logging.Formatter('%(asctime)s TEST - %(levelname)s - %(message)s'))
        logger = lm.add_file_logger('log_file.txt', format_name='test')
        logger.error("Test1")
        logger.warning("Test2")
        logger.debug("Test3")
        with open('log_file.txt', 'r') as f:
            line = f.readlines()
            self.assertEquals(len(line), 2)
            self.assertTrue(line[0].find("TEST - ERROR - Test1") != -1)
            self.assertTrue(line[1].find("TEST - WARNING - Test2") != -1)

        lm.add_format('test2',
                      logging.Formatter(fmt='TEST2 - %(asctime)s - %(levelname)s - %(message)s',
                                        datefmt='%Y'),
                      '%Y', 1)
        current_year = str(datetime.datetime.now().year)
        logger = lm.add_file_logger('log_file2.txt', format_name='test2')
        logger.error("Test1")
        logger.warning("Test2")
        logger.debug("Test3")
        with open('log_file2.txt', 'r') as f:
            line = f.readlines()
            self.assertEquals(len(line), 2)
            self.assertTrue(line[0].find("TEST2 - " + str(current_year) + " - ERROR - Test1") != -1)
            self.assertTrue(line[1].find("TEST2 - " + str(current_year) + " - WARNING - Test2") != -1)
예제 #7
0
    def test_slicing_multi(self):
        LinuxCLI().rm('./logs')
        LinuxCLI().rm('./sliced-logs')

        try:
            lm = LogManager('./logs')
            lm.set_default_log_level(logging.DEBUG)
            LOG = lm.add_file_logger('test-log')
            LOG2 = lm.add_file_logger('test-log2')

            now1 = datetime.datetime.now() + datetime.timedelta(seconds=6)
            for i in range(0, 5):
                LOG.info('test-log-line: ' + str(i))
                LOG2.info('test-log2-line: ' + str(i))
                time.sleep(2)
            LOG.info('test-log-line: ' + str(5))
            LOG2.info('test-log2-line: ' + str(5))
            now2 = datetime.datetime.now() - datetime.timedelta(seconds=6)

            lm.slice_log_files_by_time('./sliced-logs',
                                       start_time=now1,
                                       stop_time=now2,
                                       leeway=3,
                                       collated_only=False)

            self.assertAlmostEquals(2, LinuxCLI().wc('./sliced-logs/test-log.slice')['lines'], delta=1)
            self.assertAlmostEquals(2, LinuxCLI().wc('./sliced-logs/test-log2.slice')['lines'], delta=1)

        finally:
            LinuxCLI().rm('./logs')
            LinuxCLI().rm('./sliced-logs')
            pass
예제 #8
0
 def test_name_generation(self):
     lm = LogManager()
     logger1 = lm.add_file_logger('log_name_generation.txt')
     logger2 = lm.add_file_logger('log_name_generation.txt')
     self.assertIsNotNone(lm.get_logger('root0'))
     self.assertIsNotNone(lm.get_logger('root1'))
     logger3 = lm.add_file_logger('log_name_generation.txt', name='test')
     logger4 = lm.add_file_logger('log_name_generation.txt')
     self.assertIsNotNone(lm.get_logger('test'))
     self.assertIsNotNone(lm.get_logger('root3'))
예제 #9
0
    def test_boot_shutdown(self):

        lm = LogManager('./test-logs')
        ptm = PhysicalTopologyManager(root_dir=os.path.dirname(os.path.abspath(__file__)) + '/../..', log_manager=lm)

        hcfg = HostDef('test',
                       bridges={'br0': BridgeDef('br0')},
                       interfaces={'testi': InterfaceDef('testi', ['192.168.1.2'], linked_bridge='br0')})
        icfg = ImplementationDef('test', 'NetNSHost')

        h = NetNSHost(hcfg.name, ptm)
        log = lm.add_file_logger('test.log', 'test')
        h.set_logger(log)

        # Now configure the host with the definition and impl configs
        h.config_from_ptc_def(hcfg, icfg)

        h.interfaces['testi'] = DummyInterface('testi', h, h.bridges['br0'])

        h.create()

        self.assertTrue(LinuxCLI().grep_cmd('ip netns', 'test'))

        h.boot()

        h.net_up()
        h.net_finalize()

        self.assertTrue(h.cli.grep_cmd('ip l', 'testi'))  # Dummy interface
        self.assertTrue(h.cli.grep_cmd('brctl show', 'br0'))

        h.net_down()

        h.shutdown()

        self.assertFalse(h.cli.grep_cmd('brctl show', 'br0'))

        h.remove()

        self.assertFalse(LinuxCLI().grep_cmd('ip netns', 'test'))
예제 #10
0
    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()
예제 #11
0
    def test_formats(self):
        lm = LogManager()
        lm.add_format('test', logging.Formatter('%(asctime)s TEST - %(levelname)s - %(message)s'))

        try:
            lm.get_format('test_none')
        except ObjectNotFoundException:
            pass
        else:
            self.assertTrue(False, 'Getting wrong format failed to raise ObjectNotFoundException')

        try:
            lm.add_format('test', logging.Formatter('TEST'))
        except ObjectAlreadyAddedException:
            pass
        else:
            self.assertTrue(False, 'Double-adding format failed to raise ObjectAlreadyAddedException')

        self.assertIsNotNone(lm.get_format('test'))
예제 #12
0
    def test_startup(self):
        lm = LogManager('./test-logs')
        ptm = PhysicalTopologyManager(root_dir=os.path.dirname(os.path.abspath(__file__)) + '/../..', log_manager=lm)
        ptm.configure_logging(debug=True)
        root_cfg = HostDef('root',
                           bridges={'br0': BridgeDef('br0', ip_addresses=[IP('10.0.0.240')])},
                           interfaces={'zoo1eth0': InterfaceDef('zoo1eth0', linked_bridge='br0'),
                                       #'cass1eth0': InterfaceDef('cass1eth0', linked_bridge='br0'),
                                       'cmp1eth0': InterfaceDef('cmp1eth0', linked_bridge='br0')})
        zoo1_cfg = HostDef('zoo1',
                           interfaces={'eth0': InterfaceDef('eth0', ip_addresses=[IP('10.0.0.2')])})
        #cass1_cfg = HostDef('cass1',
        #                    interfaces={'eth0': InterfaceDef('eth0', ip_addresses=[IP('10.0.0.5')])})
        cmp1_cfg = HostDef('cmp1',
                           interfaces={'eth0': InterfaceDef('eth0', ip_addresses=[IP('10.0.0.8')])})

        zoo1_icfg= ImplementationDef('zoo1', 'PTM.ZookeeperHost', id='1',
                                     zookeeper_ips=['10.0.0.2'])
        #cass1_icfg= ImplementationDef('cass1', 'PTM.CassandraHost', id='1',
        #                             cassandra_ips=['10.0.0.5'],
        #                             init_token="")
        cmp1_icfg= ImplementationDef('cmp1', 'PTM.ComputeHost', id='1',
                                     zookeeper_ips=['10.0.0.2'],
                                     cassandra_ips=[])#['10.0.0.5'])
        root_icfg = ImplementationDef('root', 'PTM.RootHost')

        root = RootHost('root', ptm)
        zoo1 = ZookeeperHost(zoo1_cfg.name, ptm)
        #cass1 = CassandraHost(cass1_cfg.name, ptm)
        cmp1 = ComputeHost(cmp1_cfg.name, ptm)

        root.set_logger(lm.add_tee_logger('test.log', name='test-root', file_log_level=logging.DEBUG,
                                          stdout_log_level=logging.DEBUG))
        zoo1.set_logger(lm.add_tee_logger('test.log', name='test-zoo1', file_log_level=logging.DEBUG,
                                          stdout_log_level=logging.DEBUG))
        #cass1.set_logger(lm.add_tee_logger('test.log', name='test-cass1', file_log_level=logging.DEBUG,
        #                                   stdout_log_level=logging.DEBUG))
        cmp1.set_logger(lm.add_tee_logger('test.log', name='test-cmp1', file_log_level=logging.DEBUG,
                                          stdout_log_level=logging.DEBUG))

        # Now configure the host with the definition and impl configs
        root.config_from_ptc_def(root_cfg, root_icfg)
        zoo1.config_from_ptc_def(zoo1_cfg, zoo1_icfg)
        #cass1.config_from_ptc_def(cass1_cfg, cass1_icfg)
        cmp1.config_from_ptc_def(cmp1_cfg, cmp1_icfg)

        root.link_interface(root.interfaces['zoo1eth0'], zoo1, zoo1.interfaces['eth0'])
        #root.link_interface(root.interfaces['cass1eth0'], cass1, cass1.interfaces['eth0'])
        root.link_interface(root.interfaces['cmp1eth0'], cmp1, cmp1.interfaces['eth0'])

        ptm.hosts_by_name['root'] = root
        ptm.hosts_by_name['zoo1'] = zoo1
        #ptm.hosts_by_name['cass1'] = cass1
        ptm.hosts_by_name['cmp1'] = cmp1
        ptm.host_by_start_order.append(root)
        ptm.host_by_start_order.append(zoo1)
        #ptm.host_by_start_order.append(cass1)
        ptm.host_by_start_order.append(cmp1)

        for h in ptm.host_by_start_order:
            h.create()

        for h in ptm.host_by_start_order:
            h.boot()

        for h in ptm.host_by_start_order:
            h.net_up()

        for h in ptm.host_by_start_order:
            h.net_finalize()

        for h in ptm.host_by_start_order:
            h.prepare_config()

        for h in ptm.host_by_start_order:
            start_process = ptm.unshare_control('start', h)

            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))
            h.wait_for_process_start()

        pid = LinuxCLI().read_from_file('/run/midolman.1/pid').rstrip()
        print "PID = " + pid
        print "PS = " + LinuxCLI().cmd("ps -aef")

        self.assertTrue(LinuxCLI().is_pid_running(pid))

        for h in ptm.host_by_start_order:
            stop_process = ptm.unshare_control('stop', h)
            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 stop failed with: ' + str(stop_process.returncode))

            h.wait_for_process_stop()

        for h in ptm.host_by_start_order:
            h.net_down()

        for h in ptm.host_by_start_order:
            h.shutdown()

        for h in ptm.host_by_start_order:
            h.remove()
예제 #13
0
    def test_rollover(self):

        LinuxCLI().rm('./logs')
        LinuxCLI().rm('./logbak')

        lm = LogManager('./logs')
        lm.set_default_log_level(logging.DEBUG)

        LinuxCLI(priv=False).write_to_file('./logs/test.log', 'data')
        LinuxCLI(priv=False).write_to_file('./logs/test2.log', 'data2')

        # Run fresh rollover function before loggers are defined
        lm.rollover_logs_fresh(date_pattern='%Y', zip_file=False)
        try:
            current_year = str(datetime.datetime.now().year)
            self.assertFalse(LinuxCLI().exists('./logs/test.log'))
            self.assertFalse(LinuxCLI().exists('./logs/test2.log'))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak'))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak/test.log.' + current_year))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak/test2.log.' + current_year))
            self.assertNotEqual(0, os.path.getsize('./logs/log_bak/test.log.' + current_year))
            self.assertNotEqual(0, os.path.getsize('./logs/log_bak/test2.log.' + current_year))
        finally:
            LinuxCLI().rm('./logs/log_bak')

        l1 = lm.add_file_logger(name='main', file_name='test.log', file_overwrite=True)
        l2 = lm.add_file_logger(name='sub', file_name='test.log', file_overwrite=False)
        l3 = lm.add_file_logger(name='main2', file_name='test2.log', file_overwrite=True)

        self.assertIn(FileLocation('./logs/test.log'), lm.open_log_files)
        self.assertIn(FileLocation('./logs/test2.log'), lm.open_log_files)
        self.assertEqual(2, len(lm.open_log_files))

        # Running rollover before log files have data should be a no-op,
        # So the empty files should remain
        lm.rollover_logs_by_date()
        try:
            self.assertTrue(LinuxCLI().exists('./logs/test.log'))
            self.assertTrue(LinuxCLI().exists('./logs/test2.log'))
            self.assertEqual(0, os.path.getsize('./logs/test.log'))
            self.assertEqual(0, os.path.getsize('./logs/test2.log'))
            self.assertFalse(LinuxCLI().exists('./logs/log_bak'))
        finally:
            LinuxCLI().rm('./logs/log_bak')

        l1.info('test1')
        l2.info('test2')
        l3.info('test3')

        # Now run a standard rollover with no params, default log dir should be created
        # and regular log files should be moved and zipped
        lm.rollover_logs_by_date()
        try:
            self.assertTrue(LinuxCLI().exists('./logs/test.log'))
            self.assertTrue(LinuxCLI().exists('./logs/test2.log'))
            self.assertEqual(0, os.path.getsize('./logs/test.log'))
            self.assertEqual(0, os.path.getsize('./logs/test2.log'))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak'))
        finally:
            LinuxCLI().rm('./logs/log_bak')

        l1.info('test1')
        l2.info('test2')
        l3.info('test3')

        self.assertNotEqual(0, os.path.getsize('./logs/test.log'))
        self.assertNotEqual(0, os.path.getsize('./logs/test2.log'))

        # Same as no-params, just with a specified backup dir
        lm.rollover_logs_by_date(backup_dir='./logbak')
        try:
            self.assertTrue(LinuxCLI().exists('./logs/test.log'))
            self.assertTrue(LinuxCLI().exists('./logs/test2.log'))
            self.assertEqual(0, os.path.getsize('./logs/test.log'))
            self.assertEqual(0, os.path.getsize('./logs/test2.log'))
            self.assertTrue(LinuxCLI().exists('./logbak'))
        finally:
            LinuxCLI().rm('./logbak')

        l1.info('test1')
        l2.info('test2')
        l3.info('test3')

        # Now use a specific pattern, making it easy to test for
        # the files' existence
        new_file = lm.rollover_logs_by_date(date_pattern='%Y')
        try:
            current_year = str(datetime.datetime.now().year)
            self.assertTrue(LinuxCLI().exists('./logs/test.log'))
            self.assertTrue(LinuxCLI().exists('./logs/test2.log'))
            self.assertEqual(0, os.path.getsize('./logs/test.log'))
            self.assertEqual(0, os.path.getsize('./logs/test2.log'))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak'))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak/test.log.' + current_year + '.gz'))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak/test2.log.' + current_year + '.gz'))
        finally:
            LinuxCLI().rm('./logs/log_bak')

        l1.info('test1')
        l2.info('test2')
        l3.info('test3')

        new_file = lm.rollover_logs_by_date(date_pattern='%Y', zip_file=False)
        try:
            current_year = str(datetime.datetime.now().year)
            self.assertTrue(LinuxCLI().exists('./logs/test.log'))
            self.assertTrue(LinuxCLI().exists('./logs/test2.log'))
            self.assertEqual(0, os.path.getsize('./logs/test.log'))
            self.assertEqual(0, os.path.getsize('./logs/test2.log'))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak'))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak/test.log.' + current_year))
            self.assertTrue(LinuxCLI().exists('./logs/log_bak/test2.log.' + current_year))
        finally:
            LinuxCLI().rm('./logs')
예제 #14
0
    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()
예제 #15
0
    if len(tests) == 0:
        usage(ArgMismatchException('Must specify at least one test with the -t or --tests option'))

    root_dir = LinuxCLI().cmd('pwd').strip()
    print 'Setting root dir to: ' + root_dir

    client_impl = None
    if client_impl_type == 'neutron':
        client_impl = create_neutron_client(**client_args)
    elif client_impl_type == 'midonet':
        client_impl = create_midonet_client(**client_args)
    else:
        raise ArgMismatchException('Invalid client API implementation:' + client_impl_type)

    print 'Setting up log manager'
    log_manager = LogManager(root_dir=log_dir)
    console_log = log_manager.add_stdout_logger(name='tsm-run-console',
                                                log_level=logging.DEBUG if debug is True else logging.INFO)
    log_manager.rollover_logs_fresh(file_filter='*.log')

    console_log.debug('Setting up PTM')
    ptm = PhysicalTopologyManager(root_dir=root_dir, log_manager=log_manager)
    ptm.configure_logging(debug=debug)

    console_log.debug('Setting up VTM')
    vtm = VirtualTopologyManager(physical_topology_manager=ptm, client_api_impl=client_impl, log_manager=log_manager)

    console_log.debug('Setting up TSM')
    tsm = TestSystemManager(ptm, vtm, log_manager=log_manager)
    tsm.configure_logging(debug=debug)
예제 #16
0
 def test_stdout(self):
     lm = LogManager()
     lm.add_format('test', logging.Formatter('%(asctime)s TEST - %(levelname)s - %(message)s'))
     logger = lm.add_stdout_logger(format_name='test')
     logger.info("Test!")
     self.assertTrue(True)
예제 #17
0
    def test_collate_and_slicing_multi(self):

        LinuxCLI().rm('./logs-all')
        LinuxCLI().rm('./logs')
        LinuxCLI().rm('./logs2')
        LinuxCLI().rm('./logs3')
        LinuxCLI().rm('./logs4')
        LinuxCLI().rm('./sliced-logs')

        LinuxCLI(priv=False).mkdir('./logs2')
        LinuxCLI(priv=False).mkdir('./logs3')
        LinuxCLI(priv=False).mkdir('./logs4')

        try:
            now = datetime.datetime.now()
            LinuxCLI(priv=False).write_to_file('./logs2/test2', now.strftime('%Y-%m-%d %H:%M:%S,%f') + ' start\n')
            LinuxCLI(priv=False).write_to_file('./logs3/test3', now.strftime('%Y-%m-%d %H:%M:%S,%f') + ' start\n')
            LinuxCLI(priv=False).write_to_file('./logs4/test3', now.strftime('%Y-%m-%d %H:%M:%S,%f') + ' start\n')

            lm = LogManager('./logs')

            lm.set_default_log_level(logging.DEBUG)
            LOG1 = lm.add_file_logger('test-log')
            LOG2 = lm.add_file_logger('test-log2')
            lm.add_external_log_file(FileLocation('./logs2/test2'), '')
            lm.add_external_log_file(FileLocation('./logs3/test3'), '0')
            lm.add_external_log_file(FileLocation('./logs4/test3'), '1')

            now1 = datetime.datetime.now() + datetime.timedelta(seconds=6)
            for i in range(0, 5):
                LOG1.info('test-log-line: ' + str(i))
                LOG2.info('test-log2-line: ' + str(i))
                LinuxCLI(priv=False).write_to_file('./logs4/test3',
                                                   datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S,%f') +
                                                        ' test-log4\n',
                                                   append=True)
                time.sleep(2)
            LOG1.info('test-log-line: ' + str(5))
            LOG2.info('test-log2-line: ' + str(5))
            now2 = datetime.datetime.now() - datetime.timedelta(seconds=6)

            lm.collate_logs('./logs-all')

            lm.slice_log_files_by_time('./sliced-logs',
                                       start_time=now1,
                                       stop_time=now2,
                                       leeway=3,
                                       collated_only=True)

            self.assertAlmostEquals(2, LinuxCLI().wc('./sliced-logs/test3.1.slice')['lines'], delta=1)
            self.assertAlmostEquals(2, LinuxCLI().wc('./sliced-logs/test-log.slice')['lines'], delta=1)
            self.assertAlmostEquals(2, LinuxCLI().wc('./sliced-logs/test-log2.slice')['lines'], delta=1)

        finally:
            LinuxCLI().rm('./logs')
            LinuxCLI().rm('./sliced-logs')
            LinuxCLI().rm('./logs-all')
            LinuxCLI().rm('./logs2')
            LinuxCLI().rm('./logs3')
            LinuxCLI().rm('./logs4')
예제 #18
0
    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={'zoo1eth0': InterfaceDef('zoo1eth0', linked_bridge='br0'),
                                       #'cass1eth0': InterfaceDef('cass1eth0', linked_bridge='br0'),
                                       'cmp1eth0': InterfaceDef('cmp1eth0', linked_bridge='br0')})
        zoo1_cfg = HostDef('zoo1',
                           interfaces={'eth0': InterfaceDef('eth0', ip_addresses=[IP('10.0.0.2')])})
        #cass1_cfg = HostDef('cass1',
        #                    interfaces={'eth0': InterfaceDef('eth0', ip_addresses=[IP('10.0.0.5')])})
        cmp1_cfg = HostDef('cmp1',
                           interfaces={'eth0': InterfaceDef('eth0', ip_addresses=[IP('10.0.0.8')])})
        net_cfg = HostDef('net')

        zoo1_icfg= ImplementationDef('zoo1', 'PTM.ZookeeperHost', id='1',
                                     zookeeper_ips=['10.0.0.2'])
        #cass1_icfg= ImplementationDef('cass1', 'PTM.CassandraHost', id='1',
        #                              cassandra_ips=['10.0.0.5'],
        #                              init_token="56713727820156410577229101238628035242")
        cmp1_icfg= ImplementationDef('cmp1', 'PTM.ComputeHost', id='1',
                                     zookeeper_ips=['10.0.0.2'],
                                     cassandra_ips=[])#['10.0.0.5'])
        root_icfg = ImplementationDef('cmp1', 'PTM.RootHost')
        net_icfg = ImplementationDef('cmp1', 'PTM.NetworkHost',
                                     zookeeper_ips=['10.0.0.2'])

        root = RootHost('root', ptm)
        zoo1 = ZookeeperHost(zoo1_cfg.name, ptm)
        #cass1 = CassandraHost(cass1_cfg.name, ptm)
        cmp1 = ComputeHost(cmp1_cfg.name, ptm)
        net = NetworkHost(net_cfg.name, ptm)

        log = lm.add_file_logger('test.log', 'test')
        root.set_logger(log)
        zoo1.set_logger(log)
        #cass1.set_logger(log)
        cmp1.set_logger(log)
        net.set_logger(log)

        # Now configure the host with the definition and impl configs
        root.config_from_ptc_def(root_cfg, root_icfg)
        zoo1.config_from_ptc_def(zoo1_cfg, zoo1_icfg)
        #cass1.config_from_ptc_def(cass1_cfg, cass1_icfg)
        cmp1.config_from_ptc_def(cmp1_cfg, cmp1_icfg)
        net.config_from_ptc_def(net_cfg, net_icfg)

        root.link_interface(root.interfaces['zoo1eth0'], zoo1, zoo1.interfaces['eth0'])
        #root.link_interface(root.interfaces['cass1eth0'], cass1, cass1.interfaces['eth0'])
        root.link_interface(root.interfaces['cmp1eth0'], cmp1, cmp1.interfaces['eth0'])

        ptm.hosts_by_name['root'] = root
        ptm.hosts_by_name['zoo1'] = zoo1
        #ptm.hosts_by_name['cass1'] = cass1
        ptm.hosts_by_name['cmp1'] = cmp1
        ptm.hosts_by_name['net'] = net
        ptm.host_by_start_order.append(root)
        ptm.host_by_start_order.append(zoo1)
        #ptm.host_by_start_order.append(cass1)
        ptm.host_by_start_order.append(cmp1)
        ptm.host_by_start_order.append(net)

        for h in ptm.host_by_start_order:
            h.create()

        for h in ptm.host_by_start_order:
            h.boot()

        for h in ptm.host_by_start_order:
            h.net_up()

        for h in ptm.host_by_start_order:
            h.net_finalize()

        for h in ptm.host_by_start_order:
            h.prepare_config()

        for h in ptm.host_by_start_order:
            start_process = ptm.unshare_control('start', h)

            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))

            try:
                h.wait_for_process_start()
            except SubprocessFailedException:
                raw_input("Press Enter to continue...")

        self.assertTrue(LinuxCLI().cmd('midonet-cli --midonet-url="' +
                                       version_config.ConfigMap.get_configured_parameter('param_midonet_api_url') +
                                       '" -A -e "host list"', return_status=True) == 0)


        for h in reversed(ptm.host_by_start_order):
            stop_process = ptm.unshare_control('stop', h)
            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 stop failed with: ' + str(stop_process.returncode))

            h.wait_for_process_stop()

        time.sleep(1)
        self.assertFalse(LinuxCLI().cmd('midonet-cli '
                                        '--midonet-url="http://localhost:8080/midonet-api/" '
                                        '-A -e "hosts list"',
                                        return_status=True) == 0)

        for h in reversed(ptm.host_by_start_order):
            h.net_down()

        for h in reversed(ptm.host_by_start_order):
            h.shutdown()

        for h in reversed(ptm.host_by_start_order):
            h.remove()
예제 #19
0
            command = 'shutdown'
        elif arg in ('-c', '--config-file'):
            ptm_config_file = value
        elif arg in ('-l', '--log-dir'):
            log_dir = value
        elif arg in ('-p', '--print'):
            command = 'print'
        else:
            usage(ArgMismatchException('Invalid argument' + arg))

    if command == '':
        usage(ArgMismatchException('Must specify at least one command option'))

    root_dir = LinuxCLI().cmd('pwd').strip()

    log_manager = LogManager(root_dir=log_dir)
    if command == 'startup':
        log_manager.rollover_logs_fresh(file_filter='ptm*.log')

    ptm = PhysicalTopologyManager(root_dir=root_dir, log_manager=log_manager)
    ptm.configure_logging(debug=debug)
    ptm.configure(ptm_config_file)

    if command == 'startup':
        ptm.startup()
    elif command == 'shutdown':
        ptm.shutdown()
    elif command == 'print':
        ptm.print_config()
    else:
        usage(ArgMismatchException('Command option not recognized: ' + command))
예제 #20
0
    def test_collate(self):
        LinuxCLI().rm('./logs-all')
        LinuxCLI().rm('./logs')
        LinuxCLI().rm('./logs2')
        LinuxCLI().rm('./logs3')
        LinuxCLI().rm('./logs4')

        LinuxCLI(priv=False).mkdir('./logs2')
        LinuxCLI(priv=False).mkdir('./logs3')
        LinuxCLI(priv=False).mkdir('./logs4')

        try:
            LinuxCLI(priv=False).write_to_file('./logs2/test2.log', 'data')
            LinuxCLI(priv=False).write_to_file('./logs3/test3.log', 'data2')
            LinuxCLI(priv=False).write_to_file('./logs4/test3.log', 'data3')

            lm = LogManager('./logs')

            lm.set_default_log_level(logging.DEBUG)
            LOG1 = lm.add_file_logger('test-log.log')
            LOG2 = lm.add_file_logger('test-log2.log')
            lm.add_external_log_file(FileLocation('./logs2/test2.log'), '')
            lm.add_external_log_file(FileLocation('./logs3/test3.log'), '0')
            lm.add_external_log_file(FileLocation('./logs4/test3.log'), '1')

            LOG1.info('test')
            LOG2.info('test2')

            lm.collate_logs('./logs-all')

            self.assertTrue(LinuxCLI().exists('./logs-all/test-log.log'))
            self.assertTrue(LinuxCLI().exists('./logs-all/test-log2.log'))
            self.assertTrue(LinuxCLI().exists('./logs-all/test2.log'))
            self.assertTrue(LinuxCLI().exists('./logs-all/test3.log.0'))
            self.assertTrue(LinuxCLI().exists('./logs-all/test3.log.1'))
        finally:
            LinuxCLI().rm('./logs-all')
            LinuxCLI().rm('./logs')
            LinuxCLI().rm('./logs2')
            LinuxCLI().rm('./logs3')
            LinuxCLI().rm('./logs4')