blob: ead244d278f7f6bbeef121e20770f5e0dfbd0f6d [file] [log] [blame]
Gilles Depatie84cb1e72018-10-26 12:41:33 -04001#
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"""
18vOLT-HA Test Case Utils module
19"""
20import time
21import os
22import commands
23import subprocess
24import pexpect
Gilles Depatie1be639b2018-12-06 10:51:08 -050025import sys
Gilles Depatie84cb1e72018-10-26 12:41:33 -040026
27def configDirs(self, logDir, rootDir = None, volthaDir = None):
28 self.dirs ['log'] = logDir
29 self.dirs ['root'] = rootDir
30 self.dirs ['voltha'] = volthaDir
31
32def getDir(self, Dir):
33 return self.dirs.get(Dir)
34
35def 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 Depatie1be639b2018-12-06 10:51:08 -050045def 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 Depatie84cb1e72018-10-26 12:41:33 -040048 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 Depatie9651e462018-11-21 15:58:33 -050051 time.sleep(10)
Gilles Depatie1be639b2018-12-06 10:51:08 -050052 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 Depatie84cb1e72018-10-26 12:41:33 -040074 child.close()
75
76def send_command_to_onos_cli(logDir, cmd, logFile):
Gilles Depatie84cb1e72018-10-26 12:41:33 -040077 output = open(logDir + '/' + logFile, 'w')
Gilles Depatie1be639b2018-12-06 10:51:08 -050078 child = pexpect.spawn('ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no karaf@localhost')
Gilles Depatie84cb1e72018-10-26 12:41:33 -040079 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 Depatie1be639b2018-12-06 10:51:08 -050091def 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 Depatie84cb1e72018-10-26 12:41:33 -040097def parseFields(statusLine):
98 statusList = statusLine.split("|")
99 return statusList
100
Gilles Depatie1be639b2018-12-06 10:51:08 -0500101def 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 Depatie84cb1e72018-10-26 12:41:33 -0400108def extractIpAddr(podName):
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
124def extractPodName(shortPodName):
125 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
126 stdout=subprocess.PIPE,
127 stderr=subprocess.PIPE)
128 proc2 = subprocess.Popen(['grep', '-e', shortPodName], 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
136 proc1.stdout.close
137 proc2.stdout.close
138 out, err = proc3.communicate()
139 return out
140