aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2016-12-12 17:32:47 +0800
committerYujun Zhang <zhang.yujunz@zte.com.cn>2016-12-13 08:16:15 +0000
commit84666edf145dd3a5bb27f1038b704fe1682f451b (patch)
treee7f6a5abb2c2c6481f85f877072cdb690fddee1c
parentbef252222c8f420bc7eae25743d37dcc1745117b (diff)
refactor qtip-server structure
JIRA: QTIP-186 Change-Id: I4ecdcdc2869de0ef717ebf4bd1346e7c428b5b19 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
-rw-r--r--qtip/api/cmd/__init__.py0
-rw-r--r--qtip/api/cmd/server.py30
-rw-r--r--qtip/api/handler/__init__.py0
-rw-r--r--qtip/api/handler/db.py (renamed from qtip/api/db.py)0
-rw-r--r--qtip/api/handler/job_handler.py (renamed from qtip/api/qtip_server.py)52
-rw-r--r--qtip/api/handler/result_handler.py (renamed from qtip/api/result_handler.py)0
-rw-r--r--qtip/api/model/__init__.py0
-rw-r--r--qtip/api/model/job_model.py25
-rw-r--r--qtip/api/router/__init__.py0
-rw-r--r--qtip/api/router/mapper.py7
-rw-r--r--tests/unit/api/test_server.py (renamed from tests/unit/api/qtip_server_test.py)16
11 files changed, 79 insertions, 51 deletions
diff --git a/qtip/api/cmd/__init__.py b/qtip/api/cmd/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/api/cmd/__init__.py
diff --git a/qtip/api/cmd/server.py b/qtip/api/cmd/server.py
new file mode 100644
index 00000000..852073a7
--- /dev/null
+++ b/qtip/api/cmd/server.py
@@ -0,0 +1,30 @@
+##############################################################################
+# Copyright (c) 2016 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+from flask import Flask
+from flask_restful import Api
+from flask_restful_swagger import swagger
+import qtip.api.router.mapper as mapper
+
+app = Flask(__name__)
+api = swagger.docs(Api(app), apiVersion='0.1')
+
+
+def add_routers():
+ for (handler, url) in mapper.mappers:
+ api.add_resource(handler, url)
+
+
+def main():
+ add_routers()
+ app.run(host='0.0.0.0')
+
+
+if __name__ == "__main__":
+ main()
diff --git a/qtip/api/handler/__init__.py b/qtip/api/handler/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/api/handler/__init__.py
diff --git a/qtip/api/db.py b/qtip/api/handler/db.py
index 24fc27a5..24fc27a5 100644
--- a/qtip/api/db.py
+++ b/qtip/api/handler/db.py
diff --git a/qtip/api/qtip_server.py b/qtip/api/handler/job_handler.py
index e2ee0d27..f230e596 100644
--- a/qtip/api/qtip_server.py
+++ b/qtip/api/handler/job_handler.py
@@ -1,46 +1,13 @@
-##############################################################################
-# Copyright (c) 2016 ZTE Corp and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-from flask import Flask, abort
-from flask_restful import Api, Resource, fields, reqparse
-from flask_restful_swagger import swagger
import threading
from copy import copy
-import db
-import qtip.utils.args_handler as args_handler
-import qtip.api.result_handler as result_handler
+from flask.ext.restful import Resource, reqparse
+from flask.ext.restful_swagger import swagger
+from werkzeug.exceptions import abort
-app = Flask(__name__)
-api = swagger.docs(Api(app), apiVersion='0.1')
-
-
-@swagger.model
-class JobModel:
- resource_fields = {
- 'installer_type': fields.String,
- 'installer_ip': fields.String,
- 'max_minutes': fields.Integer,
- 'pod_name': fields.String,
- 'suite_name': fields.String,
- 'type': fields.String,
- 'benchmark_name': fields.String,
- 'testdb_url': fields.String,
- 'node_name': fields.String
- }
- required = ['installer_type', 'installer_ip']
-
-
-@swagger.model
-class JobResponseModel:
- resource_fields = {
- 'job_id': fields.String
- }
+from qtip.api.handler import db, result_handler
+from qtip.api.model.job_model import JobResponseModel
+from qtip.utils import args_handler as args_handler
class Job(Resource):
@@ -197,10 +164,3 @@ default is all benchmarks in suite with specified type,
if (result_handler.dump_suite_result(suite_name) and testdb_url):
result_handler.push_suite_result_to_db(suite_name, testdb_url, installer_type, node_name)
db.finish_job(job_id)
-
-
-api.add_resource(JobList, '/api/v1.0/jobs')
-api.add_resource(Job, '/api/v1.0/jobs/<string:id>')
-
-if __name__ == "__main__":
- app.run(host='0.0.0.0')
diff --git a/qtip/api/result_handler.py b/qtip/api/handler/result_handler.py
index de91cd2c..de91cd2c 100644
--- a/qtip/api/result_handler.py
+++ b/qtip/api/handler/result_handler.py
diff --git a/qtip/api/model/__init__.py b/qtip/api/model/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/api/model/__init__.py
diff --git a/qtip/api/model/job_model.py b/qtip/api/model/job_model.py
new file mode 100644
index 00000000..eef771b0
--- /dev/null
+++ b/qtip/api/model/job_model.py
@@ -0,0 +1,25 @@
+from flask.ext.restful import fields
+from flask.ext.restful_swagger import swagger
+
+
+@swagger.model
+class JobModel:
+ resource_fields = {
+ 'installer_type': fields.String,
+ 'installer_ip': fields.String,
+ 'max_minutes': fields.Integer,
+ 'pod_name': fields.String,
+ 'suite_name': fields.String,
+ 'type': fields.String,
+ 'benchmark_name': fields.String,
+ 'testdb_url': fields.String,
+ 'node_name': fields.String
+ }
+ required = ['installer_type', 'installer_ip']
+
+
+@swagger.model
+class JobResponseModel:
+ resource_fields = {
+ 'job_id': fields.String
+ }
diff --git a/qtip/api/router/__init__.py b/qtip/api/router/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/api/router/__init__.py
diff --git a/qtip/api/router/mapper.py b/qtip/api/router/mapper.py
new file mode 100644
index 00000000..a5f029ac
--- /dev/null
+++ b/qtip/api/router/mapper.py
@@ -0,0 +1,7 @@
+from qtip.api.handler.job_handler import Job, JobList
+
+
+mappers = [
+ (JobList, '/api/v1.0/jobs'),
+ (Job, '/api/v1.0/jobs/<string:id>'),
+]
diff --git a/tests/unit/api/qtip_server_test.py b/tests/unit/api/test_server.py
index 96544c95..e9364d3d 100644
--- a/tests/unit/api/qtip_server_test.py
+++ b/tests/unit/api/test_server.py
@@ -1,9 +1,15 @@
-import qtip.api.qtip_server as server
-import pytest
import json
-import mock
import time
+import mock
+import pytest
+
+import qtip.api.cmd.server as server
+
+
+def setup_module():
+ server.add_routers()
+
@pytest.fixture
def app():
@@ -66,7 +72,7 @@ class TestClass:
'state_detail': [{u'state': u'finished', u'benchmark': u'dhrystone_vm.yaml'}],
'result': 0})
])
- @mock.patch('qtip.api.qtip_server.args_handler.prepare_and_run_benchmark')
+ @mock.patch('qtip.utils.args_handler.prepare_and_run_benchmark')
def test_post_get_delete_job_successful(self, mock_args_handler, app_client, body, expected):
mock_args_handler.return_value = {'result': 0,
'detail': {'host': [(u'10.20.6.14', {'unreachable': 0,
@@ -107,7 +113,7 @@ class TestClass:
['job_id',
'It already has one job running now!'])
])
- @mock.patch('qtip.api.qtip_server.args_handler.prepare_and_run_benchmark',
+ @mock.patch('qtip.utils.args_handler.prepare_and_run_benchmark',
side_effect=[side_effect_sleep(0.5), side_effect_pass])
def test_post_two_jobs_unsuccessful(self, mock_args_hanler, app_client, body, expected):
reply_1 = app_client.post("/api/v1.0/jobs", data=body[0])