Ejemplo n.º 1
0
    def test_options_virt(self, parseFile, getLogger):
        self.setUpParseFile(parseFile)
        for virt in ['esx', 'hyperv', 'rhevm']:
            self.clearEnv()
            sys.argv = ["virtwho.py", "--%s" % virt, "--%s-owner=owner" % virt,
                        "--%s-env=env" % virt, "--%s-server=localhost" % virt,
                        "--%s-username=username" % virt,
                        "--%s-password=password" % virt]
            _, options = parseOptions()
            self.assertEqual(options.virtType, virt)
            self.assertEqual(options.owner, 'owner')
            self.assertEqual(options.env, 'env')
            self.assertEqual(options.server, 'localhost')
            self.assertEqual(options.username, 'username')
            self.assertEqual(options.password, 'password')

            sys.argv = ["virtwho.py"]
            virt_up = virt.upper()
            os.environ["VIRTWHO_%s" % virt_up] = "1"
            os.environ["VIRTWHO_%s_OWNER" % virt_up] = "xowner"
            os.environ["VIRTWHO_%s_ENV" % virt_up] = "xenv"
            os.environ["VIRTWHO_%s_SERVER" % virt_up] = "xlocalhost"
            os.environ["VIRTWHO_%s_USERNAME" % virt_up] = "xusername"
            os.environ["VIRTWHO_%s_PASSWORD" % virt_up] = "xpassword"
            _, options = parseOptions()
            self.assertEqual(options.virtType, virt)
            self.assertEqual(options.owner, 'xowner')
            self.assertEqual(options.env, 'xenv')
            self.assertEqual(options.server, 'xlocalhost')
            self.assertEqual(options.username, 'xusername')
            self.assertEqual(options.password, 'xpassword')
Ejemplo n.º 2
0
    def test_options_hierarchy_for_reporter_id(self, parseFile, getLogger):
        # Set the value in all three possible locations
        # Mock /etc/virt-who.conf file
        global_conf_dict = {
            'global': {
                'reporter_id': "/etc/virt-who.conf"
            }
        }
        parseFile.return_value = global_conf_dict
        # cli option
        sys.argv = ["virtwho.py", "--reporter-id=cli"]
        # environment var
        os.environ["VIRTWHO_REPORTER_ID"] = "env"
        _, options = parseOptions()
        # cli option should beat environment vars and virt-who.conf
        self.assertEqual(options.reporter_id, "cli")

        sys.argv = ["virtwho.py"]

        _, options = parseOptions()
        self.assertEqual(options.reporter_id, "env")

        self.clearEnv()

        _, options = parseOptions()
        self.assertEqual(options.reporter_id, "/etc/virt-who.conf")

        parseFile.return_value = {'global': {}}

        _, options = parseOptions()
        self.assertEqual(options.reporter_id, util.generateReporterId())
Ejemplo n.º 3
0
    def test_options_hierarchy_for_reporter_id(self, parseFile, getLogger):
        # Set the value in all three possible locations
        # Mock /etc/virt-who.conf file
        global_conf_dict = {'global': {'reporter_id': "/etc/virt-who.conf"}}
        parseFile.return_value = global_conf_dict
        # cli option
        sys.argv = ["virtwho.py", "--reporter-id=cli"]
        # environment var
        os.environ["VIRTWHO_REPORTER_ID"] = "env"
        _, options = parseOptions()
        # cli option should beat environment vars and virt-who.conf
        self.assertEqual(options.reporter_id, "cli")

        sys.argv = ["virtwho.py"]

        _, options = parseOptions()
        self.assertEqual(options.reporter_id, "env")

        self.clearEnv()

        _, options = parseOptions()
        self.assertEqual(options.reporter_id, "/etc/virt-who.conf")

        parseFile.return_value = {'global': {}}

        _, options = parseOptions()
        self.assertEqual(options.reporter_id, util.generateReporterId())
