From 2cac42dd097ffc7188ec527af944cd41ae39858c Mon Sep 17 00:00:00 2001 From: Dennis Dmitriev Date: Fri, 16 Sep 2016 15:03:01 +0300 Subject: [PATCH] Revert snapshots in each fixture as a workaround If @pytest.mark.revert_snapshot(name=) is used for test case, but the snapshot *or* it's config is missing in the environment, then each fixture will try to revert the snapshot that belongs to the fixture. Change-Id: I819dba2e775f5be38ea8cd12c6909b2e7399e3a8 --- fuel_ccp_tests/fixtures/ccp_fixtures.py | 10 +++++++--- fuel_ccp_tests/fixtures/k8s_fixtures.py | 11 +++++++---- fuel_ccp_tests/fixtures/underlay_fixtures.py | 15 ++++++++++----- fuel_ccp_tests/managers/envmanager_devops.py | 20 ++++++++++++++++++-- fuel_ccp_tests/managers/envmanager_empty.py | 10 +++------- fuel_ccp_tests/settings_oslo.py | 6 +----- 6 files changed, 46 insertions(+), 26 deletions(-) diff --git a/fuel_ccp_tests/fixtures/ccp_fixtures.py b/fuel_ccp_tests/fixtures/ccp_fixtures.py index 7b1c9c0..bf75023 100644 --- a/fuel_ccp_tests/fixtures/ccp_fixtures.py +++ b/fuel_ccp_tests/fixtures/ccp_fixtures.py @@ -59,9 +59,13 @@ def ccpcluster(revert_snapshot, config, hardware, ccp_actions.default_params = settings.CCP_CLI_PARAMS - # Try to guess environment config for reverted snapshot - if revert_snapshot and config.ccp.os_host == '0.0.0.0': - config.ccp.os_host = config.k8s.kube_host + # If no snapshot was reverted, then try to revert the snapshot + # that belongs to the fixture. + # Note: keep fixtures in strict dependences from each other! + if not revert_snapshot: + if hardware.has_snapshot(ext.SNAPSHOT.ccp_deployed) and \ + hardware.has_snapshot_config(ext.SNAPSHOT.ccp_deployed): + hardware.revert_snapshot(ext.SNAPSHOT.ccp_deployed) # Install CCP if config.ccp.os_host == '0.0.0.0': diff --git a/fuel_ccp_tests/fixtures/k8s_fixtures.py b/fuel_ccp_tests/fixtures/k8s_fixtures.py index 897a762..7736371 100644 --- a/fuel_ccp_tests/fixtures/k8s_fixtures.py +++ b/fuel_ccp_tests/fixtures/k8s_fixtures.py @@ -60,10 +60,13 @@ def k8scluster(revert_snapshot, request, config, If you want to revert 'k8s_deployed' snapshot, please use mark: @pytest.mark.revert_snapshot("k8s_deployed") """ - # Try to guess environment config for reverted snapshot - if revert_snapshot and config.k8s.kube_host == '0.0.0.0': - config.k8s.kube_host = underlay.host_by_node_name( - underlay.node_names()[0]) + # If no snapshot was reverted, then try to revert the snapshot + # that belongs to the fixture. + # Note: keep fixtures in strict dependences from each other! + if not revert_snapshot: + if hardware.has_snapshot(ext.SNAPSHOT.k8s_deployed) and \ + hardware.has_snapshot_config(ext.SNAPSHOT.k8s_deployed): + hardware.revert_snapshot(ext.SNAPSHOT.k8s_deployed) # Create k8s cluster if config.k8s.kube_host == '0.0.0.0': diff --git a/fuel_ccp_tests/fixtures/underlay_fixtures.py b/fuel_ccp_tests/fixtures/underlay_fixtures.py index 20660c7..46077df 100644 --- a/fuel_ccp_tests/fixtures/underlay_fixtures.py +++ b/fuel_ccp_tests/fixtures/underlay_fixtures.py @@ -105,7 +105,9 @@ def revert_snapshot(request, hardware): revert_snapshot = request.keywords.get('revert_snapshot', None) snapshot_name = extract_name_from_mark(revert_snapshot) - if snapshot_name and hardware.has_snapshot(snapshot_name): + if snapshot_name and \ + hardware.has_snapshot(snapshot_name) and \ + hardware.has_snapshot_config(snapshot_name): hardware.revert_snapshot(snapshot_name) return snapshot_name @@ -163,10 +165,13 @@ def underlay(request, revert_snapshot, config, hardware): - provide SSH access to underlay nodes using node names or node IPs. """ - # Try to guess environment config for reverted snapshot - if revert_snapshot and not config.underlay.ssh: - config.underlay.ssh = hardware.get_ssh_data( - roles=config.underlay.roles) + # If no snapshot was reverted, then try to revert the snapshot + # that belongs to the fixture. + # Note: keep fixtures in strict dependences from each other! + if not revert_snapshot: + if hardware.has_snapshot(ext.SNAPSHOT.underlay) and \ + hardware.has_snapshot_config(ext.SNAPSHOT.underlay): + hardware.revert_snapshot(ext.SNAPSHOT.underlay) # Create Underlay if not config.underlay.ssh: diff --git a/fuel_ccp_tests/managers/envmanager_devops.py b/fuel_ccp_tests/managers/envmanager_devops.py index 28174fe..26adefc 100644 --- a/fuel_ccp_tests/managers/envmanager_devops.py +++ b/fuel_ccp_tests/managers/envmanager_devops.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import os + from devops import error from devops.helpers import helpers from devops import models @@ -177,6 +179,15 @@ class EnvironmentManager(object): raise exceptions.EnvironmentIsNotSet() settings_oslo.save_config(self.__config, name, self._env.name) + def _get_snapshot_config_name(self, snapshot_name): + """Get config name for the environment""" + env_name = self._env.name + if env_name is None: + env_name = 'config' + test_config_path = os.path.join( + settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name)) + return test_config_path + def revert_snapshot(self, name): """Revert snapshot by name @@ -196,8 +207,9 @@ class EnvironmentManager(object): raise exceptions.EnvironmentIsNotSet() try: - settings_oslo.reload_snapshot_config(self.__config, name, - self._env.name) + test_config_path = self._get_snapshot_config_name(name) + settings_oslo.reload_snapshot_config(self.__config, + test_config_path) except cfg.ConfigFilesNotFoundError as conf_err: LOG.error("Config file(s) {0} not found!".format( conf_err.config_files)) @@ -274,6 +286,10 @@ class EnvironmentManager(object): def has_snapshot(self, name): return self._env.has_snapshot(name) + def has_snapshot_config(self, name): + test_config_path = self._get_snapshot_config_name(name) + return os.path.isfile(test_config_path) + def delete_environment(self): """Delete environment diff --git a/fuel_ccp_tests/managers/envmanager_empty.py b/fuel_ccp_tests/managers/envmanager_empty.py index 232d604..203fb1d 100644 --- a/fuel_ccp_tests/managers/envmanager_empty.py +++ b/fuel_ccp_tests/managers/envmanager_empty.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -from oslo_config import cfg - from fuel_ccp_tests import settings_oslo @@ -80,11 +78,6 @@ class EnvironmentManagerEmpty(object): raise Exception( "EnvironmentManagerEmpty cannot revert nodes from {} to {}" .format(self.__config.hardware.current_snapshot, name)) - try: - settings_oslo.reload_snapshot_config(self.__config, name) - except cfg.ConfigFilesNotFoundError: - pass - self.__config.hardware.current_snapshot = name def start(self): """Start environment""" @@ -105,6 +98,9 @@ class EnvironmentManagerEmpty(object): def has_snapshot(self, name): return self.__config.hardware.current_snapshot == name + def has_snapshot_config(self, name): + return self.__config.hardware.current_snapshot == name + def delete_environment(self): """Delete environment""" pass diff --git a/fuel_ccp_tests/settings_oslo.py b/fuel_ccp_tests/settings_oslo.py index c9cf139..105f4a4 100644 --- a/fuel_ccp_tests/settings_oslo.py +++ b/fuel_ccp_tests/settings_oslo.py @@ -155,12 +155,8 @@ def load_config(config_files): return config -def reload_snapshot_config(config, snapshot_name, env_name=None): +def reload_snapshot_config(config, test_config_path): """Reset config to the state from test_config file""" - if env_name is None: - env_name = 'config' - test_config_path = os.path.join( - settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name)) config(args=[], default_config_files=[test_config_path]) return config