blob: 4a2b58b362f8bce9dd354ad529ec6fe2ffc0464e [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
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070018Library String
19Library DateTime
20Library Process
21Library Collections
22Library RequestsLibrary
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070023
24*** Keywords ***
You Wang88e1d852018-10-05 11:44:19 -070025Login And Run Command On Remote System
Suchitra Vemuri23ddbef2019-06-24 17:47:17 -070026 [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 -070027 [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
28 ${conn_id} Login To Remote System ${ip} ${user} ${pass} ${container_type} ${container_name} ${prompt} ${prompt_timeout} ${container_prompt}
29 ${output}= Run Command On Remote System ${cmd} ${conn_id} ${user} ${pass}
Kailash Khalasibf1478b2018-10-17 11:58:58 -070030 Log ${output}
You Wang88e1d852018-10-05 11:44:19 -070031 Logout From Remote System ${conn_id}
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070032 [Return] ${output}
33
You Wang88e1d852018-10-05 11:44:19 -070034Login To Remote System
You Wang5be816a2018-10-11 16:45:31 -070035 [Arguments] ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None} ${prompt}=~$ ${prompt_timeout}=15s ${container_prompt}=#
36 [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 -070037 ${conn_id}= SSHLibrary.Open Connection ${ip} prompt=${prompt} timeout=${prompt_timeout}
You Wang59ded6c2018-10-05 17:43:44 -070038 Run Keyword If '${pass}' != '${None}' SSHLibrary.Login ${user} ${pass}
39 ... ELSE SSHLibrary.Login With Public Key ${user} %{HOME}/.ssh/id_rsa
You Wang88e1d852018-10-05 11:44:19 -070040 # Login to the lxc container
You Wang5be816a2018-10-11 16:45:31 -070041 Run Keyword If '${container_type}' == 'LXC' Run Keywords
You Wang88e1d852018-10-05 11:44:19 -070042 ... SSHLibrary.Write lxc exec ${container_name} /bin/bash AND
43 ... SSHLibrary.Read Until ${container_prompt} AND
44 ... SSHLibrary.Set Client Configuration prompt=${container_prompt}
You Wang5be816a2018-10-11 16:45:31 -070045 # Login to the k8s container
46 Run Keyword If '${container_type}' == 'K8S' Run Keywords
47 ... SSHLibrary.Write kubectl -n $(kubectl get pods --all-namespaces | grep ${container_name} | awk '{print $1}') exec ${container_name} -it /bin/bash AND
48 ... SSHLibrary.Read Until ${container_prompt} AND
49 ... SSHLibrary.Set Client Configuration prompt=${container_prompt}
50 # Try to switch to root user
51 ${conn}= SSHLibrary.Get Connection ${conn_id}
52 Run Keyword And Ignore Error SSHLibrary.Write sudo -s
53 ${output}= SSHLibrary.Read Until Regexp \#|${conn.prompt}|password for ${user}:
54 Run Keyword If 'password for ${user}:' not in '''${output}''' Return From Keyword ${conn_id}
55 SSHLibrary.Set Client Configuration prompt=\#
56 SSHLibrary.Write ${pass}
57 SSHLibrary.Read Until Prompt
58 [Return] ${conn_id}
You Wang88e1d852018-10-05 11:44:19 -070059
60Logout From Remote System
61 [Arguments] ${conn_id}
62 [Documentation] Exit from the SSH session to a remote host
63 SSHLibrary.Switch Connection ${conn_id}
Kailash Khalasic930eac2018-09-05 12:18:23 -070064 SSHLibrary.Close Connection
You Wang88e1d852018-10-05 11:44:19 -070065
66Run Command On Remote System
You Wang5be816a2018-10-11 16:45:31 -070067 [Arguments] ${cmd} ${conn_id} ${user} ${pass}=${None}
You Wang88e1d852018-10-05 11:44:19 -070068 [Documentation] Executes a command on remote host and returns output
You Wang5be816a2018-10-11 16:45:31 -070069 ${conn}= SSHLibrary.Get Connection ${conn_id}
You Wang88e1d852018-10-05 11:44:19 -070070 SSHLibrary.Switch Connection ${conn_id}
71 SSHLibrary.Write ${cmd}
You Wang5be816a2018-10-11 16:45:31 -070072 ${output}= SSHLibrary.Read Until Regexp ${conn.prompt}|password for ${user}:
You Wang88e1d852018-10-05 11:44:19 -070073 Run Keyword If 'password for ${user}:' not in '''${output}''' Return From Keyword ${output}
74 SSHLibrary.Write ${pass}
75 ${output}= SSHlibrary.Read Until Prompt
76 [Return] ${output}
Kailash Khalasic930eac2018-09-05 12:18:23 -070077
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070078Execute Command on CIAB Server in Specific VM
Kailash Khalasi2adbad82017-05-15 14:53:40 -070079 [Arguments] ${system} ${vm} ${cmd} ${user}=${VM_USER} ${password}=${VM_PASS} ${prompt}=$ ${use_key}=True ${strip_line}=True
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070080 [Documentation] SSHs into ${HOST} where CIAB is running and executes a command in the Prod Vagrant VM where all the containers are running
81 ${conn_id}= SSHLibrary.Open Connection ${system} prompt=${prompt} timeout=300s
82 Run Keyword If '${use_key}' == 'False' SSHLibrary.Login ${user} ${pass} ELSE SSHLibrary.Login With Public Key ${user} %{HOME}/.ssh/${SSH_KEY} any
83 SSHLibrary.Write ssh ${vm}
84 SSHLibrary.Read Until Prompt
85 SSHLibrary.Write ${cmd}
86 ${output}= SSHLibrary.Read Until Prompt
87 SSHLibrary.Close Connection
Kailash Khalasi2adbad82017-05-15 14:53:40 -070088 ${output_1}= Run Keyword If '${strip_line}' == 'True' Get Line ${output} 0
89 ${output}= Set Variable If '${strip_line}' == 'True' ${output_1} ${output}
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070090 [Return] ${output}
91
92Execute Command on Compute Node in CIAB
Kailash Khalasif6de2592017-09-13 13:37:14 -070093 [Arguments] ${system} ${node} ${hostname} ${cmd} ${user}=${VM_USER} ${password}=${VM_PASS} ${prompt}=$ ${use_key}=True
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070094 [Documentation] SSHs into ${HOST} where CIAB is running and executes a command in the Prod Vagrant VM where all the containers are running
95 ${conn_id}= SSHLibrary.Open Connection ${system} prompt=${prompt} timeout=300s
96 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 -070097 SSHLibrary.Write ssh ${node}
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070098 SSHLibrary.Read Until Prompt
99 SSHLibrary.Write ssh root@${hostname}
100 SSHLibrary.Read Until \#
101 SSHLibrary.Write ${cmd}
102 ${output}= SSHLibrary.Read Until \#
103 SSHLibrary.Close Connection
104 [Return] ${output}
105
Kailash Khalasi3a4e0f52017-06-01 16:35:59 -0700106Execute Command Locally
107 [Arguments] ${cmd}
108 ${output}= Run ${cmd}
109 [Return] ${output}
110
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700111Execute ONOS Command
112 [Arguments] ${onos} ${port} ${cmd} ${user}=karaf ${pass}=karaf
113 ${conn_id}= SSHLibrary.Open Connection ${onos} port=${port} prompt=onos> timeout=300s
114 SSHLibrary.Login ${user} ${pass}
115 ${output}= SSHLibrary.Execute Command ${cmd}
116 SSHLibrary.Close Connection
117 [Return] ${output}
118
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700119Get Docker Container ID
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700120 [Arguments] ${container_name}
121 [Documentation] Retrieves the id of the requested docker container running inside headnode
122 ${container_id}= Run docker ps | grep ${container_name} | awk '{print $1}'
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700123 Log ${container_id}
124 [Return] ${container_id}
125
126Get Docker Logs
127 [Arguments] ${system} ${container_id} ${user}=${USER} ${password}=${PASSWD} ${prompt}=prod:~$
128 [Documentation] Retrieves the id of the requested docker container running inside given ${HOST}
129 ##In Ciab, all containers are run in the prod vm so we must log into that
130 ${conn_id}= SSHLibrary.Open Connection ${system} prompt=$ timeout=300s
131 SSHLibrary.Login With Public Key ${USER} %{HOME}/.ssh/${SSH_KEY} any
132 #SSHLibrary.Login ${HOST_USER} ${HOST_PASSWORD}
Kailash Khalasif6de2592017-09-13 13:37:14 -0700133 SSHLibrary.Write ssh head1
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700134 SSHLibrary.Read Until ${prompt}
135 SSHLibrary.Write docker logs -t ${container_id}
136 ${container_logs}= SSHLibrary.Read Until ${prompt}
137 SSHLibrary.Close Connection
138 Log ${container_logs}
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700139 [Return] ${container_logs}
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700140
141Remove Value From List
142 [Arguments] ${list} ${val}
143 ${length}= Get Length ${list}
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700144 : FOR ${INDEX} IN RANGE 0 ${length}
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700145 \ Log ${list[${INDEX}]}
146 \ ${value}= Get Dictionary Values ${list[${INDEX}]}
147 \ Log ${value[0]}
148 \ Run Keyword If '${value[0]}' == '${val}' Remove From List ${list} ${INDEX}
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700149 \ Run Keyword If '${value[0]}' == '${val}' Exit For Loop
150
151Test Ping
Kailash Khalasi422d1ab2018-09-18 09:54:26 -0700152 [Arguments] ${status} ${src} ${user} ${pass} ${dest} ${interface} ${prompt}=$ ${prompt_timeout}=60s
Kailash Khalasi55ca9f22018-09-17 17:36:55 -0700153 [Documentation] SSH's into src and attempts to ping dest. Status determines if ping should pass | fail
154 ${conn_id}= SSHLibrary.Open Connection ${src} prompt=${prompt} timeout=${prompt_timeout}
155 SSHLibrary.Login ${user} ${pass}
Kailash Khalasi422d1ab2018-09-18 09:54:26 -0700156 ${result}= SSHLibrary.Execute Command ping -I ${interface} -c 5 ${dest}
Kailash Khalasi55ca9f22018-09-17 17:36:55 -0700157 SSHLibrary.Close Connection
158 Log ${result}
159 Run Keyword If '${status}' == 'PASS' Should Contain ${result} 64 bytes
160 Run Keyword If '${status}' == 'PASS' Should Contain ${result} 0% packet loss
161 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 100% packet loss
162 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 80% packet loss
163 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 60% packet loss
164 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 40% packet loss
165 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 20% packet loss
166 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} Destination Host Unreachable
167 Run Keyword If '${status}' == 'FAIL' Should Not Contain ${result} 64 bytes
168 Run Keyword If '${status}' == 'FAIL' Should Contain ${result} 100% packet loss
169 Log To Console \n ${result}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700170
171Clean Up Objects
172 [Arguments] ${model_api}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700173 @{ids}= Create List
174 ${resp}= CORD Get ${model_api}
175 ${jsondata}= To Json ${resp.content}
176 Log ${jsondata}
177 ${length}= Get Length ${jsondata['items']}
178 : FOR ${INDEX} IN RANGE 0 ${length}
179 \ ${value}= Get From List ${jsondata['items']} ${INDEX}
180 \ ${id}= Get From Dictionary ${value} id
181 \ Append To List ${ids} ${id}
182 : FOR ${i} IN @{ids}
183 \ CORD Delete ${model_api} ${i}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700184
185CORD Get
186 [Documentation] Make a GET call to XOS
187 [Arguments] ${service}
188 ${resp}= Get Request ${server_ip} ${service}
189 Log ${resp.content}
190 Should Be Equal As Strings ${resp.status_code} 200
191 [Return] ${resp}
192
Kailash Khalasif3e0a9f2018-09-18 13:43:42 -0700193CORD Post
194 [Documentation] Make a POST call to XOS
195 [Arguments] ${service} ${data}
196 ${data}= Evaluate json.dumps(${data}) json
197 ${resp}= Post Request ${SERVER_IP} uri=${service} data=${data}
198 Log ${resp.content}
199 Should Be Equal As Strings ${resp.status_code} 200
200 [Return] ${resp}
201
Kailash Khalasi16d95c12018-09-21 14:17:28 -0700202CORD Put
203 [Documentation] Make a PUT call to XOS
204 [Arguments] ${service} ${data} ${data_id}
205 ${data}= Evaluate json.dumps(${data}) json
206 ${resp}= Put Request ${SERVER_IP} uri=${service}/${data_id} data=${data}
207 Log ${resp.content}
208 Should Be Equal As Strings ${resp.status_code} 200
209 ${id}= Get Json Value ${resp.content} /id
210 Set Suite Variable ${id}
211 [Return] ${resp}
212
Kailash Khalasic930eac2018-09-05 12:18:23 -0700213CORD Delete
214 [Documentation] Make a DELETE call to XOS
215 [Arguments] ${service} ${data_id}
216 ${resp}= Delete Request ${SERVER_IP} uri=${service}/${data_id}
217 Log ${resp.content}
218 Should Be Equal As Strings ${resp.status_code} 200
219 [Return] ${resp}
Kailash Khalasia0810ce2018-09-10 13:03:27 -0700220
Kailash Khalasif3e0a9f2018-09-18 13:43:42 -0700221Get Service Owner Id
222 [Arguments] ${service}
223 ${resp}= CORD Get ${service}
224 ${jsondata}= To Json ${resp.content}
225 log ${jsondata}
226 ${length}= Get Length ${jsondata['items']}
227 : for ${INDEX} IN RANGE 0 ${length}
228 \ ${value}= Get From List ${jsondata['items']} ${INDEX}
229 \ ${id}= Get From Dictionary ${value} id
230 [Return] ${id}
231
Kailash Khalasia0810ce2018-09-10 13:03:27 -0700232Kill Linux Process
You Wang5be816a2018-10-11 16:45:31 -0700233 [Arguments] ${process} ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None}
234 ${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 -0700235 Should Be Equal As Integers ${rc} 0
You Wangaee35112018-09-28 16:07:49 -0700236
You Wang0c2b3662018-10-01 16:56:17 -0700237Check Remote File Contents
You Wang5be816a2018-10-11 16:45:31 -0700238 [Arguments] ${file_should_exist} ${file} ${pattern} ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None} ${prompt}=~$
239 ${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 -0700240 Run Keyword If '${file_should_exist}' == 'True' Should Contain ${output} ${pattern}
241 ... ELSE Should Not Contain ${output} ${pattern}
242
243Check Ping
You Wang5be816a2018-10-11 16:45:31 -0700244 [Arguments] ${ping_should_pass} ${dst_ip} ${iface} ${ip} ${user} ${pass}=${None} ${container_type}=${None} ${container_name}=${None}
245 ${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 -0700246 Check Ping Result ${ping_should_pass} ${result}
You Wang0c2b3662018-10-01 16:56:17 -0700247
248Check Remote System Reachability
You Wang88e1d852018-10-05 11:44:19 -0700249 [Arguments] ${reachable} ${ip}
You Wang0c2b3662018-10-01 16:56:17 -0700250 [Documentation] Check if the specified IP address is reachable or not
You Wangf2ddde62019-02-13 16:53:10 -0800251 ${result}= Run ping -c 3 ${ip}
You Wang88e1d852018-10-05 11:44:19 -0700252 Check Ping Result ${reachable} ${result}
253
254Check Ping Result
255 [Arguments] ${reachable} ${result}
You Wang0c2b3662018-10-01 16:56:17 -0700256 Run Keyword If '${reachable}' == 'True' Should Contain ${result} 64 bytes
257 Run Keyword If '${reachable}' == 'True' Should Contain Any ${result} 0% packet loss 0.0% packet loss
258 Run Keyword If '${reachable}' == 'True' Should Not Contain Any ${result} 100% packet loss 100.0% packet loss
259 Run Keyword If '${reachable}' == 'False' Should Not Contain ${result} 64 bytes
260 Run Keyword If '${reachable}' == 'False' Should Contain Any ${result} 100% packet loss 100.0% packet loss
Kailashf49efeb2019-05-23 12:12:41 -0700261
262Set Deployment Config Variables
263 [Documentation] Parses through the given deployment config and sets all the "src" and "dst" variables
264 ${source}= Evaluate ${hosts}.get("src")
265 ${length_src}= Get Length ${source}
266 ${src}= Set Variable src
267 : FOR ${INDEX} IN RANGE 0 ${length_src}
268 \ Set Suite Variable ${${src}${INDEX}} ${source[${INDEX}]}
269 ${destination}= Evaluate ${hosts}.get("dst")
270 ${length_dst}= Get Length ${destination}
271 ${dst}= Set Variable dst
272 : FOR ${INDEX} IN RANGE 0 ${length_dst}
273 \ Set Suite Variable ${${dst}${INDEX}} ${destination[${INDEX}]}
274