blob: 5f9727fada5f6495486951a6f153c343279ced2b [file] [log] [blame]
Gunaseelaned0eb1b2017-08-08 08:12:12 -05001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070015*** Settings ***
16Documentation Library for various utilities
17Library SSHLibrary
18Library HttpLibrary.HTTP
19Library String
20Library DateTime
21Library Process
22Library Collections
23Library RequestsLibrary
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070024
25*** Keywords ***
You Wang88e1d852018-10-05 11:44:19 -070026Login And Run Command On Remote System
Suchitra Vemuri23ddbef2019-06-24 17:47:17 -070027 [Arguments] ${cmd} ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None} ${prompt}=~$ ${prompt_timeout}=50s ${container_prompt}=#
You Wang5be816a2018-10-11 16:45:31 -070028 [Documentation] SSH's into a remote host (and logs into the container if container_type and container_name are specified), tries to switch to root user and executes a command and returns output
29 ${conn_id} Login To Remote System ${ip} ${user} ${pass} ${container_type} ${container_name} ${prompt} ${prompt_timeout} ${container_prompt}
30 ${output}= Run Command On Remote System ${cmd} ${conn_id} ${user} ${pass}
Kailash Khalasibf1478b2018-10-17 11:58:58 -070031 Log ${output}
You Wang88e1d852018-10-05 11:44:19 -070032 Logout From Remote System ${conn_id}
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070033 [Return] ${output}
34
You Wang88e1d852018-10-05 11:44:19 -070035Login To Remote System
You Wang5be816a2018-10-11 16:45:31 -070036 [Arguments] ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None} ${prompt}=~$ ${prompt_timeout}=15s ${container_prompt}=#
37 [Documentation] SSH's into a remote host (and logs into the container if container_type and container_name are specified) and returns connection ID
Kailash Khalasic930eac2018-09-05 12:18:23 -070038 ${conn_id}= SSHLibrary.Open Connection ${ip} prompt=${prompt} timeout=${prompt_timeout}
You Wang59ded6c2018-10-05 17:43:44 -070039 Run Keyword If '${pass}' != '${None}' SSHLibrary.Login ${user} ${pass}
40 ... ELSE SSHLibrary.Login With Public Key ${user} %{HOME}/.ssh/id_rsa
You Wang88e1d852018-10-05 11:44:19 -070041 # Login to the lxc container
You Wang5be816a2018-10-11 16:45:31 -070042 Run Keyword If '${container_type}' == 'LXC' Run Keywords
You Wang88e1d852018-10-05 11:44:19 -070043 ... SSHLibrary.Write lxc exec ${container_name} /bin/bash AND
44 ... SSHLibrary.Read Until ${container_prompt} AND
45 ... SSHLibrary.Set Client Configuration prompt=${container_prompt}
You Wang5be816a2018-10-11 16:45:31 -070046 # Login to the k8s container
47 Run Keyword If '${container_type}' == 'K8S' Run Keywords
48 ... SSHLibrary.Write kubectl -n $(kubectl get pods --all-namespaces | grep ${container_name} | awk '{print $1}') exec ${container_name} -it /bin/bash AND
49 ... SSHLibrary.Read Until ${container_prompt} AND
50 ... SSHLibrary.Set Client Configuration prompt=${container_prompt}
51 # Try to switch to root user
52 ${conn}= SSHLibrary.Get Connection ${conn_id}
53 Run Keyword And Ignore Error SSHLibrary.Write sudo -s
54 ${output}= SSHLibrary.Read Until Regexp \#|${conn.prompt}|password for ${user}:
55 Run Keyword If 'password for ${user}:' not in '''${output}''' Return From Keyword ${conn_id}
56 SSHLibrary.Set Client Configuration prompt=\#
57 SSHLibrary.Write ${pass}
58 SSHLibrary.Read Until Prompt
59 [Return] ${conn_id}
You Wang88e1d852018-10-05 11:44:19 -070060
61Logout From Remote System
62 [Arguments] ${conn_id}
63 [Documentation] Exit from the SSH session to a remote host
64 SSHLibrary.Switch Connection ${conn_id}
Kailash Khalasic930eac2018-09-05 12:18:23 -070065 SSHLibrary.Close Connection
You Wang88e1d852018-10-05 11:44:19 -070066
67Run Command On Remote System
You Wang5be816a2018-10-11 16:45:31 -070068 [Arguments] ${cmd} ${conn_id} ${user} ${pass}=${None}
You Wang88e1d852018-10-05 11:44:19 -070069 [Documentation] Executes a command on remote host and returns output
You Wang5be816a2018-10-11 16:45:31 -070070 ${conn}= SSHLibrary.Get Connection ${conn_id}
You Wang88e1d852018-10-05 11:44:19 -070071 SSHLibrary.Switch Connection ${conn_id}
72 SSHLibrary.Write ${cmd}
You Wang5be816a2018-10-11 16:45:31 -070073 ${output}= SSHLibrary.Read Until Regexp ${conn.prompt}|password for ${user}:
You Wang88e1d852018-10-05 11:44:19 -070074 Run Keyword If 'password for ${user}:' not in '''${output}''' Return From Keyword ${output}
75 SSHLibrary.Write ${pass}
76 ${output}= SSHlibrary.Read Until Prompt
77 [Return] ${output}
Kailash Khalasic930eac2018-09-05 12:18:23 -070078
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070079Execute Command on CIAB Server in Specific VM
Kailash Khalasi2adbad82017-05-15 14:53:40 -070080 [Arguments] ${system} ${vm} ${cmd} ${user}=${VM_USER} ${password}=${VM_PASS} ${prompt}=$ ${use_key}=True ${strip_line}=True
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070081 [Documentation] SSHs into ${HOST} where CIAB is running and executes a command in the Prod Vagrant VM where all the containers are running
82 ${conn_id}= SSHLibrary.Open Connection ${system} prompt=${prompt} timeout=300s
83 Run Keyword If '${use_key}' == 'False' SSHLibrary.Login ${user} ${pass} ELSE SSHLibrary.Login With Public Key ${user} %{HOME}/.ssh/${SSH_KEY} any
84 SSHLibrary.Write ssh ${vm}
85 SSHLibrary.Read Until Prompt
86 SSHLibrary.Write ${cmd}
87 ${output}= SSHLibrary.Read Until Prompt
88 SSHLibrary.Close Connection
Kailash Khalasi2adbad82017-05-15 14:53:40 -070089 ${output_1}= Run Keyword If '${strip_line}' == 'True' Get Line ${output} 0
90 ${output}= Set Variable If '${strip_line}' == 'True' ${output_1} ${output}
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070091 [Return] ${output}
92
93Execute Command on Compute Node in CIAB
Kailash Khalasif6de2592017-09-13 13:37:14 -070094 [Arguments] ${system} ${node} ${hostname} ${cmd} ${user}=${VM_USER} ${password}=${VM_PASS} ${prompt}=$ ${use_key}=True
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070095 [Documentation] SSHs into ${HOST} where CIAB is running and executes a command in the Prod Vagrant VM where all the containers are running
96 ${conn_id}= SSHLibrary.Open Connection ${system} prompt=${prompt} timeout=300s
97 Run Keyword If '${use_key}' == 'False' SSHLibrary.Login ${user} ${pass} ELSE SSHLibrary.Login With Public Key ${user} %{HOME}/.ssh/${SSH_KEY} any
Kailash Khalasif6de2592017-09-13 13:37:14 -070098 SSHLibrary.Write ssh ${node}
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070099 SSHLibrary.Read Until Prompt
100 SSHLibrary.Write ssh root@${hostname}
101 SSHLibrary.Read Until \#
102 SSHLibrary.Write ${cmd}
103 ${output}= SSHLibrary.Read Until \#
104 SSHLibrary.Close Connection
105 [Return] ${output}
106
Kailash Khalasi3a4e0f52017-06-01 16:35:59 -0700107Execute Command Locally
108 [Arguments] ${cmd}
109 ${output}= Run ${cmd}
110 [Return] ${output}
111
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700112Execute ONOS Command
113 [Arguments] ${onos} ${port} ${cmd} ${user}=karaf ${pass}=karaf
114 ${conn_id}= SSHLibrary.Open Connection ${onos} port=${port} prompt=onos> timeout=300s
115 SSHLibrary.Login ${user} ${pass}
116 ${output}= SSHLibrary.Execute Command ${cmd}
117 SSHLibrary.Close Connection
118 [Return] ${output}
119
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700120Get Docker Container ID
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700121 [Arguments] ${container_name}
122 [Documentation] Retrieves the id of the requested docker container running inside headnode
123 ${container_id}= Run docker ps | grep ${container_name} | awk '{print $1}'
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700124 Log ${container_id}
125 [Return] ${container_id}
126
127Get Docker Logs
128 [Arguments] ${system} ${container_id} ${user}=${USER} ${password}=${PASSWD} ${prompt}=prod:~$
129 [Documentation] Retrieves the id of the requested docker container running inside given ${HOST}
130 ##In Ciab, all containers are run in the prod vm so we must log into that
131 ${conn_id}= SSHLibrary.Open Connection ${system} prompt=$ timeout=300s
132 SSHLibrary.Login With Public Key ${USER} %{HOME}/.ssh/${SSH_KEY} any
133 #SSHLibrary.Login ${HOST_USER} ${HOST_PASSWORD}
Kailash Khalasif6de2592017-09-13 13:37:14 -0700134 SSHLibrary.Write ssh head1
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700135 SSHLibrary.Read Until ${prompt}
136 SSHLibrary.Write docker logs -t ${container_id}
137 ${container_logs}= SSHLibrary.Read Until ${prompt}
138 SSHLibrary.Close Connection
139 Log ${container_logs}
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700140 [Return] ${container_logs}
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700141
142Remove Value From List
143 [Arguments] ${list} ${val}
144 ${length}= Get Length ${list}
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700145 : FOR ${INDEX} IN RANGE 0 ${length}
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700146 \ Log ${list[${INDEX}]}
147 \ ${value}= Get Dictionary Values ${list[${INDEX}]}
148 \ Log ${value[0]}
149 \ Run Keyword If '${value[0]}' == '${val}' Remove From List ${list} ${INDEX}
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700150 \ Run Keyword If '${value[0]}' == '${val}' Exit For Loop
151
152Test Ping
Kailash Khalasi422d1ab2018-09-18 09:54:26 -0700153 [Arguments] ${status} ${src} ${user} ${pass} ${dest} ${interface} ${prompt}=$ ${prompt_timeout}=60s
Kailash Khalasi55ca9f22018-09-17 17:36:55 -0700154 [Documentation] SSH's into src and attempts to ping dest. Status determines if ping should pass | fail
155 ${conn_id}= SSHLibrary.Open Connection ${src} prompt=${prompt} timeout=${prompt_timeout}
156 SSHLibrary.Login ${user} ${pass}
Kailash Khalasi422d1ab2018-09-18 09:54:26 -0700157 ${result}= SSHLibrary.Execute Command ping -I ${interface} -c 5 ${dest}
Kailash Khalasi55ca9f22018-09-17 17:36:55 -0700158 SSHLibrary.Close Connection
159 Log ${result}
160 Run Keyword If '${status}' == 'PASS' Should Contain ${result} 64 bytes
161 Run Keyword If '${status}' == 'PASS' Should Contain ${result} 0% packet loss
162 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 100% packet loss
163 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 80% packet loss
164 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 60% packet loss
165 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 40% packet loss
166 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 20% packet loss
167 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} Destination Host Unreachable
168 Run Keyword If '${status}' == 'FAIL' Should Not Contain ${result} 64 bytes
169 Run Keyword If '${status}' == 'FAIL' Should Contain ${result} 100% packet loss
170 Log To Console \n ${result}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700171
172Clean Up Objects
173 [Arguments] ${model_api}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700174 @{ids}= Create List
175 ${resp}= CORD Get ${model_api}
176 ${jsondata}= To Json ${resp.content}
177 Log ${jsondata}
178 ${length}= Get Length ${jsondata['items']}
179 : FOR ${INDEX} IN RANGE 0 ${length}
180 \ ${value}= Get From List ${jsondata['items']} ${INDEX}
181 \ ${id}= Get From Dictionary ${value} id
182 \ Append To List ${ids} ${id}
183 : FOR ${i} IN @{ids}
184 \ CORD Delete ${model_api} ${i}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700185
186CORD Get
187 [Documentation] Make a GET call to XOS
188 [Arguments] ${service}
189 ${resp}= Get Request ${server_ip} ${service}
190 Log ${resp.content}
191 Should Be Equal As Strings ${resp.status_code} 200
192 [Return] ${resp}
193
Kailash Khalasif3e0a9f2018-09-18 13:43:42 -0700194CORD Post
195 [Documentation] Make a POST call to XOS
196 [Arguments] ${service} ${data}
197 ${data}= Evaluate json.dumps(${data}) json
198 ${resp}= Post Request ${SERVER_IP} uri=${service} data=${data}
199 Log ${resp.content}
200 Should Be Equal As Strings ${resp.status_code} 200
201 [Return] ${resp}
202
Kailash Khalasi16d95c12018-09-21 14:17:28 -0700203CORD Put
204 [Documentation] Make a PUT call to XOS
205 [Arguments] ${service} ${data} ${data_id}
206 ${data}= Evaluate json.dumps(${data}) json
207 ${resp}= Put Request ${SERVER_IP} uri=${service}/${data_id} data=${data}
208 Log ${resp.content}
209 Should Be Equal As Strings ${resp.status_code} 200
210 ${id}= Get Json Value ${resp.content} /id
211 Set Suite Variable ${id}
212 [Return] ${resp}
213
Kailash Khalasic930eac2018-09-05 12:18:23 -0700214CORD Delete
215 [Documentation] Make a DELETE call to XOS
216 [Arguments] ${service} ${data_id}
217 ${resp}= Delete Request ${SERVER_IP} uri=${service}/${data_id}
218 Log ${resp.content}
219 Should Be Equal As Strings ${resp.status_code} 200
220 [Return] ${resp}
Kailash Khalasia0810ce2018-09-10 13:03:27 -0700221
Kailash Khalasif3e0a9f2018-09-18 13:43:42 -0700222Get Service Owner Id
223 [Arguments] ${service}
224 ${resp}= CORD Get ${service}
225 ${jsondata}= To Json ${resp.content}
226 log ${jsondata}
227 ${length}= Get Length ${jsondata['items']}
228 : for ${INDEX} IN RANGE 0 ${length}
229 \ ${value}= Get From List ${jsondata['items']} ${INDEX}
230 \ ${id}= Get From Dictionary ${value} id
231 [Return] ${id}
232
Kailash Khalasia0810ce2018-09-10 13:03:27 -0700233Kill Linux Process
You Wang5be816a2018-10-11 16:45:31 -0700234 [Arguments] ${process} ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None}
235 ${rc}= Login And Run Command On Remote System kill $(ps aux | grep '${process}' | awk '{print $2}'); echo $? ${ip} ${user} ${pass} ${container_type} ${container_name}
You Wang88e1d852018-10-05 11:44:19 -0700236 Should Be Equal As Integers ${rc} 0
You Wangaee35112018-09-28 16:07:49 -0700237
You Wang0c2b3662018-10-01 16:56:17 -0700238Check Remote File Contents
You Wang5be816a2018-10-11 16:45:31 -0700239 [Arguments] ${file_should_exist} ${file} ${pattern} ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None} ${prompt}=~$
240 ${output}= Login And Run Command On Remote System cat ${file} | grep '${pattern}' ${ip} ${user} ${pass} ${container_type} ${container_name} ${prompt}
You Wang88e1d852018-10-05 11:44:19 -0700241 Run Keyword If '${file_should_exist}' == 'True' Should Contain ${output} ${pattern}
242 ... ELSE Should Not Contain ${output} ${pattern}
243
244Check Ping
You Wang5be816a2018-10-11 16:45:31 -0700245 [Arguments] ${ping_should_pass} ${dst_ip} ${iface} ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None}
246 ${result}= Login And Run Command On Remote System ping -I ${iface} -c 3 ${dst_ip} ${ip} ${user} ${pass} ${container_type} ${container_name}
You Wang88e1d852018-10-05 11:44:19 -0700247 Check Ping Result ${ping_should_pass} ${result}
You Wang0c2b3662018-10-01 16:56:17 -0700248
249Check Remote System Reachability
You Wang88e1d852018-10-05 11:44:19 -0700250 [Arguments] ${reachable} ${ip}
You Wang0c2b3662018-10-01 16:56:17 -0700251 [Documentation] Check if the specified IP address is reachable or not
You Wangf2ddde62019-02-13 16:53:10 -0800252 ${result}= Run ping -c 3 ${ip}
You Wang88e1d852018-10-05 11:44:19 -0700253 Check Ping Result ${reachable} ${result}
254
255Check Ping Result
256 [Arguments] ${reachable} ${result}
You Wang0c2b3662018-10-01 16:56:17 -0700257 Run Keyword If '${reachable}' == 'True' Should Contain ${result} 64 bytes
258 Run Keyword If '${reachable}' == 'True' Should Contain Any ${result} 0% packet loss 0.0% packet loss
259 Run Keyword If '${reachable}' == 'True' Should Not Contain Any ${result} 100% packet loss 100.0% packet loss
260 Run Keyword If '${reachable}' == 'False' Should Not Contain ${result} 64 bytes
261 Run Keyword If '${reachable}' == 'False' Should Contain Any ${result} 100% packet loss 100.0% packet loss
Kailashf49efeb2019-05-23 12:12:41 -0700262
263Set Deployment Config Variables
264 [Documentation] Parses through the given deployment config and sets all the "src" and "dst" variables
265 ${source}= Evaluate ${hosts}.get("src")
266 ${length_src}= Get Length ${source}
267 ${src}= Set Variable src
268 : FOR ${INDEX} IN RANGE 0 ${length_src}
269 \ Set Suite Variable ${${src}${INDEX}} ${source[${INDEX}]}
270 ${destination}= Evaluate ${hosts}.get("dst")
271 ${length_dst}= Get Length ${destination}
272 ${dst}= Set Variable dst
273 : FOR ${INDEX} IN RANGE 0 ${length_dst}
274 \ Set Suite Variable ${${dst}${INDEX}} ${destination[${INDEX}]}
275