From fd4b422ccde3795e0fdc0b14651bec62e93275cc Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Mon, 27 Feb 2017 14:33:52 +0800 Subject: make unittest cover server.py in TestAPI Change-Id: Id379e6786e1f61fb1032f80636da107156e68fb5 Signed-off-by: SerenaFeng --- testapi/opnfv_testapi/cmd/server.py | 27 ++++++++++++-------- testapi/opnfv_testapi/tests/unit/common/normal.ini | 17 +++++++++++++ testapi/opnfv_testapi/tests/unit/test_base.py | 29 ++++++++++++++++------ testapi/run_test.sh | 1 + testapi/test-requirements.txt | 1 + 5 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 testapi/opnfv_testapi/tests/unit/common/normal.ini (limited to 'testapi') diff --git a/testapi/opnfv_testapi/cmd/server.py b/testapi/opnfv_testapi/cmd/server.py index 013ee66..fa2b722 100644 --- a/testapi/opnfv_testapi/cmd/server.py +++ b/testapi/opnfv_testapi/cmd/server.py @@ -30,6 +30,7 @@ TODOs : """ import argparse +import sys import motor import tornado.ioloop @@ -38,30 +39,34 @@ from opnfv_testapi.common import config from opnfv_testapi.router import url_mappings from opnfv_testapi.tornado_swagger import swagger -# optionally get config file from command line -parser = argparse.ArgumentParser() -parser.add_argument("-c", "--config-file", dest='config_file', - help="Config file location") -args = parser.parse_args() -CONF = config.APIConfig().parse(args.config_file) +CONF = None -# connecting to MongoDB server, and choosing database -client = motor.MotorClient(CONF.mongo_url) -db = client[CONF.mongo_dbname] -swagger.docs(base_url=CONF.swagger_base_url) +def parse_config(argv=[]): + global CONF + parser = argparse.ArgumentParser() + parser.add_argument("-c", "--config-file", dest='config_file', + help="Config file location") + args = parser.parse_args(argv) + CONF = config.APIConfig().parse(args.config_file) + + +def get_db(): + return motor.MotorClient(CONF.mongo_url)[CONF.mongo_dbname] def make_app(): + swagger.docs(base_url=CONF.swagger_base_url) return swagger.Application( url_mappings.mappings, - db=db, + db=get_db(), debug=CONF.api_debug_on, auth=CONF.api_authenticate_on ) def main(): + parse_config(sys.argv[1:]) application = make_app() application.listen(CONF.api_port) tornado.ioloop.IOLoop.current().start() diff --git a/testapi/opnfv_testapi/tests/unit/common/normal.ini b/testapi/opnfv_testapi/tests/unit/common/normal.ini new file mode 100644 index 0000000..77cc6c6 --- /dev/null +++ b/testapi/opnfv_testapi/tests/unit/common/normal.ini @@ -0,0 +1,17 @@ +# to add a new parameter in the config file, +# the CONF object in config.ini must be updated +[mongo] +# URL of the mongo DB +# Mongo auth url => mongodb://user1:pwd1@host1/?authSource=db1 +url = mongodb://127.0.0.1:27017/ +dbname = test_results_collection + +[api] +# Listening port +port = 8000 +# With debug_on set to true, error traces will be shown in HTTP responses +debug = True +authenticate = False + +[swagger] +base_url = http://localhost:8000 diff --git a/testapi/opnfv_testapi/tests/unit/test_base.py b/testapi/opnfv_testapi/tests/unit/test_base.py index b2be8d5..b955f4a 100644 --- a/testapi/opnfv_testapi/tests/unit/test_base.py +++ b/testapi/opnfv_testapi/tests/unit/test_base.py @@ -7,19 +7,21 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import json +from os import path +import mock from tornado import testing -from tornado import web import fake_pymongo +from opnfv_testapi.cmd import server from opnfv_testapi.resources import models -from opnfv_testapi.router import url_mappings class TestBase(testing.AsyncHTTPTestCase): headers = {'Content-Type': 'application/json; charset=UTF-8'} def setUp(self): + self._patch_server() self.basePath = '' self.create_res = models.CreateResponse self.get_res = None @@ -30,13 +32,24 @@ class TestBase(testing.AsyncHTTPTestCase): self.addCleanup(self._clear) super(TestBase, self).setUp() + def tearDown(self): + self.db_patcher.stop() + + def _patch_server(self): + server.parse_config([ + '--config-file', + path.join(path.dirname(__file__), 'common/normal.ini') + ]) + self.db_patcher = mock.patch('opnfv_testapi.cmd.server.get_db', + self._fake_pymongo) + self.db_patcher.start() + + @staticmethod + def _fake_pymongo(): + return fake_pymongo + def get_app(self): - return web.Application( - url_mappings.mappings, - db=fake_pymongo, - debug=True, - auth=False - ) + return server.make_app() def create_d(self, *args): return self.create(self.req_d, *args) diff --git a/testapi/run_test.sh b/testapi/run_test.sh index bedb67d..4efc7af 100755 --- a/testapi/run_test.sh +++ b/testapi/run_test.sh @@ -16,6 +16,7 @@ pip install -r $SCRIPTDIR/requirements.txt pip install coverage pip install nose>=1.3.1 pip install pytest +pip install mock find . -type f -name "*.pyc" -delete diff --git a/testapi/test-requirements.txt b/testapi/test-requirements.txt index 8933319..4633ad6 100644 --- a/testapi/test-requirements.txt +++ b/testapi/test-requirements.txt @@ -3,6 +3,7 @@ # process, which may cause wedges in the gate later. tox +mock pytest pytest-cov coverage -- cgit 1.2.3-korg