From 0df72bf47c88a69a20010255e60bd18a43e4d23c Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Fri, 3 Jul 2020 08:04:09 +0200 Subject: [PATCH 1/4] Make shared resource cleanup method class instance method At present the shared resource cleanup method is a class method. This will prohibit descendents of the class to influence whether cleanup should be run. Remove the @classmethod decorator so that it will make its descisions based on class instance variables set by the descendent. --- zaza/openstack/charm_tests/test_utils.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/zaza/openstack/charm_tests/test_utils.py b/zaza/openstack/charm_tests/test_utils.py index 4b4e1d0..6abb381 100644 --- a/zaza/openstack/charm_tests/test_utils.py +++ b/zaza/openstack/charm_tests/test_utils.py @@ -100,8 +100,7 @@ class BaseCharmTest(unittest.TestCase): run_resource_cleanup = False - @classmethod - def resource_cleanup(cls): + def resource_cleanup(self): """Cleanup any resources created during the test run. Override this method with a method which removes any resources @@ -111,12 +110,13 @@ class BaseCharmTest(unittest.TestCase): """ pass - @classmethod - def tearDown(cls): + # this must be a class instance method otherwise descentents will not be + # able to influence if cleanup should be run. + def tearDown(self): """Run teardown for test class.""" - if cls.run_resource_cleanup: + if self.run_resource_cleanup: logging.info('Running resource cleanup') - cls.resource_cleanup() + self.resource_cleanup() @classmethod def setUpClass(cls, application_name=None, model_alias=None): @@ -440,6 +440,7 @@ class OpenStackBaseTest(BaseCharmTest): cls.nova_client = ( openstack_utils.get_nova_session_client(cls.keystone_session)) + def launch_guest(self, guest_name, userdata=None): """Launch two guests to use in tests. From 29356f6419ef76dd182c63322bdba5edcfcefac9 Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Fri, 3 Jul 2020 08:14:50 +0200 Subject: [PATCH 2/4] Move cleanup of launched instances to common class Use the nomenclature for resource cleanup as defined in the `BaseCharmTest`. --- zaza/openstack/charm_tests/neutron/tests.py | 20 +------------------- zaza/openstack/charm_tests/test_utils.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/zaza/openstack/charm_tests/neutron/tests.py b/zaza/openstack/charm_tests/neutron/tests.py index 2ddfda4..620767e 100644 --- a/zaza/openstack/charm_tests/neutron/tests.py +++ b/zaza/openstack/charm_tests/neutron/tests.py @@ -616,24 +616,6 @@ class NeutronNetworkingBase(test_utils.OpenStackBaseTest): super(NeutronNetworkingBase, cls).setUpClass() cls.neutron_client = ( openstack_utils.get_neutron_session_client(cls.keystone_session)) - # NOTE(fnordahl): in the event of a test failure we do not want to run - # tear down code as it will make debugging a problem virtually - # impossible. To alleviate each test method will set the - # `run_tearDown` instance variable at the end which will let us run - # tear down only when there were no failure. - cls.run_tearDown = False - - @classmethod - def tearDown(cls): - """Remove test resources.""" - if cls.run_tearDown: - logging.info('Running teardown') - for server in cls.nova_client.servers.list(): - if server.name.startswith(cls.RESOURCE_PREFIX): - openstack_utils.delete_resource( - cls.nova_client.servers, - server.id, - msg="server") @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, max=60), reraise=True, stop=tenacity.stop_after_attempt(8)) @@ -803,7 +785,7 @@ class NeutronNetworkingTest(NeutronNetworkingBase): self.launch_guests() instance_1, instance_2 = self.retrieve_guests() self.check_connectivity(instance_1, instance_2) - self.run_tearDown = True + self.run_resource_cleanup = True class NeutronNetworkingVRRPTests(NeutronNetworkingBase): diff --git a/zaza/openstack/charm_tests/test_utils.py b/zaza/openstack/charm_tests/test_utils.py index 6abb381..83595b0 100644 --- a/zaza/openstack/charm_tests/test_utils.py +++ b/zaza/openstack/charm_tests/test_utils.py @@ -440,6 +440,20 @@ class OpenStackBaseTest(BaseCharmTest): cls.nova_client = ( openstack_utils.get_nova_session_client(cls.keystone_session)) + def resource_cleanup(self): + """Remove test resources.""" + try: + logging.info('Removing instances launched by test ({}*)' + .format(self.RESOURCE_PREFIX)) + for server in self.nova_client.servers.list(): + if server.name.startswith(self.RESOURCE_PREFIX): + openstack_utils.delete_resource( + self.nova_client.servers, + server.id, + msg="server") + except AttributeError: + # Test did not define self.RESOURCE_PREFIX, ignore. + pass def launch_guest(self, guest_name, userdata=None): """Launch two guests to use in tests. From b4f155d5e32ff45c50500bbedd073ea0485db223 Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Fri, 3 Jul 2020 08:28:38 +0200 Subject: [PATCH 3/4] octavia: Consume common helpers for test instance lifecycle --- zaza/openstack/charm_tests/octavia/setup.py | 19 ------- zaza/openstack/charm_tests/octavia/tests.py | 56 +++++++++++++-------- 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/zaza/openstack/charm_tests/octavia/setup.py b/zaza/openstack/charm_tests/octavia/setup.py index 55f8903..729fb16 100644 --- a/zaza/openstack/charm_tests/octavia/setup.py +++ b/zaza/openstack/charm_tests/octavia/setup.py @@ -98,25 +98,6 @@ def configure_octavia(): pass -def prepare_payload_instance(): - """Prepare a instance we can use as payload test.""" - session = openstack.get_overcloud_keystone_session() - keystone = openstack.get_keystone_session_client(session) - neutron = openstack.get_neutron_session_client(session) - project_id = openstack.get_project_id( - keystone, 'admin', domain_name='admin_domain') - openstack.add_neutron_secgroup_rules( - neutron, - project_id, - [{'protocol': 'tcp', - 'port_range_min': '80', - 'port_range_max': '80', - 'direction': 'ingress'}]) - zaza.openstack.configure.guest.launch_instance( - glance_setup.LTS_IMAGE_NAME, - userdata='#cloud-config\npackages:\n - apache2\n') - - def centralized_fip_network(): """Create network with centralized router for connecting lb and fips. diff --git a/zaza/openstack/charm_tests/octavia/tests.py b/zaza/openstack/charm_tests/octavia/tests.py index b189484..7049942 100644 --- a/zaza/openstack/charm_tests/octavia/tests.py +++ b/zaza/openstack/charm_tests/octavia/tests.py @@ -48,12 +48,13 @@ class LBAASv2Test(test_utils.OpenStackBaseTest): def setUpClass(cls): """Run class setup for running LBaaSv2 service tests.""" super(LBAASv2Test, cls).setUpClass() - - cls.keystone_session = openstack_utils.get_overcloud_keystone_session() + cls.keystone_client = openstack_utils.get_keystone_session_client( + cls.keystone_session) cls.neutron_client = openstack_utils.get_neutron_session_client( cls.keystone_session) cls.octavia_client = openstack_utils.get_octavia_session_client( cls.keystone_session) + cls.RESOURCE_PREFIX = 'zaza-octavia' # NOTE(fnordahl): in the event of a test failure we do not want to run # tear down code as it will make debugging a problem virtually @@ -63,28 +64,24 @@ class LBAASv2Test(test_utils.OpenStackBaseTest): cls.run_tearDown = False # List of load balancers created by this test cls.loadbalancers = [] - # LIst of floating IPs created by this test + # List of floating IPs created by this test cls.fips = [] - @classmethod - def tearDown(cls): - """Remove resources created during test execution. - - Note that resources created in the configure step prior to executing - the test should not be touched here. - """ - if not cls.run_tearDown: - return - for lb in cls.loadbalancers: - cls.octavia_client.load_balancer_delete(lb['id'], cascade=True) + def resource_cleanup(self): + """Remove resources created during test execution.""" + for lb in self.loadbalancers: + self.octavia_client.load_balancer_delete(lb['id'], cascade=True) try: - cls.wait_for_lb_resource( - cls.octavia_client.load_balancer_show, lb['id'], + self.wait_for_lb_resource( + self.octavia_client.load_balancer_show, lb['id'], provisioning_status='DELETED') except osc_lib.exceptions.NotFound: pass - for fip in cls.fips: - cls.neutron_client.delete_floatingip(fip) + for fip in self.fips: + self.neutron_client.delete_floatingip(fip) + # we run the parent resource_cleanup last as it will remove instances + # referenced as members in the above cleaned up load balancers + super(LBAASv2Test, self).resource_cleanup() @staticmethod @tenacity.retry(retry=tenacity.retry_if_exception_type(AssertionError), @@ -238,12 +235,27 @@ class LBAASv2Test(test_utils.OpenStackBaseTest): def test_create_loadbalancer(self): """Create load balancer.""" - nova_client = openstack_utils.get_nova_session_client( - self.keystone_session) + # Prepare payload instances + # First we allow communication to port 80 by adding a security group + # rule + project_id = openstack_utils.get_project_id( + self.keystone_client, 'admin', domain_name='admin_domain') + openstack_utils.add_neutron_secgroup_rules( + self.neutron_client, + project_id, + [{'protocol': 'tcp', + 'port_range_min': '80', + 'port_range_max': '80', + 'direction': 'ingress'}]) + + # Then we request two Ubuntu instances with the Apache web server + # installed + instance_1, instance_2 = self.launch_guests( + userdata='#cloud-config\npackages:\n - apache2\n') # Get IP of the prepared payload instances payload_ips = [] - for server in nova_client.servers.list(): + for server in (instance_1, instance_2): payload_ips.append(server.networks['private'][0]) self.assertTrue(len(payload_ips) > 0) @@ -274,4 +286,4 @@ class LBAASv2Test(test_utils.OpenStackBaseTest): lb_fp['floating_ip_address'])) # If we get here, it means the tests passed - self.run_tearDown = True + self.run_resource_cleanup = True From 849e08528b4e57a23f4c342c501f6927a5b98ebe Mon Sep 17 00:00:00 2001 From: Frode Nordahl Date: Fri, 3 Jul 2020 10:27:24 +0200 Subject: [PATCH 4/4] octavia: fix pause/resume test and temporarily disable it Re-enable when LP: #1886202 is fixed. --- zaza/openstack/charm_tests/octavia/tests.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/zaza/openstack/charm_tests/octavia/tests.py b/zaza/openstack/charm_tests/octavia/tests.py index 7049942..e84ac3e 100644 --- a/zaza/openstack/charm_tests/octavia/tests.py +++ b/zaza/openstack/charm_tests/octavia/tests.py @@ -38,7 +38,20 @@ class CharmOperationTest(test_utils.OpenStackBaseTest): Pause service and check services are stopped, then resume and check they are started. """ - self.pause_resume(['apache2']) + services = [ + 'apache2', + 'octavia-health-manager', + 'octavia-housekeeping', + 'octavia-worker', + ] + if openstack_utils.ovn_present(): + services.append('octavia-driver-agent') + logging.info('Skipping pause resume test LP: #1886202...') + return + logging.info('Testing pause resume (services="{}")' + .format(services)) + with self.pause_resume(services, pgrep_full=True): + pass class LBAASv2Test(test_utils.OpenStackBaseTest):