blob: 3ca5f616f2b1c15dc6a0537e72bcc4073fa91b13 [file] [log] [blame]
Zack Williamsc6722d52020-01-13 16:34:33 -07001#
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"""
18Test Case Utils module
19"""
20
21from __future__ import absolute_import
22
23import time
24import subprocess
25import pexpect
26import sys
27
28
29def config_dirs(self, log_dir, root_dir=None, voltha_dir=None):
30 self.dirs["log"] = log_dir
31 self.dirs["root"] = root_dir
32 self.dirs["voltha"] = voltha_dir
33
34
35def get_dir(self, directory):
36 return self.dirs.get(directory)
37
38
39def remove_leading_line(log_dir, log_file):
40 with open(log_dir + "/" + log_file, "r+") as FILE:
41 lines = FILE.readlines()
42 FILE.seek(0)
43 lines = lines[1:]
44 for line in lines:
45 FILE.write(line)
46 FILE.truncate()
47 FILE.close()
48
49
50def send_command_to_voltha_cli(
51 log_dir,
52 log_file1,
53 cmd1,
54 log_file2=None,
55 cmd2=None,
56 log_file3=None,
57 cmd3=None,
58 host="localhost",
59):
60 output = open(log_dir + "/" + log_file1, "w")
61 child = pexpect.spawn(
62 "ssh -p 30110 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no voltha@%s"
63 % host
64 )
65 child.expect(r"[pP]assword:")
66 child.sendline("admin")
Zack Williams9a37e8c2020-01-14 13:56:33 -070067 child.expect(r"\((\x1b\[\d*;?\d+m){1,2}voltha(\x1b\[\d*;?\d+m){1,2}\)")
Zack Williamsc6722d52020-01-13 16:34:33 -070068 time.sleep(10)
69 child.sendline(cmd1)
70 i = child.expect(
71 [
Zack Williams9a37e8c2020-01-14 13:56:33 -070072 r"\((\x1b\[\d*;?\d+m){1,2}voltha(\x1b\[\d*;?\d+m){1,2}\)",
73 r"\((\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\x1b\[\d*;?\d+m){1,2}\)",
Zack Williamsc6722d52020-01-13 16:34:33 -070074 ]
75 )
76 if i == 0:
77 output.write(child.before)
78 output.close()
79 remove_leading_line(log_dir, log_file1)
80 elif i == 1:
81 if log_file2 is not None and cmd2 is not None:
82 output = open(log_dir + "/" + log_file2, "w")
83 child.sendline(cmd2)
84 child.expect(
Zack Williams9a37e8c2020-01-14 13:56:33 -070085 r"\((\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\x1b\[\d*;?\d+m){1,2}\)"
Zack Williamsc6722d52020-01-13 16:34:33 -070086 )
87 output.write(child.before)
88 output.close()
89 remove_leading_line(log_dir, log_file2)
90 if log_file3 is not None and cmd3 is not None:
91 output = open(log_dir + "/" + log_file3, "w")
92 child.sendline(cmd3)
93 child.expect(
Zack Williams9a37e8c2020-01-14 13:56:33 -070094 r"\((\x1b\[\d*;?\d+m){1,2}.*device [0-9a-f]{16}(\x1b\[\d*;?\d+m){1,2}\)"
Zack Williamsc6722d52020-01-13 16:34:33 -070095 )
96 output.write(child.before)
97 output.close()
98 remove_leading_line(log_dir, log_file3)
99 child.close()
100
101
102def send_command_to_onos_cli(log_dir, log_file, cmd, host="localhost"):
103 output = open(log_dir + "/" + log_file, "w")
104 child = pexpect.spawn(
105 "ssh -p 30115 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no karaf@%s"
106 % host
107 )
108 child.expect(r"[pP]assword:")
109 child.sendline("karaf")
Zack Williams9a37e8c2020-01-14 13:56:33 -0700110 child.expect(r"(\x1b\[\d*;?\d+m){1,2}(onos>|karaf@root >) (\x1b\[\d*;?\d+m){1,2}")
Zack Williamsc6722d52020-01-13 16:34:33 -0700111 child.sendline(cmd)
Zack Williams9a37e8c2020-01-14 13:56:33 -0700112 child.expect(r"(\x1b\[\d*;?\d+m){1,2}(onos>|karaf@root >) (\x1b\[\d*;?\d+m){1,2}")
Zack Williamsc6722d52020-01-13 16:34:33 -0700113
114 output.write(child.before)
115
116 output.close()
117 child.close()
118
119
120def get_fields_from_grep_command(self, search_word, log_file):
121 grepCommand = "grep %s %s/%s" % (search_word, get_dir(self, "log"), log_file)
122 statusLines = subprocess.getstatusoutput(grepCommand)[1]
123 return statusLines
124
125
126def parse_fields(status_line, delimiter):
127 statusList = status_line.split(delimiter)
128 return statusList
129
130
131def print_log_file(self, log_file):
132 with open(get_dir(self, "log") + "/" + log_file, "r+") as FILE:
133 lines = FILE.readlines()
134 print
135 for line in lines:
136 sys.stdout.write(line)
137
138
139def extract_pod_ip_addr(pod_name):
140 proc1 = subprocess.Popen(
141 ["/usr/bin/kubectl", "get", "svc", "--all-namespaces"],
142 stdout=subprocess.PIPE,
143 stderr=subprocess.PIPE,
144 )
145 proc2 = subprocess.Popen(
146 ["grep", "-e", pod_name],
147 stdin=proc1.stdout,
148 stdout=subprocess.PIPE,
149 stderr=subprocess.PIPE,
150 )
151 proc3 = subprocess.Popen(
152 ["awk", "{print $4}"],
153 stdin=proc2.stdout,
154 stdout=subprocess.PIPE,
155 stderr=subprocess.PIPE,
156 )
157
158 proc1.stdout.close()
159 proc2.stdout.close()
160 out, err = proc3.communicate()
161 return out
162
163
164def extract_radius_ip_addr(pod_name):
165 proc1 = subprocess.Popen(
166 ["/usr/bin/kubectl", "describe", "pod", "-n", "voltha", pod_name],
167 stdout=subprocess.PIPE,
168 stderr=subprocess.PIPE,
169 )
170 proc2 = subprocess.Popen(
171 ["grep", "^IP:"],
172 stdin=proc1.stdout,
173 stdout=subprocess.PIPE,
174 stderr=subprocess.PIPE,
175 )
176 proc3 = subprocess.Popen(
177 ["awk", "{print $2}"],
178 stdin=proc2.stdout,
179 stdout=subprocess.PIPE,
180 stderr=subprocess.PIPE,
181 )
182
183 proc1.stdout.close()
184 proc2.stdout.close()
185 out, err = proc3.communicate()
186 return out
187
188
189def extract_pod_name(short_pod_name):
190 proc1 = subprocess.Popen(
191 ["/usr/bin/kubectl", "get", "pods", "--all-namespaces"],
192 stdout=subprocess.PIPE,
193 stderr=subprocess.PIPE,
194 )
195 proc2 = subprocess.Popen(
196 ["grep", "-e", short_pod_name],
197 stdin=proc1.stdout,
198 stdout=subprocess.PIPE,
199 stderr=subprocess.PIPE,
200 )
201 proc3 = subprocess.Popen(
202 ["awk", "{print $2}"],
203 stdin=proc2.stdout,
204 stdout=subprocess.PIPE,
205 stderr=subprocess.PIPE,
206 )
207
208 proc1.stdout.close()
209 proc2.stdout.close()
210 out, err = proc3.communicate()
211 return out
212
213
214def modify_radius_ip_in_json_using_sed(self, new_ip_addr):
215 sedCommand = (
216 "sed -i '/radiusIp/c\\ \"radiusIp\":\"'%s'\",' %s/tests/atests/build/aaa_json"
217 % (new_ip_addr, get_dir(self, "voltha"))
218 )
219 status = subprocess.getstatusoutput(sedCommand)[0]
220 return status
221
222
223def discover_rg_pod_name():
224 return extract_pod_name("rg0").strip()
225
226
227def retrieve_authorized_users_device_id_and_port_number(status_line):
228 fields = parse_fields(status_line, ",")
229 deviceField = fields[2].strip()
230 deviceStr, equal, deviceId = deviceField.partition("=")
231 device_Id = deviceId
232 portField = fields[4].strip()
233 portNumStr, equal, portNum = portField.partition("=")
234 portNumber = portNum
235 return device_Id, portNumber
236
237
238def add_subscriber_access(self, device_id, port_number):
239 send_command_to_onos_cli(
240 get_dir(self, "log"),
241 "voltha_add_subscriber_access.log",
242 "volt-add-subscriber-access %s %s" % (device_id, port_number),
243 )