示例#1
0
    def test_send_current_report(self, fromConfig, fromOptions, getLogger,
                                 time):
        initial = 10
        time.side_effect = [initial, initial]

        fromOptions.return_value = Mock()
        options = Mock()
        options.interval = 6
        options.oneshot = True
        options.print_ = False
        options.log_dir = ''
        options.log_file = ''
        virtwho = Executor(Mock(), options, config_dir="/nonexistant")
        virtwho.oneshot_remaining = ['config_name']

        config = Mock()
        config.hash = "config_hash"
        config.name = "config_name"

        virtwho.send = Mock()
        virtwho.send.return_value = True
        report = HostGuestAssociationReport(config, {'hypervisors': {}})
        report.state = AbstractVirtReport.STATE_PROCESSING
        virtwho.queued_reports[config.name] = report

        virtwho.send_current_report()

        def check_report_state(report):
            report.state = AbstractVirtReport.STATE_FINISHED

        virtwho.check_report_state = Mock(side_effect=check_report_state)
        virtwho.check_reports_state()

        virtwho.send.assert_called_with(report)
        self.assertEquals(virtwho.send_after, initial + 60)
示例#2
0
    def test_same_report_filtering(self, fromConfig, fromOptions, getLogger):
        def fake_virts(logger, config):
            new_fake_virt = Mock()
            new_fake_virt.config.name = config.name
            return new_fake_virt

        fromConfig.side_effect = fake_virts
        options = Mock()
        options.interval = 0
        options.oneshot = True
        options.print_ = False
        options.log_dir = ''
        options.log_file = ''
        virtwho = Executor(self.logger, options, config_dir="/nonexistant")

        queue = Queue()
        # Create another report with same hash
        report2 = HostGuestAssociationReport(self.config,
                                             self.fake_report.association)
        self.assertEqual(self.fake_report.hash, report2.hash)

        def send(report):
            report.state = AbstractVirtReport.STATE_FINISHED
            # Put second report when the first is done
            queue.put(report2)
            return True

        virtwho.send = Mock(side_effect=send)
        virtwho.queue = queue
        virtwho.retry_after = 1
        virtwho.configManager.addConfig(self.config)
        queue.put(self.fake_report)
        virtwho.run()

        self.assertEquals(virtwho.send.call_count, 1)
示例#3
0
    def test_sending_guests(self, parseFile, fromOptions, fromConfig,
                            getLogger):
        self.setUpParseFile(parseFile)
        options = Mock()
        options.oneshot = True
        options.interval = 0
        options.print_ = False
        fake_virt = Mock()
        fake_virt.CONFIG_TYPE = 'esx'
        test_hypervisor = Hypervisor('test',
                                     guestIds=[Guest('guest1', fake_virt, 1)])
        association = {'hypervisors': [test_hypervisor]}
        options.log_dir = ''
        options.log_file = ''
        getLogger.return_value = sentinel.logger
        fromConfig.return_value.config.name = 'test'
        virtwho = Executor(self.logger, options, config_dir="/nonexistant")
        config = Config("test",
                        "esx",
                        server="localhost",
                        username="******",
                        password="******",
                        owner="owner",
                        env="env")
        virtwho.configManager.addConfig(config)
        virtwho.queue = Queue()
        virtwho.queue.put(HostGuestAssociationReport(config, association))
        virtwho.run()

        fromConfig.assert_called_with(sentinel.logger, config)
        self.assertTrue(fromConfig.return_value.start.called)
        fromOptions.assert_called_with(self.logger, options, ANY)
示例#4
0
 def mock_virtwho(self):
     options = Mock()
     options.interval = 6
     options.oneshot = False
     options.print_ = False
     virtwho = Executor(Mock(), options, config_dir="/nonexistant")
     config = Config("env/cmdline", 'libvirt')
     virtwho.configManager.addConfig(config)
     virtwho.queue = Mock()
     virtwho.send = Mock()
     return virtwho
示例#5
0
    def test_send_current_report_with_429(self, fromConfig, fromOptions,
                                          getLogger, time):
        initial = 10
        retry_after = 2
        time.return_value = initial

        fromOptions.return_value = Mock()
        options = Mock()
        options.interval = 6
        options.oneshot = True
        options.print_ = False
        options.log_dir = ''
        options.log_file = ''
        virtwho = Executor(Mock(), options, config_dir="/nonexistant")

        config = Mock()
        config.hash = "config_hash"
        config.name = "config_name"

        report = HostGuestAssociationReport(config, {'hypervisors': []})
        report.state = AbstractVirtReport.STATE_PROCESSING
        virtwho.queued_reports[config.name] = report

        virtwho.send = Mock()
        virtwho.send.return_value = False
        virtwho.send.side_effect = ManagerThrottleError(retry_after)

        virtwho.send_current_report()

        virtwho.send.assert_called_with(report)
        self.assertEquals(virtwho.send_after, initial + 60)
        self.assertEquals(len(virtwho.queued_reports), 1)

        retry_after = 120
        virtwho.send.side_effect = ManagerThrottleError(retry_after)
        virtwho.send_current_report()
        virtwho.send.assert_called_with(report)
        self.assertEquals(virtwho.send_after, initial + retry_after * 2)
        self.assertEquals(len(virtwho.queued_reports), 1)

        def finish(x):
            report.state = AbstractVirtReport.STATE_FINISHED
            return True

        virtwho.send.side_effect = finish
        virtwho.send_current_report()
        retry_after = 60
        self.assertEquals(virtwho.retry_after, retry_after)
        self.assertEquals(virtwho.send_after, initial + retry_after)
        self.assertEquals(len(virtwho.queued_reports), 0)
