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))
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_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_429_during_send_virt_guests(self): # Show that when a 429 is encountered during the sending of a # DomainListReport that we retry after waiting the appropriate # amount of time source_keys = ['source1'] config1 = Config('source1', 'esx') virt1 = Mock() virt1.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1, Guest.STATE_RUNNING) report1 = DomainListReport(config1, [guest1], hypervisor_id='hypervisor_id_1') datastore = {'source1': report1} data_to_send = {'source1': report1} config = Mock() config.polling_interval = 10 logger = Mock() error_to_throw = ManagerThrottleError(retry_after=21) manager = Mock() manager.sendVirtGuests = Mock(side_effect=[error_to_throw, report1]) terminate_event = Mock() interval = 10 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=True, options=options) destination_thread.wait = Mock() destination_thread._send_data(data_to_send) manager.sendVirtGuests.assert_has_calls([ call(report1, options=destination_thread.options), call(report1, options=destination_thread.options) ]) destination_thread.wait.assert_has_calls( [call(wait_time=error_to_throw.retry_after)])
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._send_data(data_to_send) destination_thread.wait.assert_has_calls(expected_wait_calls)
def test_send_data_429_during_send_virt_guests(self): # Show that when a 429 is encountered during the sending of a # DomainListReport that we retry after waiting the appropriate # amount of time config, d = self.create_fake_config('test', **self.default_config_args) source_keys = ['source1'] virt1 = Mock() virt1.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) report1 = DomainListReport(config, [guest1], hypervisor_id='hypervisor_id_1') datastore = {'source1': report1} data_to_send = {'source1': report1} logger = Mock() error_to_throw = ManagerThrottleError(retry_after=62) manager = Mock() manager.sendVirtGuests = Mock(side_effect=[error_to_throw, report1]) terminate_event = Mock() interval = 10 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.record_status = Mock() destination_thread.is_terminated = Mock(return_value=False) destination_thread._send_data(data_to_send) manager.sendVirtGuests.assert_has_calls( [call(report1, options=destination_thread.options)]) destination_thread.wait.assert_has_calls( [call(wait_time=error_to_throw.retry_after)])
def test_send_data_429_during_send_virt_guests(self): # Show that when a 429 is encountered during the sending of a # DomainListReport that we retry after waiting the appropriate # amount of time config, d = self.create_fake_config('test', **self.default_config_args) source_keys = ['source1'] virt1 = Mock() virt1.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) report1 = DomainListReport(config, [guest1], hypervisor_id='hypervisor_id_1') datastore = {'source1': report1} data_to_send = {'source1': report1} logger = Mock() error_to_throw = ManagerThrottleError(retry_after=62) manager = Mock() manager.sendVirtGuests = Mock(side_effect=[error_to_throw, report1]) terminate_event = Mock() interval = 10 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._send_data(data_to_send) manager.sendVirtGuests.assert_has_calls([call(report1, options=destination_thread.options)]) destination_thread.wait.assert_has_calls([call( wait_time=error_to_throw.retry_after)])
def test_send_data_domain_list_reports(self): # Show that DomainListReports are sent using the sendVirtGuests # method of the destination source_keys = ['source1'] config1, d1 = self.create_fake_config('source1', **self.default_config_args) virt1 = Mock() virt1.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) report1 = DomainListReport(config1, [guest1], hypervisor_id='hypervisor_id_1') datastore = {'source1': report1} data_to_send = {'source1': report1} config, d = self.create_fake_config('test', **self.default_config_args) logger = Mock() manager = Mock() terminate_event = Mock() interval = 10 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=True, 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) manager.sendVirtGuests.assert_has_calls( [call(report1, options=destination_thread.options)])
def test_send_data_domain_list_reports(self): # Show that DomainListReports are sent using the sendVirtGuests # method of the destination source_keys = ['source1'] config1 = Config('source1', 'esx') virt1 = Mock() virt1.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1, Guest.STATE_RUNNING) report1 = DomainListReport(config1, [guest1], hypervisor_id='hypervisor_id_1') datastore = {'source1': report1} data_to_send = {'source1': report1} config = Mock() config.polling_interval = 10 logger = Mock() manager = Mock() terminate_event = Mock() interval = 10 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=True, options=options) destination_thread.wait = Mock() destination_thread._send_data(data_to_send) manager.sendVirtGuests.assert_has_calls( [call(report1, options=destination_thread.options)])
def test_send_data_domain_list_reports(self): # Show that DomainListReports are sent using the sendVirtGuests # method of the destination source_keys = ['source1'] config1, d1 = self.create_fake_config('source1', **self.default_config_args) virt1 = Mock() virt1.CONFIG_TYPE = 'esx' guest1 = Guest('GUUID1', virt1.CONFIG_TYPE, Guest.STATE_RUNNING) report1 = DomainListReport(config1, [guest1], hypervisor_id='hypervisor_id_1') datastore = {'source1': report1} data_to_send = {'source1': report1} config, d = self.create_fake_config('test', **self.default_config_args) logger = Mock() manager = Mock() terminate_event = Mock() interval = 10 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=True, options=self.options) destination_thread.wait = Mock() destination_thread.is_terminated = Mock(return_value=False) destination_thread._send_data(data_to_send) manager.sendVirtGuests.assert_has_calls([call(report1, options=destination_thread.options)])
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 = Config('source1', 'esx') config2 = Config('source2', 'esx') 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) datastore = {'source1': report1, 'source2': report2} data_to_send = {'source1': report1, 'source2': report2} config = Mock() config.polling_interval = 10 error_to_throw = ManagerThrottleError(retry_after=20) manager = Mock() manager.hypervisorCheckIn.return_value = report1 # A closure to allow us to have a function that "modifies" the given # report in a predictable way. # In this case I want to set the state of the report to STATE_FINISHED # after the first try def check_report_state_closure(items): item_iterator = iter(items) def mock_check_report_state(report): item = next(item_iterator) if isinstance(item, Exception): raise item report.state = item return report return mock_check_report_state states = [error_to_throw, AbstractVirtReport.STATE_FINISHED] expected_wait_calls = [call(wait_time=error_to_throw.retry_after)] check_report_mock = check_report_state_closure(states) manager.check_report_state = Mock(side_effect=check_report_mock) 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=True, options=options) destination_thread.wait = Mock() destination_thread._send_data(data_to_send) destination_thread.wait.assert_has_calls(expected_wait_calls)
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 = Config('source1', 'esx') config2 = Config('source2', 'esx') 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'] batch_report1 = Mock() # The "report" to return from hypervisorCheckIn batch_report1.state = AbstractVirtReport.STATE_CREATED datastore = {'source1': report1, 'source2': report2} manager = Mock() manager.hypervisorCheckIn.return_value = batch_report1 # A closure to allow us to have a function that "modifies" the given # report in a predictable way. # In this case I want to set the state of the report to STATE_FINISHED # after the first try def check_report_state_closure(): # The local variables we'd like to use to keep track of state # must be in a dict (or some other mutable container) as all # references in the enclosing scope in python 2.X are read-only local_variables = {'count': 0} def mock_check_report_state(report): if local_variables['count'] > 0: report.state = AbstractVirtReport.STATE_FINISHED else: report.state = AbstractVirtReport.STATE_CREATED local_variables['count'] += 1 return report return mock_check_report_state check_report_mock = check_report_state_closure() manager.check_report_state = Mock(side_effect=check_report_mock) logger = Mock() config = Mock() config.polling_interval = 10 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=True, options=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._send_data(data_to_send) # We expect there two be two calls to wait with the value of the # polling_interval attr because we'd like to wait one polling # interval before making the first check. Because the mock # check_report_state function will modify the report to be in the # successful state after the first call, we expect to wait exactly # twice, both of duration config.polling_interval destination_thread.wait.assert_has_calls([ call(wait_time=config.polling_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 return from hypervisorCheckIn batch_report1.state = AbstractVirtReport.STATE_CREATED datastore = {'source1': report1, 'source2': report2} manager = Mock() manager.hypervisorCheckIn.return_value = batch_report1 # A closure to allow us to have a function that "modifies" the given # report in a predictable way. # In this case I want to set the state of the report to STATE_FINISHED # after the first try def check_report_state_closure(): # The local variables we'd like to use to keep track of state # must be in a dict (or some other mutable container) as all # references in the enclosing scope in python 2.X are read-only local_variables = {'count': 0} def mock_check_report_state(report): if local_variables['count'] > 1: report.state = AbstractVirtReport.STATE_FINISHED else: report.state = AbstractVirtReport.STATE_CREATED local_variables['count'] += 1 return report return mock_check_report_state check_report_mock = check_report_state_closure() manager.check_report_state = Mock(side_effect=check_report_mock) 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=True, 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._send_data(data_to_send) # 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) ])