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 |
| 21 | import os |
| 22 | import commands |
| 23 | import subprocess |
| 24 | import pexpect |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 25 | import sys |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 26 | |
| 27 | def configDirs(self, logDir, rootDir = None, volthaDir = None): |
| 28 | self.dirs ['log'] = logDir |
| 29 | self.dirs ['root'] = rootDir |
| 30 | self.dirs ['voltha'] = volthaDir |
| 31 | |
| 32 | def getDir(self, Dir): |
| 33 | return self.dirs.get(Dir) |
| 34 | |
| 35 | def removeLeadingLine(logDir, logFile): |
| 36 | with open(logDir + '/' + logFile, 'r+') as file: |
| 37 | lines = file.readlines() |
| 38 | file.seek(0) |
| 39 | lines = lines[1:] |
| 40 | for line in lines: |
| 41 | file.write(line) |
| 42 | file.truncate() |
| 43 | file.close() |
| 44 | |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 45 | def send_command_to_voltha_cli(logDir, logFile1, cmd1, logFile2 = None, cmd2 = None, logFile3 = None, cmd3 = None): |
| 46 | output = open(logDir + '/' + logFile1, 'w') |
| 47 | 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] | 48 | child.expect('[pP]assword:') |
| 49 | child.sendline('admin') |
| 50 | 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] | 51 | time.sleep(10) |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 52 | bytes = child.sendline(cmd1) |
| 53 | i = child.expect(['\((\\x1b\[\d*;?\d+m){1,2}voltha(\\x1b\[\d*;?\d+m){1,2}\)', |
| 54 | '\((\\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\\x1b\[\d*;?\d+m){1,2}\)']) |
| 55 | if i == 0: |
| 56 | output.write(child.before) |
| 57 | output.close() |
| 58 | removeLeadingLine(logDir, logFile1) |
| 59 | elif i == 1: |
| 60 | if logFile2 != None and cmd2 != None: |
| 61 | output = open(logDir + '/' + logFile2, 'w') |
| 62 | bytes = child.sendline(cmd2) |
| 63 | child.expect('\((\\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\\x1b\[\d*;?\d+m){1,2}\)') |
| 64 | output.write(child.before) |
| 65 | output.close() |
| 66 | removeLeadingLine(logDir, logFile2) |
| 67 | if logFile3 != None and cmd3 != None: |
| 68 | output = open(logDir + '/' + logFile3, 'w') |
| 69 | bytes = child.sendline(cmd3) |
| 70 | child.expect('\((\\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\\x1b\[\d*;?\d+m){1,2}\)') |
| 71 | output.write(child.before) |
| 72 | output.close() |
| 73 | removeLeadingLine(logDir, logFile3) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 74 | child.close() |
| 75 | |
| 76 | def send_command_to_onos_cli(logDir, cmd, logFile): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 77 | output = open(logDir + '/' + logFile, 'w') |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 78 | 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] | 79 | child.expect('[pP]assword:') |
| 80 | child.sendline('karaf') |
| 81 | child.expect('(\\x1b\[\d*;?\d+m){1,2}onos>(\\x1b\[\d*;?\d+m){1,2}') |
| 82 | child.sendline('flows') |
| 83 | child.expect('flows') |
| 84 | child.expect('(\\x1b\[\d*;?\d+m){1,2}onos>(\\x1b\[\d*;?\d+m){1,2}') |
| 85 | |
| 86 | output.write(child.before) |
| 87 | |
| 88 | output.close() |
| 89 | child.close() |
| 90 | |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 91 | def get_fields_from_grep_command(self, searchWord, logFile): |
| 92 | grepCommand =\ |
| 93 | "grep %s %s/%s" % (searchWord, getDir(self, 'log'), logFile) |
| 94 | statusLines = commands.getstatusoutput(grepCommand)[1] |
| 95 | return statusLines |
| 96 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 97 | def parseFields(statusLine): |
| 98 | statusList = statusLine.split("|") |
| 99 | return statusList |
| 100 | |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 101 | def printLogFile(self, logFile): |
| 102 | with open(getDir(self, 'log') + '/' + logFile, 'r+') as file: |
| 103 | lines = file.readlines() |
| 104 | print |
| 105 | for line in lines: |
| 106 | sys.stdout.write (line) |
| 107 | |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 108 | def extractPodIpAddr(podName): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 109 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'svc', '--all-namespaces'], |
| 110 | stdout=subprocess.PIPE, |
| 111 | stderr=subprocess.PIPE) |
| 112 | proc2 = subprocess.Popen(['grep', '-e', podName], stdin=proc1.stdout, |
| 113 | stdout=subprocess.PIPE, |
| 114 | stderr=subprocess.PIPE) |
| 115 | proc3 = subprocess.Popen(['awk', "{print $4}"], stdin=proc2.stdout, |
| 116 | stdout=subprocess.PIPE, |
| 117 | stderr=subprocess.PIPE) |
| 118 | |
| 119 | proc1.stdout.close |
| 120 | proc2.stdout.close |
| 121 | out, err = proc3.communicate() |
| 122 | return out |
| 123 | |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 124 | def extractRadiusIpAddr(podName): |
| 125 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'describe', 'pod', '-n', 'voltha', podName], |
| 126 | stdout=subprocess.PIPE, |
| 127 | stderr=subprocess.PIPE) |
| 128 | proc2 = subprocess.Popen(['grep', '^IP:'], stdin=proc1.stdout, |
| 129 | stdout=subprocess.PIPE, |
| 130 | stderr=subprocess.PIPE) |
| 131 | proc3 = subprocess.Popen(['awk', "{print $2}"], stdin=proc2.stdout, |
| 132 | stdout=subprocess.PIPE, |
| 133 | stderr=subprocess.PIPE) |
| 134 | |
| 135 | proc1.stdout.close |
| 136 | proc2.stdout.close |
| 137 | out, err = proc3.communicate() |
| 138 | return out |
| 139 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 140 | def extractPodName(shortPodName): |
| 141 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'], |
| 142 | stdout=subprocess.PIPE, |
| 143 | stderr=subprocess.PIPE) |
| 144 | proc2 = subprocess.Popen(['grep', '-e', shortPodName], stdin=proc1.stdout, |
| 145 | stdout=subprocess.PIPE, |
| 146 | stderr=subprocess.PIPE) |
| 147 | proc3 = subprocess.Popen(['awk', "{print $2}"], stdin=proc2.stdout, |
| 148 | stdout=subprocess.PIPE, |
| 149 | stderr=subprocess.PIPE) |
| 150 | |
| 151 | |
| 152 | proc1.stdout.close |
| 153 | proc2.stdout.close |
| 154 | out, err = proc3.communicate() |
| 155 | return out |
Gilles Depatie | a85fe81 | 2019-01-23 15:55:53 -0500 | [diff] [blame] | 156 | |
| 157 | def modifyRadiusIpInJsonUsingSed(self, newIpAddr): |
| 158 | sedCommand ="sed -i '/radiusIp/c\ \"radiusIp\":\"'%s'\",' %s/tests/atests/build/aaa_json" % (newIpAddr, getDir(self, 'voltha')) |
| 159 | status = commands.getstatusoutput(sedCommand)[0] |
| 160 | return status |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 161 | |