blob: bfe7c4c9f84db43002205a8d56342920c7688d5b [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
A R Karthick600563f2017-08-23 10:06:48 -070024from collections import OrderedDict
A R Karthick78669922017-07-18 12:35:03 -070025
26class CordTesterRun(object):
27 our_path = os.path.dirname(os.path.realpath(__file__))
28 exec_base = os.path.realpath(os.path.join(our_path, '..'))
29
30 @classmethod
31 def start(cls, manifest):
32 status = False
33 manifest_file = os.path.join(cls.exec_base, manifest)
34 if os.access(manifest_file, os.F_OK):
35 cmd = 'sudo {}/cord-test.py setup -m {}'.format(cls.exec_base, manifest_file)
36 ret = os.system(cmd)
37 status = True if ret == 0 else False
38
39 return status
40
41 @classmethod
42 def cleanup(cls, manifest):
43 status = False
44 manifest_file = os.path.join(cls.exec_base, manifest)
45 if os.access(manifest_file, os.F_OK):
46 cmd = 'sudo {}/cord-test.py cleanup -m {}'.format(cls.exec_base, manifest_file)
47 os.system(cmd)
48 status = True
49
50 return status
51
52 @classmethod
53 def test(cls, manifest, test, config = None):
54 manifest_file = os.path.join(cls.exec_base, manifest)
55 if not os.access(manifest_file, os.F_OK):
56 return False
57 #get test case as we could give a specific test to execute within a test case
58 test_case = test.split(':')[0]
59 cordWeb = CordTesterWebConfig(test_case)
60 if config:
61 status = cordWeb.update(config)
62 #test case is invalid
63 if status is False:
64 return status
65 cmd = 'sudo {}/cord-test.py run -m {} -t {}'.format(cls.exec_base, manifest_file, test)
66 ret = os.system(cmd)
67 status = True if ret == 0 else False
68 if config:
69 cordWeb.restore()
70 return status
A R Karthicka974c212017-07-17 19:47:12 -070071
72class CordTesterWebConfig(object):
73 our_path = os.path.dirname(os.path.realpath(__file__))
74 test_base = os.path.realpath(os.path.join(our_path, '..', '..'))
A R Karthick78669922017-07-18 12:35:03 -070075 restore_config = {}
A R Karthicka974c212017-07-17 19:47:12 -070076
77 def __init__(self, test_case):
78 self.test_case = test_case
79 self.test_path = None
80 self.test_config = None
81 test_path = os.path.join(self.test_base, self.test_case)
82 if os.path.isdir(test_path):
83 self.test_path = test_path
84 self.test_config = os.path.join(self.test_path, '{}Test.json'.format(self.test_case))
85
86 def update(self, config):
A R Karthick600563f2017-08-23 10:06:48 -070087 cur_config = OrderedDict()
A R Karthicka974c212017-07-17 19:47:12 -070088 if self.test_config:
89 if os.access(self.test_config, os.F_OK):
90 with open(self.test_config, 'r') as f:
A R Karthick600563f2017-08-23 10:06:48 -070091 cur_config = json.load(f, object_pairs_hook = OrderedDict)
A R Karthick78669922017-07-18 12:35:03 -070092 self.save(copy.copy(cur_config))
A R Karthicka974c212017-07-17 19:47:12 -070093 for k, v in config.iteritems():
94 cur_config[k] = v
A R Karthick600563f2017-08-23 10:06:48 -070095 with open(self.test_config, 'w') as f:
96 json.dump(cur_config, f, indent = 4)
A R Karthicka974c212017-07-17 19:47:12 -070097 return True
98 return False
99
A R Karthick78669922017-07-18 12:35:03 -0700100 def save(self, cur_config):
101 self.restore_config[self.test_case] = cur_config
102
A R Karthicka974c212017-07-17 19:47:12 -0700103 def restore(self):
A R Karthick78669922017-07-18 12:35:03 -0700104 config = None
A R Karthicka974c212017-07-17 19:47:12 -0700105 if self.test_config:
A R Karthick78669922017-07-18 12:35:03 -0700106 if self.test_case in self.restore_config:
107 config = self.restore_config[self.test_case]
108 with open(self.test_config, 'w') as f:
109 json.dump(config, f, indent = 4)
A R Karthicka974c212017-07-17 19:47:12 -0700110 return True
A R Karthick78669922017-07-18 12:35:03 -0700111
A R Karthicka974c212017-07-17 19:47:12 -0700112 return False
113
114 def get(self):
115 cur_config = {}
116 if self.test_config:
117 if os.access(self.test_config, os.F_OK):
118 with open(self.test_config) as f:
A R Karthick600563f2017-08-23 10:06:48 -0700119 cur_config = json.load(f, object_pairs_hook = OrderedDict)
A R Karthicka974c212017-07-17 19:47:12 -0700120 return cur_config
121
122@app.route('/')
123@app.route('/index')
124def index():
125 return 'Welcome to Cord Tester configuration page'
126
127@app.route('/get')
128def get():
129 data = request.get_json(force = True)
130 test_case = data.get('test_case', None)
131 if test_case:
132 cordWeb = CordTesterWebConfig(test_case)
133 config = cordWeb.get()
134 return jsonify(config)
135 return ('', httplib.NOT_FOUND)
136
137@app.route('/update', methods = ['POST'])
138def update():
139 data = request.get_json(force = True)
140 test_case = data.get('test_case', None)
141 config = data.get('config', None)
142 response = ('', httplib.NOT_FOUND)
143 if test_case:
144 cordWeb = CordTesterWebConfig(test_case)
145 status = cordWeb.update(config)
146 if status:
147 response = ('', httplib.OK)
148
149 return response
150
151@app.route('/restore', methods = ['POST'])
152def restore():
153 data = request.get_json(force = True)
154 test_case = data.get('test_case', None)
155 response = ('', httplib.NOT_FOUND)
156 if test_case:
157 cordWeb = CordTesterWebConfig(test_case)
158 status = cordWeb.restore()
159 if status:
160 response = ('', httplib.OK)
161 return response
A R Karthick78669922017-07-18 12:35:03 -0700162
163@app.route('/start', methods = ['POST'])
164def start():
165 data = request.get_json(force = True)
166 manifest = data.get('manifest', 'manifest.json')
167 status = CordTesterRun.start(manifest)
168 if status:
169 return ('', httplib.OK)
170 return ('', httplib.NOT_ACCEPTABLE)
171
172@app.route('/cleanup', methods = ['POST'])
173def cleanup():
174 data = request.get_json(force = True)
175 manifest = data.get('manifest', 'manifest.json')
176 status = CordTesterRun.cleanup(manifest)
177 if status:
178 return ('', httplib.OK)
179 return ('', httplib.NOT_ACCEPTABLE)
180
181@app.route('/test', methods = ['POST'])
182def test():
183 data = request.get_json(force = True)
184 manifest = data.get('manifest', 'manifest.json')
185 test = data.get('test', None)
186 config = data.get('config', None)
187 if test is None:
188 return ('', httplib.NOT_FOUND)
189 status = CordTesterRun.test(manifest, test, config = config)
190 if status:
191 return ('', httplib.OK)
192 return ('', httplib.NOT_ACCEPTABLE)