summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJing Lu <lvjing5@huawei.com>2017-01-19 07:02:38 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-01-19 07:02:38 +0000
commitcd36791a022aefc54eb258d62037274f7065a399 (patch)
treea8d67b03a8b776217d9027f66124dfdfaccba7fd
parentea706beda50f87ae37799bd8f0313d177909a4ff (diff)
parent483e2fcf41adcdddad5543c04d3ad42c60def334 (diff)
Merge "Add unittest framework for Yardstick API"
-rw-r--r--api/conf.py2
-rw-r--r--api/database/__init__.py4
-rw-r--r--api/server.py2
-rw-r--r--tests/unit/apiserver/__init__.py35
-rw-r--r--tests/unit/apiserver/resources/__init__.py0
-rw-r--r--tests/unit/apiserver/resources/test_env_action.py32
-rw-r--r--tests/unit/apiserver/utils/test_common.py (renamed from tests/unit/api/utils/test_common.py)0
-rw-r--r--tests/unit/apiserver/utils/test_influx.py (renamed from tests/unit/api/utils/test_influx.py)0
-rw-r--r--yardstick/common/constants.py2
9 files changed, 73 insertions, 4 deletions
diff --git a/api/conf.py b/api/conf.py
index abaf34a1f..a4f332533 100644
--- a/api/conf.py
+++ b/api/conf.py
@@ -15,8 +15,6 @@ with IPDB() as ip:
GATEWAY_IP = ip.routes['default'].gateway
PORT = 8086
-TEST_ACTION = ['runTestCase']
-
TEST_CASE_PATH = '../tests/opnfv/test_cases/'
SAMPLE_PATH = '../samples/'
diff --git a/api/database/__init__.py b/api/database/__init__.py
index d7cf4f9c4..753b34684 100644
--- a/api/database/__init__.py
+++ b/api/database/__init__.py
@@ -13,10 +13,12 @@ from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
+from yardstick.common import constants as consts
+
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
-engine = create_engine('sqlite:////tmp/yardstick.db', convert_unicode=True)
+engine = create_engine(consts.SQLITE, convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
diff --git a/api/server.py b/api/server.py
index 5bac1ba47..be7963481 100644
--- a/api/server.py
+++ b/api/server.py
@@ -52,7 +52,6 @@ def init_db():
Base.metadata.create_all(bind=engine)
-init_db()
reduce(lambda a, b: a.add_resource(b.resource, b.url,
endpoint=b.endpoint) or a, urlpatterns, api)
@@ -60,4 +59,5 @@ if __name__ == '__main__':
_init_logging()
logger.setLevel(logging.DEBUG)
logger.info('Starting server')
+ init_db()
app.run(host='0.0.0.0')
diff --git a/tests/unit/apiserver/__init__.py b/tests/unit/apiserver/__init__.py
new file mode 100644
index 000000000..021415296
--- /dev/null
+++ b/tests/unit/apiserver/__init__.py
@@ -0,0 +1,35 @@
+from __future__ import absolute_import
+
+import os
+import unittest
+import tempfile
+
+from oslo_serialization import jsonutils
+
+from yardstick.common import constants as consts
+
+
+class APITestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.db_fd, self.db_path = tempfile.mkstemp()
+ consts.SQLITE = 'sqlite:///{}'.format(self.db_path)
+ from api import server
+
+ server.app.config['TESTING'] = True
+ self.app = server.app.test_client()
+
+ server.init_db()
+
+ def tearDown(self):
+ os.close(self.db_fd)
+ os.unlink(self.db_path)
+
+ def _post(self, url, data):
+ headers = {'Content-Type': 'application/json'}
+ resp = self.app.post(url, data=jsonutils.dumps(data), headers=headers)
+ return jsonutils.loads(resp.data)
+
+ def _get(self, url):
+ resp = self.app.get(url)
+ return jsonutils.loads(resp.data)
diff --git a/tests/unit/apiserver/resources/__init__.py b/tests/unit/apiserver/resources/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/unit/apiserver/resources/__init__.py
diff --git a/tests/unit/apiserver/resources/test_env_action.py b/tests/unit/apiserver/resources/test_env_action.py
new file mode 100644
index 000000000..e8f99b706
--- /dev/null
+++ b/tests/unit/apiserver/resources/test_env_action.py
@@ -0,0 +1,32 @@
+from __future__ import absolute_import
+
+import time
+import unittest
+
+from tests.unit.apiserver import APITestCase
+
+
+class EnvTestCase(APITestCase):
+
+ def test_create_grafana(self):
+ url = 'yardstick/env/action'
+ data = dict(action='createGrafanaContainer')
+ resp = self._post(url, data)
+
+ time.sleep(1)
+
+ task_id = resp['result']['task_id']
+ url = '/yardstick/asynctask?task_id={}'.format(task_id)
+ resp = self._get(url)
+
+ time.sleep(2)
+
+ self.assertTrue(u'status' in resp)
+
+
+def main():
+ unittest.main()
+
+
+if __name__ == '__main__':
+ main()
diff --git a/tests/unit/api/utils/test_common.py b/tests/unit/apiserver/utils/test_common.py
index acf6e41b1..acf6e41b1 100644
--- a/tests/unit/api/utils/test_common.py
+++ b/tests/unit/apiserver/utils/test_common.py
diff --git a/tests/unit/api/utils/test_influx.py b/tests/unit/apiserver/utils/test_influx.py
index aff0cab5c..aff0cab5c 100644
--- a/tests/unit/api/utils/test_influx.py
+++ b/tests/unit/apiserver/utils/test_influx.py
diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py
index d99e216f4..ffca4b3e9 100644
--- a/yardstick/common/constants.py
+++ b/yardstick/common/constants.py
@@ -55,3 +55,5 @@ OPENSTACK_RC_FILE = join(YARDSTICK_CONFIG_DIR, 'openstack.creds')
BASE_URL = 'http://localhost:5000'
ENV_ACTION_API = BASE_URL + '/yardstick/env/action'
ASYNC_TASK_API = BASE_URL + '/yardstick/asynctask'
+
+SQLITE = 'sqlite:////tmp/yardstick.db'