Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame^] | 1 | |
| 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 Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 17 | from webserver import app |
| 18 | from flask import request, jsonify |
| 19 | import httplib |
| 20 | import json |
| 21 | import os |
| 22 | import sys |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 23 | import copy |
| 24 | |
| 25 | class 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 Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 70 | |
| 71 | class 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 Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 74 | restore_config = {} |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 75 | |
| 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 Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 91 | self.save(copy.copy(cur_config)) |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 92 | 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 Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 99 | def save(self, cur_config): |
| 100 | self.restore_config[self.test_case] = cur_config |
| 101 | |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 102 | def restore(self): |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 103 | config = None |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 104 | if self.test_config: |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 105 | 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 Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 109 | return True |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 110 | |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 111 | 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') |
| 123 | def index(): |
| 124 | return 'Welcome to Cord Tester configuration page' |
| 125 | |
| 126 | @app.route('/get') |
| 127 | def 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']) |
| 137 | def 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']) |
| 151 | def 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 Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 161 | |
| 162 | @app.route('/start', methods = ['POST']) |
| 163 | def 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']) |
| 172 | def 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']) |
| 181 | def 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) |