Refactor fixtures that check required files and settings

Refactor those fixtures in a more general way, so they can be used in
any test, not only netchecker one.
Just set class variables required_files and required_settings to use them
In that way all required files and settings will be in the test class, and not
in the fixture itself.

Change-Id: I27eb98e46978205f4a4e9769e368d4720e2f8b55
This commit is contained in:
Volodymyr Shypyguzov 2016-12-07 16:42:43 +02:00
parent b1fb1706d2
commit 493dc8b27a
2 changed files with 17 additions and 20 deletions

View File

@ -85,28 +85,21 @@ def k8scluster(revert_snapshot, request, config,
@pytest.fixture(scope='class') @pytest.fixture(scope='class')
def check_netchecker_files(request): def check_files_missing(request):
files_missing = [] LOG.info("Required files: {}".format(request.cls.required_files))
for arg in request.cls.netchecker_files: files_missing = [f for f in request.cls.required_files
if not os.path.isfile(arg): if not os.path.isfile(f)]
files_missing.append(arg)
assert len(files_missing) == 0, \ assert len(files_missing) == 0, \
("The following netchecker files not found: " "Following files are not found {0}".format(files_missing)
"{0}!".format(', '.join(files_missing)))
@pytest.fixture(scope='class') @pytest.fixture(scope='class')
def check_netchecker_images_settings(): def check_settings_missing(request):
settings_missing = [] LOG.info("Required settings: {}".format(request.cls.required_settings))
for setting in ('MCP_NETCHECKER_AGENT_IMAGE_REPO', settings_missing = [s for s in request.cls.required_settings
'MCP_NETCHECKER_AGENT_VERSION', if not getattr(settings, s, None)]
'MCP_NETCHECKER_SERVER_IMAGE_REPO',
'MCP_NETCHECKER_SERVER_VERSION'):
if not getattr(settings, setting, None):
settings_missing.append(setting)
assert len(settings_missing) == 0, \ assert len(settings_missing) == 0, \
("The following environment variables are not set: " "Following env variables are not set {}". format(settings_missing)
"{0}!".format(', '.join(settings_missing)))
@pytest.fixture(scope='class') @pytest.fixture(scope='class')

View File

@ -27,6 +27,11 @@ LOG = logger.logger
class TestFuelCCPNetCheckerMixin: class TestFuelCCPNetCheckerMixin:
required_settings = ('MCP_NETCHECKER_AGENT_IMAGE_REPO',
'MCP_NETCHECKER_AGENT_VERSION',
'MCP_NETCHECKER_SERVER_IMAGE_REPO',
'MCP_NETCHECKER_SERVER_VERSION')
kube_settings = settings.DEFAULT_CUSTOM_YAML kube_settings = settings.DEFAULT_CUSTOM_YAML
kube_settings['deploy_netchecker'] = False kube_settings['deploy_netchecker'] = False
@ -38,7 +43,7 @@ class TestFuelCCPNetCheckerMixin:
'k8s_resources/netchecker-server_svc.yaml') 'k8s_resources/netchecker-server_svc.yaml')
ds_yaml_file = os.path.join( ds_yaml_file = os.path.join(
settings.NETCHECKER_AGENT_DIR, 'netchecker-agent.yaml') settings.NETCHECKER_AGENT_DIR, 'netchecker-agent.yaml')
netchecker_files = (pod_yaml_file, svc_yaml_file, ds_yaml_file) required_files = (pod_yaml_file, svc_yaml_file, ds_yaml_file)
@property @property
def pod_spec(self): def pod_spec(self):
@ -62,8 +67,7 @@ class TestFuelCCPNetCheckerMixin:
return [i for i in yaml.load_all(ds_conf)] return [i for i in yaml.load_all(ds_conf)]
@pytest.mark.usefixtures("check_netchecker_files") @pytest.mark.usefixtures("check_files_missing", "check_settings_missing")
@pytest.mark.usefixtures("check_netchecker_images_settings")
class TestFuelCCPNetChecker(base_test.SystemBaseTest, class TestFuelCCPNetChecker(base_test.SystemBaseTest,
TestFuelCCPNetCheckerMixin): TestFuelCCPNetCheckerMixin):
"""Test class for network connectivity verification in k8s""" """Test class for network connectivity verification in k8s"""