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 + options.interval)
def test_send_data_poll_hypervisor_async_result(self): # This test's that when we have an async result from the server, # we poll for the result # Setup the test data config1, d1 = self.create_fake_config('source1', **self.default_config_args) config2, d2 = self.create_fake_config('source2', **self.default_config_args) virt1 = Mock() virt1.CONFIG_TYPE = 'esx' virt2 = Mock() virt2.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) guest2 = Guest('GUUID2', virt2.CONFIG_TYPE, 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'] batch_report1 = Mock() # The "report" to check status batch_report1.state = AbstractVirtReport.STATE_CREATED datastore = {'source1': report1, 'source2': report2} manager = Mock() items = [ManagerThrottleError(), ManagerThrottleError(), ManagerThrottleError(), AbstractVirtReport.STATE_FINISHED] manager.check_report_state = Mock(side_effect=self.check_report_state_closure(items)) logger = Mock() config, d = self.create_fake_config('test', **self.default_config_args) terminate_event = Mock() interval = 10 # Arbitrary for this test options = Mock() options.print_ = False destination_thread = DestinationThread(logger, config, source_keys=source_keys, source=datastore, dest=manager, interval=interval, terminate_event=terminate_event, oneshot=False, options=self.options) # In this test we want to see that the wait method is called when we # expect and with what parameters we expect destination_thread.wait = Mock() destination_thread.is_terminated = Mock(return_value=False) destination_thread.check_report_status(batch_report1) # There should be three waits, one after the job is submitted with duration of # MinimumJobPollingInterval. The second and third with duration MinimumJobPollInterval * 2 # (and all subsequent calls as demonstrated by the third wait) destination_thread.wait.assert_has_calls([ call(wait_time=MinimumJobPollInterval), call(wait_time=MinimumJobPollInterval * 2), call(wait_time=MinimumJobPollInterval * 2)])
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)
def test_send_data_poll_async_429(self): # This test's that when a 429 is detected during async polling # we wait for the amount of time specified source_keys = ['source1', 'source2'] config1, d1 = self.create_fake_config('source1', **self.default_config_args) config2, d2 = self.create_fake_config('source2', **self.default_config_args) virt1 = Mock() virt1.CONFIG_TYPE = 'esx' virt2 = Mock() virt2.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) guest2 = Guest('GUUID2', virt2.CONFIG_TYPE, 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) datastore = {'source1': report1, 'source2': report2} data_to_send = {'source1': report1, 'source2': report2} config, d = self.create_fake_config('test', **self.default_config_args) error_to_throw = ManagerThrottleError(retry_after=62) manager = Mock() manager.hypervisorCheckIn = Mock(side_effect=[error_to_throw, report1]) expected_wait_calls = [call(wait_time=error_to_throw.retry_after)] logger = Mock() terminate_event = Mock() interval = 10 # Arbitrary for this test options = Mock() options.print_ = False destination_thread = DestinationThread(logger, config, source_keys=source_keys, source=datastore, dest=manager, interval=interval, terminate_event=terminate_event, oneshot=False, options=self.options) destination_thread.wait = Mock() destination_thread.is_terminated = Mock(return_value=False) destination_thread.record_status = Mock() destination_thread._send_data(data_to_send) destination_thread.wait.assert_has_calls(expected_wait_calls)
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, d1 = self.create_fake_config('source1', **self.default_config_args) d1['exclude_hosts'] = [] d1['filter_hosts'] = [] config2, d2 = self.create_fake_config('source2', **self.default_config_args) d2['exclude_hosts'] = [] d2['filter_hosts'] = [] virt1 = Mock() virt1.CONFIG_TYPE = 'esx' virt2 = Mock() virt2.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) guest2 = Guest('GUUID2', virt2.CONFIG_TYPE, 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.assertEqual(report.association['hypervisors'], data_to_send.values) manager.hypervisorCheckIn = Mock(side_effect=check_hypervisorCheckIn) logger = Mock() config, d = self.create_fake_config('test', **self.default_config_args) 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=self.options) destination_thread.record_status = Mock() destination_thread._send_data(data_to_send)
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 + options.interval) self.assertEquals(len(virtwho.queued_reports), 0)
def test_per_config_options_encrypted(self, can_write): options = Mock() options.force_register = True can_write.return_value = True with tempfile.NamedTemporaryFile() as tmp: password.Password.KEYFILE = tmp.name config_dict = { "sat_server": "http://localhost:%s" % TEST_PORT, "sat_username": "******", "sat_encrypted_password": hexlify(password.Password.encrypt('password')), "type": "libvirt", } config = VirtConfigSection.from_dict(config_dict, 'test', None) config.validate() dests = DestinationToSourceMapper.parse_dests_from_dict( config._values) self.assertEqual(len(dests), 1) dest_info = dests.pop() s = Manager.fromInfo(self.logger, options, dest_info) self.assertTrue(isinstance(s, Satellite)) 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_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)
def test_job_status(self, rhsmconnection): rhsmconnection.return_value.has_capability.return_value = True config = VirtConfigSection.from_dict( { 'type': 'libvirt', 'owner': 'owner' }, 'test', None) report = HostGuestAssociationReport(config, self.mapping) self.sm.hypervisorCheckIn(report) rhsmconnection.return_value.getJob.return_value = { 'state': 'RUNNING', } self.sm.check_report_state(report) self.assertEqual(report.state, AbstractVirtReport.STATE_PROCESSING) def host(_host): return {'uuid': _host} # self.sm.connection.return_value.getJob.return_value = { rhsmconnection.return_value.getJob.return_value = { 'state': 'FINISHED', 'resultData': { 'failedUpdate': ["failed"], 'updated': [host('123')], 'created': [host('456')], 'unchanged': [host('789')] } } self.sm.logger = MagicMock() self.sm.check_report_state(report) # calls: authenticating + checking job status + 1 line about the number of unchanged self.assertEqual(self.sm.logger.debug.call_count, 3) self.assertEqual(report.state, AbstractVirtReport.STATE_FINISHED)
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_hypervisorCheckInAsync(self, rhsmconnection): owner = 'owner' env = 'env' config = VirtConfigSection.from_dict( { 'type': 'libvirt', 'owner': owner, 'env': env }, 'test', None) # Ensure we try out the new API rhsmconnection.return_value.has_capability.return_value = True self.sm.logger = MagicMock() report = HostGuestAssociationReport(config, self.mapping) self.sm.hypervisorCheckIn(report) expected = { 'hypervisors': [h.toDict() for h in self.mapping['hypervisors']] } self.sm.connection.hypervisorCheckIn.assert_called_with('owner', 'env', expected, options=None) self.sm.logger.warning.assert_called_with( "The hypervisor id '123' is assigned to 2 different systems. " "Only one will be recorded at the server.") self.sm.connection.return_value.has_capability = MagicMock( return_value=False)
def _get_report(self): if self.isHypervisor(): return HostGuestAssociationReport(self.config, self._getHostGuestMapping()) else: return DomainListReport(self.config, self._listDomains(), self._remote_host_id())
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_new_system(self): options = Mock() options.force_register = True s = Satellite(self.logger, options) config, d = self.create_fake_config('test', **TestSatellite.default_config_args) report = HostGuestAssociationReport(config, self.mapping) s.hypervisorCheckIn(report, options)
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_wrong_server(self): options = Mock() s = Satellite(self.logger, options) config, d = self.create_fake_config('test', **TestSatellite.default_config_args) d['sat_server'] = "wrong_server" d['sat_username'] = "******" d['sat_password'] = "******" report = HostGuestAssociationReport(config, self.mapping) self.assertRaises(SatelliteError, s.hypervisorCheckIn, report, options)
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 test_wrong_password(self): options = Mock() options.force_register = True s = Satellite(self.logger, options) config, d = self.create_fake_config('test', **TestSatellite.default_config_args) d['sat_server'] = "http://localhost:%s" % TEST_PORT d['sat_username'] = "******" d['sat_password'] = "******" report = HostGuestAssociationReport(config, self.mapping) self.assertRaises(SatelliteError, s.hypervisorCheckIn, report, options)
def test_per_config_options(self): options = Mock() options.force_register = True config, d = self.create_fake_config('test', **TestSatellite.default_config_args) s = Satellite(self.logger, options) 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_using_existing_channel(self): options = Mock() options.force_register = True s = Satellite(self.logger, options) self.fake_server.channel_created = True config, d = self.create_fake_config('test', **TestSatellite.default_config_args) 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_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 _get_report(self): if self.status: # this will make the authentication happen # a failure will get picked up in the _run method self.statusConfirmConnection() return StatusReport(self.config) if self.isHypervisor(): return HostGuestAssociationReport(self.config, self._getHostGuestMapping()) else: return DomainListReport(self.config, self._listDomains(), self._remote_host_id())
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, 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 test_status_pending_hypervisor_async_result(self): # This test's that when we have an async result from the server, # we poll for the status on the interval until we get completed result # Setup the test data config1, d1 = self.create_fake_config('source1', **self.default_config_args) config2, d2 = self.create_fake_config('source2', **self.default_config_args) virt1 = Mock() virt1.CONFIG_TYPE = 'esx' virt2 = Mock() virt2.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) guest2 = Guest('GUUID2', virt2.CONFIG_TYPE, 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) report1.job_id = 'job1' report2.job_id = 'job2' data_to_send = {'source1': report1, 'source2': report2} source_keys = ['source1', 'source2'] batch_report1 = Mock() # The "report" to check status batch_report1.state = AbstractVirtReport.STATE_CREATED datastore = {'source1': report1, 'source2': report2} manager = Mock() items = [AbstractVirtReport.STATE_PROCESSING, AbstractVirtReport.STATE_PROCESSING, AbstractVirtReport.STATE_PROCESSING, AbstractVirtReport.STATE_FINISHED, AbstractVirtReport.STATE_FINISHED] manager.check_report_state = Mock(side_effect=self.check_report_state_closure(items)) logger = Mock() config, d = self.create_fake_config('test', **self.default_config_args) terminate_event = Mock() interval = 10 # Arbitrary for this test options = Mock() options.print_ = False destination_thread = DestinationThread(logger, config, source_keys=source_keys, source=datastore, dest=manager, interval=interval, terminate_event=terminate_event, oneshot=False, options=self.options) # In this test we want to see that the wait method is called when we # expect and with what parameters we expect destination_thread.wait = Mock() destination_thread.is_terminated = Mock(return_value=False) destination_thread.submitted_report_and_hash_for_source ={'source1':(report1, 'hash1'),'source2':(report2, 'hash2')} reports = destination_thread._get_data_common(source_keys) self.assertEqual(0, len(reports)) reports = destination_thread._get_data_common(source_keys) self.assertEqual(1, len(reports)) reports = destination_thread._get_data_common(source_keys) self.assertEqual(2, len(reports))
class TestManager(TestBase): """ Test of all available subscription managers. """ guest1 = Guest('9c927368-e888-43b4-9cdb-91b10431b258', xvirt, Guest.STATE_RUNNING) guest2 = Guest('d5ffceb5-f79d-41be-a4c1-204f836e144a', xvirt, Guest.STATE_SHUTOFF) guestInfo = [guest1] hypervisor_id = "HYPERVISOR_ID" config = Config('test', 'libvirt', owner='OWNER', env='ENV') host_guest_report = HostGuestAssociationReport(config, { 'hypervisors': [ Hypervisor('9c927368-e888-43b4-9cdb-91b10431b258', []), Hypervisor('ad58b739-5288-4cbc-a984-bd771612d670', [guest1, guest2]) ] }) domain_report = DomainListReport(config, [guest1], hypervisor_id)
def test_per_config_options(self): options = Options(None, None, None) options.force_register = True config = Config('test', 'libvirt', sat_server="http://localhost:%s" % TEST_PORT, sat_username='******', sat_password='******') s = Satellite(self.logger, options) 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_hypervisorCheckInAsync(self, rhsmconnection): owner = 'owner' env = 'env' config = Config("test", "esx", owner=owner, env=env) # Ensure we try out the new API rhsmconnection.return_value.has_capability.return_value = True report = HostGuestAssociationReport(config, self.mapping) self.sm.hypervisorCheckIn(report) expected = {'hypervisors': [h.toDict() for h in self.mapping['hypervisors']]} self.sm.connection.hypervisorCheckIn.assert_called_with( owner, env, expected, options=None )
def filter_hosts(self, filter_something, filter_type=''): config_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, config_dir) with open(os.path.join(config_dir, "test.conf"), "w") as f: f.write(""" [test] type=esx server=does.not.exist username=username password=password owner=owner env=env {filter_something} {filter_type} """.format(filter_something=filter_something, filter_type=filter_type)) conf = parse_file(os.path.join(config_dir, "test.conf")) test_conf_values = conf.pop('test') effective_config = EffectiveConfig() effective_config['test'] = VirtConfigSection.from_dict( test_conf_values, 'test', effective_config) effective_config.validate() config_manager = DestinationToSourceMapper(effective_config) self.assertEqual(len(config_manager.configs), 1) config = config_manager.configs[0][1] included_hypervisor = Hypervisor('12345', guestIds=[ Guest('guest-2', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING), ]) excluded_hypervisor = Hypervisor('00000', guestIds=[ Guest('guest-1', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING), ]) assoc = { 'hypervisors': [ excluded_hypervisor, included_hypervisor, ] } report = HostGuestAssociationReport(config, assoc) assert report.association == {'hypervisors': [included_hypervisor]}
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, d1 = self.create_fake_config('source1', **self.default_config_args) d1['exclude_hosts'] = [] d1['filter_hosts'] = [] config2, d2 = self.create_fake_config('source2', **self.default_config_args) d2['exclude_hosts'] = [] d2['filter_hosts'] = [] virt1 = Mock() virt1.CONFIG_TYPE = 'esx' virt2 = Mock() virt2.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) guest2 = Guest('GUUID2', virt2.CONFIG_TYPE, 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.assertEqual(report.association['hypervisors'], data_to_send.values) manager.hypervisorCheckIn = Mock(side_effect=check_hypervisorCheckIn) logger = Mock() config, d = self.create_fake_config('test', **self.default_config_args) 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=self.options) destination_thread._send_data(data_to_send)
def test_hypervisorCheckIn_preregistered(self): temp, filename = tempfile.mkstemp(suffix=TEST_SYSTEM_ID) self.addCleanup(os.unlink, filename) f = os.fdopen(temp, "wb") pickle.dump({'system_id': TEST_SYSTEM_ID}, f) f.close() options = Mock() s = Satellite(self.logger, options) s.HYPERVISOR_SYSTEMID_FILE = filename.replace(TEST_SYSTEM_ID, '%s') config, d = self.create_fake_config('test', **TestSatellite.default_config_args) 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_oneshot(self, mock_client): expected_assoc = '"well formed HostGuestMapping"' expected_report = HostGuestAssociationReport(self.esx.config, expected_assoc) updateSet = Mock() updateSet.version = 'some_new_version_string' updateSet.truncated = False mock_client.return_value.service.WaitForUpdatesEx.return_value = updateSet datastore = Datastore() self.esx.applyUpdates = Mock() getHostGuestMappingMock = Mock() getHostGuestMappingMock.return_value = expected_assoc self.esx.getHostGuestMapping = getHostGuestMappingMock self.run_once(datastore) result_report = datastore.get(self.esx.config.name) self.assertEqual(expected_report.config.hash, result_report.config.hash) self.assertEqual(expected_report._assoc, result_report._assoc)
def test_per_config_options_encrypted(self, can_write): options = Options(None, None, None) options.force_register = True can_write.return_value = True with tempfile.NamedTemporaryFile() as tmp: password.Password.KEYFILE = tmp.name config = Config('test', 'libvirt', sat_server="http://localhost:%s" % TEST_PORT, sat_username='******', sat_encrypted_password=hexlify( password.Password.encrypt('password'))) s = Manager.fromOptions(self.logger, options, config) self.assertTrue(isinstance(s, Satellite)) 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)