示例#6
0
    def test_report_hash_added_after_send(self, fromConfig, fromOptions,
                                          getLogger):
        # Side effect for fromConfig
        def fake_virts(logger, config):
            new_fake_virt = Mock()
            new_fake_virt.config.name = config.name
            return new_fake_virt

        fromConfig.side_effect = fake_virts
        options = Mock()
        options.interval = 0
        options.oneshot = True
        options.print_ = False
        options.log_file = ''
        options.log_dir = ''
        virtwho = Executor(self.logger, options, config_dir="/nonexistant")

        def send(report):
            report.state = AbstractVirtReport.STATE_FINISHED
            return True

        virtwho.send = Mock(side_effect=send)
        queue = Queue()
        virtwho.queue = queue
        virtwho.retry_after = 1
        virtwho.configManager.addConfig(self.config)
        virtwho.configManager.addConfig(self.second_config)
        queue.put(self.fake_report)
        queue.put(self.fake_domain_list)
        virtwho.run()

        self.assertEquals(virtwho.send.call_count, 2)
        self.assertEqual(virtwho.last_reports_hash[self.config.name],
                         self.fake_report.hash)
        self.assertEqual(virtwho.last_reports_hash[self.second_config.name],
                         self.fake_domain_list.hash)
示例#7
0
def main():
    logger = effective_config = None
    try:
        logger, effective_config = parse_options()
        # We now have the effective_config
    except OptionError as e:
        print(str(e), file=sys.stderr)
        exit(1, status="virt-who can't be started: %s" % str(e))

    lock = PIDLock(PIDFILE)
    if lock.is_locked():
        msg = "virt-who seems to be already running. If not, remove %s" % \
              PIDFILE
        print(msg, file=sys.stderr)
        exit(1, status=msg)

    if not effective_config[VW_GLOBAL].is_valid():
        message = "Required section 'global' is invalid:\n"
        message += "\n".join([
            msg
            for (level, msg) in effective_config[VW_GLOBAL].validation_messages
        ])
        message += "\n"
        exit(1, "virt-who can't be started: %s" % message)

    valid_virt_sections = [(name, section)
                           for (name,
                                section) in effective_config.virt_sections()
                           if section.is_valid()]

    if not valid_virt_sections:
        err = "virt-who can't be started: no valid configuration found"
        logger.error(err)
        exit(1, err)

    global executor
    has_error = False
    try:
        executor = Executor(logger, effective_config)
    except (InvalidKeyFile, InvalidPasswordFormat) as e:
        logger.error(str(e))
        exit(1, "virt-who can't be started: %s" % str(e))

    if len(executor.dest_to_source_mapper.dests) == 0:
        if has_error:
            err = "virt-who can't be started: no valid destination found"
            logger.error(err)
            exit(1, err)

    if len(effective_config[VW_GLOBAL]['configs']) > 0:
        # When config file is provided using -c or --config, then other config
        # files in /etc/virt-who.d are ignored. When it is not possible to read
        # any configuration file, then virt-who should be terminated
        cli_config_file_readable = False
        for file_name in effective_config[VW_GLOBAL]['configs']:
            if os.path.isfile(file_name):
                cli_config_file_readable = True

        if cli_config_file_readable is False:
            err = 'No valid configuration file provided using -c/--config'
            logger.error(err)
            exit(1, "virt-who can't be started: %s" % str(err))

    for name, config in executor.dest_to_source_mapper.configs:
        logger.info('Using configuration "%s" ("%s" mode)', name,
                    config['type'])

    logger.info("Using reporter_id='%s'",
                effective_config[VW_GLOBAL]['reporter_id'])
    log.closeLogger(logger)

    with lock:
        signal.signal(signal.SIGHUP, reload)
        signal.signal(signal.SIGTERM, atexit_fn)

        executor.logger = logger = log.getLogger(name='main', queue=True)

        sd_notify("READY=1\nMAINPID=%d" % os.getpid())
        while True:
            try:
                return _main(executor)
            except ReloadRequest:
                logger.info("Reloading")
                continue
            except ExitRequest as e:
                exit(e.code, status=e.message)
