diff options
Diffstat (limited to 'utils/test/testapi')
11 files changed, 290 insertions, 8 deletions
diff --git a/utils/test/testapi/docker/Dockerfile b/utils/test/testapi/docker/Dockerfile index b0272e609..86513e05b 100644 --- a/utils/test/testapi/docker/Dockerfile +++ b/utils/test/testapi/docker/Dockerfile @@ -48,5 +48,5 @@ RUN git clone https://gerrit.opnfv.org/gerrit/releng /home/releng WORKDIR /home/releng/utils/test/testapi/ RUN pip install -r requirements.txt -RUN python setup.py install +RUN bash install.sh CMD ["bash", "docker/start-server.sh"] diff --git a/utils/test/testapi/htmlize/finish.sh b/utils/test/testapi/htmlize/finish.sh index dc3aa868b..d24ae056c 100644 --- a/utils/test/testapi/htmlize/finish.sh +++ b/utils/test/testapi/htmlize/finish.sh @@ -13,3 +13,5 @@ if [ $proc_number -gt 0 ]; then echo "Kill opnfv-testapi server $procs" ps -ef | grep opnfv-testapi | grep -v grep | awk '{print $2}' | xargs kill -kill &>/dev/null fi + +deactivate diff --git a/utils/test/testapi/htmlize/prepare.sh b/utils/test/testapi/htmlize/prepare.sh index 3c265aaa0..67158f211 100644 --- a/utils/test/testapi/htmlize/prepare.sh +++ b/utils/test/testapi/htmlize/prepare.sh @@ -26,5 +26,3 @@ cd utils/test/testapi/ pip install -r requirements.txt ./install.sh opnfv-testapi -c ../../../testapi_venv/etc/opnfv_testapi/config.ini & - -deactivate diff --git a/utils/test/testapi/opnfv_testapi/resources/handlers.py b/utils/test/testapi/opnfv_testapi/resources/handlers.py index f1ad15e97..9fc5d6be1 100644 --- a/utils/test/testapi/opnfv_testapi/resources/handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/handlers.py @@ -43,6 +43,7 @@ class GenericApiHandler(RequestHandler): self.db_pods = 'pods' self.db_testcases = 'testcases' self.db_results = 'results' + self.db_scenarios = 'scenarios' def prepare(self): if self.request.method != "GET" and self.request.method != "DELETE": diff --git a/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py b/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py new file mode 100644 index 000000000..7bf3d5d53 --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/resources/scenario_handlers.py @@ -0,0 +1,75 @@ +from opnfv_testapi.common.constants import HTTP_FORBIDDEN +from opnfv_testapi.resources.handlers import GenericApiHandler +from opnfv_testapi.resources.scenario_models import Scenario +from opnfv_testapi.tornado_swagger import swagger + + +class GenericScenarioHandler(GenericApiHandler): + def __init__(self, application, request, **kwargs): + super(GenericScenarioHandler, self).__init__(application, + request, + **kwargs) + self.table = self.db_scenarios + self.table_cls = Scenario + + +class ScenariosCLHandler(GenericScenarioHandler): + @swagger.operation(nickname="List scenarios by queries") + def get(self): + """ + @description: Retrieve scenario(s). + @notes: Retrieve scenario(s) + @return 200: all scenarios consist with query, + empty list if no scenario is found + @rtype: L{Scenarios} + """ + self._list() + + @swagger.operation(nickname="Create a new scenario") + def post(self): + """ + @description: create a new scenario by name + @param body: scenario to be created + @type body: L{ScenarioCreateRequest} + @in body: body + @rtype: L{CreateResponse} + @return 200: scenario is created. + @raise 403: scenario already exists + @raise 400: body or name not provided + """ + def query(data): + return {'name': data.name} + + def error(data): + message = '{} already exists as a scenario'.format(data.name) + return HTTP_FORBIDDEN, message + + miss_checks = ['name'] + db_checks = [(self.table, False, query, error)] + self._create(miss_checks=miss_checks, db_checks=db_checks) + + +class ScenarioGURHandler(GenericScenarioHandler): + @swagger.operation(nickname='Get the scenario by name') + def get(self, name): + """ + @description: get a single scenario by name + @rtype: L{Scenario} + @return 200: scenario exist + @raise 404: scenario not exist + """ + pass + + @swagger.operation(nickname="Update the scenario by name") + def put(self, name): + """ + @description: update a single scenario by name + @param body: fields to be updated + @type body: L{string} + @in body: body + @rtype: L{Scenario} + @return 200: update success + @raise 404: scenario not exist + @raise 403: nothing to update + """ + pass diff --git a/utils/test/testapi/opnfv_testapi/resources/scenario_models.py b/utils/test/testapi/opnfv_testapi/resources/scenario_models.py new file mode 100644 index 000000000..b4bb3634b --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/resources/scenario_models.py @@ -0,0 +1,98 @@ +import models +from opnfv_testapi.tornado_swagger import swagger + + +@swagger.model() +class ScenarioTI(models.ModelBase): + def __init__(self, date=None, status='silver'): + self.date = date + self.status = status + + +@swagger.model() +class ScenarioScore(models.ModelBase): + def __init__(self, date=None, score=''): + self.date = date + self.score = score + + +@swagger.model() +class ScenarioProject(models.ModelBase): + """ + @property customs: + @ptype customs: C{list} of L{string} + @property scores: + @ptype scores: C{list} of L{ScenarioScore} + @property trust_indicators: + @ptype trust_indicators: C{list} of L{ScenarioTI} + """ + def __init__(self, + name='', + customs=None, + scores=None, + trust_indicators=None): + self.name = name + self.customs = customs + self.scores = scores + self.trust_indicator = trust_indicators + + +@swagger.model() +class ScenarioVersion(models.ModelBase): + """ + @property projects: + @ptype projects: C{list} of L{ScenarioProject} + """ + def __init__(self, version, projects=None): + self.version = version + self.projects = projects + + +@swagger.model() +class ScenarioInstaller(models.ModelBase): + """ + @property versions: + @ptype versions: C{list} of L{ScenarioVersion} + """ + def __init__(self, installer=None, owner=None, versions=None): + self.installer = installer + self.owner = owner + self.versions = versions if versions else list() + + +@swagger.model() +class ScenarioCreateRequest(models.ModelBase): + """ + @property installers: + @ptype installers: C{list} of L{ScenarioInstaller} + """ + def __init__(self, name='', installers=None): + self.name = name + self.installers = installers if installers else list() + + +@swagger.model() +class Scenario(models.ModelBase): + """ + @property installers: + @ptype installers: C{list} of L{ScenarioInstaller} + """ + def __init__(self, name='', create_date='', _id='', installers=None): + self.name = name + self._id = _id + self.creation_date = create_date + self.installers = installers if installers else list() + + +@swagger.model() +class Scenarios(models.ModelBase): + """ + @property scenarios: + @ptype scenarios: C{list} of L{Scenario} + """ + def __init__(self): + self.scenarios = list() + + @staticmethod + def attr_parser(): + return {'scenarios': Scenario} diff --git a/utils/test/testapi/opnfv_testapi/router/url_mappings.py b/utils/test/testapi/opnfv_testapi/router/url_mappings.py index eb648ecbb..0ae3c31c3 100644 --- a/utils/test/testapi/opnfv_testapi/router/url_mappings.py +++ b/utils/test/testapi/opnfv_testapi/router/url_mappings.py @@ -14,7 +14,8 @@ from opnfv_testapi.resources.project_handlers import ProjectCLHandler, \ ProjectGURHandler from opnfv_testapi.resources.result_handlers import ResultsCLHandler, \ ResultsGURHandler - +from opnfv_testapi.resources.scenario_handlers import ScenariosCLHandler +from opnfv_testapi.resources.scenario_handlers import ScenarioGURHandler mappings = [ # GET /versions => GET API version @@ -45,4 +46,8 @@ mappings = [ # (project, case, and pod) (r"/api/v1/results", ResultsCLHandler), (r"/api/v1/results/([^/]+)", ResultsGURHandler), + + # scenarios + (r"/api/v1/scenarios", ScenariosCLHandler), + (r"/api/v1/scenarios/([^/]+)", ScenarioGURHandler), ] 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 3dd87e603..d86d8eadf 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py @@ -189,3 +189,4 @@ pods = MemDb() projects = MemDb() testcases = MemDb() results = MemDb() +scenarios = MemDb() diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/scenario-create.json b/utils/test/testapi/opnfv_testapi/tests/unit/scenario-create.json new file mode 100644 index 000000000..eba8b6c0a --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/tests/unit/scenario-create.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": [] + } + ] + } + ] + } + ] +}
\ 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 ff1a1932c..9343ab2fb 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: - req = req.format() + if req and not isinstance(req, str): + req = json.dumps(req.format()) res = self.fetch(self._update_uri(uri, *args), method='POST', - body=json.dumps(req), + body=req if req else json.dumps(None), headers=self.headers) return self._get_return(res, self.create_res) @@ -123,9 +123,17 @@ class TestBase(AsyncHTTPTestCase): self.assertIn(self.basePath, body.href) def assert_create_body(self, body, req=None, *args): + import inspect if not req: req = self.req_d - new_args = args + tuple([req.name]) + resource_name = '' + if inspect.isclass(req): + resource_name = req.name + elif isinstance(req, dict): + resource_name = req['name'] + elif isinstance(req, str): + resource_name = json.loads(req)['name'] + new_args = args + tuple([resource_name]) self.assertIn(self._get_uri(*new_args), body.href) @staticmethod @@ -134,3 +142,4 @@ class TestBase(AsyncHTTPTestCase): fake_pymongo.projects.clear() fake_pymongo.testcases.clear() fake_pymongo.results.clear() + fake_pymongo.scenarios.clear() diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py new file mode 100644 index 000000000..8e827813c --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py @@ -0,0 +1,55 @@ +import json +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 ScenarioCreateRequest +from test_testcase import TestBase + + +class TestScenarioBase(TestBase): + def setUp(self): + super(TestScenarioBase, self).setUp() + self.basePath = '/api/v1/scenarios' + self.load_request('scenario-create.json') + + def tearDown(self): + pass + + 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)) + f.close() + + +class TestScenarioCreate(TestScenarioBase): + def test_withoutBody(self): + (code, body) = self.create() + self.assertEqual(code, HTTP_BAD_REQUEST) + + def test_emptyName(self): + req_empty = ScenarioCreateRequest('') + (code, body) = self.create(req_empty) + self.assertEqual(code, HTTP_BAD_REQUEST) + self.assertIn('name missing', body) + + def test_noneName(self): + req_none = ScenarioCreateRequest(None) + (code, body) = self.create(req_none) + self.assertEqual(code, HTTP_BAD_REQUEST) + self.assertIn('name missing', body) + + def test_success(self): + (code, body) = self.create_d() + self.assertEqual(code, HTTP_OK) + self.assert_create_body(body) + + def test_alreadyExist(self): + self.create_d() + (code, body) = self.create_d() + self.assertEqual(code, HTTP_FORBIDDEN) + self.assertIn('already exists', body) |