def test_getHostGuestMapping(self, mock_client): expected_hostname = 'Fake_hostname' expected_hypervisorId = 'Fake_uuid' expected_guestId = 'guest1UUID' expected_guest_state = Guest.STATE_RUNNING 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': 'poweredOn', 'config.uuid': expected_guestId}} self.esx.vms = fake_vms fake_host = {'hardware.systemInfo.uuid': expected_hypervisorId, 'name': expected_hostname, '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) ] ) result = self.esx.getHostGuestMapping()['hypervisors'][0] self.assertTrue(expected_result.toDict() == result.toDict())
def test_getHostGuestMapping_incomplete_data(self, mock_client): expected_hostname = None 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, '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, hypervisorType='vmware') ] ) result = self.esx.getHostGuestMapping()['hypervisors'][0] self.assertEqual(expected_result.toDict(), result.toDict())
def setUp(self): self.config = Config('config', 'esx', server='localhost', username='******', password='******', owner='owner', env='env', log_dir='', log_file='') self.second_config = Config('second_config', 'esx', server='localhost', username='******', password='******', owner='owner', env='env', log_dir='', log_file='') fake_virt = Mock() fake_virt.CONFIG_TYPE = 'esx' guests = [Guest('guest1', fake_virt, 1)] test_hypervisor = Hypervisor('test', guestIds=[Guest('guest1', fake_virt, 1)]) assoc = {'hypervisors': [test_hypervisor]} self.fake_domain_list = DomainListReport(self.second_config, guests) self.fake_report = HostGuestAssociationReport(self.config, assoc)
def test_sending_guests(self, fromOptions, fromConfig, getLogger): options = Mock() options.oneshot = True options.interval = 0 options.print_ = False fake_virt = Mock() fake_virt.CONFIG_TYPE = 'esx' test_hypervisor = Hypervisor('test', guestIds=[Guest('guest1', fake_virt, 1)]) association = {'hypervisors': [test_hypervisor]} options.log_dir = '' options.log_file = '' getLogger.return_value = sentinel.logger fromConfig.return_value.config.name = 'test' virtwho = VirtWho(self.logger, options, config_dir="/nonexistant") config = Config("test", "esx", server="localhost", username="******", password="******", owner="owner", env="env") virtwho.configManager.addConfig(config) virtwho.queue = Queue() virtwho.queue.put(HostGuestAssociationReport(config, association)) virtwho.run() fromConfig.assert_called_with(sentinel.logger, config) self.assertTrue(fromConfig.return_value.start.called) fromOptions.assert_called_with(self.logger, options)
def 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' 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', '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 expected_result = Hypervisor( hypervisorId=expected_hypervisorId, name=expected_hostname, guestIds=[ Guest( expected_guestId, self.esx, expected_guest_state, hypervisorType='vmware' ) ], facts={ 'cpu.cpu_socket(s)': '1', } ) result = self.esx.getHostGuestMapping()['hypervisors'][0] self.assertEqual(expected_result.toDict(), result.toDict())
class TestManager(TestBase): """ Test of all available subscription managers. """ guest1 = Guest('9c927368-e888-43b4-9cdb-91b10431b258', xvirt, Guest.STATE_RUNNING, hypervisorType='QEMU') guest2 = Guest('d5ffceb5-f79d-41be-a4c1-204f836e144a', xvirt, Guest.STATE_SHUTOFF, hypervisorType='QEMU') guestInfo = [guest1] mapping = { 'hypervisors': [ Hypervisor('9c927368-e888-43b4-9cdb-91b10431b258', []), Hypervisor('ad58b739-5288-4cbc-a984-bd771612d670', [guest1, guest2]) ] }
def test_getHostGuestMapping(self, mock_client): expected_hostname = 'Fake_hostname' expected_hypervisorId = 'Fake_uuid' expected_guestId = 'guest1UUID' expected_guest_state = Guest.STATE_RUNNING 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': 'poweredOn', 'config.uuid': expected_guestId } } self.esx.vms = fake_vms fake_host = { 'hardware.systemInfo.uuid': expected_hypervisorId, 'name': expected_hostname, '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, hypervisorType='vmware') ]) result = self.esx.getHostGuestMapping()['hypervisors'][0] self.assertEqual(expected_result.toDict(), result.toDict())
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')] } @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): self.sm.sendVirtGuests(self.guestList) self.sm.connection.updateConsumer.assert_called_with( 123, guest_uuids=[g.toDict() for g in self.guestList]) @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 self.sm.hypervisorCheckIn(config, self.mapping) 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'])) @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 self.sm.hypervisorCheckIn(config, self.mapping) expected = { 'hypervisors': [h.toDict() for h in self.mapping['hypervisors']] } self.sm.connection.hypervisorCheckIn.assert_called_with( owner, env, expected)
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'))
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) #self.assertRaises(SatelliteError, s.connect, "wrong_server", "abc", "def") options.env = "ENV" options.owner = "OWNER" s.hypervisorCheckIn(options, {'hypervisors': []}, "test") #self.assertRaises(SatelliteError, s.connect, "localhost", "abc", "def") def test_new_system(self): options = Options("http://localhost:%s" % TEST_PORT, "username", "password") options.force_register = True s = Satellite(self.logger, options) # Register with wrong username #self.assertRaises(SatelliteError, s.connect, "http://localhost:8080", "wrong", "password", force_register=True) # Register with wrong password #self.assertRaises(SatelliteError, s.connect, "http://localhost:8080", "username", "wrong", force_register=True) def test_hypervisorCheckIn(self): options = Options("http://localhost:%s" % TEST_PORT, "username", "password") options.force_register = True options.env = "ENV" options.owner = "OWNER" s = Satellite(self.logger, options) result = s.hypervisorCheckIn(options, self.mapping, "type") self.assertTrue("failedUpdate" in result) self.assertTrue("created" in result) self.assertTrue("updated" in result) def test_hypervisorCheckIn_preregistered(self): temp, filename = tempfile.mkstemp(suffix=TEST_SYSTEM_ID) self.addCleanup(os.unlink, filename) f = os.fdopen(temp, "wb") pickle.dump({'system_id': TEST_SYSTEM_ID}, f) f.close() options = Options("http://localhost:%s" % TEST_PORT, "username", "password") options.env = "ENV" options.owner = "OWNER" s = Satellite(self.logger, options) s.HYPERVISOR_SYSTEMID_FILE = filename.replace(TEST_SYSTEM_ID, '%s') result = s.hypervisorCheckIn(options, self.mapping, "type") self.assertTrue("failedUpdate" in result) self.assertTrue("created" in result) self.assertTrue("updated" in result)
def getHostGuestMapping(self): mapping = {'hypervisors': []} for host_id, host in self.hosts.items(): parent = host['parent'].value if self.config.exclude_host_parents is not None and parent in self.config.exclude_host_parents: self.logger.debug( "Skipping host '%s' because its parent '%s' is excluded" % (host_id, parent)) continue if self.config.filter_host_parents is not None and parent not in self.config.filter_host_parents: self.logger.debug( "Skipping host '%s' because its parent '%s' is not included" % (host_id, parent)) continue guests = [] try: if self.config.hypervisor_id == 'uuid': uuid = host['hardware.systemInfo.uuid'] elif self.config.hypervisor_id == 'hwuuid': uuid = host_id elif self.config.hypervisor_id == 'hostname': uuid = host['name'] else: raise virt.VirtError( 'Reporting of hypervisor %s is not implemented in %s backend' % (self.config.hypervisor_id, self.CONFIG_TYPE)) except KeyError: self.logger.debug( "Host '%s' doesn't have hypervisor_id property" % host_id) continue if host['vm']: for vm_id in host['vm'].ManagedObjectReference: if vm_id.value not in self.vms: self.logger.debug( "Host '%s' references non-existing guest '%s'" % (host_id, vm_id.value)) continue vm = self.vms[vm_id.value] if 'config.uuid' not in vm: self.logger.debug( "Guest '%s' doesn't have 'config.uuid' property" % vm_id.value) continue state = virt.Guest.STATE_UNKNOWN try: if vm['runtime.powerState'] == 'poweredOn': state = virt.Guest.STATE_RUNNING elif vm['runtime.powerState'] == 'suspended': state = virt.Guest.STATE_PAUSED elif vm['runtime.powerState'] == 'poweredOff': state = virt.Guest.STATE_SHUTOFF except KeyError: self.logger.debug( "Guest '%s' doesn't have 'runtime.powerState' property" % vm_id.value) guests.append( virt.Guest(vm['config.uuid'], self, state, hypervisorType=host.get( 'config.product.name', 'vmware'), hypervisorVersion=host.get( 'config.product.version', None))) mapping['hypervisors'].append( Hypervisor(hypervisorId=uuid, guestIds=guests, name=host.get('name', None))) return mapping