summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/tests/unit
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-02-23 16:59:09 +0800
committerAric Gardner <agardner@linuxfoundation.org>2017-02-24 21:52:00 +0000
commita04daa829c481fab9a734ca2e22fe69ca0d492fc (patch)
treed317418aa2f2f807bf66cccbfa99aabbbd9b594b /testapi/opnfv_testapi/tests/unit
parent105fcee68c3a74e7edde9f8d7fd7846d9aedc253 (diff)
add unittest of config.py
Change-Id: I96639c47d27ef449d02528efad23e2499daa3def Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'testapi/opnfv_testapi/tests/unit')
-rw-r--r--testapi/opnfv_testapi/tests/unit/common/__init__.py0
-rw-r--r--testapi/opnfv_testapi/tests/unit/common/noparam.ini16
-rw-r--r--testapi/opnfv_testapi/tests/unit/common/nosection.ini11
-rw-r--r--testapi/opnfv_testapi/tests/unit/common/notboolean.ini17
-rw-r--r--testapi/opnfv_testapi/tests/unit/common/notint.ini17
-rw-r--r--testapi/opnfv_testapi/tests/unit/common/test_config.py36
6 files changed, 97 insertions, 0 deletions
diff --git a/testapi/opnfv_testapi/tests/unit/common/__init__.py b/testapi/opnfv_testapi/tests/unit/common/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testapi/opnfv_testapi/tests/unit/common/__init__.py
diff --git a/testapi/opnfv_testapi/tests/unit/common/noparam.ini b/testapi/opnfv_testapi/tests/unit/common/noparam.ini
new file mode 100644
index 0000000..fda2a09
--- /dev/null
+++ b/testapi/opnfv_testapi/tests/unit/common/noparam.ini
@@ -0,0 +1,16 @@
+# 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/
+
+[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/common/nosection.ini b/testapi/opnfv_testapi/tests/unit/common/nosection.ini
new file mode 100644
index 0000000..9988fc0
--- /dev/null
+++ b/testapi/opnfv_testapi/tests/unit/common/nosection.ini
@@ -0,0 +1,11 @@
+# to add a new parameter in the config file,
+# the CONF object in config.ini must be updated
+[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/common/notboolean.ini b/testapi/opnfv_testapi/tests/unit/common/notboolean.ini
new file mode 100644
index 0000000..b3f3276
--- /dev/null
+++ b/testapi/opnfv_testapi/tests/unit/common/notboolean.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 = notboolean
+
+[swagger]
+base_url = http://localhost:8000
diff --git a/testapi/opnfv_testapi/tests/unit/common/notint.ini b/testapi/opnfv_testapi/tests/unit/common/notint.ini
new file mode 100644
index 0000000..d1b752a
--- /dev/null
+++ b/testapi/opnfv_testapi/tests/unit/common/notint.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 = notint
+# 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/common/test_config.py b/testapi/opnfv_testapi/tests/unit/common/test_config.py
new file mode 100644
index 0000000..aaff6bb
--- /dev/null
+++ b/testapi/opnfv_testapi/tests/unit/common/test_config.py
@@ -0,0 +1,36 @@
+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)
+ 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.swagger_base_url == 'http://localhost:8000'