Exemplo n.º 1
0
    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)])
Exemplo n.º 2
0
    def test_merge_status(self):
        self.tmp_dir = tempfile.mkdtemp()
        status_pid_file_name = self.tmp_dir + os.path.sep + 'virt-who-status.pid'
        status_pid_file_patcher = patch('virtwho.virt.virt.STATUS_LOCK',
                                        status_pid_file_name)
        status_pid_file_patcher.start()
        self.addCleanup(status_pid_file_patcher.stop)

        status_file_name = self.tmp_dir + os.path.sep + 'run_data.json'
        status_data_file_patcher = patch('virtwho.virt.virt.STATUS_DATA',
                                         status_file_name)
        status_data_file_patcher.start()
        self.addCleanup(status_data_file_patcher.stop)
        self.addCleanup(shutil.rmtree, self.tmp_dir)

        with open(status_file_name, "w+") as f:
            f.write("""
{
    "sources": {
        "source1": {
            "last_successful_retrieve": "2020-02-28 07:25:25 UTC",
            "hypervisors": 20,
            "guests": 37
        },
        "source2": {
            "last_successful_retrieve": null
        }
    },
    "destinations": {
        "source1": {
            "last_successful_send": "2020-02-28 07:25:27 UTC",
            "last_job_id": "hypervisor12345"
        },
        "source2": {
            "last_successful_send": null,
            "last_job_id": null
        }
    }
}

        """)
        os.chmod(status_file_name, 444)

        config1, d1 = self.create_fake_config('source1',
                                              **self.default_config_args)
        config2, d2 = self.create_fake_config('source2',
                                              **self.default_config_args)

        report1 = StatusReport(config1)
        report2 = StatusReport(config2)

        data_to_send = {'source1': report1, 'source2': report2}

        source_keys = ['source1', 'source2']
        datastore = {'source1': Mock(), 'source2': Mock()}
        manager = Mock()
        options = Mock()
        options.print_ = False

        manager.hypervisorCheckIn = Mock()
        logger = Mock()
        config, d = self.create_fake_config('test', **self.default_config_args)
        config.owner = 'test_owner'
        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,
                                               status=True)

        def check_report_status(report, status_call):
            self.assertTrue(status_call)
            report.last_job_status = "FINISHED"

        destination_thread.record_status = Mock()
        destination_thread.is_terminated = Mock(return_value=False)
        destination_thread.check_report_status = Mock(
            side_effect=check_report_status)
        destination_thread._send_data(data_to_send)
        self.assertEqual(report1.data['source']['last_successful_retrieve'],
                         "2020-02-28 07:25:25 UTC")
        self.assertEqual(report1.data['source']['hypervisors'], 20)
        self.assertEqual(report1.data['source']['guests'], 37)
        self.assertEqual(report1.data['destination']["last_successful_send"],
                         "2020-02-28 07:25:27 UTC")
        self.assertEqual(
            report1.data['destination']["last_successful_send_job_status"],
            "FINISHED")
        self.assertEqual(report2.data['source']['last_successful_retrieve'],
                         None)
        self.assertEqual(report2.data['source']['hypervisors'], None)
        self.assertEqual(report2.data['source']['guests'], None)
        self.assertEqual(report2.data['destination']["last_successful_send"],
                         None)
        self.assertEqual(
            report2.data['destination']["last_successful_send_job_status"],
            None)