blob: 1fe4f82c1f75b4022874475f6ddba41bf5114220 [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')
Gilles Depatie2e683692019-02-22 16:06:52 -050085 child.expect('(\\x1b\[\d*;?\d+m){1,2}onos> (\\x1b\[\d*;?\d+m){1,2}')
Gilles Depatie0bf31352019-02-04 13:48:41 -050086 child.sendline(cmd)
Gilles Depatie2e683692019-02-22 16:06:52 -050087 child.expect('(\\x1b\[\d*;?\d+m){1,2}onos> (\\x1b\[\d*;?\d+m){1,2}')
Gilles Depatie84cb1e72018-10-26 12:41:33 -040088
89 output.write(child.before)
90
91 output.close()
92 child.close()
93
Gilles Depatie0bf31352019-02-04 13:48:41 -050094
95def get_fields_from_grep_command(self, search_word, log_file):
Gilles Depatie1be639b2018-12-06 10:51:08 -050096 grepCommand =\
Gilles Depatie0bf31352019-02-04 13:48:41 -050097 "grep %s %s/%s" % (search_word, get_dir(self, 'log'), log_file)
Gilles Depatie1be639b2018-12-06 10:51:08 -050098 statusLines = commands.getstatusoutput(grepCommand)[1]
99 return statusLines
100
Gilles Depatie0bf31352019-02-04 13:48:41 -0500101
Gilles Depatie2e683692019-02-22 16:06:52 -0500102def parse_fields(status_line, delimiter):
103 statusList = status_line.split(delimiter)
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400104 return statusList
105
Gilles Depatie0bf31352019-02-04 13:48:41 -0500106
107def print_log_file(self, log_file):
108 with open(get_dir(self, 'log') + '/' + log_file, 'r+') as FILE:
109 lines = FILE.readlines()
Gilles Depatie1be639b2018-12-06 10:51:08 -0500110 print
111 for line in lines:
Gilles Depatie0bf31352019-02-04 13:48:41 -0500112 sys.stdout.write(line)
Gilles Depatie1be639b2018-12-06 10:51:08 -0500113
Gilles Depatie0bf31352019-02-04 13:48:41 -0500114
115def extract_pod_ip_addr(pod_name):
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400116 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'svc', '--all-namespaces'],
117 stdout=subprocess.PIPE,
118 stderr=subprocess.PIPE)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500119 proc2 = subprocess.Popen(['grep', '-e', pod_name], stdin=proc1.stdout,
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400120 stdout=subprocess.PIPE,
121 stderr=subprocess.PIPE)
122 proc3 = subprocess.Popen(['awk', "{print $4}"], stdin=proc2.stdout,
123 stdout=subprocess.PIPE,
124 stderr=subprocess.PIPE)
125
Gilles Depatie0bf31352019-02-04 13:48:41 -0500126 proc1.stdout.close()
127 proc2.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400128 out, err = proc3.communicate()
129 return out
130
Gilles Depatie0bf31352019-02-04 13:48:41 -0500131
132def extract_radius_ip_addr(pod_name):
133 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'describe', 'pod', '-n', 'voltha', pod_name],
Gilles Depatiea85fe812019-01-23 15:55:53 -0500134 stdout=subprocess.PIPE,
135 stderr=subprocess.PIPE)
136 proc2 = subprocess.Popen(['grep', '^IP:'], stdin=proc1.stdout,
137 stdout=subprocess.PIPE,
138 stderr=subprocess.PIPE)
139 proc3 = subprocess.Popen(['awk', "{print $2}"], stdin=proc2.stdout,
140 stdout=subprocess.PIPE,
141 stderr=subprocess.PIPE)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500142
143 proc1.stdout.close()
144 proc2.stdout.close()
Gilles Depatiea85fe812019-01-23 15:55:53 -0500145 out, err = proc3.communicate()
146 return out
147
Gilles Depatie0bf31352019-02-04 13:48:41 -0500148
149def extract_pod_name(short_pod_name):
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400150 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
151 stdout=subprocess.PIPE,
152 stderr=subprocess.PIPE)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500153 proc2 = subprocess.Popen(['grep', '-e', short_pod_name], stdin=proc1.stdout,
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400154 stdout=subprocess.PIPE,
155 stderr=subprocess.PIPE)
156 proc3 = subprocess.Popen(['awk', "{print $2}"], stdin=proc2.stdout,
157 stdout=subprocess.PIPE,
158 stderr=subprocess.PIPE)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500159
160 proc1.stdout.close()
161 proc2.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400162 out, err = proc3.communicate()
163 return out
Gilles Depatiea85fe812019-01-23 15:55:53 -0500164
Gilles Depatie0bf31352019-02-04 13:48:41 -0500165
166def modify_radius_ip_in_json_using_sed(self, new_ip_addr):
167 sedCommand = "sed -i '/radiusIp/c\ \"radiusIp\":\"'%s'\",' %s/tests/atests/build/aaa_json" \
168 % (new_ip_addr, get_dir(self, 'voltha'))
Gilles Depatiea85fe812019-01-23 15:55:53 -0500169 status = commands.getstatusoutput(sedCommand)[0]
170 return status
Gilles Depatieed99efe2019-03-12 16:12:26 -0400171
172
173def discover_rg_pod_name():
174 return extract_pod_name('rg-').strip()