blob: e9784bb64e8b6288458459f5cfa0207769870e0f [file] [log] [blame]
Gilles Depatie84cb1e72018-10-26 12:41:33 -04001#
2# Copyright 2018 the original author or authors.
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
17"""
18vOLT-HA Test Case Utils module
19"""
20import time
Gilles Depatie84cb1e72018-10-26 12:41:33 -040021import commands
22import subprocess
23import pexpect
Gilles Depatie1be639b2018-12-06 10:51:08 -050024import sys
Gilles Depatie84cb1e72018-10-26 12:41:33 -040025
Gilles Depatie0bf31352019-02-04 13:48:41 -050026
27def config_dirs(self, log_dir, root_dir=None, voltha_dir=None):
28 self.dirs['log'] = log_dir
29 self.dirs['root'] = root_dir
30 self.dirs['voltha'] = voltha_dir
Gilles Depatie84cb1e72018-10-26 12:41:33 -040031
Gilles Depatie0bf31352019-02-04 13:48:41 -050032
33def get_dir(self, directory):
34 return self.dirs.get(directory)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040035
Gilles Depatie0bf31352019-02-04 13:48:41 -050036
37def remove_leading_line(log_dir, log_file):
38 with open(log_dir + '/' + log_file, 'r+') as FILE:
39 lines = FILE.readlines()
40 FILE.seek(0)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040041 lines = lines[1:]
42 for line in lines:
Gilles Depatie0bf31352019-02-04 13:48:41 -050043 FILE.write(line)
44 FILE.truncate()
45 FILE.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040046
Gilles Depatie0bf31352019-02-04 13:48:41 -050047
Gilles Depatie17b0d922019-02-28 16:21:14 -050048def send_command_to_voltha_cli(log_dir, log_file1, cmd1, log_file2=None, cmd2=None, log_file3=None, cmd3=None, host='localhost'):
Gilles Depatie0bf31352019-02-04 13:48:41 -050049 output = open(log_dir + '/' + log_file1, 'w')
Gilles Depatie17b0d922019-02-28 16:21:14 -050050 child = pexpect.spawn('ssh -p 30110 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no voltha@%s' % host)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040051 child.expect('[pP]assword:')
52 child.sendline('admin')
53 child.expect('\((\\x1b\[\d*;?\d+m){1,2}voltha(\\x1b\[\d*;?\d+m){1,2}\)')
Gilles Depatie9651e462018-11-21 15:58:33 -050054 time.sleep(10)
Gilles Depatie0bf31352019-02-04 13:48:41 -050055 child.sendline(cmd1)
Gilles Depatie1be639b2018-12-06 10:51:08 -050056 i = child.expect(['\((\\x1b\[\d*;?\d+m){1,2}voltha(\\x1b\[\d*;?\d+m){1,2}\)',
Gilles Depatie0bf31352019-02-04 13:48:41 -050057 '\((\\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\\x1b\[\d*;?\d+m){1,2}\)'])
Gilles Depatie1be639b2018-12-06 10:51:08 -050058 if i == 0:
59 output.write(child.before)
60 output.close()
Gilles Depatie0bf31352019-02-04 13:48:41 -050061 remove_leading_line(log_dir, log_file1)
Gilles Depatie1be639b2018-12-06 10:51:08 -050062 elif i == 1:
Gilles Depatie0bf31352019-02-04 13:48:41 -050063 if log_file2 is not None and cmd2 is not None:
64 output = open(log_dir + '/' + log_file2, 'w')
65 child.sendline(cmd2)
Gilles Depatie1be639b2018-12-06 10:51:08 -050066 child.expect('\((\\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\\x1b\[\d*;?\d+m){1,2}\)')
67 output.write(child.before)
68 output.close()
Gilles Depatie0bf31352019-02-04 13:48:41 -050069 remove_leading_line(log_dir, log_file2)
70 if log_file3 is not None and cmd3 is not None:
71 output = open(log_dir + '/' + log_file3, 'w')
72 child.sendline(cmd3)
Gilles Depatie1be639b2018-12-06 10:51:08 -050073 child.expect('\((\\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\\x1b\[\d*;?\d+m){1,2}\)')
74 output.write(child.before)
75 output.close()
Gilles Depatie0bf31352019-02-04 13:48:41 -050076 remove_leading_line(log_dir, log_file3)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040077 child.close()
78
Gilles Depatie0bf31352019-02-04 13:48:41 -050079
Gilles Depatie17b0d922019-02-28 16:21:14 -050080def send_command_to_onos_cli(log_dir, log_file, cmd, host='localhost'):
Gilles Depatie0bf31352019-02-04 13:48:41 -050081 output = open(log_dir + '/' + log_file, 'w')
Gilles Depatie17b0d922019-02-28 16:21:14 -050082 child = pexpect.spawn('ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no karaf@%s' % host)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040083 child.expect('[pP]assword:')
84 child.sendline('karaf')
Carmelo Cascone6e72a742020-01-16 18:18:20 -080085 # Expected prompt:
86 # onos> (ONOS 1.x)
87 # karaf@root > (ONOS 2.x)
88 child.expect(['(\\x1b\[\d*;?\d+m){1,2}onos> (\\x1b\[\d*;?\d+m){1,2}', 'karaf@root >'])
Gilles Depatie0bf31352019-02-04 13:48:41 -050089 child.sendline(cmd)
Carmelo Cascone6e72a742020-01-16 18:18:20 -080090 child.expect(['(\\x1b\[\d*;?\d+m){1,2}onos> (\\x1b\[\d*;?\d+m){1,2}', 'karaf@root >'])
Gilles Depatie84cb1e72018-10-26 12:41:33 -040091
92 output.write(child.before)
93
94 output.close()
95 child.close()
96
Gilles Depatie0bf31352019-02-04 13:48:41 -050097
98def get_fields_from_grep_command(self, search_word, log_file):
Gilles Depatie1be639b2018-12-06 10:51:08 -050099 grepCommand =\
Gilles Depatie0bf31352019-02-04 13:48:41 -0500100 "grep %s %s/%s" % (search_word, get_dir(self, 'log'), log_file)
Gilles Depatie1be639b2018-12-06 10:51:08 -0500101 statusLines = commands.getstatusoutput(grepCommand)[1]
102 return statusLines
103
Gilles Depatie0bf31352019-02-04 13:48:41 -0500104
Gilles Depatie2e683692019-02-22 16:06:52 -0500105def parse_fields(status_line, delimiter):
106 statusList = status_line.split(delimiter)
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400107 return statusList
108
Gilles Depatie0bf31352019-02-04 13:48:41 -0500109
110def print_log_file(self, log_file):
111 with open(get_dir(self, 'log') + '/' + log_file, 'r+') as FILE:
112 lines = FILE.readlines()
Gilles Depatie1be639b2018-12-06 10:51:08 -0500113 print
114 for line in lines:
Gilles Depatie0bf31352019-02-04 13:48:41 -0500115 sys.stdout.write(line)
Gilles Depatie1be639b2018-12-06 10:51:08 -0500116
Gilles Depatie0bf31352019-02-04 13:48:41 -0500117
118def extract_pod_ip_addr(pod_name):
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400119 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'svc', '--all-namespaces'],
120 stdout=subprocess.PIPE,
121 stderr=subprocess.PIPE)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500122 proc2 = subprocess.Popen(['grep', '-e', pod_name], stdin=proc1.stdout,
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400123 stdout=subprocess.PIPE,
124 stderr=subprocess.PIPE)
125 proc3 = subprocess.Popen(['awk', "{print $4}"], stdin=proc2.stdout,
126 stdout=subprocess.PIPE,
127 stderr=subprocess.PIPE)
128
Gilles Depatie0bf31352019-02-04 13:48:41 -0500129 proc1.stdout.close()
130 proc2.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400131 out, err = proc3.communicate()
132 return out
133
Gilles Depatie0bf31352019-02-04 13:48:41 -0500134
135def extract_radius_ip_addr(pod_name):
136 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'describe', 'pod', '-n', 'voltha', pod_name],
Gilles Depatiea85fe812019-01-23 15:55:53 -0500137 stdout=subprocess.PIPE,
138 stderr=subprocess.PIPE)
139 proc2 = subprocess.Popen(['grep', '^IP:'], stdin=proc1.stdout,
140 stdout=subprocess.PIPE,
141 stderr=subprocess.PIPE)
142 proc3 = subprocess.Popen(['awk', "{print $2}"], stdin=proc2.stdout,
143 stdout=subprocess.PIPE,
144 stderr=subprocess.PIPE)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500145
146 proc1.stdout.close()
147 proc2.stdout.close()
Gilles Depatiea85fe812019-01-23 15:55:53 -0500148 out, err = proc3.communicate()
149 return out
150
Gilles Depatie0bf31352019-02-04 13:48:41 -0500151
152def extract_pod_name(short_pod_name):
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400153 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
154 stdout=subprocess.PIPE,
155 stderr=subprocess.PIPE)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500156 proc2 = subprocess.Popen(['grep', '-e', short_pod_name], stdin=proc1.stdout,
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400157 stdout=subprocess.PIPE,
158 stderr=subprocess.PIPE)
159 proc3 = subprocess.Popen(['awk', "{print $2}"], stdin=proc2.stdout,
160 stdout=subprocess.PIPE,
161 stderr=subprocess.PIPE)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500162
163 proc1.stdout.close()
164 proc2.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400165 out, err = proc3.communicate()
166 return out
Gilles Depatiea85fe812019-01-23 15:55:53 -0500167
Gilles Depatie0bf31352019-02-04 13:48:41 -0500168
169def modify_radius_ip_in_json_using_sed(self, new_ip_addr):
170 sedCommand = "sed -i '/radiusIp/c\ \"radiusIp\":\"'%s'\",' %s/tests/atests/build/aaa_json" \
171 % (new_ip_addr, get_dir(self, 'voltha'))
Gilles Depatiea85fe812019-01-23 15:55:53 -0500172 status = commands.getstatusoutput(sedCommand)[0]
173 return status
Gilles Depatieed99efe2019-03-12 16:12:26 -0400174
175
176def discover_rg_pod_name():
Andy Bavier7e215ed2019-06-12 13:26:21 -0700177 return extract_pod_name('rg0').strip()
Gilles Depatie88c281a2019-07-30 16:17:03 -0400178
179
180def retrieve_authorized_users_device_id_and_port_number(status_line):
181 fields = parse_fields(status_line, ',')
182 deviceField = fields[2].strip()
183 deviceStr, equal, deviceId = deviceField.partition('=')
184 device_Id = deviceId
185 portField = fields[4].strip()
186 portNumStr, equal, portNum = portField.partition('=')
187 portNumber = portNum
188 return device_Id, portNumber
189
190
191def add_subscriber_access(self, device_id, port_number):
192 send_command_to_onos_cli(get_dir(self, 'log'),
193 'voltha_add_subscriber_access.log', 'volt-add-subscriber-access %s %s'
194 % (device_id, port_number))