diff options
Diffstat (limited to 'utils')
5 files changed, 43 insertions, 105 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/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/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/views.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py index ca4f01cfc..42b37483b 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py @@ -8,7 +8,6 @@ ############################################################################## import inspect import json -import os.path import tornado.template import tornado.web @@ -32,9 +31,8 @@ class SwaggerUIHandler(tornado.web.RequestHandler): return self.static_path def get(self): - discovery_url = os.path.join( - self.base_url, - self.reverse_url(settings.RESOURCE_LISTING_NAME)) + resource_url = self.reverse_url(settings.RESOURCE_LISTING_NAME) + discovery_url = self.base_url + resource_url self.render('index.html', discovery_url=discovery_url) diff --git a/utils/test/testapi/requirements.txt b/utils/test/testapi/requirements.txt index e1fd9e4cc..ee144d247 100644 --- a/utils/test/testapi/requirements.txt +++ b/utils/test/testapi/requirements.txt @@ -4,6 +4,6 @@ pbr>=1.6 setuptools>=16.0 -tornado>=3.1 +tornado>=3.1,<=4.3 epydoc>=0.3.1 motor |