예제 #1
0
    def test_getHostGuestMapping(self, mock_client):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = 'Fake_uuid'
        expected_guestId = 'guest1UUID'
        expected_guest_state = Guest.STATE_RUNNING

        fake_parent = MagicMock()
        fake_parent.value = 'fake_parent_id'
        fake_parent._type = 'ClusterComputeResource'

        fake_vm_id = MagicMock()
        fake_vm_id.value = 'guest1'

        fake_vm = MagicMock()
        fake_vm.ManagedObjectReference = [fake_vm_id]
        fake_vms = {
            'guest1': {
                'runtime.powerState': 'poweredOn',
                'config.uuid': expected_guestId
            }
        }
        self.esx.vms = fake_vms

        fake_host = {
            'hardware.systemInfo.uuid': expected_hypervisorId,
            'config.network.dnsConfig.hostName': 'hostname',
            'config.network.dnsConfig.domainName': 'domainname',
            'config.product.version': '1.2.3',
            'hardware.cpuInfo.numCpuPackages': '1',
            'name': expected_hostname,
            'parent': fake_parent,
            'vm': fake_vm,
        }
        fake_hosts = {'random-host-id': fake_host}
        self.esx.hosts = fake_hosts

        fake_cluster = {'name': 'Fake_cluster_name'}
        self.esx.clusters = {'fake_parent_id': fake_cluster}

        expected_result = Hypervisor(hypervisorId=expected_hypervisorId,
                                     name=expected_hostname,
                                     guestIds=[
                                         Guest(
                                             expected_guestId,
                                             self.esx.CONFIG_TYPE,
                                             expected_guest_state,
                                         )
                                     ],
                                     facts={
                                         Hypervisor.CPU_SOCKET_FACT:
                                         '1',
                                         Hypervisor.HYPERVISOR_TYPE_FACT:
                                         'vmware',
                                         Hypervisor.HYPERVISOR_VERSION_FACT:
                                         '1.2.3',
                                         Hypervisor.HYPERVISOR_CLUSTER:
                                         'Fake_cluster_name',
                                     })
        result = self.esx.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #2
0
    def test_pending_vm(self):
        client = Mock()
        client.get_nodes.return_value = self.nodes()
        client.get_vms.return_value = self.pending_vms()

        config = self.create_config(name='test',
                                    wrapper=None,
                                    type='kubevirt',
                                    owner='owner',
                                    kubeconfig='/etc/hosts')

        with patch.dict('os.environ', {'KUBECONFIG': '/dev/null'}):
            kubevirt = Virt.from_config(self.logger, config, Datastore())

            kubevirt._client = client

            expected_result = Hypervisor(
                hypervisorId='52c01ad890e84b15a1be4be18bd64ecd',
                name='main',
                guestIds=[],
                facts={
                    Hypervisor.CPU_SOCKET_FACT: '2',
                    Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu',
                    Hypervisor.SYSTEM_UUID_FACT:
                    '52c01ad890e84b15a1be4be18bd64ecd',
                    Hypervisor.HYPERVISOR_VERSION_FACT: 'v1.9.1+a0ce1bc657',
                })
            result = kubevirt.getHostGuestMapping()['hypervisors'][0]
            self.assertEqual(expected_result.toDict(), result.toDict())
예제 #3
0
    def test_getHostGuestMapping_with_hm(self):
        client = Mock()
        client.get_nodes.return_value = self.nodes()
        client.get_vms.return_value = self.vms()

        config = self.create_config(name='test', wrapper=None, type='kubevirt',
                                    owner='owner', kubeconfig='/etc/hosts',
                                    hypervisor_id='hostname')

        with patch.dict('os.environ', {'KUBECONFIG':'/dev/null'}):
            kubevirt = Virt.from_config(self.logger, config, Datastore())

            kubevirt._client = client

            expected_result = Hypervisor(
                hypervisorId='minikube',
                name='master',
                guestIds=[
                    Guest(
                        'f83c5f73-5244-4bd1-90cf-02bac2dda608',
                        kubevirt.CONFIG_TYPE,
                        Guest.STATE_RUNNING,
                    )
                ],
                facts={
                    Hypervisor.CPU_SOCKET_FACT: '2',
                    Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu',
                    Hypervisor.HYPERVISOR_VERSION_FACT: 'v1.9.1+a0ce1bc657',
                }
            )
            result = kubevirt.getHostGuestMapping()['hypervisors'][0]
            self.assertEqual(expected_result.toDict(), result.toDict())
