def test_update_report_to_send(self, fromConfig, fromOptions, getLogger): options = Mock() options.interval = 0 options.oneshot = True options.print_ = False options.log_dir = '' options.log_file = '' virtwho = VirtWho(self.logger, options, config_dir="/nonexistant") report = Mock() report.hash.return_value = "hash" config = Mock() report.config = config config.hash.return_value = "config_hash" config.name.return_value = "config_name" self.assertTrue(virtwho.update_report_to_send(report)) self.assertTrue(len(virtwho.configs_ready) == 1 and config in virtwho.configs_ready) self.assertTrue(virtwho.reports_to_send[config.hash].hash == report.hash) # Pretend we sent the report for that config virtwho.configs_ready = [] virtwho.reports[config.hash] = report.hash del virtwho.reports_to_send[config.hash] # if we receive the same report twice we should not send it self.assertFalse(virtwho.update_report_to_send(report)) self.assertFalse(virtwho.configs_ready) self.assertFalse(virtwho.reports_to_send)
def test_update_report_to_send(self, fromConfig, fromOptions, getLogger): options = Mock() options.interval = 0 options.oneshot = True options.print_ = False options.log_dir = '' options.log_file = '' virtwho = VirtWho(self.logger, options, config_dir="/nonexistant") report = Mock() report.hash.return_value = "hash" config = Mock() report.config = config config.hash.return_value = "config_hash" config.name.return_value = "config_name" self.assertTrue(virtwho.update_report_to_send(report)) self.assertTrue( len(virtwho.configs_ready) == 1 and config in virtwho.configs_ready) self.assertTrue( virtwho.reports_to_send[config.hash].hash == report.hash) # Pretend we sent the report for that config virtwho.configs_ready = [] virtwho.reports[config.hash] = report.hash del virtwho.reports_to_send[config.hash] # if we receive the same report twice we should not send it self.assertFalse(virtwho.update_report_to_send(report)) self.assertFalse(virtwho.configs_ready) self.assertFalse(virtwho.reports_to_send)
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 = VirtWho(self.logger, options, config_dir="/nonexistant") virtwho.send = Mock(wraps=virtwho.send) queue = Queue() virtwho.queue = queue 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.assertTrue(virtwho.reports[self.config.hash] == self.fake_report.hash) self.assertTrue(virtwho.reports[self.second_config.hash] == self.fake_domain_list.hash)
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 = VirtWho(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_sending_guests(self, fromOptions, fromConfig, getLogger): 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 = VirtWho(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)
def mock_virtwho(self): options = Mock() options.interval = 6 options.oneshot = False options.print_ = False virtwho = VirtWho(Mock(), options, config_dir="/nonexistant") config = Config("env/cmdline", 'libvirt') virtwho.configManager.addConfig(config) virtwho.queue = Mock() virtwho.send = Mock() return virtwho
def test_sending_guests(self, fromOptions, fromConfig): logger = logging.getLogger() options = Mock() options.oneshot = True virtwho = VirtWho(logger, options) config = Config("test", "esx", "localhost", "username", "password", "owner", "env") virtwho.configManager.addConfig(config) self.assertTrue(virtwho.send()) fromConfig.assert_called_with(logger, config) self.assertTrue(fromConfig.return_value.getHostGuestMapping.called) fromOptions.assert_called_with(logger, options)
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 = VirtWho(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)
def test_send_current_report_with_429(self, fromConfig, fromOptions, getLogger, time): initial = 0 start_time = 0 end_time = 2 send_after_start_time = 0 retry_after = 2 expected_429_count = 1 time.side_effect = [ initial, initial, start_time, send_after_start_time ] fromOptions.return_value = Mock() options = Mock() options.interval = 6 options.oneshot = True options.print_ = False options.log_dir = '' options.log_file = '' virtwho = VirtWho(Mock(), options, config_dir="/nonexistant") expected_queue_timeout = retry_after**expected_429_count expected_send_after = expected_queue_timeout + send_after_start_time config = Mock() config.hash = "config_hash" config.name = "config_name" virtwho.configs_ready.append(config) virtwho.reports_to_send[config.hash] = sentinel.report virtwho.send = Mock() virtwho.send.return_value = False virtwho.send.side_effect = RestlibException( "429", "429", {"Retry-After": retry_after}) result_config, result_report = virtwho.send_current_report() self.assertEquals(expected_queue_timeout, virtwho.queue_timeout) self.assertEquals(expected_send_after, virtwho.send_after) self.assertEquals(result_config, config) self.assertEquals(None, result_report) self.assertEquals(len(virtwho.reports_to_send), 1) self.assertTrue(config in virtwho.configs_ready)
def test_sending_guests_errors(self, fromOptions, fromConfig): logger = logging.getLogger() options = Mock() options.oneshot = True virtwho = VirtWho(logger, options) config = Config("test", "esx", "localhost", "username", "password", "owner", "env") virtwho.configManager.addConfig(config) fromConfig.return_value.getHostGuestMapping.side_effect = VirtError self.assertFalse(virtwho.send()) fromConfig.assert_called_with(logger, config) self.assertTrue(fromConfig.return_value.getHostGuestMapping.called) fromOptions.assert_not_called() fromConfig.return_value.getHostGuestMapping.side_effect = None fromOptions.return_value.hypervisorCheckIn.side_effect = ManagerError self.assertFalse(virtwho.send()) fromConfig.assert_called_with(logger, config) self.assertTrue(fromConfig.return_value.getHostGuestMapping.called) fromOptions.assert_called() self.assertTrue(fromOptions.return_value.hypervisorCheckIn.called)
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 = VirtWho(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_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 = VirtWho(self.logger, options, config_dir="/nonexistant") virtwho.send = Mock(wraps=virtwho.send) queue = Queue() virtwho.queue = queue 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.assertTrue( virtwho.reports[self.config.hash] == self.fake_report.hash) self.assertTrue(virtwho.reports[self.second_config.hash] == self.fake_domain_list.hash)
def test_send_current_report_with_429(self, fromConfig, fromOptions, getLogger, time): initial = 0 start_time = 0 end_time = 2 send_after_start_time = 0 retry_after = 2 expected_429_count = 1 time.side_effect = [initial, initial, start_time, send_after_start_time] fromOptions.return_value = Mock() options = Mock() options.interval = 6 options.oneshot = True options.print_ = False options.log_dir = '' options.log_file = '' virtwho = VirtWho(Mock(), options, config_dir="/nonexistant") expected_queue_timeout = retry_after ** expected_429_count expected_send_after = expected_queue_timeout + send_after_start_time config = Mock() config.hash = "config_hash" config.name = "config_name" virtwho.configs_ready.append(config) virtwho.reports_to_send[config.hash] = sentinel.report virtwho.send = Mock() virtwho.send.return_value = False virtwho.send.side_effect = RestlibException("429", "429", {"Retry-After": retry_after}) result_config, result_report = virtwho.send_current_report() self.assertEquals(expected_queue_timeout, virtwho.queue_timeout) self.assertEquals(expected_send_after, virtwho.send_after) self.assertEquals(result_config, config) self.assertEquals(None, result_report) self.assertEquals(len(virtwho.reports_to_send), 1) self.assertTrue(config in virtwho.configs_ready)
def test_send_current_report(self, fromConfig, fromOptions, getLogger, time): initial = 0 start_time = 0 end_time = 2 send_after_start_time = 0 expected_delta = end_time - start_time time.side_effect = [ initial, start_time, end_time, send_after_start_time ] fromOptions.return_value = Mock() options = Mock() options.interval = 6 options.oneshot = True options.print_ = False options.log_dir = '' options.log_file = '' virtwho = VirtWho(Mock(), options, config_dir="/nonexistant") expected_queue_timeout = max(0, options.interval - expected_delta) expected_send_after = expected_queue_timeout + send_after_start_time config = Mock() config.hash = "config_hash" config.name = "config_name" virtwho.send = Mock() virtwho.send.return_value = True virtwho.reports_to_send[config.hash] = sentinel.report virtwho.configs_ready.append(config) result_config, result_report = virtwho.send_current_report() self.assertEquals(expected_queue_timeout, virtwho.queue_timeout) self.assertEquals(expected_send_after, virtwho.send_after) self.assertEquals(config, result_config) self.assertEquals(sentinel.report, result_report) self.assertTrue(not virtwho.reports_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 = VirtWho(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_current_report(self, fromConfig, fromOptions, getLogger, time): initial = 0 start_time = 0 end_time = 2 send_after_start_time = 0 expected_delta = end_time - start_time time.side_effect = [initial, start_time, end_time, send_after_start_time] fromOptions.return_value = Mock() options = Mock() options.interval = 6 options.oneshot = True options.print_ = False options.log_dir = '' options.log_file = '' virtwho = VirtWho(Mock(), options, config_dir="/nonexistant") expected_queue_timeout = max(0, options.interval - expected_delta) expected_send_after = expected_queue_timeout + send_after_start_time config = Mock() config.hash = "config_hash" config.name = "config_name" virtwho.send = Mock() virtwho.send.return_value = True virtwho.reports_to_send[config.hash] = sentinel.report virtwho.configs_ready.append(config) result_config, result_report = virtwho.send_current_report() self.assertEquals(expected_queue_timeout, virtwho.queue_timeout) self.assertEquals(expected_send_after, virtwho.send_after) self.assertEquals(config, result_config) self.assertEquals(sentinel.report, result_report) self.assertTrue(not virtwho.reports_to_send)
def setupVirtWho(self, oneshot=True): options = Mock() options.oneshot = oneshot options.interval = 0 options.print_ = False options.log_dir = '' options.log_file = '' virtwho = VirtWho(self.logger, options, config_dir="/nonexistant") config = Config("test", "esx", server="localhost", username="******", password="******", owner="owner", env="env") virtwho.configManager.addConfig(config) return virtwho