class PayloadActions(): """ payload wrapper to handle foreman calls """ def __init__(self): log.info("initializing payload actions") self.consumer_map = ConsumerMap("/tmp/hyp.json") self.katello = Katello() def find_or_create_hypervisor(self, payload): hyp_host = payload.get('host') # look up hypervisor in hypervisor hostname/uuid dict try: hyp_consumer_uuid = self.consumer_map.find_hypervisor_consumer_uuid(local_identifier=hyp_host) log.info("hypervisor consumer %s found, uuid is %s" % (hyp_host, hyp_consumer_uuid)) except KeyError: log.info("hypervisor consumer for %s not found locally, looking for record" % hyp_host) hyp_consumer_uuid = self.katello.find_hypervisor(hyp_host) if not hyp_consumer_uuid: log.error("no hypervisor found for %s" % hyp_host) raise RuntimeError("no hypervisor found for %s" % hyp_host) self.consumer_map.add_hypervisor_consumer_uuid(local_identifier=hyp_host, hyp_uuid=hyp_consumer_uuid) log.info("saved uuid %s for hypervisor %s" % (hyp_consumer_uuid, hyp_host)) return hyp_consumer_uuid def create_guest_mapping(self, payload, hypervisor_consumer_uuid): instance_id = payload.get('instance_id') log.info("associating guest %s with hypervisor %s" % (instance_id, hypervisor_consumer_uuid)) self.katello.associate_guest(instance_id, hypervisor_consumer_uuid) def delete_guest_mapping(self, payload, hypervisor_consumer_uuid): instance_id = payload.get('instance_id') log.info("removing guest %s from hypervisor %s" % (instance_id, hypervisor_consumer_uuid)) self.katello.unassociate_guest(instance_id, hypervisor_consumer_uuid)
def test_remove_hypervisor(self, mock_load_map, mock_save_map): mock_load_map.return_value = { 'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b' } self.consumer_map = ConsumerMap('/unused/path/name') self.consumer_map.remove_hypervisor_consumer_uuid( local_identifier="some_hostname") mock_save_map.assert_called_once_with(data={}, fname='/unused/path/name')
def test_hypervisor_found(self, mock_load_map): mock_load_map.return_value = { 'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b' } self.consumer_map = ConsumerMap('/unused/path/name') self.assertEquals( self.consumer_map.find_hypervisor_consumer_uuid( local_identifier="some_hostname"), 'a575563c-7c8c-11e3-b6bd-40d7db0f677b')
class TestConsumerMap(unittest.TestCase): def setUp(self): self.temp_json = NamedTemporaryFile(delete=False) self.consumer_map = ConsumerMap(self.temp_json.name) def test_no_hypervisor_found(self): self.consumer_map = ConsumerMap('/unused/path/name') self.assertRaises(KeyError, self.consumer_map.find_hypervisor_consumer_uuid, "some_hostname") @patch('katello_notification.consumer_map.ConsumerMap._load_consumer_map') def test_hypervisor_found(self, mock_load_map): mock_load_map.return_value = { 'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b' } self.consumer_map = ConsumerMap('/unused/path/name') self.assertEquals( self.consumer_map.find_hypervisor_consumer_uuid( local_identifier="some_hostname"), 'a575563c-7c8c-11e3-b6bd-40d7db0f677b') @patch('katello_notification.consumer_map.ConsumerMap._save_consumer_map') @patch('katello_notification.consumer_map.ConsumerMap._load_consumer_map') def test_remove_hypervisor(self, mock_load_map, mock_save_map): mock_load_map.return_value = { 'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b' } self.consumer_map = ConsumerMap('/unused/path/name') self.consumer_map.remove_hypervisor_consumer_uuid( local_identifier="some_hostname") mock_save_map.assert_called_once_with(data={}, fname='/unused/path/name') @patch('katello_notification.consumer_map.ConsumerMap._save_consumer_map') @patch('katello_notification.consumer_map.ConsumerMap._load_consumer_map') def test_add_hypervisor(self, mock_load_map, mock_save_map): # confirm that we overwrite the existing uuid mock_load_map.return_value = { 'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b' } self.consumer_map = ConsumerMap('/unused/path/name') self.consumer_map.add_hypervisor_consumer_uuid( local_identifier="some_hostname", hyp_uuid='9eddc386-7c91-11e3-99b6-40d7db0f677b') mock_save_map.assert_called_once_with( data={'some_hostname': '9eddc386-7c91-11e3-99b6-40d7db0f677b'}, fname='/unused/path/name') # confirm that we add a value now self.consumer_map.add_hypervisor_consumer_uuid( local_identifier="some_hostname_two", hyp_uuid='3d07466c-7c9d-11e3-bba9-40d7db0f677b') mock_save_map.assert_called_with(data={ 'some_hostname': '9eddc386-7c91-11e3-99b6-40d7db0f677b', 'some_hostname_two': '3d07466c-7c9d-11e3-bba9-40d7db0f677b' }, fname='/unused/path/name')
def test_add_hypervisor(self, mock_load_map, mock_save_map): # confirm that we overwrite the existing uuid mock_load_map.return_value = {'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b'} self.consumer_map = ConsumerMap('/unused/path/name') self.consumer_map.add_hypervisor_consumer_uuid(local_identifier="some_hostname", hyp_uuid='9eddc386-7c91-11e3-99b6-40d7db0f677b') mock_save_map.assert_called_once_with(data={'some_hostname': '9eddc386-7c91-11e3-99b6-40d7db0f677b'}, fname='/unused/path/name') # confirm that we add a value now self.consumer_map.add_hypervisor_consumer_uuid(local_identifier="some_hostname_two", hyp_uuid='3d07466c-7c9d-11e3-bba9-40d7db0f677b') mock_save_map.assert_called_with(data={'some_hostname': '9eddc386-7c91-11e3-99b6-40d7db0f677b', 'some_hostname_two': '3d07466c-7c9d-11e3-bba9-40d7db0f677b'}, fname='/unused/path/name')
def test_add_hypervisor(self, mock_load_map, mock_save_map): # confirm that we overwrite the existing uuid mock_load_map.return_value = { 'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b' } self.consumer_map = ConsumerMap('/unused/path/name') self.consumer_map.add_hypervisor_consumer_uuid( local_identifier="some_hostname", hyp_uuid='9eddc386-7c91-11e3-99b6-40d7db0f677b') mock_save_map.assert_called_once_with( data={'some_hostname': '9eddc386-7c91-11e3-99b6-40d7db0f677b'}, fname='/unused/path/name') # confirm that we add a value now self.consumer_map.add_hypervisor_consumer_uuid( local_identifier="some_hostname_two", hyp_uuid='3d07466c-7c9d-11e3-bba9-40d7db0f677b') mock_save_map.assert_called_with(data={ 'some_hostname': '9eddc386-7c91-11e3-99b6-40d7db0f677b', 'some_hostname_two': '3d07466c-7c9d-11e3-bba9-40d7db0f677b' }, fname='/unused/path/name')
class TestConsumerMap(unittest.TestCase): def setUp(self): self.temp_json = NamedTemporaryFile(delete=False) self.consumer_map = ConsumerMap(self.temp_json.name) def test_no_hypervisor_found(self): self.consumer_map = ConsumerMap('/unused/path/name') self.assertRaises(KeyError, self.consumer_map.find_hypervisor_consumer_uuid, "some_hostname") @patch('katello_notification.consumer_map.ConsumerMap._load_consumer_map') def test_hypervisor_found(self, mock_load_map): mock_load_map.return_value = {'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b'} self.consumer_map = ConsumerMap('/unused/path/name') self.assertEquals(self.consumer_map.find_hypervisor_consumer_uuid(local_identifier="some_hostname"), 'a575563c-7c8c-11e3-b6bd-40d7db0f677b') @patch('katello_notification.consumer_map.ConsumerMap._save_consumer_map') @patch('katello_notification.consumer_map.ConsumerMap._load_consumer_map') def test_remove_hypervisor(self, mock_load_map, mock_save_map): mock_load_map.return_value = {'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b'} self.consumer_map = ConsumerMap('/unused/path/name') self.consumer_map.remove_hypervisor_consumer_uuid(local_identifier="some_hostname") mock_save_map.assert_called_once_with(data={}, fname='/unused/path/name') @patch('katello_notification.consumer_map.ConsumerMap._save_consumer_map') @patch('katello_notification.consumer_map.ConsumerMap._load_consumer_map') def test_add_hypervisor(self, mock_load_map, mock_save_map): # confirm that we overwrite the existing uuid mock_load_map.return_value = {'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b'} self.consumer_map = ConsumerMap('/unused/path/name') self.consumer_map.add_hypervisor_consumer_uuid(local_identifier="some_hostname", hyp_uuid='9eddc386-7c91-11e3-99b6-40d7db0f677b') mock_save_map.assert_called_once_with(data={'some_hostname': '9eddc386-7c91-11e3-99b6-40d7db0f677b'}, fname='/unused/path/name') # confirm that we add a value now self.consumer_map.add_hypervisor_consumer_uuid(local_identifier="some_hostname_two", hyp_uuid='3d07466c-7c9d-11e3-bba9-40d7db0f677b') mock_save_map.assert_called_with(data={'some_hostname': '9eddc386-7c91-11e3-99b6-40d7db0f677b', 'some_hostname_two': '3d07466c-7c9d-11e3-bba9-40d7db0f677b'}, fname='/unused/path/name')
def test_remove_hypervisor(self, mock_load_map, mock_save_map): mock_load_map.return_value = {'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b'} self.consumer_map = ConsumerMap('/unused/path/name') self.consumer_map.remove_hypervisor_consumer_uuid(local_identifier="some_hostname") mock_save_map.assert_called_once_with(data={}, fname='/unused/path/name')
def test_hypervisor_found(self, mock_load_map): mock_load_map.return_value = {'some_hostname': 'a575563c-7c8c-11e3-b6bd-40d7db0f677b'} self.consumer_map = ConsumerMap('/unused/path/name') self.assertEquals(self.consumer_map.find_hypervisor_consumer_uuid(local_identifier="some_hostname"), 'a575563c-7c8c-11e3-b6bd-40d7db0f677b')
def test_no_hypervisor_found(self): self.consumer_map = ConsumerMap('/unused/path/name') self.assertRaises(KeyError, self.consumer_map.find_hypervisor_consumer_uuid, "some_hostname")
def setUp(self): self.temp_json = NamedTemporaryFile(delete=False) self.consumer_map = ConsumerMap(self.temp_json.name)
def __init__(self): log.info("initializing payload actions") self.consumer_map = ConsumerMap("/tmp/hyp.json") self.katello = Katello()