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 | |
| 48 | def send_command_to_voltha_cli(log_dir, log_file1, cmd1, log_file2=None, cmd2=None, log_file3=None, cmd3=None): |
| 49 | output = open(log_dir + '/' + log_file1, 'w') |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 50 | child = pexpect.spawn('ssh -p 30110 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no voltha@localhost') |
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 | |
| 80 | def send_command_to_onos_cli(log_dir, cmd, log_file): |
| 81 | output = open(log_dir + '/' + log_file, 'w') |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 82 | child = pexpect.spawn('ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no karaf@localhost') |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 83 | child.expect('[pP]assword:') |
| 84 | child.sendline('karaf') |
| 85 | child.expect('(\\x1b\[\d*;?\d+m){1,2}onos>(\\x1b\[\d*;?\d+m){1,2}') |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 86 | child.sendline(cmd) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 87 | child.expect('(\\x1b\[\d*;?\d+m){1,2}onos>(\\x1b\[\d*;?\d+m){1,2}') |
| 88 | |
| 89 | output.write(child.before) |
| 90 | |
| 91 | output.close() |
| 92 | child.close() |
| 93 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 94 | |
| 95 | def get_fields_from_grep_command(self, search_word, log_file): |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 96 | grepCommand =\ |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 97 | "grep %s %s/%s" % (search_word, get_dir(self, 'log'), log_file) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 98 | statusLines = commands.getstatusoutput(grepCommand)[1] |
| 99 | return statusLines |
| 100 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 101 | |
| 102 | def parse_fields(status_line): |
| 103 | statusList = status_line.split("|") |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 104 | return statusList |
| 105 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 106 | |
| 107 | def print_log_file(self, log_file): |
| 108 | with open(get_dir(self, 'log') + '/' + log_file, 'r+') as FILE: |
| 109 | lines = FILE.readlines() |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 110 | print |
| 111 | for line in lines: |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 112 | sys.stdout.write(line) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 113 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 114 | |
| 115 | def extract_pod_ip_addr(pod_name): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 116 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'svc', '--all-namespaces'], |
| 117 | stdout=subprocess.PIPE, |
| 118 | stderr=subprocess.PIPE) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 119 | proc2 = subprocess.Popen(['grep', '-e', pod_name], stdin=proc1.stdout, |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 120 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 126 | proc1.stdout.close() |
| 127 | proc2.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 128 | out, err = proc3.communicate() |
| 129 | return out |
| 130 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 131 | |
| 132 | def extract_radius_ip_addr(pod_name): |
| 133 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'describe', 'pod', '-n', 'voltha', pod_name], |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 134 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 142 | |
| 143 | proc1.stdout.close() |
| 144 | proc2.stdout.close() |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 145 | out, err = proc3.communicate() |
| 146 | return out |
| 147 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 148 | |
| 149 | def extract_pod_name(short_pod_name): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 150 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'], |
| 151 | stdout=subprocess.PIPE, |
| 152 | stderr=subprocess.PIPE) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 153 | proc2 = subprocess.Popen(['grep', '-e', short_pod_name], stdin=proc1.stdout, |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 154 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 159 | |
| 160 | proc1.stdout.close() |
| 161 | proc2.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 162 | out, err = proc3.communicate() |
| 163 | return out |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 164 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 165 | |
| 166 | def 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 Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 169 | status = commands.getstatusoutput(sedCommand)[0] |
| 170 | return status |