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