From 8ce27ce11a5916d9b725ff74be304805c0c3ebe7 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Tue, 17 Jan 2017 14:55:02 +0800 Subject: implement get/query scenario(s) and add unittest supported queries: scenario name/version/installer/project JIRA: RELENG-163 Change-Id: I3f6ea6e3c81edff30569918e0789e8c1e693b9fc Signed-off-by: SerenaFeng --- .../opnfv_testapi/tests/unit/fake_pymongo.py | 68 +++++++++++++++-- .../opnfv_testapi/tests/unit/scenario-c1.json | 38 ++++++++++ .../opnfv_testapi/tests/unit/scenario-c2.json | 73 ++++++++++++++++++ .../opnfv_testapi/tests/unit/scenario-create.json | 38 ---------- .../testapi/opnfv_testapi/tests/unit/test_base.py | 8 +- .../opnfv_testapi/tests/unit/test_scenario.py | 86 +++++++++++++++++++++- 6 files changed, 257 insertions(+), 54 deletions(-) create mode 100644 utils/test/testapi/opnfv_testapi/tests/unit/scenario-c1.json create mode 100644 utils/test/testapi/opnfv_testapi/tests/unit/scenario-c2.json delete mode 100644 utils/test/testapi/opnfv_testapi/tests/unit/scenario-create.json (limited to 'utils/test/testapi/opnfv_testapi/tests') diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py b/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py index d86d8eadf..3c4fd01a3 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py @@ -55,7 +55,8 @@ class MemCursor(object): class MemDb(object): - def __init__(self): + def __init__(self, name): + self.name = name self.contents = [] pass @@ -109,8 +110,59 @@ class MemDb(object): return True return False - @staticmethod - def _in(content, *args): + def _in(self, content, *args): + if self.name == 'scenarios': + return self._in_scenarios(content, *args) + else: + return self._in_others(content, *args) + + def _in_scenarios_installer(self, installer, content): + hit = False + for s_installer in content['installers']: + if installer == s_installer['installer']: + hit = True + + return hit + + def _in_scenarios_version(self, version, content): + hit = False + for s_installer in content['installers']: + for s_version in s_installer['versions']: + if version == s_version['version']: + hit = True + return hit + + def _in_scenarios_project(self, project, content): + hit = False + for s_installer in content['installers']: + for s_version in s_installer['versions']: + for s_project in s_version['projects']: + if project == s_project['project']: + hit = True + + return hit + + def _in_scenarios(self, content, *args): + for arg in args: + for k, v in arg.iteritems(): + if k == 'installers': + for inner in v.values(): + for i_k, i_v in inner.iteritems(): + if i_k == 'installer': + return self._in_scenarios_installer(i_v, + content) + elif i_k == 'versions.version': + return self._in_scenarios_version(i_v, + content) + elif i_k == 'versions.projects.project': + return self._in_scenarios_project(i_v, + content) + elif content.get(k, None) != v: + return False + + return True + + def _in_others(self, content, *args): for arg in args: for k, v in arg.iteritems(): if k == 'start_date': @@ -185,8 +237,8 @@ def __getattr__(name): return globals()[name] -pods = MemDb() -projects = MemDb() -testcases = MemDb() -results = MemDb() -scenarios = MemDb() +pods = MemDb('pods') +projects = MemDb('projects') +testcases = MemDb('testcases') +results = MemDb('results') +scenarios = MemDb('scenarios') diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/scenario-c1.json b/utils/test/testapi/opnfv_testapi/tests/unit/scenario-c1.json new file mode 100644 index 000000000..187802215 --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/tests/unit/scenario-c1.json @@ -0,0 +1,38 @@ +{ + "name": "nosdn-nofeature-ha", + "installers": + [ + { + "installer": "apex", + "versions": + [ + { + "owner": "Luke", + "version": "master", + "projects": + [ + { + "project": "functest", + "customs": [ "healthcheck", "vping_ssh"], + "scores": + [ + { + "date": "2017-01-08 22:46:44", + "score": "12/14" + } + + ], + "trust_indicators": [] + }, + { + "project": "yardstick", + "customs": [], + "scores": [], + "trust_indicators": [] + } + ] + } + ] + } + ] +} diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/scenario-c2.json b/utils/test/testapi/opnfv_testapi/tests/unit/scenario-c2.json new file mode 100644 index 000000000..b6a3b83ab --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/tests/unit/scenario-c2.json @@ -0,0 +1,73 @@ +{ + "name": "odl_2-nofeature-ha", + "installers": + [ + { + "installer": "fuel", + "versions": + [ + { + "owner": "Lucky", + "version": "colorado", + "projects": + [ + { + "project": "functest", + "customs": [ "healthcheck", "vping_ssh"], + "scores": [], + "trust_indicators": [ + { + "date": "2017-01-18 22:46:44", + "status": "silver" + } + + ] + }, + { + "project": "yardstick", + "customs": ["suite-a"], + "scores": [ + { + "date": "2017-01-08 22:46:44", + "score": "0" + } + ], + "trust_indicators": [ + { + "date": "2017-01-18 22:46:44", + "status": "gold" + } + ] + } + ] + }, + { + "owner": "Luke", + "version": "colorado", + "projects": + [ + { + "project": "functest", + "customs": [ "healthcheck", "vping_ssh"], + "scores": + [ + { + "date": "2017-01-09 22:46:44", + "score": "11/14" + } + + ], + "trust_indicators": [] + }, + { + "project": "yardstick", + "customs": [], + "scores": [], + "trust_indicators": [] + } + ] + } + ] + } + ] +} diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/scenario-create.json b/utils/test/testapi/opnfv_testapi/tests/unit/scenario-create.json deleted file mode 100644 index eba8b6c0a..000000000 --- a/utils/test/testapi/opnfv_testapi/tests/unit/scenario-create.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "name": "nosdn-nofeature-ha", - "installers": - [ - { - "installer": "apex", - "versions": - [ - { - "owner": "Luke", - "version": "master", - "projects": - [ - { - "project": "functest", - "customs": [ "healthcheck", "vping_ssh"], - "scores": - [ - { - "date": "2017-01-08 22:46:44", - "score": "12/14" - } - - ], - "trust_indicators": [] - }, - { - "project": "yardstick", - "customs": [], - "scores": [], - "trust_indicators": [] - } - ] - } - ] - } - ] -} \ No newline at end of file diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py index 9343ab2fb..fc780e44c 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py @@ -47,11 +47,11 @@ class TestBase(AsyncHTTPTestCase): return self.create_help(self.basePath, req, *args) def create_help(self, uri, req, *args): - if req and not isinstance(req, str): - req = json.dumps(req.format()) + if req and not isinstance(req, str) and hasattr(req, 'format'): + req = req.format() res = self.fetch(self._update_uri(uri, *args), method='POST', - body=req if req else json.dumps(None), + body=json.dumps(req), headers=self.headers) return self._get_return(res, self.create_res) @@ -97,7 +97,7 @@ class TestBase(AsyncHTTPTestCase): return uri.count('%s') def _get_query_uri(self, query): - return self.basePath + '?' + query + return self.basePath + '?' + query if query else self.basePath def _get_uri(self, *args): return self._update_uri(self.basePath, *args) diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py index 8e827813c..ff5979524 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py @@ -4,15 +4,20 @@ import os from opnfv_testapi.common.constants import HTTP_BAD_REQUEST from opnfv_testapi.common.constants import HTTP_FORBIDDEN from opnfv_testapi.common.constants import HTTP_OK +from opnfv_testapi.resources.scenario_models import Scenario from opnfv_testapi.resources.scenario_models import ScenarioCreateRequest +from opnfv_testapi.resources.scenario_models import Scenarios from test_testcase import TestBase class TestScenarioBase(TestBase): def setUp(self): super(TestScenarioBase, self).setUp() + self.get_res = Scenario + self.list_res = Scenarios self.basePath = '/api/v1/scenarios' - self.load_request('scenario-create.json') + self.req_d = self._load_request('scenario-c1.json') + self.req_2 = self._load_request('scenario-c2.json') def tearDown(self): pass @@ -20,10 +25,26 @@ class TestScenarioBase(TestBase): def assert_body(self, project, req=None): pass - def load_request(self, f_req): - with open(os.path.join(os.path.dirname(__file__), f_req), 'r') as f: - self.req_d = json.dumps(json.load(f)) + @staticmethod + def _load_request(f_req): + abs_file = os.path.join(os.path.dirname(__file__), f_req) + with open(abs_file, 'r') as f: + loader = json.load(f) f.close() + return loader + + def create_return_name(self, req): + _, res = self.create(req) + return res.href.split('/')[-1] + + def assert_res(self, code, scenario, req=None): + self.assertEqual(code, HTTP_OK) + if req is None: + req = self.req_d + scenario_dict = scenario.format_http() + self.assertIsNotNone(scenario_dict['_id']) + self.assertIsNotNone(scenario_dict['creation_date']) + self.assertDictContainsSubset(req, scenario_dict) class TestScenarioCreate(TestScenarioBase): @@ -53,3 +74,60 @@ class TestScenarioCreate(TestScenarioBase): (code, body) = self.create_d() self.assertEqual(code, HTTP_FORBIDDEN) self.assertIn('already exists', body) + + +class TestScenarioGet(TestScenarioBase): + def setUp(self): + super(TestScenarioGet, self).setUp() + self.scenario_1 = self.create_return_name(self.req_d) + self.scenario_2 = self.create_return_name(self.req_2) + + def test_getByName(self): + code, body = self.get(self.scenario_1) + self.assert_res(code, body, req=self.req_d) + + def test_getAll(self): + self._query_and_assert(query=None, reqs=[self.req_d, self.req_2]) + + def test_queryName(self): + query = self._set_query('name=nosdn-nofeature-ha') + self._query_and_assert(query, reqs=[self.req_d]) + + def test_queryInstaller(self): + query = self._set_query('installer=apex') + self._query_and_assert(query, reqs=[self.req_d]) + + def test_queryVersion(self): + query = self._set_query('version=master') + self._query_and_assert(query, reqs=[self.req_d]) + + def test_queryProject(self): + query = self._set_query('project=functest') + self._query_and_assert(query, reqs=[self.req_d, self.req_2]) + + def test_queryCombination(self): + query = self._set_query('name=nosdn-nofeature-ha', + 'installer=apex', + 'version=master', + 'project=functest') + + self._query_and_assert(query, reqs=[self.req_d]) + + @staticmethod + def _set_query(*args): + uri = '' + for arg in args: + uri += arg + '&' + return uri[0: -1] + + def _query_and_assert(self, query, found=True, reqs=None): + code, body = self.query(query) + if not found: + self.assertEqual(code, HTTP_OK) + self.assertEqual(0, len(body.scenarios)) + else: + self.assertEqual(len(reqs), len(body.scenarios)) + for req in reqs: + for scenario in body.scenarios: + if req['name'] == scenario.name: + self.assert_res(code, scenario, req) -- cgit 1.2.3-korg