From 8e129128b2b26e726c2130e45d0ab15815a3695f Mon Sep 17 00:00:00 2001 From: licanwei Date: Fri, 14 Sep 2018 14:05:27 +0800 Subject: [PATCH] improve scenario test move some common functions to base class Change-Id: I577c35f427519136828d8efb87b3e00f37ec2f5c --- watcher_tempest_plugin/tests/scenario/base.py | 103 ++++++++++++++++++ .../tests/scenario/test_execute_actuator.py | 76 ------------- .../scenario/test_execute_basic_optim.py | 75 ------------- .../test_execute_workload_balancing.py | 103 ------------------ .../scenario/test_execute_zone_migration.py | 102 ----------------- 5 files changed, 103 insertions(+), 356 deletions(-) diff --git a/watcher_tempest_plugin/tests/scenario/base.py b/watcher_tempest_plugin/tests/scenario/base.py index 302f8c79..db0104e9 100644 --- a/watcher_tempest_plugin/tests/scenario/base.py +++ b/watcher_tempest_plugin/tests/scenario/base.py @@ -68,6 +68,64 @@ class BaseInfraOptimScenarioTest(manager.ScenarioTest): """Ensure that all created objects get destroyed.""" super(BaseInfraOptimScenarioTest, cls).resource_cleanup() + @classmethod + def get_hypervisors_setup(cls): + hypervisors_client = cls.mgr.hypervisor_client + hypervisors = hypervisors_client.list_hypervisors( + detail=True)['hypervisors'] + return hypervisors + + @classmethod + def get_compute_nodes_setup(cls): + services_client = cls.mgr.services_client + available_services = services_client.list_services()['services'] + + return [srv for srv in available_services + if srv.get('binary') == 'nova-compute'] + + @classmethod + def wait_for_compute_node_setup(cls): + + def _are_compute_nodes_setup(): + try: + hypervisors = cls.get_hypervisors_setup() + available_hypervisors = set( + hyp['hypervisor_hostname'] for hyp in hypervisors + if hyp['state'] == 'up') + available_services = set( + service['host'] + for service in cls.get_compute_nodes_setup() + if service['state'] == 'up') + return ( + len(available_hypervisors) == len(available_services) and + len(hypervisors) >= 2) + except Exception as exc: + LOG.exception(exc) + return False + + assert test_utils.call_until_true( + func=_are_compute_nodes_setup, + duration=600, + sleep_for=2 + ) + + @classmethod + def rollback_compute_nodes_status(cls): + current_compute_nodes_setup = cls.get_compute_nodes_setup() + for cn_setup in current_compute_nodes_setup: + cn_hostname = cn_setup.get('host') + matching_cns = [ + cns for cns in cls.initial_compute_nodes_setup + if cns.get('host') == cn_hostname + ] + initial_cn_setup = matching_cns[0] # Should return a single result + if cn_setup.get('status') != initial_cn_setup.get('status'): + if initial_cn_setup.get('status') == 'enabled': + rollback_func = cls.mgr.services_client.enable_service + else: + rollback_func = cls.mgr.services_client.disable_service + rollback_func(binary='nova-compute', host=cn_hostname) + @classmethod def wait_for(cls, condition, timeout=30): start_time = time.time() @@ -96,6 +154,51 @@ class BaseInfraOptimScenarioTest(manager.ScenarioTest): sleep_for=5 ) + def _migrate_server_to(self, server_id, dest_host, volume_backed=False): + kwargs = dict() + kwargs['disk_over_commit'] = False + block_migration = (CONF.compute_feature_enabled. + block_migration_for_live_migration and + not volume_backed) + body = self.mgr.servers_client.live_migrate_server( + server_id, host=dest_host, block_migration=block_migration, + **kwargs) + return body + + def _create_one_instance_per_host(self): + """Create 1 instance per compute node + + This goes up to the min_compute_nodes threshold so that things don't + get crazy if you have 1000 compute nodes but set min to 3. + """ + host_client = self.mgr.hosts_client + all_hosts = host_client.list_hosts()['hosts'] + compute_nodes = [x for x in all_hosts if x['service'] == 'compute'] + + created_instances = [] + for _ in compute_nodes[:CONF.compute.min_compute_nodes]: + # by getting to active state here, this means this has + # landed on the host in question. + instance = self.create_server(image_id=CONF.compute.image_ref, + wait_until='ACTIVE', + clients=self.os_primary) + # get instance object again as admin + instance = self.mgr.servers_client.show_server( + instance['id'])['server'] + created_instances.append(instance) + self.make_instance_statistic(instance) + + return created_instances + + def _pack_all_created_instances_on_one_host(self, instances): + hypervisors = [ + hyp['hypervisor_hostname'] for hyp in self.get_hypervisors_setup() + if hyp['state'] == 'up'] + node = hypervisors[0] + for instance in instances: + if instance.get('OS-EXT-SRV-ATTR:hypervisor_hostname') != node: + self._migrate_server_to(instance['id'], node) + # ### GNOCCHI ### # def create_resource(self, **kwargs): diff --git a/watcher_tempest_plugin/tests/scenario/test_execute_actuator.py b/watcher_tempest_plugin/tests/scenario/test_execute_actuator.py index 8d367ed5..bf67d248 100644 --- a/watcher_tempest_plugin/tests/scenario/test_execute_actuator.py +++ b/watcher_tempest_plugin/tests/scenario/test_execute_actuator.py @@ -78,82 +78,6 @@ class TestExecuteActionsViaActuator(base.BaseInfraOptimScenarioTest): "Less than 2 compute nodes are enabled, " "skipping multinode tests.") - @classmethod - def get_compute_nodes_setup(cls): - services_client = cls.mgr.services_client - available_services = services_client.list_services()['services'] - - return [srv for srv in available_services - if srv.get('binary') == 'nova-compute'] - - @classmethod - def wait_for_compute_node_setup(cls): - - def _are_compute_nodes_setup(): - try: - hypervisors_client = cls.mgr.hypervisor_client - hypervisors = hypervisors_client.list_hypervisors( - detail=True)['hypervisors'] - available_hypervisors = set( - hyp['hypervisor_hostname'] for hyp in hypervisors) - available_services = set( - service['host'] - for service in cls.get_compute_nodes_setup()) - - return ( - available_hypervisors == available_services and - len(hypervisors) >= 2) - except Exception: - return False - - assert test_utils.call_until_true( - func=_are_compute_nodes_setup, - duration=600, - sleep_for=2 - ) - - @classmethod - def rollback_compute_nodes_status(cls): - current_compute_nodes_setup = cls.get_compute_nodes_setup() - for cn_setup in current_compute_nodes_setup: - cn_hostname = cn_setup.get('host') - matching_cns = [ - cns for cns in cls.initial_compute_nodes_setup - if cns.get('host') == cn_hostname - ] - initial_cn_setup = matching_cns[0] # Should return a single result - if cn_setup.get('status') != initial_cn_setup.get('status'): - if initial_cn_setup.get('status') == 'enabled': - rollback_func = cls.mgr.services_client.enable_service - else: - rollback_func = cls.mgr.services_client.disable_service - rollback_func(binary='nova-compute', host=cn_hostname) - - def _create_one_instance_per_host(self): - """Create 1 instance per compute node - - This goes up to the min_compute_nodes threshold so that things don't - get crazy if you have 1000 compute nodes but set min to 3. - """ - host_client = self.mgr.hosts_client - all_hosts = host_client.list_hosts()['hosts'] - compute_nodes = [x for x in all_hosts if x['service'] == 'compute'] - - created_servers = [] - for _ in compute_nodes[:CONF.compute.min_compute_nodes]: - # by getting to active state here, this means this has - # landed on the host in question. - instance = self.create_server(image_id=CONF.compute.image_ref, - wait_until='ACTIVE', - clients=self.os_primary) - # get instance object again as admin - instance = self.mgr.servers_client.show_server( - instance['id'])['server'] - created_servers.append(instance) - self.make_instance_statistic(instance) - - return created_servers - def _get_flavors(self): return self.mgr.flavors_client.list_flavors()['flavors'] diff --git a/watcher_tempest_plugin/tests/scenario/test_execute_basic_optim.py b/watcher_tempest_plugin/tests/scenario/test_execute_basic_optim.py index 70f1d261..20b3cb81 100644 --- a/watcher_tempest_plugin/tests/scenario/test_execute_basic_optim.py +++ b/watcher_tempest_plugin/tests/scenario/test_execute_basic_optim.py @@ -56,81 +56,6 @@ class TestExecuteBasicStrategy(base.BaseInfraOptimScenarioTest): "Less than 2 compute nodes are enabled, " "skipping multinode tests.") - @classmethod - def get_compute_nodes_setup(cls): - services_client = cls.mgr.services_client - available_services = services_client.list_services()['services'] - - return [srv for srv in available_services - if srv.get('binary') == 'nova-compute'] - - @classmethod - def wait_for_compute_node_setup(cls): - - def _are_compute_nodes_setup(): - try: - hypervisors_client = cls.mgr.hypervisor_client - hypervisors = hypervisors_client.list_hypervisors( - detail=True)['hypervisors'] - available_hypervisors = set( - hyp['hypervisor_hostname'] for hyp in hypervisors) - available_services = set( - service['host'] - for service in cls.get_compute_nodes_setup()) - - return ( - available_hypervisors == available_services and - len(hypervisors) >= 2) - except Exception: - return False - - assert test_utils.call_until_true( - func=_are_compute_nodes_setup, - duration=600, - sleep_for=2 - ) - - @classmethod - def rollback_compute_nodes_status(cls): - current_compute_nodes_setup = cls.get_compute_nodes_setup() - for cn_setup in current_compute_nodes_setup: - cn_hostname = cn_setup.get('host') - matching_cns = [ - cns for cns in cls.initial_compute_nodes_setup - if cns.get('host') == cn_hostname - ] - initial_cn_setup = matching_cns[0] # Should return a single result - if cn_setup.get('status') != initial_cn_setup.get('status'): - if initial_cn_setup.get('status') == 'enabled': - rollback_func = cls.mgr.services_client.enable_service - else: - rollback_func = cls.mgr.services_client.disable_service - rollback_func(binary='nova-compute', host=cn_hostname) - - def _create_one_instance_per_host(self): - """Create 1 instance per compute node - - This goes up to the min_compute_nodes threshold so that things don't - get crazy if you have 1000 compute nodes but set min to 3. - """ - host_client = self.mgr.hosts_client - all_hosts = host_client.list_hosts()['hosts'] - compute_nodes = [x for x in all_hosts if x['service'] == 'compute'] - - for idx, _ in enumerate( - compute_nodes[:CONF.compute.min_compute_nodes], start=1): - # by getting to active state here, this means this has - # landed on the host in question. - instance = self.create_server( - name="instance-%d" % idx, - image_id=CONF.compute.image_ref, - wait_until='ACTIVE', - clients=self.os_primary) - # get instance object again as admin - instance = self.mgr.servers_client.show_server( - instance['id'])['server'] - self.make_instance_statistic(instance) - def test_execute_basic_action_plan(self): """Execute an action plan based on the BASIC strategy diff --git a/watcher_tempest_plugin/tests/scenario/test_execute_workload_balancing.py b/watcher_tempest_plugin/tests/scenario/test_execute_workload_balancing.py index 5f62355d..2a5232fa 100644 --- a/watcher_tempest_plugin/tests/scenario/test_execute_workload_balancing.py +++ b/watcher_tempest_plugin/tests/scenario/test_execute_workload_balancing.py @@ -58,109 +58,6 @@ class TestExecuteWorkloadBalancingStrategy(base.BaseInfraOptimScenarioTest): "Less than 2 compute nodes are enabled, " "skipping multinode tests.") - @classmethod - def get_hypervisors_setup(cls): - hypervisors_client = cls.mgr.hypervisor_client - hypervisors = hypervisors_client.list_hypervisors( - detail=True)['hypervisors'] - return hypervisors - - @classmethod - def get_compute_nodes_setup(cls): - services_client = cls.mgr.services_client - available_services = services_client.list_services()['services'] - - return [srv for srv in available_services - if srv.get('binary') == 'nova-compute'] - - def _migrate_server_to(self, server_id, dest_host, volume_backed=False): - kwargs = dict() - kwargs['disk_over_commit'] = False - block_migration = (CONF.compute_feature_enabled. - block_migration_for_live_migration and - not volume_backed) - body = self.mgr.servers_client.live_migrate_server( - server_id, host=dest_host, block_migration=block_migration, - **kwargs) - return body - - @classmethod - def wait_for_compute_node_setup(cls): - - def _are_compute_nodes_setup(): - try: - hypervisors = cls.get_hypervisors_setup() - available_hypervisors = set( - hyp['hypervisor_hostname'] for hyp in hypervisors - if hyp['state'] == 'up') - available_services = set( - service['host'] - for service in cls.get_compute_nodes_setup() - if service['state'] == 'up') - return ( - len(available_hypervisors) == len(available_services) and - len(hypervisors) >= 2) - except Exception as exc: - LOG.exception(exc) - return False - - assert test_utils.call_until_true( - func=_are_compute_nodes_setup, - duration=600, - sleep_for=2 - ) - - @classmethod - def rollback_compute_nodes_status(cls): - current_compute_nodes_setup = cls.get_compute_nodes_setup() - for cn_setup in current_compute_nodes_setup: - cn_hostname = cn_setup.get('host') - matching_cns = [ - cns for cns in cls.initial_compute_nodes_setup - if cns.get('host') == cn_hostname - ] - initial_cn_setup = matching_cns[0] # Should return a single result - if cn_setup.get('status') != initial_cn_setup.get('status'): - if initial_cn_setup.get('status') == 'enabled': - rollback_func = cls.mgr.services_client.enable_service - else: - rollback_func = cls.mgr.services_client.disable_service - rollback_func(binary='nova-compute', host=cn_hostname) - - def _create_one_instance_per_host(self): - """Create 1 instance per compute node - - This goes up to the min_compute_nodes threshold so that things don't - get crazy if you have 1000 compute nodes but set min to 3. - """ - host_client = self.mgr.hosts_client - all_hosts = host_client.list_hosts()['hosts'] - compute_nodes = [x for x in all_hosts if x['service'] == 'compute'] - - created_instances = [] - for _ in compute_nodes[:CONF.compute.min_compute_nodes]: - # by getting to active state here, this means this has - # landed on the host in question. - instance = self.create_server(image_id=CONF.compute.image_ref, - wait_until='ACTIVE', - clients=self.os_primary) - # get instance object again as admin - instance = self.mgr.servers_client.show_server( - instance['id'])['server'] - created_instances.append(instance) - self.make_instance_statistic(instance) - - return created_instances - - def _pack_all_created_instances_on_one_host(self, instances): - hypervisors = [ - hyp['hypervisor_hostname'] for hyp in self.get_hypervisors_setup() - if hyp['state'] == 'up'] - node = hypervisors[0] - for instance in instances: - if instance.get('OS-EXT-SRV-ATTR:hypervisor_hostname') != node: - self._migrate_server_to(instance['id'], node) - def test_execute_workload_stabilization(self): """Execute an action plan using the workload_stabilization strategy""" self.addCleanup(self.rollback_compute_nodes_status) diff --git a/watcher_tempest_plugin/tests/scenario/test_execute_zone_migration.py b/watcher_tempest_plugin/tests/scenario/test_execute_zone_migration.py index 9cebedc8..82d1cb57 100644 --- a/watcher_tempest_plugin/tests/scenario/test_execute_zone_migration.py +++ b/watcher_tempest_plugin/tests/scenario/test_execute_zone_migration.py @@ -54,108 +54,6 @@ class TestExecuteZoneMigrationStrategy(base.BaseInfraOptimScenarioTest): "Less than 2 compute nodes are enabled, " "skipping multinode tests.") - @classmethod - def get_hypervisors_setup(cls): - hypervisors_client = cls.mgr.hypervisor_client - hypervisors = hypervisors_client.list_hypervisors( - detail=True)['hypervisors'] - return hypervisors - - @classmethod - def get_compute_nodes_setup(cls): - services_client = cls.mgr.services_client - available_services = services_client.list_services()['services'] - - return [srv for srv in available_services - if srv.get('binary') == 'nova-compute'] - - def _migrate_server_to(self, server_id, dest_host, volume_backed=False): - kwargs = dict() - kwargs['disk_over_commit'] = False - block_migration = (CONF.compute_feature_enabled. - block_migration_for_live_migration and - not volume_backed) - body = self.mgr.servers_client.live_migrate_server( - server_id, host=dest_host, block_migration=block_migration, - **kwargs) - return body - - @classmethod - def wait_for_compute_node_setup(cls): - - def _are_compute_nodes_setup(): - try: - hypervisors = cls.get_hypervisors_setup() - available_hypervisors = set( - hyp['hypervisor_hostname'] for hyp in hypervisors - if hyp['state'] == 'up') - available_services = set( - service['host'] - for service in cls.get_compute_nodes_setup() - if service['state'] == 'up') - return ( - len(available_hypervisors) == len(available_services) and - len(hypervisors) >= 2) - except Exception as exc: - LOG.exception(exc) - return False - - assert test_utils.call_until_true( - func=_are_compute_nodes_setup, - duration=600, - sleep_for=2 - ) - - @classmethod - def rollback_compute_nodes_status(cls): - current_compute_nodes_setup = cls.get_compute_nodes_setup() - for cn_setup in current_compute_nodes_setup: - cn_hostname = cn_setup.get('host') - matching_cns = [ - cns for cns in cls.initial_compute_nodes_setup - if cns.get('host') == cn_hostname - ] - initial_cn_setup = matching_cns[0] # Should return a single result - if cn_setup.get('status') != initial_cn_setup.get('status'): - if initial_cn_setup.get('status') == 'enabled': - rollback_func = cls.mgr.services_client.enable_service - else: - rollback_func = cls.mgr.services_client.disable_service - rollback_func(binary='nova-compute', host=cn_hostname) - - def _create_one_instance_per_host(self): - """Create 1 instance per compute node - - This goes up to the min_compute_nodes threshold so that things don't - get crazy if you have 1000 compute nodes but set min to 3. - """ - host_client = self.mgr.hosts_client - all_hosts = host_client.list_hosts()['hosts'] - compute_nodes = [x for x in all_hosts if x['service'] == 'compute'] - - created_instances = [] - for _ in compute_nodes[:CONF.compute.min_compute_nodes]: - # by getting to active state here, this means this has - # landed on the host in question. - instance = self.create_server(image_id=CONF.compute.image_ref, - wait_until='ACTIVE', - clients=self.os_primary) - instance = self.mgr.servers_client.show_server( - instance['id'])['server'] - created_instances.append(instance) - - return created_instances - - def _pack_all_created_instances_on_one_host(self, instances): - hypervisors = [ - hyp['hypervisor_hostname'] for hyp in self.get_hypervisors_setup() - if hyp['state'] == 'up'] - node = hypervisors[0] - for instance in instances: - if instance.get('OS-EXT-SRV-ATTR:hypervisor_hostname') != node: - self._migrate_server_to(instance['id'], node) - return node - def test_execute_zone_migration_live_migration(self): """Execute an action plan using the zone migration strategy""" self.addCleanup(self.rollback_compute_nodes_status)