From fb0ab37c323717ca10ac3f3bda24ae390635495e Mon Sep 17 00:00:00 2001 From: spisarski Date: Wed, 7 Mar 2018 14:52:44 -0700 Subject: Added members to VmInst that will contain the availability_zone and compute_host names while deprecating the method get_vm_info(). JIRA: SNAPS-277 Change-Id: Idc8578b3f2cf2be8ef90f52dd025dbea729b222b Signed-off-by: spisarski --- docs/how-to-use/IntegrationTests.rst | 6 +- snaps/domain/test/vm_inst_tests.py | 7 +- snaps/domain/vm_inst.py | 11 ++- snaps/openstack/create_instance.py | 4 + snaps/openstack/tests/create_instance_tests.py | 104 ++++++++++++++++++------- snaps/openstack/utils/nova_utils.py | 4 +- 6 files changed, 105 insertions(+), 31 deletions(-) diff --git a/docs/how-to-use/IntegrationTests.rst b/docs/how-to-use/IntegrationTests.rst index 42f581e..deee6c7 100644 --- a/docs/how-to-use/IntegrationTests.rst +++ b/docs/how-to-use/IntegrationTests.rst @@ -609,7 +609,11 @@ create_instance_tests.py - CreateInstanceSimpleTests | Test Name | API Versions | Description | +=======================================+===============+===========================================================+ | test_create_delete_instance | Nova 2 | Ensures that the OpenStackVmInstance.clean() method | -| | Neutron 2 | deletes the instance | +| | Neutron 2 | deletes the instance as well as ensuring the VmInst | +| | | availability_zone is populated and compute_host is None | ++---------------------------------------+---------------+-----------------------------------------------------------+ +| test_create_admin_instance | Nova 2 | Ensures that the VmInst object members availability_zone | +| | Neutron 2 | and compute_host return a value | +---------------------------------------+---------------+-----------------------------------------------------------+ create_instance_tests.py - SimpleHealthCheck diff --git a/snaps/domain/test/vm_inst_tests.py b/snaps/domain/test/vm_inst_tests.py index ad7a9ce..c90837d 100644 --- a/snaps/domain/test/vm_inst_tests.py +++ b/snaps/domain/test/vm_inst_tests.py @@ -24,7 +24,7 @@ class VmInstDomainObjectTests(unittest.TestCase): def test_construction_positional(self): vm_inst = VmInst('name', 'id', '456', '123', list(), 'kp-name', - ['foo', 'bar'], ['123', '456']) + ['foo', 'bar'], ['123', '456'], 'host1', 'zone1') self.assertEqual('name', vm_inst.name) self.assertEqual('id', vm_inst.id) self.assertEqual('456', vm_inst.image_id) @@ -33,9 +33,12 @@ class VmInstDomainObjectTests(unittest.TestCase): self.assertEqual('kp-name', vm_inst.keypair_name) self.assertEqual(['foo', 'bar'], vm_inst.sec_grp_names) self.assertEqual(['123', '456'], vm_inst.volume_ids) + self.assertEqual('host1', vm_inst.compute_host) + self.assertEqual('zone1', vm_inst.availability_zone) def test_construction_named(self): vm_inst = VmInst( + availability_zone='zone1', compute_host='host1', volume_ids=['123', '456'], sec_grp_names=['foo', 'bar'], ports=list(), inst_id='id', name='name', flavor_id='123', image_id='456', keypair_name='kp-name') @@ -47,6 +50,8 @@ class VmInstDomainObjectTests(unittest.TestCase): self.assertEqual('kp-name', vm_inst.keypair_name) self.assertEqual(['foo', 'bar'], vm_inst.sec_grp_names) self.assertEqual(['123', '456'], vm_inst.volume_ids) + self.assertEqual('host1', vm_inst.compute_host) + self.assertEqual('zone1', vm_inst.availability_zone) class FloatingIpDomainObjectTests(unittest.TestCase): diff --git a/snaps/domain/vm_inst.py b/snaps/domain/vm_inst.py index c49b03e..f3b5381 100644 --- a/snaps/domain/vm_inst.py +++ b/snaps/domain/vm_inst.py @@ -20,7 +20,8 @@ class VmInst: are shared amongst cloud providers """ def __init__(self, name, inst_id, image_id, flavor_id, ports, - keypair_name, sec_grp_names, volume_ids): + keypair_name, sec_grp_names, volume_ids, compute_host, + availability_zone): """ Constructor :param name: the image's name @@ -32,6 +33,11 @@ class VmInst: :param keypair_name: the name of the associated keypair :param sec_grp_names: list of security group names :param volume_ids: list of attached volume IDs + :param compute_host: the name of the host on which this VM is running + When the user requesting this query is not part of + the 'admin' role, this value will be None + :param availability_zone: the name of the availability zone to which + this VM has been assigned """ self.name = name self.id = inst_id @@ -41,6 +47,8 @@ class VmInst: self.keypair_name = keypair_name self.sec_grp_names = sec_grp_names self.volume_ids = volume_ids + self.compute_host = compute_host + self.availability_zone = availability_zone def __eq__(self, other): return (self.name == other.name and @@ -49,7 +57,6 @@ class VmInst: self.flavor_id == other.flavor_id and self.ports == other.ports and self.keypair_name == other.keypair_name and - self.sec_grp_names == other.sec_grp_names and self.volume_ids == other.volume_ids) diff --git a/snaps/openstack/create_instance.py b/snaps/openstack/create_instance.py index f897434..a964723 100644 --- a/snaps/openstack/create_instance.py +++ b/snaps/openstack/create_instance.py @@ -431,6 +431,10 @@ class OpenStackVmInstance(OpenStackComputeObject): Returns a dictionary of a VMs info as returned by OpenStack :return: a dict() """ + from warnings import warn + warn('Do not use the returned dict() structure', + DeprecationWarning) + return nova_utils.get_server_info(self._nova, self.__vm) def __get_first_provisioning_floating_ip(self): diff --git a/snaps/openstack/tests/create_instance_tests.py b/snaps/openstack/tests/create_instance_tests.py index 77d5833..12ae10b 100644 --- a/snaps/openstack/tests/create_instance_tests.py +++ b/snaps/openstack/tests/create_instance_tests.py @@ -48,9 +48,8 @@ from snaps.openstack.create_volume import OpenStackVolume from snaps.openstack.tests import openstack_tests, validation_utils from snaps.openstack.tests.os_source_file_test import ( OSIntegrationTestCase, OSComponentTestCase) -from snaps.openstack.utils import nova_utils +from snaps.openstack.utils import nova_utils, keystone_utils, neutron_utils from snaps.openstack.utils.nova_utils import RebootType -from snaps.openstack.utils import nova_utils, settings_utils, neutron_utils __author__ = 'spisarski' @@ -411,23 +410,16 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): def setUp(self): """ - Instantiates the CreateImage object that is responsible for downloading - and creating an OS image file - within OpenStack + Setup the objects required for the test """ super(self.__class__, self).__start__() - guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) - self.vm_inst_name = guid + '-inst' + self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) + self.vm_inst_name = self.guid + '-inst' self.nova = nova_utils.nova_client(self.os_creds) self.neutron = neutron_utils.neutron_client(self.os_creds) os_image_settings = openstack_tests.cirros_image_settings( - name=guid + '-image', image_metadata=self.image_metadata) - - net_config = openstack_tests.get_priv_net_config( - net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet', - router_name=guid + '-pub-router', external_net=self.ext_net_name, - netconf_override=self.netconf_override) + name=self.guid + '-image', image_metadata=self.image_metadata) # Initialize for tearDown() self.image_creator = None @@ -445,19 +437,10 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): # Create Flavor self.flavor_creator = OpenStackFlavor( self.admin_os_creds, - FlavorConfig(name=guid + '-flavor-name', ram=256, disk=10, + FlavorConfig(name=self.guid + '-flavor-name', ram=256, disk=10, vcpus=2, metadata=self.flavor_metadata)) self.flavor_creator.create() - - # Create Network - self.network_creator = OpenStackNetwork( - self.os_creds, net_config.network_settings) - self.network_creator.create() - - self.port_settings = PortConfig( - name=guid + '-port', - network_name=net_config.network_settings.name) - + self.network_creator = None except Exception as e: self.tearDown() raise e @@ -504,6 +487,21 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): Tests the creation of an OpenStack instance with a single port with a static IP without a Floating IP. """ + # Create Network + net_config = openstack_tests.get_priv_net_config( + net_name=self.guid + '-pub-net', + subnet_name=self.guid + '-pub-subnet', + router_name=self.guid + '-pub-router', + external_net=self.ext_net_name, + netconf_override=self.netconf_override) + self.network_creator = OpenStackNetwork( + self.os_creds, net_config.network_settings) + self.network_creator.create() + + self.port_settings = PortConfig( + name=self.guid + '-port', + network_name=net_config.network_settings.name) + instance_settings = VmInstanceConfig( name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, @@ -513,11 +511,14 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): self.os_creds, instance_settings, self.image_creator.image_settings) - vm_inst = self.inst_creator.create() + vm_inst = self.inst_creator.create(block=True) self.assertIsNotNone(nova_utils.get_server( self.nova, self.neutron, self.keystone, vm_inst_settings=instance_settings)) + self.assertIsNotNone(self.inst_creator.get_vm_inst().availability_zone) + self.assertIsNone(self.inst_creator.get_vm_inst().compute_host) + # Delete instance nova_utils.delete_vm_instance(self.nova, vm_inst) @@ -529,6 +530,53 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): # Exception should not be thrown self.inst_creator.clean() + def test_create_admin_instance(self): + """ + Tests the creation of an OpenStack instance with a single port with a + static IP without a Floating IP. + """ + # Create Network + net_config = openstack_tests.get_priv_net_config( + net_name=self.guid + '-pub-net', + subnet_name=self.guid + '-pub-subnet', + router_name=self.guid + '-pub-router', + external_net=self.ext_net_name, + netconf_override=self.netconf_override) + self.network_creator = OpenStackNetwork( + self.admin_os_creds, net_config.network_settings) + self.network_creator.create() + + self.port_settings = PortConfig( + name=self.guid + '-port', + network_name=net_config.network_settings.name) + + instance_settings = VmInstanceConfig( + name=self.vm_inst_name, + flavor=self.flavor_creator.flavor_settings.name, + port_settings=[self.port_settings]) + + self.inst_creator = OpenStackVmInstance( + self.admin_os_creds, instance_settings, + self.image_creator.image_settings) + + admin_nova = nova_utils.nova_client(self.admin_os_creds) + admin_neutron = neutron_utils.neutron_client(self.admin_os_creds) + admin_key = keystone_utils.keystone_client(self.admin_os_creds) + vm_inst = self.inst_creator.create(block=True) + + self.assertIsNotNone(vm_inst) + vm_inst_get = nova_utils.get_server( + admin_nova, admin_neutron, admin_key, + vm_inst_settings=instance_settings) + self.assertEqual(vm_inst, vm_inst_get) + + self.assertIsNone(nova_utils.get_server( + self.nova, self.neutron, self.keystone, + vm_inst_settings=instance_settings)) + + self.assertIsNotNone(self.inst_creator.get_vm_inst().availability_zone) + self.assertIsNotNone(self.inst_creator.get_vm_inst().compute_host) + class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): """ @@ -1594,7 +1642,11 @@ class CreateInstanceOnComputeHost(OSIntegrationTestCase): self.admin_os_creds, instance_settings, self.image_creator.image_settings) self.inst_creators.append(inst_creator) - inst_creator.create() + inst_creator.create(block=True) + avail_zone = inst_creator.get_vm_inst().availability_zone + self.assertTrue(avail_zone in zone) + compute_host = inst_creator.get_vm_inst().compute_host + self.assertTrue(compute_host in zone) # Validate instances to ensure they've been deployed to the correct # server diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py index 5e6a037..faa2d75 100644 --- a/snaps/openstack/utils/nova_utils.py +++ b/snaps/openstack/utils/nova_utils.py @@ -214,7 +214,9 @@ def __map_os_server_obj_to_vm_inst(neutron, keystone, os_server, name=os_server.name, inst_id=os_server.id, image_id=os_server.image['id'], flavor_id=os_server.flavor['id'], ports=out_ports, keypair_name=os_server.key_name, - sec_grp_names=sec_grp_names, volume_ids=volumes) + sec_grp_names=sec_grp_names, volume_ids=volumes, + compute_host=os_server._info.get('OS-EXT-SRV-ATTR:host'), + availability_zone=os_server._info.get('OS-EXT-AZ:availability_zone')) def __get_latest_server_os_object(nova, server): -- cgit 1.2.3-korg