Ejemplo n.º 4
0
    def test_options_debug(self, parseFile, getLogger):
        self.setUpParseFile(parseFile)
        sys.argv = ["virtwho.py", "-d"]
        _, options = parseOptions()
        self.assertTrue(options.debug)

        sys.argv = ["virtwho.py"]
        os.environ["VIRTWHO_DEBUG"] = "1"
        _, options = parseOptions()
        self.assertTrue(options.debug)
Ejemplo n.º 5
0
    def test_options_debug(self, parseFile, getLogger):
        self.setUpParseFile(parseFile)
        sys.argv = ["virtwho.py", "-d"]
        _, options = parseOptions()
        self.assertTrue(options.debug)

        sys.argv = ["virtwho.py"]
        os.environ["VIRTWHO_DEBUG"] = "1"
        _, options = parseOptions()
        self.assertTrue(options.debug)
Ejemplo n.º 6
0
 def test_sm_config_cmd(self):
     os.environ = {}
     sys.argv = ["virt-who", "--sam", "--libvirt"]
     logger, options = parseOptions()
     config = Config("env/cmdline", options.virtType, defaults={}, **options)
     config.checkOptions(logger)
     manager = Manager.fromOptions(logger, options, config)
     self.assertTrue(isinstance(manager, SubscriptionManager))
Ejemplo n.º 7
0
    def test_minimum_interval_options(self, parseFile, getLogger):
        self.setUpParseFile(parseFile)
        sys.argv = ["virtwho.py", "--interval=5"]
        _, options = parseOptions()
        self.assertEqual(options.interval, 60)

        sys.argv = ["virtwho.py"]
        os.environ["VIRTWHO_INTERVAL"] = '1'

        _, options = parseOptions()
        self.assertEqual(options.interval, 60)

        self.clearEnv()
        bad_conf = {'global': {'interval': 1}}
        parseFile.return_value = bad_conf

        _, options = parseOptions()
        self.assertEqual(options.interval, 60)
Ejemplo n.º 8
0
    def test_minimum_interval_options(self, parseFile, getLogger):
        self.setUpParseFile(parseFile)
        sys.argv = ["virtwho.py", "--interval=5"]
        _, options = parseOptions()
        self.assertEqual(options.interval, 60)

        sys.argv = ["virtwho.py"]
        os.environ["VIRTWHO_INTERVAL"] = '1'

        _, options = parseOptions()
        self.assertEqual(options.interval, 60)

        self.clearEnv()
        bad_conf = {'global': {'interval': 1}}
        parseFile.return_value = bad_conf

        _, options = parseOptions()
        self.assertEqual(options.interval, 60)
Ejemplo n.º 9
0
 def test_sm_config_env(self):
     os.environ = {
         "VIRTWHO_SAM": '1',
         "VIRTWHO_LIBVIRT": '1'
     }
     sys.argv = ["virt-who"]
     logger, options = parseOptions()
     config = Config("env/cmdline", options.virtType, defaults={}, **options)
     config.checkOptions(logger)
     manager = Manager.fromOptions(logger, options, config)
     self.assertTrue(isinstance(manager, SubscriptionManager))
Ejemplo n.º 10
0
 def test_default_cmdline_options(self, parseFile, getLogger):
     self.setUpParseFile(parseFile)
     sys.argv = ["virtwho.py"]
     _, options = parseOptions()
     self.assertFalse(options.debug)
     self.assertFalse(options.background)
     self.assertFalse(options.oneshot)
     self.assertEqual(options.interval, 3600)
     self.assertEqual(options.smType, 'sam')
     self.assertEqual(options.virtType, None)
     self.assertEqual(options.reporter_id, util.generateReporterId())
Ejemplo n.º 11
0
 def test_default_cmdline_options(self, parseFile, getLogger):
     self.setUpParseFile(parseFile)
     sys.argv = ["virtwho.py"]
     _, options = parseOptions()
     self.assertFalse(options.debug)
     self.assertFalse(options.background)
     self.assertFalse(options.oneshot)
     self.assertEqual(options.interval, 3600)
     self.assertEqual(options.smType, 'sam')
     self.assertEqual(options.virtType, None)
     self.assertEqual(options.reporter_id, util.generateReporterId())
