blob: 287d7a3ddbf0d472b0a21e3660195287fd09a266 [file] [log] [blame]
A R Karthick23c96072017-08-21 20:34:21 -07001#!/usr/bin/env python
2
3# Copyright 2017-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16from argparse import ArgumentParser
17import os
18import sys
19utils_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../utils')
20cli_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../cli')
21sys.path.append(utils_dir)
22sys.path.append(cli_dir)
23sys.path.insert(1, '/usr/local/lib/python2.7/dist-packages')
24from CordTestUtils import getstatusoutput
25import time
26import requests
27import httplib
28import json
29import signal
30
31class CordTesterWebClient(object):
32
33 def __init__(self, host = 'localhost', port = 5000):
34 self.host = host
35 self.port = port
36 self.rest = 'http://{}:{}'.format(self.host, self.port)
37
38 def get_config(self, test_case):
39 rest_uri = '{}/get'.format(self.rest)
40 config = { 'test_case' : test_case }
41 resp = requests.get(rest_uri, data = json.dumps(config))
42 if resp.ok and resp.status_code == 200:
43 config = resp.json()
44 return config
45 return None
46
47 def set_config(self, test_case, test_config):
48 rest_uri = '{}/update'.format(self.rest)
49 config = { 'test_case' : test_case, 'config' : test_config }
50 resp = requests.post(rest_uri, data = json.dumps(config))
51 return resp.ok, resp.status_code
52
53 def restore_config(self, test_case):
54 rest_uri = '{}/restore'.format(self.rest)
55 config = { 'test_case' : test_case }
56 resp = requests.post(rest_uri, data = json.dumps(config))
57 return resp.ok, resp.status_code
58
59 def start(self, manifest = 'manifest.json'):
60 rest_uri = '{}/start'.format(self.rest)
61 config = { 'manifest' : manifest }
62 resp = requests.post(rest_uri, data = json.dumps(config))
63 return resp.ok, resp.status_code
64
65 def cleanup(self, manifest = 'manifest.json'):
66 rest_uri = '{}/cleanup'.format(self.rest)
67 config = { 'manifest' : manifest }
68 resp = requests.post(rest_uri, data = json.dumps(config))
69 return resp.ok, resp.status_code
70
71 def test(self, test, manifest = 'manifest.json', test_config = None):
72 rest_uri = '{}/test'.format(self.rest)
73 config = { 'manifest' : manifest, 'test' : test }
74 if test_config:
75 config['config'] = test_config
76 resp = requests.post(rest_uri, data = json.dumps(config))
77 return resp.ok, resp.status_code
78
79class Tester(CordTesterWebClient):
80
81 def __init__(self, host = 'localhost', port = 5000):
82 super(Tester, self).__init__(host = host, port = port)
83
84 def execute(self, test_case, manifest = 'manifest.json', test_config = None):
85 print('Executing test %s' %test_case)
86 _, status = self.start(manifest = manifest)
87 assert status == httplib.OK, 'Test setup failed with status code %d' %status
88 _, status = self.test(test_case, manifest = manifest, test_config = test_config)
89 assert status == httplib.OK, 'Test run for test %s failed with status %d' %(test_case, status)
90 _, status = self.cleanup(manifest = manifest)
91 assert status == httplib.OK, 'Test cleanup failed with status %d' %status
92 print('Test executed successfully')
93
94
95class CordTesterWeb(object):
96
97 def __init__(self, args, start_in = 3):
98 self.args = args
99 self.tester = Tester()
100 self.start_in = start_in
101
102 def run(self):
103 manifest = self.args.manifest
104 olt_type = self.args.olt_type
105 test_type = self.args.test_type
106 test_config = { 'VOLTHA_HOST' : self.args.voltha_host,
107 'VOLTHA_OLT_TYPE' : self.args.olt_type,
108 }
109 if olt_type.startswith('tibit'):
110 test_config['VOLTHA_OLT_MAC'] = self.args.olt_arg
111 elif olt_type.startswith('maple'):
112 test_config['VOLTHA_OLT_IP'] = self.args.olt_arg
113 elif olt_type.startswith('ponsim'):
114 test_config['VOLTHA_PONSIM_HOST'] = self.args.olt_arg
115 else:
116 print('Unsupported OLT type %s' %olt_type)
117 return 127
118
119 if self.start_in:
120 time.sleep(self.start_in)
121
122 _, status = self.tester.start(manifest = manifest)
123 assert status == httplib.OK, 'Test setup failed with status %d' %status
124
125 for test in test_type.split(','):
126 print('Running test case %s' %(test))
127 _, status = self.tester.test(test, manifest = manifest, test_config = test_config)
128 if status != httplib.OK:
129 print('Test case %s failed with status code %d' %(test, status))
130
131 print('Cleaning up the test')
132 self.tester.cleanup(manifest = manifest)
133 return 0 if status == httplib.OK else 127
134
135class CordTesterWebServer(object):
136
137 server_path = os.path.dirname(os.path.realpath(__file__))
138 server = 'webserver-run.py'
139 pattern = 'pgrep -f "python ./{}"'.format(server)
140
141 def running(self):
142 st, _ = getstatusoutput(self.pattern)
143 return True if st == 0 else False
144
145 def kill(self):
146 st, output = getstatusoutput(self.pattern)
147 if st == 0 and output:
148 pids = output.strip().splitlines()
149 for pid in pids:
150 try:
151 os.kill(int(pid), signal.SIGKILL)
152 except:
153 pass
154
155 def start(self):
156 if self.running() is False:
157 print('Starting CordTester Web Server')
158 cmd = 'cd {} && python ./{} &'.format(self.server_path, self.server)
159 os.system(cmd)
160
161def run_test(args):
162 testWebServer = CordTesterWebServer()
163 testWebServer.start()
164 testWeb = CordTesterWeb(args, start_in = 3)
165 status = testWeb.run()
166 testWebServer.kill()
167 return status
168
169if __name__ == '__main__':
170 parser = ArgumentParser(description = 'Olt tester')
171 parser.add_argument('-test-type', '--test-type', default = 'tls:eap_auth_exchange.test_eap_tls', help = 'Test type to run')
172 parser.add_argument('-manifest', '--manifest', default='manifest-voltha.json', help = 'Manifest file to use')
173 parser.add_argument('-voltha-host', '--voltha-host', default='172.17.0.1', help = 'VOLTHA host ip')
174 parser.add_argument('-olt-type', '--olt-type', default = 'ponsim_olt', help = 'OLT type')
175 parser.add_argument('-olt-arg', '--olt-arg', default = '172.17.0.1', help = 'OLT type argument')
176 parser.set_defaults(func = run_test)
177 args = parser.parse_args()
178 res = args.func(args)
179 sys.exit(res)