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 |
| 25 | |
| 26 | def configDirs(self, logDir, rootDir = None, volthaDir = None): |
| 27 | self.dirs ['log'] = logDir |
| 28 | self.dirs ['root'] = rootDir |
| 29 | self.dirs ['voltha'] = volthaDir |
| 30 | |
| 31 | def getDir(self, Dir): |
| 32 | return self.dirs.get(Dir) |
| 33 | |
| 34 | def removeLeadingLine(logDir, logFile): |
| 35 | with open(logDir + '/' + logFile, 'r+') as file: |
| 36 | lines = file.readlines() |
| 37 | file.seek(0) |
| 38 | lines = lines[1:] |
| 39 | for line in lines: |
| 40 | file.write(line) |
| 41 | file.truncate() |
| 42 | file.close() |
| 43 | |
| 44 | def send_command_to_voltha_cli(logDir, cmd, logFile): |
| 45 | vcliIp = extractIpAddr('vcli') |
| 46 | print (vcliIp) |
| 47 | print (cmd) |
| 48 | output = open(logDir + '/' + logFile, 'w') |
| 49 | child = pexpect.spawn('ssh -p 5022 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no voltha@' + vcliIp) |
| 50 | child.expect('[pP]assword:') |
| 51 | child.sendline('admin') |
| 52 | 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] | 53 | time.sleep(10) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 54 | bytes = child.sendline(cmd) |
| 55 | child.expect('\((\\x1b\[\d*;?\d+m){1,2}voltha(\\x1b\[\d*;?\d+m){1,2}\)') |
| 56 | print (child.before) |
| 57 | output.write(child.before) |
| 58 | output.close() |
| 59 | removeLeadingLine(logDir, logFile) |
| 60 | child.close() |
| 61 | |
| 62 | def send_command_to_onos_cli(logDir, cmd, logFile): |
| 63 | onosIp = extractIpAddr('onos-ssh') |
| 64 | print (onosIp) |
| 65 | output = open(logDir + '/' + logFile, 'w') |
| 66 | child = pexpect.spawn('ssh -p 8101 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no karaf@' + onosIp) |
| 67 | |
| 68 | child.expect('[pP]assword:') |
| 69 | child.sendline('karaf') |
| 70 | child.expect('(\\x1b\[\d*;?\d+m){1,2}onos>(\\x1b\[\d*;?\d+m){1,2}') |
| 71 | child.sendline('flows') |
| 72 | child.expect('flows') |
| 73 | child.expect('(\\x1b\[\d*;?\d+m){1,2}onos>(\\x1b\[\d*;?\d+m){1,2}') |
| 74 | |
| 75 | output.write(child.before) |
| 76 | |
| 77 | output.close() |
| 78 | child.close() |
| 79 | |
| 80 | def parseFields(statusLine): |
| 81 | statusList = statusLine.split("|") |
| 82 | return statusList |
| 83 | |
| 84 | def extractIpAddr(podName): |
| 85 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'svc', '--all-namespaces'], |
| 86 | stdout=subprocess.PIPE, |
| 87 | stderr=subprocess.PIPE) |
| 88 | proc2 = subprocess.Popen(['grep', '-e', podName], stdin=proc1.stdout, |
| 89 | stdout=subprocess.PIPE, |
| 90 | stderr=subprocess.PIPE) |
| 91 | proc3 = subprocess.Popen(['awk', "{print $4}"], stdin=proc2.stdout, |
| 92 | stdout=subprocess.PIPE, |
| 93 | stderr=subprocess.PIPE) |
| 94 | |
| 95 | proc1.stdout.close |
| 96 | proc2.stdout.close |
| 97 | out, err = proc3.communicate() |
| 98 | return out |
| 99 | |
| 100 | def extractPodName(shortPodName): |
| 101 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'], |
| 102 | stdout=subprocess.PIPE, |
| 103 | stderr=subprocess.PIPE) |
| 104 | proc2 = subprocess.Popen(['grep', '-e', shortPodName], stdin=proc1.stdout, |
| 105 | stdout=subprocess.PIPE, |
| 106 | stderr=subprocess.PIPE) |
| 107 | proc3 = subprocess.Popen(['awk', "{print $2}"], stdin=proc2.stdout, |
| 108 | stdout=subprocess.PIPE, |
| 109 | stderr=subprocess.PIPE) |
| 110 | |
| 111 | |
| 112 | proc1.stdout.close |
| 113 | proc2.stdout.close |
| 114 | out, err = proc3.communicate() |
| 115 | return out |
| 116 | |