Ejemplo n.º 12
0
 def test_satellite_config_cmd(self):
     os.environ = {}
     sys.argv = ["virt-who", "--satellite",
                 "--satellite-server=sat.example.com",
                 "--satellite-username=username",
                 "--satellite-password=password",
                 "--libvirt"]
     logger, options = parseOptions()
     config = Config("env/cmdline", options.virtType, defaults={}, **options)
     config.checkOptions(logger)
     manager = Manager.fromOptions(logger, options, config)
     self.assertTrue(isinstance(manager, Satellite))
Ejemplo n.º 13
0
    def test_options_virt_satellite(self, parseFile, getLogger):
        self.setUpParseFile(parseFile)
        for virt in ['esx', 'hyperv', 'rhevm']:
            self.clearEnv()
            sys.argv = ["virtwho.py",
                        "--satellite",
                        "--satellite-server=localhost",
                        "--satellite-username=username",
                        "--satellite-password=password",
                        "--%s" % virt,
                        "--%s-server=localhost" % virt,
                        "--%s-username=username" % virt,
                        "--%s-password=password" % virt]
            _, options = parseOptions()
            self.assertEqual(options.virtType, virt)
            self.assertEqual(options.owner, '')
            self.assertEqual(options.env, '')
            self.assertEqual(options.server, 'localhost')
            self.assertEqual(options.username, 'username')
            self.assertEqual(options.password, 'password')

            sys.argv = ["virtwho.py"]
            virt_up = virt.upper()
            os.environ["VIRTWHO_SATELLITE"] = "1"
            os.environ["VIRTWHO_SATELLITE_SERVER"] = "xlocalhost"
            os.environ["VIRTWHO_SATELLITE_USERNAME"] = "******"
            os.environ["VIRTWHO_SATELLITE_PASSWORD"] = "******"
            os.environ["VIRTWHO_%s" % virt_up] = "1"
            os.environ["VIRTWHO_%s_SERVER" % virt_up] = "xlocalhost"
            os.environ["VIRTWHO_%s_USERNAME" % virt_up] = "xusername"
            os.environ["VIRTWHO_%s_PASSWORD" % virt_up] = "xpassword"
            _, options = parseOptions()
            self.assertEqual(options.virtType, virt)
            self.assertEqual(options.owner, '')
            self.assertEqual(options.env, '')
            self.assertEqual(options.server, 'xlocalhost')
            self.assertEqual(options.username, 'xusername')
            self.assertEqual(options.password, 'xpassword')
Ejemplo n.º 14
0
    def test_options_virt_satellite(self, parseFile, getLogger):
        self.setUpParseFile(parseFile)
        for virt in ['esx', 'hyperv', 'rhevm']:
            self.clearEnv()
            sys.argv = [
                "virtwho.py", "--satellite", "--satellite-server=localhost",
                "--satellite-username=username",
                "--satellite-password=password",
                "--%s" % virt,
                "--%s-server=localhost" % virt,
                "--%s-username=username" % virt,
                "--%s-password=password" % virt
            ]
            _, options = parseOptions()
            self.assertEqual(options.virtType, virt)
            self.assertEqual(options.owner, '')
            self.assertEqual(options.env, '')
            self.assertEqual(options.server, 'localhost')
            self.assertEqual(options.username, 'username')
            self.assertEqual(options.password, 'password')

            sys.argv = ["virtwho.py"]
            virt_up = virt.upper()
            os.environ["VIRTWHO_SATELLITE"] = "1"
            os.environ["VIRTWHO_SATELLITE_SERVER"] = "xlocalhost"
            os.environ["VIRTWHO_SATELLITE_USERNAME"] = "******"
            os.environ["VIRTWHO_SATELLITE_PASSWORD"] = "******"
            os.environ["VIRTWHO_%s" % virt_up] = "1"
            os.environ["VIRTWHO_%s_SERVER" % virt_up] = "xlocalhost"
            os.environ["VIRTWHO_%s_USERNAME" % virt_up] = "xusername"
            os.environ["VIRTWHO_%s_PASSWORD" % virt_up] = "xpassword"
            _, options = parseOptions()
            self.assertEqual(options.virtType, virt)
            self.assertEqual(options.owner, '')
            self.assertEqual(options.env, '')
            self.assertEqual(options.server, 'xlocalhost')
            self.assertEqual(options.username, 'xusername')
            self.assertEqual(options.password, 'xpassword')
