blob: fe76687b77ca820c6faa0db462fe024053bf47e4 [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 Depatiea85fe812019-01-23 15:55:53 -0500108def extractPodIpAddr(podName):
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400109 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 Depatiea85fe812019-01-23 15:55:53 -0500124def 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 Depatie84cb1e72018-10-26 12:41:33 -0400140def 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 Depatiea85fe812019-01-23 15:55:53 -0500156
157def 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 Depatie84cb1e72018-10-26 12:41:33 -0400161