Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 1 | # |
| 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 | """ |
| 18 | vOLT-HA Test Case Utils module |
| 19 | """ |
| 20 | import time |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 21 | import commands |
| 22 | import subprocess |
| 23 | import pexpect |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 24 | import sys |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 25 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 26 | |
| 27 | def 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 Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 31 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 32 | |
| 33 | def get_dir(self, directory): |
| 34 | return self.dirs.get(directory) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 35 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 36 | |
| 37 | def 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 Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 41 | lines = lines[1:] |
| 42 | for line in lines: |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 43 | FILE.write(line) |
| 44 | FILE.truncate() |
| 45 | FILE.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 46 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 47 | |
Gilles Depatie | 17b0d92 | 2019-02-28 16:21:14 -0500 | [diff] [blame] | 48 | def send_command_to_voltha_cli(log_dir, log_file1, cmd1, log_file2=None, cmd2=None, log_file3=None, cmd3=None, host='localhost'): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 49 | output = open(log_dir + '/' + log_file1, 'w') |
Gilles Depatie | 17b0d92 | 2019-02-28 16:21:14 -0500 | [diff] [blame] | 50 | child = pexpect.spawn('ssh -p 30110 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no voltha@%s' % host) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 51 | 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 Depatie | 9651e46 | 2018-11-21 15:58:33 -0500 | [diff] [blame] | 54 | time.sleep(10) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 55 | child.sendline(cmd1) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 56 | i = child.expect(['\((\\x1b\[\d*;?\d+m){1,2}voltha(\\x1b\[\d*;?\d+m){1,2}\)', |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 57 | '\((\\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\\x1b\[\d*;?\d+m){1,2}\)']) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 58 | if i == 0: |
| 59 | output.write(child.before) |
| 60 | output.close() |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 61 | remove_leading_line(log_dir, log_file1) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 62 | elif i == 1: |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 63 | if log_file2 is not None and cmd2 is not None: |
| 64 | output = open(log_dir + '/' + log_file2, 'w') |
| 65 | child.sendline(cmd2) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 66 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 69 | 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 Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 73 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 76 | remove_leading_line(log_dir, log_file3) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 77 | child.close() |
| 78 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 79 | |
Gilles Depatie | 17b0d92 | 2019-02-28 16:21:14 -0500 | [diff] [blame] | 80 | def send_command_to_onos_cli(log_dir, log_file, cmd, host='localhost'): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 81 | output = open(log_dir + '/' + log_file, 'w') |
Gilles Depatie | 17b0d92 | 2019-02-28 16:21:14 -0500 | [diff] [blame] | 82 | child = pexpect.spawn('ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no karaf@%s' % host) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 83 | child.expect('[pP]assword:') |
| 84 | child.sendline('karaf') |
Carmelo Cascone | 6e72a74 | 2020-01-16 18:18:20 -0800 | [diff] [blame] | 85 | # 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 89 | child.sendline(cmd) |
Carmelo Cascone | 6e72a74 | 2020-01-16 18:18:20 -0800 | [diff] [blame] | 90 | child.expect(['(\\x1b\[\d*;?\d+m){1,2}onos> (\\x1b\[\d*;?\d+m){1,2}', 'karaf@root >']) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 91 | |
| 92 | output.write(child.before) |
| 93 | |
| 94 | output.close() |
| 95 | child.close() |
| 96 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 97 | |
| 98 | def get_fields_from_grep_command(self, search_word, log_file): |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 99 | grepCommand =\ |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 100 | "grep %s %s/%s" % (search_word, get_dir(self, 'log'), log_file) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 101 | statusLines = commands.getstatusoutput(grepCommand)[1] |
| 102 | return statusLines |
| 103 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 104 | |
Gilles Depatie | 2e68369 | 2019-02-22 16:06:52 -0500 | [diff] [blame] | 105 | def parse_fields(status_line, delimiter): |
| 106 | statusList = status_line.split(delimiter) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 107 | return statusList |
| 108 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 109 | |
| 110 | def print_log_file(self, log_file): |
| 111 | with open(get_dir(self, 'log') + '/' + log_file, 'r+') as FILE: |
| 112 | lines = FILE.readlines() |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 113 | print |
| 114 | for line in lines: |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 115 | sys.stdout.write(line) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 116 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 117 | |
| 118 | def extract_pod_ip_addr(pod_name): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 119 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'svc', '--all-namespaces'], |
| 120 | stdout=subprocess.PIPE, |
| 121 | stderr=subprocess.PIPE) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 122 | proc2 = subprocess.Popen(['grep', '-e', pod_name], stdin=proc1.stdout, |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 123 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 129 | proc1.stdout.close() |
| 130 | proc2.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 131 | out, err = proc3.communicate() |
| 132 | return out |
| 133 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 134 | |
| 135 | def extract_radius_ip_addr(pod_name): |
| 136 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'describe', 'pod', '-n', 'voltha', pod_name], |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 137 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 145 | |
| 146 | proc1.stdout.close() |
| 147 | proc2.stdout.close() |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 148 | out, err = proc3.communicate() |
| 149 | return out |
| 150 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 151 | |
| 152 | def extract_pod_name(short_pod_name): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 153 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'], |
| 154 | stdout=subprocess.PIPE, |
| 155 | stderr=subprocess.PIPE) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 156 | proc2 = subprocess.Popen(['grep', '-e', short_pod_name], stdin=proc1.stdout, |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 157 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 162 | |
| 163 | proc1.stdout.close() |
| 164 | proc2.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 165 | out, err = proc3.communicate() |
| 166 | return out |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 167 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 168 | |
| 169 | def 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 Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 172 | status = commands.getstatusoutput(sedCommand)[0] |
| 173 | return status |
Gilles Depatie | ed99efe | 2019-03-12 16:12:26 -0400 | [diff] [blame] | 174 | |
| 175 | |
| 176 | def discover_rg_pod_name(): |
Andy Bavier | 7e215ed | 2019-06-12 13:26:21 -0700 | [diff] [blame] | 177 | return extract_pod_name('rg0').strip() |
Gilles Depatie | 88c281a | 2019-07-30 16:17:03 -0400 | [diff] [blame] | 178 | |
| 179 | |
| 180 | def 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 | |
| 191 | def 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)) |