示例#8
0
文件: main.py 项目: shihliu/virt-who
def main():
    logger = options = None
    try:
        logger, options = parseOptions()
    except OptionError as e:
        print >>sys.stderr, str(e)
        exit(1, status="virt-who can't be started: %s" % str(e))

    lock = PIDLock(PIDFILE)
    if lock.is_locked():
        msg = "virt-who seems to be already running. If not, remove %s" % PIDFILE
        print >>sys.stderr, msg
        exit(1, status=msg)

    global executor
    try:
        executor = Executor(logger, options)
    except (InvalidKeyFile, InvalidPasswordFormat) as e:
        logger.error(str(e))
        exit(1, "virt-who can't be started: %s" % str(e))

    if options.virtType is not None:
        config = Config("env/cmdline", options.virtType, executor.configManager._defaults, **options)
        try:
            config.checkOptions(logger)
        except InvalidOption as e:
            err = "virt-who can't be started: %s" % str(e)
            logger.error(err)
            exit(1, err)
        executor.configManager.addConfig(config)
    has_error = False
    for conffile in options.configs:
        try:
            executor.configManager.readFile(conffile)
        except InvalidPasswordFormat as e:
            err = "virt-who can't be started: %s" % str(e)
            logger.error(err)
            exit(1, err)
        except Exception as e:
            logger.error('Config file "%s" skipped because of an error: %s', conffile, str(e))
            has_error = True

    if len(executor.configManager.configs) == 0:
        if has_error:
            err = "virt-who can't be started: no valid configuration found"
            logger.error(err)
            exit(1, err)
        # In order to keep compatibility with older releases of virt-who,
        # fallback to using libvirt as default virt backend
        logger.info("No configurations found, using libvirt as backend")
        executor.configManager.addConfig(Config("env/cmdline", "libvirt"))

    for config in executor.configManager.configs:
        if config.name is None:
            logger.info('Using commandline or sysconfig configuration ("%s" mode)', config.type)
        else:
            logger.info('Using configuration "%s" ("%s" mode)', config.name, config.type)

    logger.info("Using reporter_id='%s'", options.reporter_id)
    log.closeLogger(logger)
    if options.background:
        locker = lambda: daemon.DaemonContext(pidfile=lock)  # flake8: noqa
    else:
        locker = lambda: lock  # flake8: noqa

    with locker():
        signal.signal(signal.SIGHUP, reload)
        signal.signal(signal.SIGTERM, atexit_fn)

        executor.logger = logger = log.getLogger(name='main', config=None, queue=True)

        sd_notify("READY=1\nMAINPID=%d" % os.getpid())
        while True:
            try:
                return _main(executor)
            except ReloadRequest:
                logger.info("Reloading")
                continue
示例#9
0
def main():
    logger = options = None
    try:
        logger, options = parse_options()
        # We now have the effective_config
    except OptionError as e:
        print(str(e), file=sys.stderr)
        exit(1, status="virt-who can't be started: %s" % str(e))

    lock = PIDLock(PIDFILE)
    if lock.is_locked():
        msg = "virt-who seems to be already running. If not, remove %s" % \
              PIDFILE
        print(msg, file=sys.stderr)
        exit(1, status=msg)

    if not options[VW_GLOBAL].is_valid():
        message = "Required section 'global' is invalid:\n"
        message += "\n".join([msg for (level, msg) in options[VW_GLOBAL].validation_messages])
        message += "\n"
        exit(1, "virt-who can't be started: %s" % message)

    valid_virt_sections = [(name, section) for (name, section) in options.virt_sections()
                           if section.is_valid()]

    if not valid_virt_sections:
        err = "virt-who can't be started: no valid configuration found"
        logger.error(err)
        exit(1, err)

    global executor
    has_error = False
    try:
        executor = Executor(logger, options)
    except (InvalidKeyFile, InvalidPasswordFormat) as e:
        logger.error(str(e))
        exit(1, "virt-who can't be started: %s" % str(e))

    if len(executor.dest_to_source_mapper.dests) == 0:
        if has_error:
            err = "virt-who can't be started: no valid destination found"
            logger.error(err)
            exit(1, err)

    for name, config in executor.dest_to_source_mapper.configs:
        logger.info('Using configuration "%s" ("%s" mode)', name,
                    config['type'])

    logger.info("Using reporter_id='%s'", options[VW_GLOBAL]['reporter_id'])
    log.closeLogger(logger)

    with lock:
        signal.signal(signal.SIGHUP, reload)
        signal.signal(signal.SIGTERM, atexit_fn)

        executor.logger = logger = log.getLogger(name='main', queue=True)

        sd_notify("READY=1\nMAINPID=%d" % os.getpid())
        while True:
            try:
                return _main(executor)
            except ReloadRequest:
                logger.info("Reloading")
                continue
            except ExitRequest as e:
                exit(e.code, status=e.message)