Ejemplo n.º 15
0
 def test_satellite_config_env(self):
     os.environ = {
         "VIRTWHO_SATELLITE": '1',
         "VIRTWHO_SATELLITE_SERVER": 'sat.example.com',
         "VIRTWHO_SATELLITE_USERNAME": '******',
         "VIRTWHO_SATELLITE_PASSWORD": '******',
         "VIRTWHO_LIBVIRT": '1'
     }
     sys.argv = ["virt-who"]
     logger, options = parseOptions()
     config = Config("env/cmdline", options.virtType, defaults={}, **options)
     config.checkOptions(logger)
     manager = Manager.fromOptions(logger, options, config)
     self.assertTrue(isinstance(manager, Satellite))
Ejemplo n.º 16
0
 def test_satellite_config_cmd(self):
     os.environ = {}
     sys.argv = [
         "virt-who", "--satellite", "--satellite-server=sat.example.com",
         "--satellite-username=username", "--satellite-password=password",
         "--libvirt"
     ]
     logger, options = parseOptions()
     config = Config("env/cmdline",
                     options.virtType,
                     defaults={},
                     **options)
     config.checkOptions(logger)
     manager = Manager.fromOptions(logger, options, config)
     self.assertTrue(isinstance(manager, Satellite))
Ejemplo n.º 17
0
    def test_options_virt(self, parseFile, getLogger):
        self.setUpParseFile(parseFile)
        for virt in ['esx', 'hyperv', 'rhevm']:
            self.clearEnv()
            sys.argv = [
                "virtwho.py",
                "--%s" % virt,
                "--%s-owner=owner" % virt,
                "--%s-env=env" % virt,
                "--%s-server=localhost" % virt,
                "--%s-username=username" % virt,
                "--%s-password=password" % virt
            ]
            _, options = parseOptions()
            self.assertEqual(options.virtType, virt)
            self.assertEqual(options.owner, 'owner')
            self.assertEqual(options.env, 'env')
            self.assertEqual(options.server, 'localhost')
            self.assertEqual(options.username, 'username')
            self.assertEqual(options.password, 'password')

            sys.argv = ["virtwho.py"]
            virt_up = virt.upper()
            os.environ["VIRTWHO_%s" % virt_up] = "1"
            os.environ["VIRTWHO_%s_OWNER" % virt_up] = "xowner"
            os.environ["VIRTWHO_%s_ENV" % virt_up] = "xenv"
            os.environ["VIRTWHO_%s_SERVER" % virt_up] = "xlocalhost"
            os.environ["VIRTWHO_%s_USERNAME" % virt_up] = "xusername"
            os.environ["VIRTWHO_%s_PASSWORD" % virt_up] = "xpassword"
            _, options = parseOptions()
            self.assertEqual(options.virtType, virt)
            self.assertEqual(options.owner, 'xowner')
            self.assertEqual(options.env, 'xenv')
            self.assertEqual(options.server, 'xlocalhost')
            self.assertEqual(options.username, 'xusername')
            self.assertEqual(options.password, 'xpassword')
Ejemplo n.º 18
0
 def test_satellite_config_env(self):
     os.environ = {
         "VIRTWHO_SATELLITE": '1',
         "VIRTWHO_SATELLITE_SERVER": 'sat.example.com',
         "VIRTWHO_SATELLITE_USERNAME": '******',
         "VIRTWHO_SATELLITE_PASSWORD": '******',
         "VIRTWHO_LIBVIRT": '1'
     }
     sys.argv = ["virt-who"]
     logger, options = parseOptions()
     config = Config("env/cmdline",
                     options.virtType,
                     defaults={},
                     **options)
     config.checkOptions(logger)
     manager = Manager.fromOptions(logger, options, config)
     self.assertTrue(isinstance(manager, Satellite))
Ejemplo n.º 19
0
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