Add API(v2) to create test suite 29/37829/1
authorchenjiankun <chenjiankun1@huawei.com>
Thu, 20 Jul 2017 04:04:40 +0000 (04:04 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Thu, 20 Jul 2017 04:04:40 +0000 (04:04 +0000)
JIRA: YARDSTICK-746

API: /api/v2/yardstick/testsuites/action
METHOD: POST
PARAMS:
{
    'action': 'create_suite',
    'args': {
        'name': 'case_name',
        'testcases': [
            'opnfv_yardstick_tc002'
        ]
    }
}

Change-Id: I7e07c599e64e7eedda274c72344e394f5535f055
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
api/resources/v2/testsuites.py [new file with mode: 0644]

diff --git a/api/resources/v2/testsuites.py b/api/resources/v2/testsuites.py
new file mode 100644 (file)
index 0000000..9a3a07a
--- /dev/null
@@ -0,0 +1,44 @@
+import os
+import logging
+
+import yaml
+
+from api import ApiResource
+from yardstick.common.utils import result_handler
+from yardstick.common import constants as consts
+
+LOG = logging.getLogger(__name__)
+LOG.setLevel(logging.DEBUG)
+
+
+class V2Testsuites(ApiResource):
+
+    def post(self):
+        return self._dispatch_post()
+
+    def create_suite(self, args):
+        try:
+            suite_name = args['name']
+        except KeyError:
+            return result_handler(consts.API_ERROR, 'name must be provided')
+
+        try:
+            testcases = args['testcases']
+        except KeyError:
+            return result_handler(consts.API_ERROR, 'testcases must be provided')
+
+        testcases = [{'file_name': '{}.yaml'.format(t)} for t in testcases]
+
+        suite = os.path.join(consts.TESTSUITE_DIR, '{}.yaml'.format(suite_name))
+        suite_content = {
+            'schema': 'yardstick:suite:0.1',
+            'name': suite_name,
+            'test_cases_dir': 'tests/opnfv/test_cases/',
+            'test_cases': testcases
+        }
+
+        LOG.info('write test suite')
+        with open(suite, 'w') as f:
+            yaml.dump(suite_content, f, default_flow_style=False)
+
+        return result_handler(consts.API_SUCCESS, {'suite': suite_name})