Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [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 | Test Case Utils module |
| 19 | """ |
| 20 | |
| 21 | from __future__ import absolute_import |
| 22 | |
| 23 | import time |
| 24 | import subprocess |
| 25 | import pexpect |
| 26 | import sys |
| 27 | |
| 28 | |
| 29 | def config_dirs(self, log_dir, root_dir=None, voltha_dir=None): |
| 30 | self.dirs["log"] = log_dir |
| 31 | self.dirs["root"] = root_dir |
| 32 | self.dirs["voltha"] = voltha_dir |
| 33 | |
| 34 | |
| 35 | def get_dir(self, directory): |
| 36 | return self.dirs.get(directory) |
| 37 | |
| 38 | |
| 39 | def remove_leading_line(log_dir, log_file): |
| 40 | with open(log_dir + "/" + log_file, "r+") as FILE: |
| 41 | lines = FILE.readlines() |
| 42 | FILE.seek(0) |
| 43 | lines = lines[1:] |
| 44 | for line in lines: |
| 45 | FILE.write(line) |
| 46 | FILE.truncate() |
| 47 | FILE.close() |
| 48 | |
| 49 | |
| 50 | def send_command_to_voltha_cli( |
| 51 | log_dir, |
| 52 | log_file1, |
| 53 | cmd1, |
| 54 | log_file2=None, |
| 55 | cmd2=None, |
| 56 | log_file3=None, |
| 57 | cmd3=None, |
| 58 | host="localhost", |
| 59 | ): |
Zack Williams | d9af149 | 2020-01-14 15:28:13 -0700 | [diff] [blame^] | 60 | output = open(log_dir + "/" + log_file1, "wb") |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 61 | child = pexpect.spawn( |
| 62 | "ssh -p 30110 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no voltha@%s" |
| 63 | % host |
| 64 | ) |
| 65 | child.expect(r"[pP]assword:") |
| 66 | child.sendline("admin") |
Zack Williams | 9a37e8c | 2020-01-14 13:56:33 -0700 | [diff] [blame] | 67 | child.expect(r"\((\x1b\[\d*;?\d+m){1,2}voltha(\x1b\[\d*;?\d+m){1,2}\)") |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 68 | time.sleep(10) |
| 69 | child.sendline(cmd1) |
| 70 | i = child.expect( |
| 71 | [ |
Zack Williams | 9a37e8c | 2020-01-14 13:56:33 -0700 | [diff] [blame] | 72 | r"\((\x1b\[\d*;?\d+m){1,2}voltha(\x1b\[\d*;?\d+m){1,2}\)", |
| 73 | r"\((\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\x1b\[\d*;?\d+m){1,2}\)", |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 74 | ] |
| 75 | ) |
| 76 | if i == 0: |
| 77 | output.write(child.before) |
| 78 | output.close() |
| 79 | remove_leading_line(log_dir, log_file1) |
| 80 | elif i == 1: |
| 81 | if log_file2 is not None and cmd2 is not None: |
Zack Williams | d9af149 | 2020-01-14 15:28:13 -0700 | [diff] [blame^] | 82 | output = open(log_dir + "/" + log_file2, "wb") |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 83 | child.sendline(cmd2) |
| 84 | child.expect( |
Zack Williams | 9a37e8c | 2020-01-14 13:56:33 -0700 | [diff] [blame] | 85 | r"\((\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\x1b\[\d*;?\d+m){1,2}\)" |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 86 | ) |
| 87 | output.write(child.before) |
| 88 | output.close() |
| 89 | remove_leading_line(log_dir, log_file2) |
| 90 | if log_file3 is not None and cmd3 is not None: |
Zack Williams | d9af149 | 2020-01-14 15:28:13 -0700 | [diff] [blame^] | 91 | output = open(log_dir + "/" + log_file3, "wb") |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 92 | child.sendline(cmd3) |
| 93 | child.expect( |
Zack Williams | 9a37e8c | 2020-01-14 13:56:33 -0700 | [diff] [blame] | 94 | r"\((\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\x1b\[\d*;?\d+m){1,2}\)" |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 95 | ) |
| 96 | output.write(child.before) |
| 97 | output.close() |
| 98 | remove_leading_line(log_dir, log_file3) |
| 99 | child.close() |
| 100 | |
| 101 | |
| 102 | def send_command_to_onos_cli(log_dir, log_file, cmd, host="localhost"): |
Zack Williams | d9af149 | 2020-01-14 15:28:13 -0700 | [diff] [blame^] | 103 | output = open(log_dir + "/" + log_file, "wb") |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 104 | child = pexpect.spawn( |
| 105 | "ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no karaf@%s" |
| 106 | % host |
| 107 | ) |
| 108 | child.expect(r"[pP]assword:") |
| 109 | child.sendline("karaf") |
Zack Williams | 9a37e8c | 2020-01-14 13:56:33 -0700 | [diff] [blame] | 110 | child.expect(r"(\x1b\[\d*;?\d+m){1,2}(onos>|karaf@root >) (\x1b\[\d*;?\d+m){1,2}") |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 111 | child.sendline(cmd) |
Zack Williams | 9a37e8c | 2020-01-14 13:56:33 -0700 | [diff] [blame] | 112 | child.expect(r"(\x1b\[\d*;?\d+m){1,2}(onos>|karaf@root >) (\x1b\[\d*;?\d+m){1,2}") |
Zack Williams | c6722d5 | 2020-01-13 16:34:33 -0700 | [diff] [blame] | 113 | |
| 114 | output.write(child.before) |
| 115 | |
| 116 | output.close() |
| 117 | child.close() |
| 118 | |
| 119 | |
| 120 | def get_fields_from_grep_command(self, search_word, log_file): |
| 121 | grepCommand = "grep %s %s/%s" % (search_word, get_dir(self, "log"), log_file) |
| 122 | statusLines = subprocess.getstatusoutput(grepCommand)[1] |
| 123 | return statusLines |
| 124 | |
| 125 | |
| 126 | def parse_fields(status_line, delimiter): |
| 127 | statusList = status_line.split(delimiter) |
| 128 | return statusList |
| 129 | |
| 130 | |
| 131 | def print_log_file(self, log_file): |
| 132 | with open(get_dir(self, "log") + "/" + log_file, "r+") as FILE: |
| 133 | lines = FILE.readlines() |
| 134 | print |
| 135 | for line in lines: |
| 136 | sys.stdout.write(line) |
| 137 | |
| 138 | |
| 139 | def extract_pod_ip_addr(pod_name): |
| 140 | proc1 = subprocess.Popen( |
| 141 | ["/usr/bin/kubectl", "get", "svc", "--all-namespaces"], |
| 142 | stdout=subprocess.PIPE, |
| 143 | stderr=subprocess.PIPE, |
| 144 | ) |
| 145 | proc2 = subprocess.Popen( |
| 146 | ["grep", "-e", pod_name], |
| 147 | stdin=proc1.stdout, |
| 148 | stdout=subprocess.PIPE, |
| 149 | stderr=subprocess.PIPE, |
| 150 | ) |
| 151 | proc3 = subprocess.Popen( |
| 152 | ["awk", "{print $4}"], |
| 153 | stdin=proc2.stdout, |
| 154 | stdout=subprocess.PIPE, |
| 155 | stderr=subprocess.PIPE, |
| 156 | ) |
| 157 | |
| 158 | proc1.stdout.close() |
| 159 | proc2.stdout.close() |
| 160 | out, err = proc3.communicate() |
| 161 | return out |
| 162 | |
| 163 | |
| 164 | def extract_radius_ip_addr(pod_name): |
| 165 | proc1 = subprocess.Popen( |
| 166 | ["/usr/bin/kubectl", "describe", "pod", "-n", "voltha", pod_name], |
| 167 | stdout=subprocess.PIPE, |
| 168 | stderr=subprocess.PIPE, |
| 169 | ) |
| 170 | proc2 = subprocess.Popen( |
| 171 | ["grep", "^IP:"], |
| 172 | stdin=proc1.stdout, |
| 173 | stdout=subprocess.PIPE, |
| 174 | stderr=subprocess.PIPE, |
| 175 | ) |
| 176 | proc3 = subprocess.Popen( |
| 177 | ["awk", "{print $2}"], |
| 178 | stdin=proc2.stdout, |
| 179 | stdout=subprocess.PIPE, |
| 180 | stderr=subprocess.PIPE, |
| 181 | ) |
| 182 | |
| 183 | proc1.stdout.close() |
| 184 | proc2.stdout.close() |
| 185 | out, err = proc3.communicate() |
| 186 | return out |
| 187 | |
| 188 | |
| 189 | def extract_pod_name(short_pod_name): |
| 190 | proc1 = subprocess.Popen( |
| 191 | ["/usr/bin/kubectl", "get", "pods", "--all-namespaces"], |
| 192 | stdout=subprocess.PIPE, |
| 193 | stderr=subprocess.PIPE, |
| 194 | ) |
| 195 | proc2 = subprocess.Popen( |
| 196 | ["grep", "-e", short_pod_name], |
| 197 | stdin=proc1.stdout, |
| 198 | stdout=subprocess.PIPE, |
| 199 | stderr=subprocess.PIPE, |
| 200 | ) |
| 201 | proc3 = subprocess.Popen( |
| 202 | ["awk", "{print $2}"], |
| 203 | stdin=proc2.stdout, |
| 204 | stdout=subprocess.PIPE, |
| 205 | stderr=subprocess.PIPE, |
| 206 | ) |
| 207 | |
| 208 | proc1.stdout.close() |
| 209 | proc2.stdout.close() |
| 210 | out, err = proc3.communicate() |
| 211 | return out |
| 212 | |
| 213 | |
| 214 | def modify_radius_ip_in_json_using_sed(self, new_ip_addr): |
| 215 | sedCommand = ( |
| 216 | "sed -i '/radiusIp/c\\ \"radiusIp\":\"'%s'\",' %s/tests/atests/build/aaa_json" |
| 217 | % (new_ip_addr, get_dir(self, "voltha")) |
| 218 | ) |
| 219 | status = subprocess.getstatusoutput(sedCommand)[0] |
| 220 | return status |
| 221 | |
| 222 | |
| 223 | def discover_rg_pod_name(): |
| 224 | return extract_pod_name("rg0").strip() |
| 225 | |
| 226 | |
| 227 | def retrieve_authorized_users_device_id_and_port_number(status_line): |
| 228 | fields = parse_fields(status_line, ",") |
| 229 | deviceField = fields[2].strip() |
| 230 | deviceStr, equal, deviceId = deviceField.partition("=") |
| 231 | device_Id = deviceId |
| 232 | portField = fields[4].strip() |
| 233 | portNumStr, equal, portNum = portField.partition("=") |
| 234 | portNumber = portNum |
| 235 | return device_Id, portNumber |
| 236 | |
| 237 | |
| 238 | def add_subscriber_access(self, device_id, port_number): |
| 239 | send_command_to_onos_cli( |
| 240 | get_dir(self, "log"), |
| 241 | "voltha_add_subscriber_access.log", |
| 242 | "volt-add-subscriber-access %s %s" % (device_id, port_number), |
| 243 | ) |