From 6f514d34870261ca4775ee2d843301965d260303 Mon Sep 17 00:00:00 2001 From: Artem Roma Date: Wed, 25 Jun 2014 14:52:18 +0300 Subject: [PATCH] OS access credentials processing added What is done: - os access credentials retriving from POST request body on wsgi controller for TestRun entity added; - needed environment variables setting added in nose StoragePlugin; - settuping process for HealthCheck updated; - integration tests for ostf adapter updated; - unit tests also updated; - change was tested on deployed env. Change-Id: I96580e4e59db0314a75c27f2dda49cf268b05b95 Closes-Bug: #1281838 --- fuel_health/config.py | 20 ++++++-- .../ostf_adapter/nose_plugin/nose_adapter.py | 16 ++++-- .../nose_plugin/nose_storage_plugin.py | 8 ++- fuel_plugin/ostf_adapter/storage/models.py | 8 +-- fuel_plugin/ostf_adapter/wsgi/controllers.py | 3 ++ fuel_plugin/ostf_client/client.py | 13 +++++ .../dummy_tests/test_environment_variables.py | 35 +++++++++++++ fuel_plugin/testing/tests/functional/tests.py | 50 ++++++++++++++++++- fuel_plugin/testing/tests/unit/base.py | 7 ++- .../testing/tests/unit/test_nose_discovery.py | 4 +- .../tests/unit/test_wsgi_controllers.py | 13 +++-- 11 files changed, 157 insertions(+), 20 deletions(-) create mode 100644 fuel_plugin/testing/fixture/dummy_tests/test_environment_variables.py diff --git a/fuel_health/config.py b/fuel_health/config.py index 02ba676f..0ee31295 100644 --- a/fuel_health/config.py +++ b/fuel_health/config.py @@ -523,9 +523,23 @@ class NailgunConfig(object): data = response.json() LOG.info('RESPONSE FROM %s - %s' % (api_url, data)) access_data = data['editable']['access'] - self.identity.admin_tenant_name = access_data['tenant']['value'] - self.identity.admin_username = access_data['user']['value'] - self.identity.admin_password = access_data['password']['value'] + + self.identity.admin_tenant_name = \ + ( + os.environ.get('OSTF_OS_TENANT_NAME') or + access_data['tenant']['value'] + ) + self.identity.admin_username = \ + ( + os.environ.get('OSTF_OS_USERNAME') or + access_data['user']['value'] + ) + self.identity.admin_password = \ + ( + os.environ.get('OSTF_OS_PASSWORD') or + access_data['password']['value'] + ) + api_url = '/api/clusters/%s' % self.cluster_id cluster_data = self.req_session.get(self.nailgun_url + api_url).json() network_provider = cluster_data.get('net_provider', 'nova_network') diff --git a/fuel_plugin/ostf_adapter/nose_plugin/nose_adapter.py b/fuel_plugin/ostf_adapter/nose_plugin/nose_adapter.py index 6c95f6ec..1ff7feef 100644 --- a/fuel_plugin/ostf_adapter/nose_plugin/nose_adapter.py +++ b/fuel_plugin/ostf_adapter/nose_plugin/nose_adapter.py @@ -40,7 +40,12 @@ class NoseDriver(object): def __init__(self): LOG.warning('Initializing Nose Driver') - def run(self, test_run, test_set, dbpath, tests=None): + def run(self, test_run, test_set, dbpath, ostf_os_access_creds=None, + tests=None): + + if not ostf_os_access_creds: + ostf_os_access_creds = dict() + tests = tests or test_run.enabled_tests if tests: argv_add = [nose_utils.modify_test_name_for_nose(test) @@ -54,10 +59,11 @@ class NoseDriver(object): dbpath, test_run.id, test_run.cluster_id, + ostf_os_access_creds, argv_add).pid - def _run_tests(self, lock_path, dbpath, - test_run_id, cluster_id, argv_add): + def _run_tests(self, lock_path, dbpath, test_run_id, cluster_id, + ostf_os_access_creds, argv_add): cleanup_flag = False def raise_exception_handler(signum, stack_frame): @@ -83,7 +89,9 @@ class NoseDriver(object): nose_test_runner.SilentTestProgram( addplugins=[nose_storage_plugin.StoragePlugin( - session, test_run_id, str(cluster_id))], + session, test_run_id, str(cluster_id), + ostf_os_access_creds + )], exit=False, argv=['ostf_tests'] + argv_add) diff --git a/fuel_plugin/ostf_adapter/nose_plugin/nose_storage_plugin.py b/fuel_plugin/ostf_adapter/nose_plugin/nose_storage_plugin.py index 6449528d..1b3e2b40 100644 --- a/fuel_plugin/ostf_adapter/nose_plugin/nose_storage_plugin.py +++ b/fuel_plugin/ostf_adapter/nose_plugin/nose_storage_plugin.py @@ -33,10 +33,13 @@ class StoragePlugin(plugins.Plugin): name = 'storage' score = 15000 - def __init__(self, session, test_run_id, cluster_id): + def __init__(self, session, test_run_id, cluster_id, + ostf_os_access_creds): self.session = session self.test_run_id = test_run_id self.cluster_id = cluster_id + self.ostf_os_access_creds = ostf_os_access_creds + super(StoragePlugin, self).__init__() self._start_time = None @@ -46,6 +49,9 @@ class StoragePlugin(plugins.Plugin): if self.cluster_id: env['CLUSTER_ID'] = str(self.cluster_id) + for var_name in self.ostf_os_access_creds: + env[var_name.upper()] = self.ostf_os_access_creds[var_name] + def configure(self, options, conf): self.conf = conf diff --git a/fuel_plugin/ostf_adapter/storage/models.py b/fuel_plugin/ostf_adapter/storage/models.py index c7fd8e69..530fe924 100644 --- a/fuel_plugin/ostf_adapter/storage/models.py +++ b/fuel_plugin/ostf_adapter/storage/models.py @@ -367,12 +367,13 @@ class TestRun(BASE): # flush test_run data to db session.commit() - plugin.run(test_run, test_set, dbpath) + plugin.run(test_run, test_set, dbpath, + metadata.get('ostf_os_access_creds')) return test_run.frontend return {} - def restart(self, session, dbpath, tests=None): + def restart(self, session, dbpath, ostf_os_access_creds, tests=None): """Restart test run with if tests given they will be enabled """ @@ -386,7 +387,8 @@ class TestRun(BASE): Test.update_test_run_tests( session, self.id, tests) - plugin.run(self, self.test_set, dbpath, tests) + plugin.run(self, self.test_set, dbpath, + ostf_os_access_creds, tests) return self.frontend return {} diff --git a/fuel_plugin/ostf_adapter/wsgi/controllers.py b/fuel_plugin/ostf_adapter/wsgi/controllers.py index 44d6e960..3dbc602e 100644 --- a/fuel_plugin/ostf_adapter/wsgi/controllers.py +++ b/fuel_plugin/ostf_adapter/wsgi/controllers.py @@ -160,6 +160,8 @@ class TestrunsController(BaseRestController): for test_run in test_runs: status = test_run.get('status') tests = test_run.get('tests', []) + ostf_os_access_creds = test_run.get('ostf_os_access_creds') + test_run = models.TestRun.get_test_run(request.session, test_run['id']) if status == 'stopped': @@ -167,5 +169,6 @@ class TestrunsController(BaseRestController): elif status == 'restarted': data.append(test_run.restart(request.session, cfg.CONF.adapter.dbpath, + ostf_os_access_creds, tests=tests)) return data diff --git a/fuel_plugin/ostf_client/client.py b/fuel_plugin/ostf_client/client.py index c0425982..aca2d2da 100644 --- a/fuel_plugin/ostf_client/client.py +++ b/fuel_plugin/ostf_client/client.py @@ -24,7 +24,20 @@ class TestingAdapterClient(object): def _request(self, method, url, data=None): headers = {'content-type': 'application/json'} + ostf_os_access_creds = { + 'ostf_os_username': 'ostf', + 'ostf_os_password': 'ostf', + 'ostf_os_tenant_name': 'ostf' + } + if data: + for data_el in data: + if 'metadata' in data_el: + data_el['metadata']['ostf_os_access_creds'] = \ + ostf_os_access_creds + else: + data_el['ostf_os_access_creds'] = ostf_os_access_creds + data = dumps({'objects': data}) r = requests.request( diff --git a/fuel_plugin/testing/fixture/dummy_tests/test_environment_variables.py b/fuel_plugin/testing/fixture/dummy_tests/test_environment_variables.py new file mode 100644 index 00000000..d301dde5 --- /dev/null +++ b/fuel_plugin/testing/fixture/dummy_tests/test_environment_variables.py @@ -0,0 +1,35 @@ +# Copyright 2013 Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +__profile__ = { + "id": "environment_variables", + "driver": "nose", + "test_path": ("fuel_plugin/tests/functional/dummy_tests/" + "test_environment_variables.py"), + "description": ("Test for presence of env variables inside of" + " testrun subprocess"), + "deployment_tags": [], + "test_runs_ordering_priority": 12, + "exclusive_testsets": [] +} + +import os +import unittest2 + + +class TestEnvVariables(unittest2.TestCase): + def test_os_credentials_env_variables(self): + self.assertTrue(os.environ.get('OSTF_OS_USERNAME')) + self.assertTrue(os.environ.get('OSTF_OS_PASSWORD')) + self.assertTrue(os.environ.get('OSTF_OS_TENANT_NAME')) diff --git a/fuel_plugin/testing/tests/functional/tests.py b/fuel_plugin/testing/tests/functional/tests.py index fbde016d..42d539e0 100644 --- a/fuel_plugin/testing/tests/functional/tests.py +++ b/fuel_plugin/testing/tests/functional/tests.py @@ -52,7 +52,10 @@ class AdapterTests(BaseAdapterTest): ('fuel_plugin.testing.fixture.dummy_tests.deployment_types_tests.' 'ha_deployment_test.HATest.test_ha_depl'): 'ha_depl', ('fuel_plugin.testing.fixture.dummy_tests.deployment_types_tests.' - 'ha_deployment_test.HATest.test_ha_rhel_depl'): 'ha_rhel_depl' + 'ha_deployment_test.HATest.test_ha_rhel_depl'): 'ha_rhel_depl', + ('fuel_plugin.testing.fixture.dummy_tests.' + 'test_environment_variables.TestEnvVariables.' + 'test_os_credentials_env_variables'): 'test_env_vars' } cls.testsets = { "ha_deployment_test": [], @@ -623,3 +626,48 @@ class AdapterTests(BaseAdapterTest): ]) self.compare(resp, assertions) + + def test_env_variables_are_set(self): + assertions = Response([ + { + 'testset': 'environment_variables', + 'status': 'finished', + 'tests': [ + { + 'id': ( + 'fuel_plugin.testing.fixture.' + 'dummy_tests.test_environment_variables.' + 'TestEnvVariables.' + 'test_os_credentials_env_variables' + ), + 'status': 'success' + }, + ] + }, + ]) + + def check_testrun_res(): + resp = self.client.testruns() + self.compare(resp, assertions) + + cluster_id = 1 + testset = 'environment_variables' + tests = [ + ('fuel_plugin.testing.fixture.' + 'dummy_tests.test_environment_variables.' + 'TestEnvVariables.' + 'test_os_credentials_env_variables') + ] + + # make sure we have all needed data in db + self.adapter.testsets(cluster_id) + + self.adapter.start_testrun(testset, cluster_id) + time.sleep(5) + + check_testrun_res() + + self.client.restart_tests_last(testset, tests, cluster_id) + time.sleep(5) + + check_testrun_res() diff --git a/fuel_plugin/testing/tests/unit/base.py b/fuel_plugin/testing/tests/unit/base.py index 4eae2e74..494fe24c 100644 --- a/fuel_plugin/testing/tests/unit/base.py +++ b/fuel_plugin/testing/tests/unit/base.py @@ -41,7 +41,8 @@ class BaseWSGITest(unittest2.TestCase): 'deployment_tags': set(['ha', 'rhel', 'nova_network']) }, 'test_sets': ['general_test', - 'stopped_test', 'ha_deployment_test'], + 'stopped_test', 'ha_deployment_test', + 'environment_variables'], 'tests': [cls.ext_id + test for test in [ ('deployment_types_tests.ha_deployment_test.' 'HATest.test_ha_depl'), @@ -56,7 +57,9 @@ class BaseWSGITest(unittest2.TestCase): 'general_test.Dummy_test.test_skip_directly', 'stopped_test.dummy_tests_stopped.test_really_long', 'stopped_test.dummy_tests_stopped.test_one_no_so_long', - 'stopped_test.dummy_tests_stopped.test_not_long_at_all' + 'stopped_test.dummy_tests_stopped.test_not_long_at_all', + ('test_environment_variables.TestEnvVariables.' + 'test_os_credentials_env_variables') ]] } diff --git a/fuel_plugin/testing/tests/unit/test_nose_discovery.py b/fuel_plugin/testing/tests/unit/test_nose_discovery.py index 5560af54..66b3ba98 100644 --- a/fuel_plugin/testing/tests/unit/test_nose_discovery.py +++ b/fuel_plugin/testing/tests/unit/test_nose_discovery.py @@ -55,8 +55,8 @@ class TestNoseDiscovery(unittest2.TestCase): def test_discovery(self): expected = { - 'test_sets_count': 8, - 'tests_count': 26 + 'test_sets_count': 9, + 'tests_count': 27 } self.assertTrue( diff --git a/fuel_plugin/testing/tests/unit/test_wsgi_controllers.py b/fuel_plugin/testing/tests/unit/test_wsgi_controllers.py index 9a8c567d..416b4099 100644 --- a/fuel_plugin/testing/tests/unit/test_wsgi_controllers.py +++ b/fuel_plugin/testing/tests/unit/test_wsgi_controllers.py @@ -49,7 +49,8 @@ class TestTestSetsController(base.BaseWSGITest): self.expected['test_set_description'] = [ 'General fake tests', 'Long running 25 secs fake tests', - 'Fake tests for HA deployment' + 'Fake tests for HA deployment', + 'Test for presence of env variables inside of testrun subprocess' ] res = self.controller.get(self.expected['cluster']['id']) @@ -67,7 +68,8 @@ class TestTestSetsController(base.BaseWSGITest): test_set_order = { 'general_test': 0, 'stopped_test': 1, - 'ha_deployment_test': 2 + 'ha_deployment_test': 2, + 'environment_variables': 3 } resp_elements = [testset['id'] for testset in res] @@ -241,7 +243,8 @@ class TestClusterRedeployment(base.BaseWSGITest): 'deployment_tags': set(['multinode', 'ubuntu', 'nova_network']) }, 'test_sets': ['general_test', - 'stopped_test', 'multinode_deployment_test'], + 'stopped_test', 'multinode_deployment_test', + 'environment_variables'], 'tests': [self.ext_id + test for test in [ ('deployment_types_tests.multinode_deployment_test.' 'MultinodeTest.test_multi_novanet_depl'), @@ -256,7 +259,9 @@ class TestClusterRedeployment(base.BaseWSGITest): 'general_test.Dummy_test.test_skip_directly', 'stopped_test.dummy_tests_stopped.test_really_long', 'stopped_test.dummy_tests_stopped.test_one_no_so_long', - 'stopped_test.dummy_tests_stopped.test_not_long_at_all' + 'stopped_test.dummy_tests_stopped.test_not_long_at_all', + ('test_environment_variables.TestEnvVariables.' + 'test_os_credentials_env_variables') ]] }