def setUp(self): self.config = Config('config', 'esx', server='localhost', username='******', password='******', owner='owner', env='env', log_dir='', log_file='') self.second_config = Config('second_config', 'esx', server='localhost', username='******', password='******', owner='owner', env='env', log_dir='', log_file='') fake_virt = Mock() fake_virt.CONFIG_TYPE = 'esx' guests = [Guest('guest1', fake_virt, 1)] test_hypervisor = Hypervisor('test', guestIds=[Guest('guest1', fake_virt, 1)]) assoc = {'hypervisors': [test_hypervisor]} self.fake_domain_list = DomainListReport(self.second_config, guests) self.fake_report = HostGuestAssociationReport(self.config, assoc)
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))
def test_sendVirtGuests(self, create_from_file, connection): self.prepare(create_from_file, connection) config = Config('test', 'libvirt') config.smType = 'sam' manager = Manager.fromOptions(self.logger, self.options, config) manager.sendVirtGuests(self.domain_report, self.options) manager.connection.updateConsumer.assert_called_with( ANY, guest_uuids=[guest.toDict() for guest in self.guestInfo], hypervisor_id=self.hypervisor_id)
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))
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))
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))
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))
def test_hypervisorCheckIn(self, server): options = MagicMock() server.return_value.registration.new_system_user_pass.return_value = { 'system_id': '123' } config = Config('test', 'libvirt', sat_server='localhost') manager = Manager.fromOptions(self.logger, options, config) options.env = "ENV" options.owner = "OWNER" manager.hypervisorCheckIn(self.host_guest_report, options) manager.server_xmlrpc.registration.virt_notify.assert_called_with(ANY, [ [0, "exists", "system", {"identity": "host", "uuid": "0000000000000000"}], [0, "crawl_began", "system", {}], [0, "exists", "domain", { "memory_size": 0, "name": "VM 9c927368-e888-43b4-9cdb-91b10431b258 from libvirt hypervisor ad58b739-5288-4cbc-a984-bd771612d670", "state": "running", "uuid": "9c927368e88843b49cdb91b10431b258", "vcpus": 1, "virt_type": "fully_virtualized" }], [0, "exists", "domain", { "memory_size": 0, "name": "VM d5ffceb5-f79d-41be-a4c1-204f836e144a from libvirt hypervisor ad58b739-5288-4cbc-a984-bd771612d670", "state": "shutoff", "uuid": "d5ffceb5f79d41bea4c1204f836e144a", "vcpus": 1, "virt_type": "fully_virtualized" }], [0, "crawl_ended", "system", {}] ])
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)
def test_remote_hostname_with_username(self, virt): config = Config('test', 'libvirt', server='server', username='******') virt.return_value.getCapabilities.return_value = LIBVIRT_CAPABILITIES_XML virt.return_value.getType.return_value = "LIBVIRT_TYPE" virt.return_value.getVersion.return_value = "VERSION 1337" self.run_virt(config) virt.assert_called_with('qemu+ssh://user@server/system?no_tty=1')
def test_read(self, virt): config = Config('test', 'libvirt') virt.return_value.getCapabilities.return_value = LIBVIRT_CAPABILITIES_XML virt.return_value.getType.return_value = "LIBVIRT_TYPE" virt.return_value.getVersion.return_value = "VERSION 1337" self.run_virt(config) virt.assert_called_with("")
def test_remote_url(self, virt): config = Config('test', 'libvirt', server='abc://server/test') virt.return_value.getCapabilities.return_value = LIBVIRT_CAPABILITIES_XML virt.return_value.getType.return_value = "LIBVIRT_TYPE" virt.return_value.getVersion.return_value = "VERSION 1337" self.run_virt(config) virt.assert_called_with('abc://server/test?no_tty=1')
def test_sendVirtGuests(self, rhsmconnection): config = Config('test', 'libvirt') report = DomainListReport(config, self.guestList, self.hypervisor_id) self.sm.sendVirtGuests(report) self.sm.connection.updateConsumer.assert_called_with( 123, guest_uuids=[g.toDict() for g in self.guestList], hypervisor_id=self.hypervisor_id)
def test_wrong_password(self): options = Options("http://localhost:%s" % TEST_PORT, "username", "wrong") options.force_register = True s = Satellite(self.logger, options) config = Config('test', 'libvirt') report = HostGuestAssociationReport(config, self.mapping) self.assertRaises(SatelliteError, s.hypervisorCheckIn, report, options)
def test_send_data_batch_hypervisor_checkin(self): # This tests that reports of the right type are batched into one # and that the hypervisorCheckIn method of the destination is called # with the right parameters config1 = Config('source1', 'esx') config1.exclude_hosts = [] config1.filter_hosts = [] config2 = Config('source2', 'esx') config2.exclude_hosts = [] config2.filter_hosts = [] virt1 = Mock() virt1.CONFIG_TYPE = 'esx' virt2 = Mock() virt2.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1, Guest.STATE_RUNNING) guest2 = Guest('GUUID2', virt2, Guest.STATE_RUNNING) assoc1 = {'hypervisors': [Hypervisor('hypervisor_id_1', [guest1])]} assoc2 = {'hypervisors': [Hypervisor('hypervisor_id_2', [guest2])]} report1 = HostGuestAssociationReport(config1, assoc1) report2 = HostGuestAssociationReport(config2, assoc2) data_to_send = {'source1': report1, 'source2': report2} source_keys = ['source1', 'source2'] report1 = Mock() report2 = Mock() report1.hash = "report1_hash" report2.hash = "report2_hash" datastore = {'source1': report1, 'source2': report2} manager = Mock() options = Mock() options.print_ = False def check_hypervisorCheckIn(report, options=None): self.assertEquals(report.association['hypervisors'], data_to_send.values) manager.hypervisorCheckIn = Mock(side_effect=check_hypervisorCheckIn) logger = Mock() config = Mock() terminate_event = Mock() interval = 10 # Arbitrary for this test destination_thread = DestinationThread(logger, config, source_keys=source_keys, source=datastore, dest=manager, interval=interval, terminate_event=terminate_event, oneshot=True, options=options) destination_thread._send_data(data_to_send)
def setUp(self): config = Config('test', 'hyperv', server='localhost', username='******', password='******', owner='owner', env='env') self.hyperv = HyperV(self.logger, config)
def setUp(self): config = Config('test', 'vdsm') def fakeSecureConnect(self): return MagicMock() Vdsm._secureConnect = fakeSecureConnect self.vdsm = Vdsm(self.logger, config) self.vdsm.prepare()
def setUp(self): config = Config('test', 'xen', server='localhost', username='******', password='******', owner='owner', env='env') self.xen = Xen(self.logger, config)
def test_new_system(self): options = Options("http://localhost:%s" % TEST_PORT, "username", "password") options.force_register = True s = Satellite(self.logger, options) config = Config('test', 'libvirt') report = HostGuestAssociationReport(config, self.mapping) s.hypervisorCheckIn(report, options)
def setUp(self): config = Config('test', 'esx', server='localhost', username='******', password='******', owner='owner', env='env') self.esx = Esx(self.logger, config, None) # No dest given here
def setUp(self): config = Config('test', 'rhevm', server='localhost', username='******', password='******', owner='owner', env='env') self.rhevm = RhevM(self.logger, config)
def test_mapping_has_no_hostname_when_unavailible(self, virt): config = Config('test', 'libvirt', server='abc://server/test') datastore = Datastore() virt.return_value.getCapabilities.return_value = LIBVIRT_CAPABILITIES_NO_HOSTNAME_XML virt.return_value.getType.return_value = "LIBVIRT_TYPE" virt.return_value.getVersion.return_value = "VERSION 1337" self.run_virt(config, datastore) result = datastore.get(config.name) for host in result.association['hypervisors']: self.assertTrue(host.name is None)
def test_duplicate_reports_are_ignored(self): """ Test that duplicate reports are filtered out when retrieving items from the data store """ source_keys = ['source1', 'source2'] interval = 1 terminate_event = Mock() options = Mock() options.print_ = False config1 = Config('source1', 'esx') virt1 = Mock() virt1.CONFIG_TYPE = 'esx' config = Mock() manager = Mock() guest1 = Guest('GUUID1', virt1, Guest.STATE_RUNNING) report1 = DomainListReport(config1, [guest1], hypervisor_id='hypervisor_id_1') report2 = DomainListReport(config1, [guest1], hypervisor_id='hypervisor_id_2') report3 = DomainListReport(config1, [guest1], hypervisor_id='hypervisor_id_3') datastore = { 'source1': report1, # Not changing, should be excluded later 'source2': report2, # Will change the report sent for source2 } data_to_send = { 'source1': report1, 'source2': report2, } logger = Mock() destination_thread = DestinationThread(logger, config, source_keys=source_keys, source=datastore, dest=manager, interval=interval, terminate_event=terminate_event, oneshot=False, options=options) destination_thread._send_data(data_to_send=data_to_send) expected_hashes = {} for source_key, report in data_to_send.iteritems(): expected_hashes[source_key] = report.hash self.assertEqual(destination_thread.last_report_for_source, expected_hashes) # Pretend there were updates to the datastore from elsewhere destination_thread.source['source2'] = report3 next_data_to_send = destination_thread._get_data() expected_next_data_to_send = {'source2': report3} self.assertEqual(next_data_to_send, expected_next_data_to_send)
def setUpClass(cls, rhsmcert, rhsmconfig): super(TestSubscriptionManager, cls).setUpClass() config = Config('test', 'libvirt') cls.tempdir = tempfile.mkdtemp() with open(os.path.join(cls.tempdir, 'cert.pem'), 'w') as f: f.write("\n") rhsmcert.return_value.subject = {'CN': 123} rhsmconfig.return_value.get.side_effect = lambda group, key: {'consumerCertDir': cls.tempdir}.get(key, DEFAULT) cls.sm = SubscriptionManager(cls.logger, config) cls.sm.cert_uuid = 123
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
def test_remote_url_with_username_and_password(self, virt): config = Config('test', 'libvirt', server='abc://server/test', username='******', password='******') virt.return_value.getCapabilities.return_value = LIBVIRT_CAPABILITIES_XML virt.return_value.getType.return_value = "LIBVIRT_TYPE" virt.return_value.getVersion.return_value = "VERSION 1337" self.run_virt(config) virt.assert_called_with('abc://user@server/test?no_tty=1', ANY, ANY)
def test_hypervisorCheckIn(self): options = Options("http://localhost:%s" % TEST_PORT, "username", "password") options.force_register = True s = Satellite(self.logger, options) config = Config('test', 'libvirt') report = HostGuestAssociationReport(config, self.mapping) result = s.hypervisorCheckIn(report, options) self.assertTrue("failedUpdate" in result) self.assertTrue("created" in result) self.assertTrue("updated" in result)
def test_hypervisorCheckIn(self, create_from_file, connection): self.prepare(create_from_file, connection) config = Config('test', 'libvirt') config.smType = 'sam' manager = Manager.fromOptions(self.logger, self.options, config) self.options.env = "ENV" self.options.owner = "OWNER" manager.hypervisorCheckIn(self.host_guest_report, self.options) manager.connection.hypervisorCheckIn.assert_called_with( self.options.owner, self.options.env, dict( ( host.hypervisorId, [ guest.toDict() for guest in host.guestIds ] ) for host in self.host_guest_report.association['hypervisors']), options=self.options)
def test_creating_channel(self): options = Options("http://localhost:%s" % TEST_PORT, "username", "password") options.force_register = True s = Satellite(self.logger, options) config = Config('test', 'libvirt') report = HostGuestAssociationReport(config, self.mapping) result = s.hypervisorCheckIn(report, options) self.assertTrue(self.fake_server.channel_created) self.assertIsNotNone(self.fake_server.created_system) self.assertTrue("created" in result)
def test_hypervisorCheckIn(self, rhsmconnection): owner = "owner" env = "env" config = Config("test", "esx", owner=owner, env=env) # Ensure the data takes the proper for for the old API rhsmconnection.return_value.has_capability.return_value = False report = HostGuestAssociationReport(config, self.mapping) self.sm.hypervisorCheckIn(report) self.sm.connection.hypervisorCheckIn.assert_called_with( owner, env, dict((host.hypervisorId, [g.toDict() for g in host.guestIds]) for host in self.mapping['hypervisors']), options=None)
def get_config(self): config = None parser = SafeConfigParser() parser.add_section(self.config_name) for field in self.all_fields: value = getattr(self, field) if value: parser.set(self.config_name, field, value) for section in parser.sections(): config = Config.fromParser(section, parser) return config
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