diff options
Diffstat (limited to 'utils/test/testapi/opnfv_testapi')
19 files changed, 147 insertions, 183 deletions
diff --git a/utils/test/testapi/opnfv_testapi/cmd/server.py b/utils/test/testapi/opnfv_testapi/cmd/server.py index fa2b72250..8b092b89e 100644 --- a/utils/test/testapi/opnfv_testapi/cmd/server.py +++ b/utils/test/testapi/opnfv_testapi/cmd/server.py @@ -48,7 +48,9 @@ def parse_config(argv=[]): 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) + if args.config_file: + config.Config.CONFIG = args.config_file + CONF = config.Config() def get_db(): @@ -60,8 +62,8 @@ def make_app(): return swagger.Application( url_mappings.mappings, db=get_db(), - debug=CONF.api_debug_on, - auth=CONF.api_authenticate_on + debug=CONF.api_debug, + auth=CONF.api_authenticate ) diff --git a/utils/test/testapi/opnfv_testapi/common/check.py b/utils/test/testapi/opnfv_testapi/common/check.py index be4b1df12..4d9902cd0 100644 --- a/utils/test/testapi/opnfv_testapi/common/check.py +++ b/utils/test/testapi/opnfv_testapi/common/check.py @@ -8,9 +8,11 @@ ############################################################################## import functools -from tornado import web, gen +from tornado import gen +from tornado import web -from opnfv_testapi.common import raises, message +from opnfv_testapi.common import message +from opnfv_testapi.common import raises def authenticate(method): diff --git a/utils/test/testapi/opnfv_testapi/common/config.py b/utils/test/testapi/opnfv_testapi/common/config.py index 362fca640..70d7bd63f 100644 --- a/utils/test/testapi/opnfv_testapi/common/config.py +++ b/utils/test/testapi/opnfv_testapi/common/config.py @@ -11,83 +11,41 @@ import ConfigParser import os -class ParseError(Exception): - """ - Custom exception class for config file - """ - - def __init__(self, message): - self.msg = message - - def __str__(self): - return 'error parsing config file : %s' % self.msg - - -class APIConfig(object): - """ - The purpose of this class is to load values correctly from the config file. - Each key is declared as an attribute in __init__() and linked in parse() - """ +class Config(object): + CONFIG = None def __init__(self): - self._set_default_config() - self.mongo_url = None - self.mongo_dbname = None - self.api_port = None - self.api_debug_on = None - self.api_authenticate_on = None - self._parser = None - self.swagger_base_url = None + self.file = self.CONFIG if self.CONFIG else self._default_config() + self._parse() - def _set_default_config(self): - venv = os.getenv('VIRTUAL_ENV') - self._default_config = os.path.join('/' if not venv else venv, - 'etc/opnfv_testapi/config.ini') + def _parse(self): + if not os.path.exists(self.file): + raise Exception("%s not found" % self.file) - def _get_parameter(self, section, param): - try: - return self._parser.get(section, param) - except ConfigParser.NoOptionError: - raise ParseError("No parameter: [%s.%s]" % (section, param)) - - def _get_int_parameter(self, section, param): - try: - return int(self._get_parameter(section, param)) - except ValueError: - raise ParseError("Not int: [%s.%s]" % (section, param)) + config = ConfigParser.RawConfigParser() + config.read(self.file) + self._parse_section(config) - def _get_bool_parameter(self, section, param): - result = self._get_parameter(section, param) - if str(result).lower() == 'true': - return True - if str(result).lower() == 'false': - return False + def _parse_section(self, config): + [self._parse_item(config, section) for section in (config.sections())] - raise ParseError( - "Not boolean: [%s.%s : %s]" % (section, param, result)) + def _parse_item(self, config, section): + [setattr(self, '{}_{}'.format(section, k), self._parse_value(v)) + for k, v in config.items(section)] @staticmethod - def parse(config_location=None): - obj = APIConfig() - - if config_location is None: - config_location = obj._default_config - - if not os.path.exists(config_location): - raise ParseError("%s not found" % config_location) - - obj._parser = ConfigParser.SafeConfigParser() - obj._parser.read(config_location) - - # Linking attributes to keys from file with their sections - obj.mongo_url = obj._get_parameter("mongo", "url") - obj.mongo_dbname = obj._get_parameter("mongo", "dbname") - - obj.api_port = obj._get_int_parameter("api", "port") - obj.api_debug_on = obj._get_bool_parameter("api", "debug") - obj.api_authenticate_on = obj._get_bool_parameter("api", - "authenticate") - - obj.swagger_base_url = obj._get_parameter("swagger", "base_url") + def _parse_value(value): + try: + value = int(value) + except: + if str(value).lower() == 'true': + value = True + elif str(value).lower() == 'false': + value = False + return value - return obj + @staticmethod + def _default_config(): + is_venv = os.getenv('VIRTUAL_ENV') + return os.path.join('/' if not is_venv else is_venv, + 'etc/opnfv_testapi/config.ini') diff --git a/utils/test/testapi/opnfv_testapi/resources/handlers.py b/utils/test/testapi/opnfv_testapi/resources/handlers.py index 955fbbef7..dbf94eb75 100644 --- a/utils/test/testapi/opnfv_testapi/resources/handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/handlers.py @@ -26,10 +26,10 @@ import json from tornado import gen from tornado import web -import models from opnfv_testapi.common import check from opnfv_testapi.common import message from opnfv_testapi.common import raises +from opnfv_testapi.resources import models from opnfv_testapi.tornado_swagger import swagger DEFAULT_REPRESENTATION = "application/json" diff --git a/utils/test/testapi/opnfv_testapi/resources/models.py b/utils/test/testapi/opnfv_testapi/resources/models.py index 0ea482fd2..e8fc532b7 100644 --- a/utils/test/testapi/opnfv_testapi/resources/models.py +++ b/utils/test/testapi/opnfv_testapi/resources/models.py @@ -14,9 +14,8 @@ # feng.xiaowei@zte.com.cn mv TestResut to result_models.py 5-23-2016 # feng.xiaowei@zte.com.cn add ModelBase 12-20-2016 ############################################################################## -import copy import ast - +import copy from opnfv_testapi.tornado_swagger import swagger diff --git a/utils/test/testapi/opnfv_testapi/resources/pod_handlers.py b/utils/test/testapi/opnfv_testapi/resources/pod_handlers.py index e21841d33..502988752 100644 --- a/utils/test/testapi/opnfv_testapi/resources/pod_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/pod_handlers.py @@ -7,8 +7,8 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import handlers +from opnfv_testapi.resources import pod_models from opnfv_testapi.tornado_swagger import swagger -import pod_models class GenericPodHandler(handlers.GenericApiHandler): diff --git a/utils/test/testapi/opnfv_testapi/resources/pod_models.py b/utils/test/testapi/opnfv_testapi/resources/pod_models.py index 26a9e6788..2c3ea978b 100644 --- a/utils/test/testapi/opnfv_testapi/resources/pod_models.py +++ b/utils/test/testapi/opnfv_testapi/resources/pod_models.py @@ -6,7 +6,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import models +from opnfv_testapi.resources import models from opnfv_testapi.tornado_swagger import swagger diff --git a/utils/test/testapi/opnfv_testapi/resources/project_handlers.py b/utils/test/testapi/opnfv_testapi/resources/project_handlers.py index d79cd3b61..be2950705 100644 --- a/utils/test/testapi/opnfv_testapi/resources/project_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/project_handlers.py @@ -7,9 +7,9 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import handlers +from opnfv_testapi.resources import handlers +from opnfv_testapi.resources import project_models from opnfv_testapi.tornado_swagger import swagger -import project_models class GenericProjectHandler(handlers.GenericApiHandler): diff --git a/utils/test/testapi/opnfv_testapi/resources/project_models.py b/utils/test/testapi/opnfv_testapi/resources/project_models.py index f7323c1c4..3243882bd 100644 --- a/utils/test/testapi/opnfv_testapi/resources/project_models.py +++ b/utils/test/testapi/opnfv_testapi/resources/project_models.py @@ -6,7 +6,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import models +from opnfv_testapi.resources import models from opnfv_testapi.tornado_swagger import swagger diff --git a/utils/test/testapi/opnfv_testapi/resources/result_models.py b/utils/test/testapi/opnfv_testapi/resources/result_models.py index 50445fc22..62a6dacff 100644 --- a/utils/test/testapi/opnfv_testapi/resources/result_models.py +++ b/utils/test/testapi/opnfv_testapi/resources/result_models.py @@ -6,7 +6,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import models +from opnfv_testapi.resources import models from opnfv_testapi.tornado_swagger import swagger diff --git a/utils/test/testapi/opnfv_testapi/resources/scenario_models.py b/utils/test/testapi/opnfv_testapi/resources/scenario_models.py index b84accf4d..467cff241 100644 --- a/utils/test/testapi/opnfv_testapi/resources/scenario_models.py +++ b/utils/test/testapi/opnfv_testapi/resources/scenario_models.py @@ -1,4 +1,4 @@ -import models +from opnfv_testapi.resources import models from opnfv_testapi.tornado_swagger import swagger diff --git a/utils/test/testapi/opnfv_testapi/resources/testcase_models.py b/utils/test/testapi/opnfv_testapi/resources/testcase_models.py index 8cc3c6c6a..2379dfc4c 100644 --- a/utils/test/testapi/opnfv_testapi/resources/testcase_models.py +++ b/utils/test/testapi/opnfv_testapi/resources/testcase_models.py @@ -6,19 +6,20 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import models +from opnfv_testapi.resources import models from opnfv_testapi.tornado_swagger import swagger @swagger.model() class TestcaseCreateRequest(models.ModelBase): def __init__(self, name, url=None, description=None, - tier=None, ci_loop=None, criteria=None, - blocking=None, dependencies=None, run=None, + catalog_description=None, tier=None, ci_loop=None, + criteria=None, blocking=None, dependencies=None, run=None, domains=None, tags=None, version=None): self.name = name self.url = url self.description = description + self.catalog_description = catalog_description self.tier = tier self.ci_loop = ci_loop self.criteria = criteria @@ -34,11 +35,12 @@ class TestcaseCreateRequest(models.ModelBase): @swagger.model() class TestcaseUpdateRequest(models.ModelBase): def __init__(self, name=None, description=None, project_name=None, - tier=None, ci_loop=None, criteria=None, - blocking=None, dependencies=None, run=None, + catalog_description=None, tier=None, ci_loop=None, + criteria=None, blocking=None, dependencies=None, run=None, domains=None, tags=None, version=None, trust=None): self.name = name self.description = description + self.catalog_description = catalog_description self.project_name = project_name self.tier = tier self.ci_loop = ci_loop @@ -56,14 +58,15 @@ class TestcaseUpdateRequest(models.ModelBase): class Testcase(models.ModelBase): def __init__(self, _id=None, name=None, project_name=None, description=None, url=None, creation_date=None, - tier=None, ci_loop=None, criteria=None, - blocking=None, dependencies=None, run=None, + catalog_description=None, tier=None, ci_loop=None, + criteria=None, blocking=None, dependencies=None, run=None, domains=None, tags=None, version=None, trust=None): self._id = None self.name = None self.project_name = None self.description = None + self.catalog_description = None self.url = None self.creation_date = None self.tier = None diff --git a/utils/test/testapi/opnfv_testapi/resources/ui_handlers.py b/utils/test/testapi/opnfv_testapi/resources/ui_handlers.py new file mode 100644 index 000000000..ac8f816a4 --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/resources/ui_handlers.py @@ -0,0 +1,14 @@ +from opnfv_testapi.resources.handlers import GenericApiHandler +from opnfv_testapi.tornado_swagger import settings + + +class UIHandler(GenericApiHandler): + def initialize(self, **kwargs): + self.static_path = settings.docs_settings.get('static_path') + self.base_url = 'http://localhost:8000' + + def get_template_path(self): + return self.static_path + + def get(self): + self.render('testapi-ui/index.html') diff --git a/utils/test/testapi/opnfv_testapi/router/url_mappings.py b/utils/test/testapi/opnfv_testapi/router/url_mappings.py index 39cf006af..94e71c62d 100644 --- a/utils/test/testapi/opnfv_testapi/router/url_mappings.py +++ b/utils/test/testapi/opnfv_testapi/router/url_mappings.py @@ -12,9 +12,11 @@ from opnfv_testapi.resources import project_handlers from opnfv_testapi.resources import result_handlers from opnfv_testapi.resources import scenario_handlers from opnfv_testapi.resources import testcase_handlers +import opnfv_testapi.resources.ui_handlers mappings = [ # GET /versions => GET API version + (r'/', opnfv_testapi.resources.ui_handlers.UIHandler), (r"/versions", handlers.VersionHandler), # few examples: diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/common/test_config.py b/utils/test/testapi/opnfv_testapi/tests/unit/common/test_config.py index aaff6bb91..446b9442a 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/common/test_config.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/common/test_config.py @@ -1,36 +1,16 @@ -import ConfigParser import os -import pytest - from opnfv_testapi.common import config -@pytest.fixture() -def config_dir(): - return os.path.dirname(__file__) - - -@pytest.mark.parametrize('exception, config_file, excepted', [ - (config.ParseError, None, '/etc/opnfv_testapi/config.ini not found'), - (ConfigParser.NoSectionError, 'nosection.ini', 'No section:'), - (config.ParseError, 'noparam.ini', 'No parameter:'), - (config.ParseError, 'notint.ini', 'Not int:'), - (config.ParseError, 'notboolean.ini', 'Not boolean:')]) -def pytest_config_exceptions(config_dir, exception, config_file, excepted): - file = '{}/{}'.format(config_dir, config_file) if config_file else None - with pytest.raises(exception) as error: - config.APIConfig().parse(file) - assert excepted in str(error.value) - - def test_config_success(): - config_dir = os.path.join(os.path.dirname(__file__), - '../../../../etc/config.ini') - conf = config.APIConfig().parse(config_dir) + config_file = os.path.join(os.path.dirname(__file__), + '../../../../etc/config.ini') + config.Config.CONFIG = config_file + conf = config.Config() assert conf.mongo_url == 'mongodb://127.0.0.1:27017/' assert conf.mongo_dbname == 'test_results_collection' assert conf.api_port == 8000 - assert conf.api_debug_on is True - assert conf.api_authenticate_on is False + assert conf.api_debug is True + assert conf.api_authenticate is False assert conf.swagger_base_url == 'http://localhost:8000' diff --git a/utils/test/testapi/opnfv_testapi/tornado_swagger/handlers.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/handlers.py index 2154b4697..c9c8a0863 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/handlers.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/handlers.py @@ -6,38 +6,37 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from tornado.web import URLSpec, StaticFileHandler +import tornado.web -from settings import default_settings, \ - SWAGGER_API_DOCS, SWAGGER_API_LIST, SWAGGER_API_SPEC -from views import SwaggerUIHandler, SwaggerResourcesHandler, SwaggerApiHandler +from opnfv_testapi.tornado_swagger import settings +from opnfv_testapi.tornado_swagger import views def swagger_handlers(): - prefix = default_settings.get('swagger_prefix', '/swagger') + prefix = settings.docs_settings.get('swagger_prefix', '/swagger') if prefix[-1] != '/': prefix += '/' def _path(suffix): return prefix + suffix return [ - URLSpec( + tornado.web.URLSpec( _path(r'spec.html$'), - SwaggerUIHandler, - default_settings, - name=SWAGGER_API_DOCS), - URLSpec( - _path(r'spec.json$'), - SwaggerResourcesHandler, - default_settings, - name=SWAGGER_API_LIST), - URLSpec( - _path(r'spec$'), - SwaggerApiHandler, - default_settings, - name=SWAGGER_API_SPEC), + views.SwaggerUIHandler, + settings.docs_settings, + name=settings.API_DOCS_NAME), + tornado.web.URLSpec( + _path(r'resources.json$'), + views.SwaggerResourcesHandler, + settings.docs_settings, + name=settings.RESOURCE_LISTING_NAME), + tornado.web.URLSpec( + _path(r'APIs$'), + views.SwaggerApiHandler, + settings.docs_settings, + name=settings.API_DECLARATION_NAME), ( - _path(r'(.*\.(css|png|gif|js))'), - StaticFileHandler, - {'path': default_settings.get('static_path')}), + _path(r'(.*\.(css|png|gif|js|html|json))'), + tornado.web.StaticFileHandler, + {'path': settings.docs_settings.get('static_path')}), ] diff --git a/utils/test/testapi/opnfv_testapi/tornado_swagger/settings.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/settings.py index 88d0d0f88..03e9bbdff 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/settings.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/settings.py @@ -8,25 +8,21 @@ ############################################################################## import os.path -SWAGGER_VERSION = '1.2' -SWAGGER_API_DOCS = 'swagger-api-docs' -SWAGGER_API_LIST = 'swagger-api-list' -SWAGGER_API_SPEC = 'swagger-api-spec' +API_DOCS_NAME = 'swagger-api-docs' +RESOURCE_LISTING_NAME = 'swagger-resource-listing' +API_DECLARATION_NAME = 'swagger-api-declaration' STATIC_PATH = os.path.join(os.path.dirname(os.path.normpath(__file__)), 'static') -default_settings = { +docs_settings = { 'base_url': '', 'static_path': STATIC_PATH, 'swagger_prefix': '/swagger', 'api_version': 'v1.0', + 'swagger_version': '1.2', 'api_key': '', 'enabled_methods': ['get', 'post', 'put', 'patch', 'delete'], 'exclude_namespaces': [], } models = [] - - -def basePath(): - return default_settings.get('base_url') diff --git a/utils/test/testapi/opnfv_testapi/tornado_swagger/swagger.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/swagger.py index 3d21edefb..83f389a6b 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/swagger.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/swagger.py @@ -6,15 +6,15 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import inspect -from functools import wraps from HTMLParser import HTMLParser +from functools import wraps +import inspect import epydoc.markup import tornado.web -from settings import default_settings, models -from handlers import swagger_handlers +from opnfv_testapi.tornado_swagger import handlers +from opnfv_testapi.tornado_swagger import settings class EpytextParser(HTMLParser): @@ -204,7 +204,7 @@ class model(DocParser): if '__init__' in dir(cls): self._parse_args(cls.__init__) self.parse_docstring(inspect.getdoc(cls)) - models.append(self) + settings.models.append(self) def _parse_args(self, func): argspec = inspect.getargspec(func) @@ -276,15 +276,16 @@ class operation(DocParser): def docs(**opts): - default_settings.update(opts) + settings.docs_settings.update(opts) class Application(tornado.web.Application): - def __init__(self, handlers=None, + def __init__(self, app_handlers=None, default_host="", transforms=None, **settings): - super(Application, self).__init__(swagger_handlers() + handlers, - default_host, - transforms, - **settings) + super(Application, self).__init__( + handlers.swagger_handlers() + app_handlers, + default_host, + transforms, + **settings) diff --git a/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py index 25083195b..ca4f01cfc 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py @@ -8,47 +8,52 @@ ############################################################################## import inspect import json +import os.path import tornado.template import tornado.web -from settings import SWAGGER_VERSION, SWAGGER_API_LIST, SWAGGER_API_SPEC -from settings import models, basePath +from opnfv_testapi.tornado_swagger import settings def json_dumps(obj, pretty=False): - return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': ')) \ - if pretty else json.dumps(obj) + return json.dumps(obj, + sort_keys=True, + indent=4, + separators=(',', ': ')) if pretty else json.dumps(obj) class SwaggerUIHandler(tornado.web.RequestHandler): - def initialize(self, static_path, **kwds): - self.static_path = static_path + def initialize(self, **kwargs): + self.static_path = kwargs.get('static_path') + self.base_url = kwargs.get('base_url') def get_template_path(self): return self.static_path def get(self): - discovery_url = basePath() + self.reverse_url(SWAGGER_API_LIST) + discovery_url = os.path.join( + self.base_url, + self.reverse_url(settings.RESOURCE_LISTING_NAME)) self.render('index.html', discovery_url=discovery_url) class SwaggerResourcesHandler(tornado.web.RequestHandler): - def initialize(self, api_version, exclude_namespaces, **kwds): - self.api_version = api_version - self.exclude_namespaces = exclude_namespaces + def initialize(self, **kwargs): + self.api_version = kwargs.get('api_version') + self.swagger_version = kwargs.get('swagger_version') + self.base_url = kwargs.get('base_url') + self.exclude_namespaces = kwargs.get('exclude_namespaces') def get(self): self.set_header('content-type', 'application/json') resources = { 'apiVersion': self.api_version, - 'swaggerVersion': SWAGGER_VERSION, - 'basePath': basePath(), - 'produces': ["application/json"], - 'description': 'Test Api Spec', + 'swaggerVersion': self.swagger_version, + 'basePath': self.base_url, 'apis': [{ - 'path': self.reverse_url(SWAGGER_API_SPEC), - 'description': 'Test Api Spec' + 'path': self.reverse_url(settings.API_DECLARATION_NAME), + 'description': 'Restful APIs Specification' }] } @@ -56,9 +61,10 @@ class SwaggerResourcesHandler(tornado.web.RequestHandler): class SwaggerApiHandler(tornado.web.RequestHandler): - def initialize(self, api_version, base_url, **kwds): - self.api_version = api_version - self.base_url = base_url + def initialize(self, **kwargs): + self.api_version = kwargs.get('api_version') + self.swagger_version = kwargs.get('swagger_version') + self.base_url = kwargs.get('base_url') def get(self): self.set_header('content-type', 'application/json') @@ -68,11 +74,13 @@ class SwaggerApiHandler(tornado.web.RequestHandler): specs = { 'apiVersion': self.api_version, - 'swaggerVersion': SWAGGER_VERSION, - 'basePath': basePath(), + 'swaggerVersion': self.swagger_version, + 'basePath': self.base_url, + 'resourcePath': '/', + 'produces': ["application/json"], 'apis': [self.__get_api_spec__(path, spec, operations) for path, spec, operations in apis], - 'models': self.__get_models_spec(models) + 'models': self.__get_models_spec(settings.models) } self.finish(json_dumps(specs, self.get_arguments('pretty'))) |