예제 #4
0
    def test_getHostGuestMapping(self, session):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = '12345678-90AB-CDEF-1234-567890ABCDEF'
        expected_guestId = '12345678-90AB-CDEF-1234-567890ABCDEF'
        expected_guest_state = Guest.STATE_UNKNOWN

        session.return_value.post.side_effect = HyperVMock.post

        expected_result = Hypervisor(
            hypervisorId=expected_hypervisorId,
            name=expected_hostname,
            guestIds=[
                Guest(
                    expected_guestId,
                    self.hyperv.CONFIG_TYPE,
                    expected_guest_state,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '1',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'hyperv',
                Hypervisor.HYPERVISOR_VERSION_FACT: '0.1.2345.67890',
                Hypervisor.SYSTEM_UUID_FACT: expected_hypervisorId
            }
        )
        result = self.hyperv.getHostGuestMapping()['hypervisors'][0]
        assert expected_result.toDict() == result.toDict()
예제 #5
0
    def test_milicpu(self):
        client = Mock()
        client.get_nodes.return_value = self.new_nodes()
        client.get_vms.return_value = self.vms()

        config = self.create_config(name='test',
                                    wrapper=None,
                                    type='kubevirt',
                                    owner='owner',
                                    kubeconfig='/etc/hosts')

        with patch.dict('os.environ', {'KUBECONFIG': '/dev/null'}):
            kubevirt = Virt.from_config(self.logger, config, Datastore())
            kubevirt._client = client

            expected_result = Hypervisor(
                hypervisorId='52c01ad890e84b15a1be4be18bd64ecd',
                name='main',
                guestIds=[
                    Guest(
                        'f83c5f73-5244-4bd1-90cf-02bac2dda608',
                        kubevirt.CONFIG_TYPE,
                        Guest.STATE_RUNNING,
                    )
                ],
                facts={
                    Hypervisor.CPU_SOCKET_FACT: '7',
                    Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu',
                    Hypervisor.SYSTEM_UUID_FACT:
                    '52c01ad890e84b15a1be4be18bd64ecd',
                    Hypervisor.HYPERVISOR_VERSION_FACT: 'v1.18.0-rc.1',
                })
            result = kubevirt.getHostGuestMapping()['hypervisors'][0]
            self.assertEqual(expected_result.toDict(), result.toDict())
예제 #6
0
    def test_getHostGuestMapping(self, get):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = uuids['host']
        expected_guestId = uuids['vm']
        expected_guest_state = Guest.STATE_SHUTOFF

        get.side_effect = [
            MagicMock(text=CLUSTERS_XML),
            MagicMock(text=HOSTS_XML),
            MagicMock(text=VMS_XML),
        ]

        expected_result = Hypervisor(hypervisorId=expected_hypervisorId,
                                     name=expected_hostname,
                                     guestIds=[
                                         Guest(
                                             expected_guestId,
                                             self.rhevm,
                                             expected_guest_state,
                                         )
                                     ],
                                     facts={
                                         Hypervisor.CPU_SOCKET_FACT:
                                         '1',
                                         Hypervisor.HYPERVISOR_TYPE_FACT:
                                         'qemu',
                                         Hypervisor.HYPERVISOR_VERSION_FACT:
                                         '1.2.3',
                                     })
        result = self.rhevm.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #7
0
    def test_getHostGuestMapping(self):
        client = Mock()
        client.get_nodes.return_value = self.nodes()
        client.get_vms.return_value = self.vms()

        config = self.create_config(name='test', wrapper=None, type='kubevirt',
                                    owner='owner', kubeconfig='/etc/hosts')

        with patch.dict('os.environ', {'KUBECONFIG':'/dev/null'}):
            kubevirt = Virt.from_config(self.logger, config, Datastore())

            kubevirt._client = client

            expected_result = Hypervisor(
                hypervisorId='52c01ad890e84b15a1be4be18bd64ecd',
                name='master',
                guestIds=[
                    Guest(
                        'f83c5f73-5244-4bd1-90cf-02bac2dda608',
                        kubevirt.CONFIG_TYPE,
                        Guest.STATE_RUNNING,
                    )
                ],
                facts={
                    Hypervisor.CPU_SOCKET_FACT: '2',
                    Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu',
                    Hypervisor.HYPERVISOR_VERSION_FACT: 'v1.9.1+a0ce1bc657',
                }
            )
            result = kubevirt.getHostGuestMapping()['hypervisors'][0]
            self.assertEqual(expected_result.toDict(), result.toDict())
예제 #8
0
    def test_getHostGuestMapping(self):
        kube_api = Mock()
        kube_api.list_node.return_value = self.nodes()

        kubevirt_api = Mock()
        kubevirt_api.list_virtual_machine_instance_for_all_namespaces.return_value = self.vms(
        )

        self.kubevirt.kube_api = kube_api
        self.kubevirt.kubevirt_api = kubevirt_api

        expected_result = Hypervisor(
            hypervisorId='52c01ad890e84b15a1be4be18bd64ecd',
            name='master',
            guestIds=[
                Guest(
                    'default/win-2016',
                    self.kubevirt.CONFIG_TYPE,
                    Guest.STATE_RUNNING,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '2',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu',
                Hypervisor.HYPERVISOR_VERSION_FACT: 'v1.9.1+a0ce1bc657',
            })
        result = self.kubevirt.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #9
0
    def test_new_status(self, get):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = uuids['host']
        expected_guestId = uuids['vm']
        expected_guest_state = Guest.STATE_RUNNING

        get.side_effect = [
            MagicMock(text=CLUSTERS_XML),
            MagicMock(text=HOSTS_XML),
            MagicMock(text=VMS_XML_STATUS),
        ]

        expected_result = Hypervisor(
            hypervisorId=expected_hypervisorId,
            name=expected_hostname,
            guestIds=[
                Guest(
                    expected_guestId,
                    self.rhevm,
                    expected_guest_state,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '1',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu',
                Hypervisor.HYPERVISOR_VERSION_FACT: '1.2.3',
            }
        )
        result = self.rhevm.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #10
0
    def test_getHostGuestMapping(self, session):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = '12345678-90AB-CDEF-1234-567890ABCDEF'
        expected_guestId = '12345678-90AB-CDEF-1234-567890ABCDEF'
        expected_guest_state = Guest.STATE_UNKNOWN

        session.return_value.post.side_effect = HyperVMock.post

        expected_result = Hypervisor(hypervisorId=expected_hypervisorId,
                                     name=expected_hostname,
                                     guestIds=[
                                         Guest(
                                             expected_guestId,
                                             self.hyperv,
                                             expected_guest_state,
                                         )
                                     ],
                                     facts={
                                         Hypervisor.CPU_SOCKET_FACT:
                                         '1',
                                         Hypervisor.HYPERVISOR_TYPE_FACT:
                                         'hyperv',
                                         Hypervisor.HYPERVISOR_VERSION_FACT:
                                         '0.1.2345.67890',
                                     })
        result = self.hyperv.getHostGuestMapping()['hypervisors'][0]
        assert expected_result.toDict() == result.toDict()
예제 #11
0
    def test_new_status(self, get):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = uuids['host']
        expected_guestId = uuids['vm']
        expected_guest_state = Guest.STATE_RUNNING

        get.side_effect = [
            MagicMock(content=CLUSTERS_XML),
            MagicMock(content=HOSTS_XML),
            MagicMock(content=VMS_XML_STATUS),
        ]

        expected_result = Hypervisor(
            hypervisorId=expected_hypervisorId,
            name=expected_hostname,
            guestIds=[
                Guest(
                    expected_guestId,
                    self.rhevm.CONFIG_TYPE,
                    expected_guest_state,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '1',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu',
                Hypervisor.HYPERVISOR_VERSION_FACT: '1.2.3',
                Hypervisor.HYPERVISOR_CLUSTER: 'Cetus',
                Hypervisor.SYSTEM_UUID_FACT: 'db5a7a9f-6e33-3bfd-8129-c8010e4e1497',
            }
        )
        result = self.rhevm.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #12
0
    def test_getHostGuestMapping(self, get):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = uuids['host']
        expected_guestId = uuids['vm']
        expected_guest_state = Guest.STATE_SHUTOFF

        get.side_effect = [
            MagicMock(content=CLUSTERS_XML),
            MagicMock(content=HOSTS_XML),
            MagicMock(content=VMS_XML),
        ]

        expected_result = Hypervisor(
            hypervisorId=expected_hypervisorId,
            name=expected_hostname,
            guestIds=[
                Guest(
                    expected_guestId,
                    self.rhevm.CONFIG_TYPE,
                    expected_guest_state,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '1',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu',
                Hypervisor.HYPERVISOR_VERSION_FACT: '1.2.3',
                Hypervisor.HYPERVISOR_CLUSTER: 'Cetus',
                Hypervisor.SYSTEM_UUID_FACT: 'db5a7a9f-6e33-3bfd-8129-c8010e4e1497',
            }
        )
        result = self.rhevm.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #13
0
    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)
예제 #14
0
    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))
예제 #15
0
    def test_getHostGuestMapping(self, mock_client):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = 'Fake_uuid'
        expected_guestId = 'guest1UUID'
        expected_guest_state = Guest.STATE_RUNNING

        fake_parent = MagicMock()
        fake_parent.value = 'fake_parent_id'
        fake_parent._type = 'ClusterComputeResource'

        fake_vm_id = MagicMock()
        fake_vm_id.value = 'guest1'

        fake_vm = MagicMock()
        fake_vm.ManagedObjectReference = [fake_vm_id]
        fake_vms = {'guest1': {'runtime.powerState': 'poweredOn',
                               'config.uuid': expected_guestId}}
        self.esx.vms = fake_vms

        fake_host = {'hardware.systemInfo.uuid': expected_hypervisorId,
                     'config.network.dnsConfig.hostName': 'hostname',
                     'config.network.dnsConfig.domainName': 'domainname',
                     'config.product.version': '1.2.3',
                     'hardware.cpuInfo.numCpuPackages': '1',
                     'name': expected_hostname,
                     'parent': fake_parent,
                     'vm': fake_vm,
                     }
        fake_hosts = {'random-host-id': fake_host}
        self.esx.hosts = fake_hosts

        fake_cluster = {'name': 'Fake_cluster_name'}
        self.esx.clusters = {'fake_parent_id': fake_cluster}

        expected_result = Hypervisor(
            hypervisorId=expected_hypervisorId,
            name=expected_hostname,
            guestIds=[
                Guest(
                    expected_guestId,
                    self.esx.CONFIG_TYPE,
                    expected_guest_state,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '1',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'vmware',
                Hypervisor.HYPERVISOR_VERSION_FACT: '1.2.3',
                Hypervisor.HYPERVISOR_CLUSTER: 'Fake_cluster_name',
                Hypervisor.SYSTEM_UUID_FACT: expected_hypervisorId
            }
        )
        result = self.esx.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #16
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)])
예제 #17
0
    def test_getHostGuestMapping_incomplete_data(self, mock_client):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = 'Fake_uuid'
        expected_guestId = 'guest1UUID'
        expected_guest_state = Guest.STATE_UNKNOWN

        fake_parent = MagicMock()
        fake_parent.value = 'Fake_parent'

        fake_vm_id = MagicMock()
        fake_vm_id.value = 'guest1'

        fake_vm = MagicMock()
        fake_vm.ManagedObjectReference = [fake_vm_id]
        fake_vms = {
            'guest1': {
                'runtime.powerState': 'BOGUS_STATE',
                'config.uuid': expected_guestId
            }
        }
        self.esx.vms = fake_vms

        fake_host = {
            'hardware.systemInfo.uuid': expected_hypervisorId,
            'config.network.dnsConfig.hostName': 'hostname',
            'config.network.dnsConfig.domainName': 'domainname',
            'config.product.version': '1.2.3',
            'hardware.cpuInfo.numCpuPackages': '1',
            'parent': fake_parent,
            'vm': fake_vm
        }
        fake_hosts = {'random-host-id': fake_host}
        self.esx.hosts = fake_hosts

        expected_result = Hypervisor(hypervisorId=expected_hypervisorId,
                                     name=expected_hostname,
                                     guestIds=[
                                         Guest(
                                             expected_guestId,
                                             self.esx,
                                             expected_guest_state,
                                         )
                                     ],
                                     facts={
                                         Hypervisor.CPU_SOCKET_FACT:
                                         '1',
                                         Hypervisor.HYPERVISOR_TYPE_FACT:
                                         'vmware',
                                         Hypervisor.HYPERVISOR_VERSION_FACT:
                                         '1.2.3',
                                     })
        result = self.esx.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #18
