A R Karthick | 390b0bf | 2017-07-18 14:48:11 -0700 | [diff] [blame] | 1 | import requests |
| 2 | import json |
| 3 | import httplib |
| 4 | |
| 5 | class CordTesterWebClient(object): |
| 6 | |
| 7 | def __init__(self, host = 'localhost', port = 5000): |
| 8 | self.host = host |
| 9 | self.port = port |
| 10 | self.rest = 'http://{}:{}'.format(self.host, self.port) |
| 11 | |
| 12 | def get_config(self, test_case): |
| 13 | rest_uri = '{}/get'.format(self.rest) |
| 14 | config = { 'test_case' : test_case } |
| 15 | resp = requests.get(rest_uri, data = json.dumps(config)) |
| 16 | if resp.ok and resp.status_code == 200: |
| 17 | config = resp.json() |
| 18 | return config |
| 19 | return None |
| 20 | |
| 21 | def set_config(self, test_case, test_config): |
| 22 | rest_uri = '{}/update'.format(self.rest) |
| 23 | config = { 'test_case' : test_case, 'config' : test_config } |
| 24 | resp = requests.post(rest_uri, data = json.dumps(config)) |
| 25 | return resp.ok, resp.status_code |
| 26 | |
| 27 | def restore_config(self, test_case): |
| 28 | rest_uri = '{}/restore'.format(self.rest) |
| 29 | config = { 'test_case' : test_case } |
| 30 | resp = requests.post(rest_uri, data = json.dumps(config)) |
| 31 | return resp.ok, resp.status_code |
| 32 | |
| 33 | def start(self, manifest = 'manifest.json'): |
| 34 | rest_uri = '{}/start'.format(self.rest) |
| 35 | config = { 'manifest' : manifest } |
| 36 | resp = requests.post(rest_uri, data = json.dumps(config)) |
| 37 | return resp.ok, resp.status_code |
| 38 | |
| 39 | def cleanup(self, manifest = 'manifest.json'): |
| 40 | rest_uri = '{}/cleanup'.format(self.rest) |
| 41 | config = { 'manifest' : manifest } |
| 42 | resp = requests.post(rest_uri, data = json.dumps(config)) |
| 43 | return resp.ok, resp.status_code |
| 44 | |
| 45 | def test(self, test, manifest = 'manifest.json', test_config = None): |
| 46 | rest_uri = '{}/test'.format(self.rest) |
| 47 | config = { 'manifest' : manifest, 'test' : test } |
| 48 | if test_config: |
| 49 | config['config'] = test_config |
| 50 | resp = requests.post(rest_uri, data = json.dumps(config)) |
| 51 | return resp.ok, resp.status_code |
| 52 | |
| 53 | class Tester(CordTesterWebClient): |
| 54 | |
| 55 | def __init__(self, host = 'localhost', port = 5000): |
| 56 | super(Tester, self).__init__(host = host, port = port) |
| 57 | |
| 58 | def execute(self, test_case, manifest = 'manifest.json', test_config = None): |
| 59 | print('Executing test %s' %test_case) |
| 60 | _, status = self.start(manifest = manifest) |
| 61 | assert status == httplib.OK, 'Test setup failed with status code %d' %status |
| 62 | _, status = self.test(test_case, manifest = manifest, test_config = test_config) |
| 63 | assert status == httplib.OK, 'Test run for test %s failed with status %d' %(test_case, status) |
| 64 | _, status = self.cleanup(manifest = manifest) |
| 65 | assert status == httplib.OK, 'Test cleanup failed with status %d' %status |
| 66 | print('Test executed successfully') |
| 67 | |
| 68 | if __name__ == '__main__': |
| 69 | tester = Tester() |
| 70 | tests = ('tls', 'igmp',) |
| 71 | for test in tests: |
| 72 | print('Getting config for test %s' %test) |
| 73 | config = tester.get_config(test) |
| 74 | print('%s' %config) |
| 75 | |
| 76 | tls_cfg = { 'VOLTHA_OLT_MAC' : '00:0c:e2:31:10:00' } |
| 77 | igmp_cfg = { 'PORT_RX_DEFAULT' : 1, 'PORT_TX_DEFAULT' : 2, 'IGMP_TEST_TIMEOUT' : 10 } |
| 78 | manifest = 'manifest-ponsim.json' |
| 79 | tests = ( ('tls:eap_auth_exchange.test_eap_tls', tls_cfg, manifest), |
| 80 | ('igmp:igmp_exchange.test_igmp_join_verify_traffic', igmp_cfg, manifest), |
| 81 | ) |
| 82 | |
| 83 | print('Setting up the test with %s' %manifest) |
| 84 | _, status = tester.start(manifest = manifest) |
| 85 | assert status == httplib.OK, 'Test setup failed with status code %d' %status |
| 86 | |
| 87 | for t, cfg, m in tests: |
| 88 | _, status = tester.test(t, manifest = m, test_config = cfg) |
| 89 | if status != httplib.OK: |
| 90 | print('Test case %s failed with status code %d' %(t, status)) |
| 91 | else: |
| 92 | print('Test case %s executed successfully' %t) |
| 93 | |
| 94 | print('Cleaning up the test with %s' %manifest) |
| 95 | tester.cleanup(manifest = manifest) |