A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 1 | from webserver import app |
| 2 | from flask import request, jsonify |
| 3 | import httplib |
| 4 | import json |
| 5 | import os |
| 6 | import sys |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 7 | import copy |
| 8 | |
| 9 | class CordTesterRun(object): |
| 10 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 11 | exec_base = os.path.realpath(os.path.join(our_path, '..')) |
| 12 | |
| 13 | @classmethod |
| 14 | def start(cls, manifest): |
| 15 | status = False |
| 16 | manifest_file = os.path.join(cls.exec_base, manifest) |
| 17 | if os.access(manifest_file, os.F_OK): |
| 18 | cmd = 'sudo {}/cord-test.py setup -m {}'.format(cls.exec_base, manifest_file) |
| 19 | ret = os.system(cmd) |
| 20 | status = True if ret == 0 else False |
| 21 | |
| 22 | return status |
| 23 | |
| 24 | @classmethod |
| 25 | def cleanup(cls, manifest): |
| 26 | status = False |
| 27 | manifest_file = os.path.join(cls.exec_base, manifest) |
| 28 | if os.access(manifest_file, os.F_OK): |
| 29 | cmd = 'sudo {}/cord-test.py cleanup -m {}'.format(cls.exec_base, manifest_file) |
| 30 | os.system(cmd) |
| 31 | status = True |
| 32 | |
| 33 | return status |
| 34 | |
| 35 | @classmethod |
| 36 | def test(cls, manifest, test, config = None): |
| 37 | manifest_file = os.path.join(cls.exec_base, manifest) |
| 38 | if not os.access(manifest_file, os.F_OK): |
| 39 | return False |
| 40 | #get test case as we could give a specific test to execute within a test case |
| 41 | test_case = test.split(':')[0] |
| 42 | cordWeb = CordTesterWebConfig(test_case) |
| 43 | if config: |
| 44 | status = cordWeb.update(config) |
| 45 | #test case is invalid |
| 46 | if status is False: |
| 47 | return status |
| 48 | cmd = 'sudo {}/cord-test.py run -m {} -t {}'.format(cls.exec_base, manifest_file, test) |
| 49 | ret = os.system(cmd) |
| 50 | status = True if ret == 0 else False |
| 51 | if config: |
| 52 | cordWeb.restore() |
| 53 | return status |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 54 | |
| 55 | class CordTesterWebConfig(object): |
| 56 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 57 | test_base = os.path.realpath(os.path.join(our_path, '..', '..')) |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 58 | restore_config = {} |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 59 | |
| 60 | def __init__(self, test_case): |
| 61 | self.test_case = test_case |
| 62 | self.test_path = None |
| 63 | self.test_config = None |
| 64 | test_path = os.path.join(self.test_base, self.test_case) |
| 65 | if os.path.isdir(test_path): |
| 66 | self.test_path = test_path |
| 67 | self.test_config = os.path.join(self.test_path, '{}Test.json'.format(self.test_case)) |
| 68 | |
| 69 | def update(self, config): |
| 70 | cur_config = {} |
| 71 | if self.test_config: |
| 72 | if os.access(self.test_config, os.F_OK): |
| 73 | with open(self.test_config, 'r') as f: |
| 74 | cur_config = json.load(f) |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 75 | self.save(copy.copy(cur_config)) |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 76 | for k, v in config.iteritems(): |
| 77 | cur_config[k] = v |
| 78 | with open(self.test_config, 'w') as f: |
| 79 | json.dump(cur_config, f, indent = 4) |
| 80 | return True |
| 81 | return False |
| 82 | |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 83 | def save(self, cur_config): |
| 84 | self.restore_config[self.test_case] = cur_config |
| 85 | |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 86 | def restore(self): |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 87 | config = None |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 88 | if self.test_config: |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 89 | if self.test_case in self.restore_config: |
| 90 | config = self.restore_config[self.test_case] |
| 91 | with open(self.test_config, 'w') as f: |
| 92 | json.dump(config, f, indent = 4) |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 93 | return True |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 94 | |
A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame] | 95 | return False |
| 96 | |
| 97 | def get(self): |
| 98 | cur_config = {} |
| 99 | if self.test_config: |
| 100 | if os.access(self.test_config, os.F_OK): |
| 101 | with open(self.test_config) as f: |
| 102 | cur_config = json.load(f) |
| 103 | return cur_config |
| 104 | |
| 105 | @app.route('/') |
| 106 | @app.route('/index') |
| 107 | def index(): |
| 108 | return 'Welcome to Cord Tester configuration page' |
| 109 | |
| 110 | @app.route('/get') |
| 111 | def get(): |
| 112 | data = request.get_json(force = True) |
| 113 | test_case = data.get('test_case', None) |
| 114 | if test_case: |
| 115 | cordWeb = CordTesterWebConfig(test_case) |
| 116 | config = cordWeb.get() |
| 117 | return jsonify(config) |
| 118 | return ('', httplib.NOT_FOUND) |
| 119 | |
| 120 | @app.route('/update', methods = ['POST']) |
| 121 | def update(): |
| 122 | data = request.get_json(force = True) |
| 123 | test_case = data.get('test_case', None) |
| 124 | config = data.get('config', None) |
| 125 | response = ('', httplib.NOT_FOUND) |
| 126 | if test_case: |
| 127 | cordWeb = CordTesterWebConfig(test_case) |
| 128 | status = cordWeb.update(config) |
| 129 | if status: |
| 130 | response = ('', httplib.OK) |
| 131 | |
| 132 | return response |
| 133 | |
| 134 | @app.route('/restore', methods = ['POST']) |
| 135 | def restore(): |
| 136 | data = request.get_json(force = True) |
| 137 | test_case = data.get('test_case', None) |
| 138 | response = ('', httplib.NOT_FOUND) |
| 139 | if test_case: |
| 140 | cordWeb = CordTesterWebConfig(test_case) |
| 141 | status = cordWeb.restore() |
| 142 | if status: |
| 143 | response = ('', httplib.OK) |
| 144 | return response |
A R Karthick | 7866992 | 2017-07-18 12:35:03 -0700 | [diff] [blame] | 145 | |
| 146 | @app.route('/start', methods = ['POST']) |
| 147 | def start(): |
| 148 | data = request.get_json(force = True) |
| 149 | manifest = data.get('manifest', 'manifest.json') |
| 150 | status = CordTesterRun.start(manifest) |
| 151 | if status: |
| 152 | return ('', httplib.OK) |
| 153 | return ('', httplib.NOT_ACCEPTABLE) |
| 154 | |
| 155 | @app.route('/cleanup', methods = ['POST']) |
| 156 | def cleanup(): |
| 157 | data = request.get_json(force = True) |
| 158 | manifest = data.get('manifest', 'manifest.json') |
| 159 | status = CordTesterRun.cleanup(manifest) |
| 160 | if status: |
| 161 | return ('', httplib.OK) |
| 162 | return ('', httplib.NOT_ACCEPTABLE) |
| 163 | |
| 164 | @app.route('/test', methods = ['POST']) |
| 165 | def test(): |
| 166 | data = request.get_json(force = True) |
| 167 | manifest = data.get('manifest', 'manifest.json') |
| 168 | test = data.get('test', None) |
| 169 | config = data.get('config', None) |
| 170 | if test is None: |
| 171 | return ('', httplib.NOT_FOUND) |
| 172 | status = CordTesterRun.test(manifest, test, config = config) |
| 173 | if status: |
| 174 | return ('', httplib.OK) |
| 175 | return ('', httplib.NOT_ACCEPTABLE) |