0
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)
예제 #19
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)
예제 #20
0
 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)
예제 #21
0
    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)
예제 #22
0
    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]}
예제 #23
0
    def test_getHostGuestMapping_incomplete_data(self, mock_client):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = 'Fake_uuid'
        expected_guestId = 'guest1UUID'
        expected_guest_state = Guest.STATE_UNKNOWN

        fake_parent = MagicMock()
        fake_parent.value = 'Fake_parent'

        fake_vm_id = MagicMock()
        fake_vm_id.value = 'guest1'

        fake_vm = MagicMock()
        fake_vm.ManagedObjectReference = [fake_vm_id]
        fake_vms = {'guest1': {'runtime.powerState': 'BOGUS_STATE',
                               'config.uuid': expected_guestId}}
        self.esx.vms = fake_vms

        fake_host = {'hardware.systemInfo.uuid': expected_hypervisorId,
                     'config.network.dnsConfig.hostName': 'hostname',
                     'config.network.dnsConfig.domainName': 'domainname',
                     'config.product.version': '1.2.3',
                     'hardware.cpuInfo.numCpuPackages': '1',
                     'parent': fake_parent,
                     'vm': fake_vm
                     }
        fake_hosts = {'random-host-id': fake_host}
        self.esx.hosts = fake_hosts

        expected_result = Hypervisor(
            hypervisorId=expected_hypervisorId,
            name=expected_hostname,
            guestIds=[
                Guest(
                    expected_guestId,
                    self.esx,
                    expected_guest_state,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '1',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'vmware',
                Hypervisor.HYPERVISOR_VERSION_FACT: '1.2.3',
            }
        )
        result = self.esx.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #24
0
 def _process_hypervisor(self, hypervisor):
     guests = []
     for guest in hypervisor['guests']:
         guests.append(self._process_guest(guest))
     return Hypervisor(hypervisor['uuid'],
                       guests,
                       hypervisor.get('name'),
                       hypervisor.get('facts'))
예제 #25
0
    def test_multiple_hosts(self, session):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = 'Fake_uuid'
        expected_guestId = 'guest1UUID'
        expected_guest_state = Guest.STATE_UNKNOWN

        xenapi = session.return_value.xenapi

        hosts = []
        for i in range(3):
            hosts.append({
                'uuid': expected_hypervisorId + str(i),
                'hostname': expected_hostname + str(i),
                'cpu_info': {
                    'socket_count': '1'
                },
                'software_version': {
                    'product_brand': 'XenServer',
                    'product_version': '1.2.3',
                },
            })

        guest = {
            'uuid': expected_guestId,
            'power_state': 'unknown',
        }

        xenapi.host.get_all.return_value = hosts
        xenapi.host.get_resident_VMs.return_value = [
            guest,
        ]
        xenapi.host.get_record = lambda x: x
        xenapi.VM.get_record = lambda x: x

        expected_result = [
            Hypervisor(
                hypervisorId=expected_hypervisorId + str(i),
                name=expected_hostname + str(i),
                guestIds=[
                    Guest(
                        expected_guestId,
                        self.xen.CONFIG_TYPE,
                        expected_guest_state,
                    )
                ],
                facts={
                    Hypervisor.CPU_SOCKET_FACT: '1',
                    Hypervisor.HYPERVISOR_TYPE_FACT: 'XenServer',
                    Hypervisor.HYPERVISOR_VERSION_FACT: '1.2.3',
                    Hypervisor.SYSTEM_UUID_FACT: expected_hypervisorId + str(i)
                }
            ) for i in range(3)]
        self.xen._prepare()
        result = self.xen.getHostGuestMapping()['hypervisors']
        self.assertEqual(len(result), 3, "3 hosts should be reported")
        self.assertEqual([x.toDict() for x in expected_result], [x.toDict() for x in result])
예제 #26
0
 def _getHostGuestMapping(self):
     mapping = {'hypervisors': []}
     facts = {
         Hypervisor.CPU_SOCKET_FACT: self._remote_host_sockets(),
         Hypervisor.HYPERVISOR_TYPE_FACT: self.virt.getType(),
         Hypervisor.HYPERVISOR_VERSION_FACT: self.virt.getVersion(),
     }
     host = Hypervisor(hypervisorId=self._remote_host_id(),
                       guestIds=self._listDomains(),
                       name=self._remote_host_name(),
                       facts=facts)
     mapping['hypervisors'].append(host)
     return mapping
예제 #27
0
    def filter_hosts(self, config):
        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
{config}
""".format(config=config))
        config_manager = ConfigManager(self.logger, config_dir)
        self.assertEqual(len(config_manager.configs), 1)
        config = config_manager.configs[0]

        included_hypervisor = Hypervisor('12345',
                                         guestIds=[
                                             Guest('guest-2', xvirt,
                                                   Guest.STATE_RUNNING),
                                         ])
        excluded_hypervisor = Hypervisor('00000',
                                         guestIds=[
                                             Guest('guest-1', xvirt,
                                                   Guest.STATE_RUNNING),
                                         ])

        assoc = {
            'hypervisors': [
                excluded_hypervisor,
                included_hypervisor,
            ]
        }

        report = HostGuestAssociationReport(config, assoc)
        assert report.association == {'hypervisors': [included_hypervisor]}
예제 #28
0
    def test_hypervisorCheckIn_deleted(self):
        '''Test running hypervisorCheckIn on system that was deleted from Satellite'''
        system_id = 'wrong-system-id'
        temp, filename = tempfile.mkstemp(suffix=system_id)
        self.addCleanup(os.unlink, filename)
        with os.fdopen(temp, "wb") as f:
            pickle.dump({'system_id': system_id}, f)

        options = Options("http://localhost:%s" % TEST_PORT, "username",
                          "password")
        s = Satellite(self.logger, options)

        s.HYPERVISOR_SYSTEMID_FILE = filename.replace(system_id, '%s')
        config = Config('test', 'libvirt')
        mapping = {'hypervisors': [Hypervisor(system_id, [])]}
        report = HostGuestAssociationReport(config, mapping)
        s.hypervisorCheckIn(report, options)
        with open(filename, "rb") as f:
            data = pickle.load(f)
        self.assertEqual(data['system_id'], TEST_SYSTEM_ID)
예제 #29
0
    def test_hypervisorCheckIn_deleted(self):
        '''Test running hypervisorCheckIn on system that was deleted from Satellite'''
        system_id = 'wrong-system-id'
        temp, filename = tempfile.mkstemp(suffix=system_id)
        self.addCleanup(os.unlink, filename)
        with os.fdopen(temp, "wb") as f:
            pickle.dump({'system_id': system_id}, f)

        options = Mock()
        s = Satellite(self.logger, options)

        s.HYPERVISOR_SYSTEMID_FILE = filename.replace(system_id, '%s')
        config, d = self.create_fake_config(
            'test', **TestSatellite.default_config_args)
        mapping = {'hypervisors': [Hypervisor(system_id, [])]}
        report = HostGuestAssociationReport(config, mapping)
        s.hypervisorCheckIn(report, options)
        with open(filename, "rb") as f:
            data = pickle.load(f)
        self.assertEqual(data['system_id'], TEST_SYSTEM_ID)
예제 #30
0
    def test_getHostGuestMapping(self, host_to_uvm_map):
        host_to_uvm_map.return_value = HOST_UVM_MAP

        expected_result = []

        for host_uuid in HOST_UVM_MAP:
            host = HOST_UVM_MAP[host_uuid]
            hypervisor_id = host_uuid
            host_name = host['name']
            cluster_uuid = host['cluster_uuid']
            guests = []
            for guest_vm in host['guest_list']:
                state = virt.Guest.STATE_RUNNING
                guests.append(
                    Guest(guest_vm['uuid'], self.ahv.CONFIG_TYPE, state))

            facts = {
                Hypervisor.CPU_SOCKET_FACT: '2',
                Hypervisor.HYPERVISOR_TYPE_FACT: u'kKvm',
                Hypervisor.HYPERVISOR_VERSION_FACT: 'Nutanix 20180802.100874',
                Hypervisor.HYPERVISOR_CLUSTER: str(cluster_uuid),
                Hypervisor.SYSTEM_UUID_FACT: str(host_uuid)
            }

            expected_result.append(
                Hypervisor(name=host_name,
                           hypervisorId=hypervisor_id,
                           guestIds=guests,
                           facts=facts))

        result = self.ahv.getHostGuestMapping()['hypervisors']

        self.assertEqual(len(result), len(expected_result),
                         'lists length do not match')

        for index in range(0, len(result)):
            self.assertEqual(expected_result[index].toDict(),
                             result[index].toDict())
class TestSubscriptionManager(TestBase):
    guestList = [
        Guest('222', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING),
        Guest('111', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING),
        Guest('333', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING),
    ]
    mapping = {
        'hypervisors': [
            Hypervisor('123', guestList, name='TEST_HYPERVISOR'),
            Hypervisor('123', guestList, name='TEST_HYPERVISOR2')
        ]
    }
    hypervisor_id = "HYPERVISOR_ID"
    uep_connection = None

    @classmethod
    @patch('rhsm.config.initConfig')
    @patch('rhsm.certificate.create_from_file')
    def setUpClass(cls, rhsmcert, rhsmconfig):
        super(TestSubscriptionManager, cls).setUpClass()
        config = VirtConfigSection.from_dict({'type': 'libvirt'}, 'test', None)
        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.connection = MagicMock()
        cls.sm.connection.return_value.has_capability = MagicMock(
            return_value=False)
        cls.sm.connection.return_value.getConsumer = MagicMock(return_value={})
        cls.sm.connection.return_value.getOwner = MagicMock(
            return_value={'key': 'owner'})
        cls.uep_connection = patch('rhsm.connection.UEPConnection',
                                   cls.sm.connection)
        cls.uep_connection.start()
        cls.sm.cert_uuid = 123

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.tempdir)
        cls.uep_connection.stop()

    def test_sendVirtGuests(self):
        config = VirtConfigSection.from_dict({'type': 'libvirt'}, 'test', None)
        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_hypervisorCheckIn(self):
        owner = "owner"
        config = VirtConfigSection.from_dict(
            {
                'type': 'libvirt',
                'owner': owner
            }, 'test', None)
        # Ensure the data takes the proper for for the old API
        self.sm.connection.return_value.has_capability = MagicMock(
            return_value=False)
        self.sm.logger = MagicMock()
        report = HostGuestAssociationReport(config, self.mapping)
        self.sm.hypervisorCheckIn(report)
        self.sm.connection.hypervisorCheckIn.assert_called_with(
            owner,
            '',
            dict((host.hypervisorId, [g.toDict() for g in host.guestIds])
                 for host in self.mapping['hypervisors']),
            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.")

    @patch('rhsm.connection.UEPConnection')
    # def test_hypervisorCheckInAsync(self):
    def test_hypervisorCheckInAsync(self, rhsmconnection):
        owner = 'owner'
        config = VirtConfigSection.from_dict(
            {
                'type': 'libvirt',
                'owner': owner
            }, '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',
                                                                '',
                                                                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)

    @patch('rhsm.connection.UEPConnection')
    # def test_hypervisorHeartbeat(self):
    def test_hypervisorHeartbeat(self, rhsmconnection):
        owner = 'owner'
        config = VirtConfigSection.from_dict(
            {
                'type': 'libvirt',
                'owner': owner
            }, 'test', None)
        # Ensure we try out the new API
        rhsmconnection.return_value.has_capability.return_value = True
        self.sm.logger = MagicMock()
        self.sm.hypervisorHeartbeat(config, options=None)
        self.sm.connection.hypervisorHeartbeat.assert_called_with(owner, None)

    @patch('rhsm.connection.UEPConnection')
    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)
예제 #32
0
class TestSatellite(TestBase):
    mapping = {
        'hypervisors': [
            Hypervisor('host-1', [
                Guest('guest1-1', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING),
                Guest('guest1-2', xvirt.CONFIG_TYPE, Guest.STATE_SHUTOFF)
            ]),
            Hypervisor('host-2', [
                Guest('guest2-1', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING),
                Guest('guest2-2', xvirt.CONFIG_TYPE, Guest.STATE_SHUTOFF),
                Guest('guest2-3', xvirt.CONFIG_TYPE, Guest.STATE_RUNNING)
            ])
        ]
    }

    # Used as the default configuration
    # Override in other tests if need be
    default_config_args = {
        'type': 'libvirt',
        'hypervisor_id': 'uuid',
        'simplified_vim': True,
        'sat_server': "http://localhost:%s" % TEST_PORT,
        'sat_username': '******',
        'sat_password': '******',
    }

    @classmethod
    def setUpClass(cls):
        super(TestSatellite, cls).setUpClass()
        cls.fake_server = FakeSatellite()
        cls.thread = threading.Thread(target=cls.fake_server.serve_forever)
        cls.thread.start()

    @classmethod
    def tearDownClass(cls):
        cls.fake_server.shutdown()

    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_wrong_username(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://*****:*****@patch('virtwho.password.Password._can_write')
    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)
예제 #33
0
    def test_getHostGuestMapping(self, session):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = 'Fake_uuid'
        expected_guestId = 'guest1UUID'
        expected_guest_state = Guest.STATE_UNKNOWN

        xenapi = session.return_value.xenapi

        host = {
            'uuid': expected_hypervisorId,
            'hostname': expected_hostname,
            'cpu_info': {
                'socket_count': '1'
            },
            'software_version': {
                'product_brand': 'XenServer',
                'product_version': '1.2.3',
            },
        }
        xenapi.host.get_all.return_value = [
            host
        ]
        xenapi.host.get_record.return_value = host
        control_domain = {
            'uuid': '0',
            'is_control_domain': True,
        }
        guest = {
            'uuid': expected_guestId,
            'power_state': 'unknown',
        }
        snapshot = {
            'uuid': '12345678-90AB-CDEF-1234-567890ABCDEF',
            'is_a_snapshot': True,
            'power_state': 'unknown',
        }

        xenapi.host.get_resident_VMs.return_value = [
            control_domain,
            snapshot,
            guest,
        ]
        xenapi.VM.get_record = lambda x: x

        expected_result = Hypervisor(
            hypervisorId=expected_hypervisorId,
            name=expected_hostname,
            guestIds=[
                Guest(
                    expected_guestId,
                    self.xen,
                    expected_guest_state,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '1',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'XenServer',
                Hypervisor.HYPERVISOR_VERSION_FACT: '1.2.3',
            }
        )
        self.xen._prepare()
        result = self.xen.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #34
0
    def test_getHostGuestMapping(self, session):
        expected_hostname = 'hostname.domainname'
        expected_hypervisorId = 'Fake_uuid'
        expected_guestId = 'guest1UUID'
        expected_guest_state = Guest.STATE_UNKNOWN

        xenapi = session.return_value.xenapi

        host = {
            'uuid': expected_hypervisorId,
            'hostname': expected_hostname,
            'cpu_info': {
                'socket_count': '1'
            },
            'software_version': {
                'product_brand': 'XenServer',
                'product_version': '1.2.3',
            },
        }
        xenapi.host.get_all.return_value = [
            host
        ]
        xenapi.host.get_record.return_value = host
        control_domain = {
            'uuid': '0',
            'is_control_domain': True,
        }
        guest = {
            'uuid': expected_guestId,
            'power_state': 'unknown',
        }
        snapshot = {
            'uuid': '12345678-90AB-CDEF-1234-567890ABCDEF',
            'is_a_snapshot': True,
            'power_state': 'unknown',
        }

        xenapi.host.get_resident_VMs.return_value = [
            control_domain,
            snapshot,
            guest,
        ]
        xenapi.VM.get_record = lambda x: x

        expected_result = Hypervisor(
            hypervisorId=expected_hypervisorId,
            name=expected_hostname,
            guestIds=[
                Guest(
                    expected_guestId,
                    self.xen,
                    expected_guest_state,
                )
            ],
            facts={
                Hypervisor.CPU_SOCKET_FACT: '1',
                Hypervisor.HYPERVISOR_TYPE_FACT: 'XenServer',
                Hypervisor.HYPERVISOR_VERSION_FACT: '1.2.3',
            }
        )
        self.xen._prepare()
        result = self.xen.getHostGuestMapping()['hypervisors'][0]
        self.assertEqual(expected_result.toDict(), result.toDict())
예제 #35
0
class TestSubscriptionManager(TestBase):
    guestList = [
        Guest('222', xvirt, Guest.STATE_RUNNING),
        Guest('111', xvirt, Guest.STATE_RUNNING),
        Guest('333', xvirt, Guest.STATE_RUNNING),
    ]
    mapping = {
        'hypervisors': [Hypervisor('123', guestList, name='TEST_HYPERVISOR')]
    }
    hypervisor_id = "HYPERVISOR_ID"

    @classmethod
    @patch('rhsm.config.initConfig')
    @patch('rhsm.certificate.create_from_file')
    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

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.tempdir)

    @patch('rhsm.connection.UEPConnection')
    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)

    @patch('rhsm.connection.UEPConnection')
    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)

    @patch('rhsm.connection.UEPConnection')
    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
        )

    @patch('rhsm.connection.UEPConnection')
    def test_job_status(self, rhsmconnection):
        rhsmconnection.return_value.has_capability.return_value = True
        config = Config("test", "esx", owner='owner', env='env')
        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_guest(host, guests):
            return {
                'uuid': host,
                'guestIds': [{'guestId': guest} for guest in guests]
            }
        rhsmconnection.return_value.getJob.return_value = {
            'state': 'FINISHED',
            'resultData': {
                'failedUpdate': ["failed"],
                'updated': [
                    host_guest('123', ['111', '222'])
                ],
                'created': [
                    host_guest('456', ['333', '444'])
                ],
                'unchanged': [
                    host_guest('789', ['555', '666'])
                ]
            }
        }
        self.sm.logger = MagicMock()
        self.sm.check_report_state(report)
        # calls: authenticating + checking job status + 3 host guest lines
        self.assertEqual(self.sm.logger.debug.call_count, 5)
        self.assertEqual(report.state, AbstractVirtReport.STATE_FINISHED)
예제 #36
0
class TestSatellite(TestBase):
    mapping = {
        'hypervisors': [
            Hypervisor('host-1', [
                Guest('guest1-1', xvirt, Guest.STATE_RUNNING),
                Guest('guest1-2', xvirt, Guest.STATE_SHUTOFF)
            ]),
            Hypervisor('host-2', [
                Guest('guest2-1', xvirt, Guest.STATE_RUNNING),
                Guest('guest2-2', xvirt, Guest.STATE_SHUTOFF),
                Guest('guest2-3', xvirt, Guest.STATE_RUNNING)
            ])
        ]
    }

    @classmethod
    def setUpClass(cls):
        super(TestSatellite, cls).setUpClass()
        cls.fake_server = FakeSatellite()
        cls.thread = threading.Thread(target=cls.fake_server.serve_forever)
        cls.thread.start()

    @classmethod
    def tearDownClass(cls):
        cls.fake_server.shutdown()

    def test_wrong_server(self):
        options = Options("wrong_server", "abc", "def")
        s = Satellite(self.logger, options)
        config = Config('test', 'libvirt')
        report = HostGuestAssociationReport(config, self.mapping)
        self.assertRaises(SatelliteError, s.hypervisorCheckIn, report, options)

    def test_wrong_username(self):
        options = Options("http://*****:*****@patch('virtwho.password.Password._can_write')
    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)
예제 #37
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 = 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)