blob: 52447148507119f16b7c3d6e225da73324b8ac0e [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
A R Karthicka974c212017-07-17 19:47:12 -070017from webserver import app
18from flask import request, jsonify
19import httplib
20import json
21import os
22import sys
A R Karthick78669922017-07-18 12:35:03 -070023import copy
24
25class CordTesterRun(object):
26 our_path = os.path.dirname(os.path.realpath(__file__))
27 exec_base = os.path.realpath(os.path.join(our_path, '..'))
28
29 @classmethod
30 def start(cls, manifest):
31 status = False
32 manifest_file = os.path.join(cls.exec_base, manifest)
33 if os.access(manifest_file, os.F_OK):
34 cmd = 'sudo {}/cord-test.py setup -m {}'.format(cls.exec_base, manifest_file)
35 ret = os.system(cmd)
36 status = True if ret == 0 else False
37
38 return status
39
40 @classmethod
41 def cleanup(cls, manifest):
42 status = False
43 manifest_file = os.path.join(cls.exec_base, manifest)
44 if os.access(manifest_file, os.F_OK):
45 cmd = 'sudo {}/cord-test.py cleanup -m {}'.format(cls.exec_base, manifest_file)
46 os.system(cmd)
47 status = True
48
49 return status
50
51 @classmethod
52 def test(cls, manifest, test, config = None):
53 manifest_file = os.path.join(cls.exec_base, manifest)
54 if not os.access(manifest_file, os.F_OK):
55 return False
56 #get test case as we could give a specific test to execute within a test case
57 test_case = test.split(':')[0]
58 cordWeb = CordTesterWebConfig(test_case)
59 if config:
60 status = cordWeb.update(config)
61 #test case is invalid
62 if status is False:
63 return status
64 cmd = 'sudo {}/cord-test.py run -m {} -t {}'.format(cls.exec_base, manifest_file, test)
65 ret = os.system(cmd)
66 status = True if ret == 0 else False
67 if config:
68 cordWeb.restore()
69 return status
A R Karthicka974c212017-07-17 19:47:12 -070070
71class CordTesterWebConfig(object):
72 our_path = os.path.dirname(os.path.realpath(__file__))
73 test_base = os.path.realpath(os.path.join(our_path, '..', '..'))
A R Karthick78669922017-07-18 12:35:03 -070074 restore_config = {}
A R Karthicka974c212017-07-17 19:47:12 -070075
76 def __init__(self, test_case):
77 self.test_case = test_case
78 self.test_path = None
79 self.test_config = None
80 test_path = os.path.join(self.test_base, self.test_case)
81 if os.path.isdir(test_path):
82 self.test_path = test_path
83 self.test_config = os.path.join(self.test_path, '{}Test.json'.format(self.test_case))
84
85 def update(self, config):
86 cur_config = {}
87 if self.test_config:
88 if os.access(self.test_config, os.F_OK):
89 with open(self.test_config, 'r') as f:
90 cur_config = json.load(f)
A R Karthick78669922017-07-18 12:35:03 -070091 self.save(copy.copy(cur_config))
A R Karthicka974c212017-07-17 19:47:12 -070092 for k, v in config.iteritems():
93 cur_config[k] = v
94 with open(self.test_config, 'w') as f:
95 json.dump(cur_config, f, indent = 4)
96 return True
97 return False
98
A R Karthick78669922017-07-18 12:35:03 -070099 def save(self, cur_config):
100 self.restore_config[self.test_case] = cur_config
101
A R Karthicka974c212017-07-17 19:47:12 -0700102 def restore(self):
A R Karthick78669922017-07-18 12:35:03 -0700103 config = None
A R Karthicka974c212017-07-17 19:47:12 -0700104 if self.test_config:
A R Karthick78669922017-07-18 12:35:03 -0700105 if self.test_case in self.restore_config:
106 config = self.restore_config[self.test_case]
107 with open(self.test_config, 'w') as f:
108 json.dump(config, f, indent = 4)
A R Karthicka974c212017-07-17 19:47:12 -0700109 return True
A R Karthick78669922017-07-18 12:35:03 -0700110
A R Karthicka974c212017-07-17 19:47:12 -0700111 return False
112
113 def get(self):
114 cur_config = {}
115 if self.test_config:
116 if os.access(self.test_config, os.F_OK):
117 with open(self.test_config) as f:
118 cur_config = json.load(f)
119 return cur_config
120
121@app.route('/')
122@app.route('/index')
123def index():
124 return 'Welcome to Cord Tester configuration page'
125
126@app.route('/get')
127def get():
128 data = request.get_json(force = True)
129 test_case = data.get('test_case', None)
130 if test_case:
131 cordWeb = CordTesterWebConfig(test_case)
132 config = cordWeb.get()
133 return jsonify(config)
134 return ('', httplib.NOT_FOUND)
135
136@app.route('/update', methods = ['POST'])
137def update():
138 data = request.get_json(force = True)
139 test_case = data.get('test_case', None)
140 config = data.get('config', None)
141 response = ('', httplib.NOT_FOUND)
142 if test_case:
143 cordWeb = CordTesterWebConfig(test_case)
144 status = cordWeb.update(config)
145 if status:
146 response = ('', httplib.OK)
147
148 return response
149
150@app.route('/restore', methods = ['POST'])
151def restore():
152 data = request.get_json(force = True)
153 test_case = data.get('test_case', None)
154 response = ('', httplib.NOT_FOUND)
155 if test_case:
156 cordWeb = CordTesterWebConfig(test_case)
157 status = cordWeb.restore()
158 if status:
159 response = ('', httplib.OK)
160 return response
A R Karthick78669922017-07-18 12:35:03 -0700161
162@app.route('/start', methods = ['POST'])
163def start():
164 data = request.get_json(force = True)
165 manifest = data.get('manifest', 'manifest.json')
166 status = CordTesterRun.start(manifest)
167 if status:
168 return ('', httplib.OK)
169 return ('', httplib.NOT_ACCEPTABLE)
170
171@app.route('/cleanup', methods = ['POST'])
172def cleanup():
173 data = request.get_json(force = True)
174 manifest = data.get('manifest', 'manifest.json')
175 status = CordTesterRun.cleanup(manifest)
176 if status:
177 return ('', httplib.OK)
178 return ('', httplib.NOT_ACCEPTABLE)
179
180@app.route('/test', methods = ['POST'])
181def test():
182 data = request.get_json(force = True)
183 manifest = data.get('manifest', 'manifest.json')
184 test = data.get('test', None)
185 config = data.get('config', None)
186 if test is None:
187 return ('', httplib.NOT_FOUND)
188 status = CordTesterRun.test(manifest, test, config = config)
189 if status:
190 return ('', httplib.OK)
191 return ('', httplib.NOT_ACCEPTABLE)