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 |
A R Karthick | 600563f | 2017-08-23 10:06:48 -0700 | [diff] [blame] | 24 | from collections import OrderedDict |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 25 | |
| 26 | class 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 Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 71 | |
| 72 | class 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 Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 75 | restore_config = {} |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 76 | |
| 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 Karthick | 600563f | 2017-08-23 10:06:48 -0700 | [diff] [blame] | 87 | cur_config = OrderedDict() |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 88 | 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 Karthick | 600563f | 2017-08-23 10:06:48 -0700 | [diff] [blame] | 91 | cur_config = json.load(f, object_pairs_hook = OrderedDict) |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 92 | self.save(copy.copy(cur_config)) |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 93 | for k, v in config.iteritems(): |
| 94 | cur_config[k] = v |
A R Karthick | 600563f | 2017-08-23 10:06:48 -0700 | [diff] [blame] | 95 | with open(self.test_config, 'w') as f: |
| 96 | json.dump(cur_config, f, indent = 4) |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 97 | return True |
| 98 | return False |
| 99 | |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 100 | def save(self, cur_config): |
| 101 | self.restore_config[self.test_case] = cur_config |
| 102 | |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 103 | def restore(self): |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 104 | config = None |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 105 | if self.test_config: |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 106 | 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 Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 110 | return True |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 111 | |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 112 | 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 Karthick | 600563f | 2017-08-23 10:06:48 -0700 | [diff] [blame] | 119 | cur_config = json.load(f, object_pairs_hook = OrderedDict) |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 120 | return cur_config |
| 121 | |
| 122 | @app.route('/') |
| 123 | @app.route('/index') |
| 124 | def index(): |
| 125 | return 'Welcome to Cord Tester configuration page' |
| 126 | |
| 127 | @app.route('/get') |
| 128 | def 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']) |
| 138 | def 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']) |
| 152 | def 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 Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 162 | |
| 163 | @app.route('/start', methods = ['POST']) |
| 164 | def 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']) |
| 173 | def 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']) |
| 182 | def 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) |