blob: 5bd801a436beac08f5f5a416e056533ee4a6c13d [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
27 [Arguments] ${cmd} ${ip} ${user} ${pass}=${EMPTY} ${key}=${EMPTY} ${container_name}=${EMPTY} ${prompt}=~$ ${prompt_timeout}=15s ${container_prompt}=~#
28 [Documentation] SSH's into a remote host and executes a command and returns output. If container_name is specified, login to the container before executing the command
29 ${conn_id} ${prompt}= Login To Remote System ${ip} ${user} ${pass} ${key} ${container_name} ${prompt} ${prompt_timeout} ${container_prompt}
30 ${output}= Run Command On Remote System ${cmd} ${conn_id} ${user} ${prompt} ${pass}
31 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
35 [Arguments] ${ip} ${user} ${pass}=${EMPTY} ${key}=${EMPTY} ${container_name}=${EMPTY} ${prompt}=~$ ${prompt_timeout}=15s ${container_prompt}=~#
36 [Documentation] SSH's into a remote host and returns connection ID. If container_name is specified, login to the container before returning
Kailash Khalasic930eac2018-09-05 12:18:23 -070037 ${conn_id}= SSHLibrary.Open Connection ${ip} prompt=${prompt} timeout=${prompt_timeout}
You Wang88e1d852018-10-05 11:44:19 -070038 Run Keyword If '${key}' != '${EMPTY}' SSHLibrary.Login With Public Key ${user} ${key}
39 ... ELSE SSHLibrary.Login ${user} ${pass}
40 # Login to the lxc container
41 Run Keyword If '${container_name}' != '${EMPTY}' Run Keywords
42 ... SSHLibrary.Write lxc exec ${container_name} /bin/bash AND
43 ... SSHLibrary.Read Until ${container_prompt} AND
44 ... SSHLibrary.Set Client Configuration prompt=${container_prompt}
45 ${prompt}= Set Variable If '${container_name}' != '${EMPTY}' ${container_prompt} ${prompt}
46 [Return] ${conn_id} ${prompt}
47
48Logout From Remote System
49 [Arguments] ${conn_id}
50 [Documentation] Exit from the SSH session to a remote host
51 SSHLibrary.Switch Connection ${conn_id}
Kailash Khalasic930eac2018-09-05 12:18:23 -070052 SSHLibrary.Close Connection
You Wang88e1d852018-10-05 11:44:19 -070053
54Run Command On Remote System
55 [Arguments] ${cmd} ${conn_id} ${user} ${prompt} ${pass}=${EMPTY}
56 [Documentation] Executes a command on remote host and returns output
57 SSHLibrary.Switch Connection ${conn_id}
58 SSHLibrary.Write ${cmd}
59 ${output}= SSHLibrary.Read Until Regexp ${prompt}|password for ${user}:
60 Run Keyword If 'password for ${user}:' not in '''${output}''' Return From Keyword ${output}
61 SSHLibrary.Write ${pass}
62 ${output}= SSHlibrary.Read Until Prompt
63 [Return] ${output}
Kailash Khalasic930eac2018-09-05 12:18:23 -070064
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070065Execute Command on CIAB Server in Specific VM
Kailash Khalasi2adbad82017-05-15 14:53:40 -070066 [Arguments] ${system} ${vm} ${cmd} ${user}=${VM_USER} ${password}=${VM_PASS} ${prompt}=$ ${use_key}=True ${strip_line}=True
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070067 [Documentation] SSHs into ${HOST} where CIAB is running and executes a command in the Prod Vagrant VM where all the containers are running
68 ${conn_id}= SSHLibrary.Open Connection ${system} prompt=${prompt} timeout=300s
69 Run Keyword If '${use_key}' == 'False' SSHLibrary.Login ${user} ${pass} ELSE SSHLibrary.Login With Public Key ${user} %{HOME}/.ssh/${SSH_KEY} any
70 SSHLibrary.Write ssh ${vm}
71 SSHLibrary.Read Until Prompt
72 SSHLibrary.Write ${cmd}
73 ${output}= SSHLibrary.Read Until Prompt
74 SSHLibrary.Close Connection
Kailash Khalasi2adbad82017-05-15 14:53:40 -070075 ${output_1}= Run Keyword If '${strip_line}' == 'True' Get Line ${output} 0
76 ${output}= Set Variable If '${strip_line}' == 'True' ${output_1} ${output}
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070077 [Return] ${output}
78
79Execute Command on Compute Node in CIAB
Kailash Khalasif6de2592017-09-13 13:37:14 -070080 [Arguments] ${system} ${node} ${hostname} ${cmd} ${user}=${VM_USER} ${password}=${VM_PASS} ${prompt}=$ ${use_key}=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
Kailash Khalasif6de2592017-09-13 13:37:14 -070084 SSHLibrary.Write ssh ${node}
Kailash Khalasib6e87fc2017-04-18 15:08:19 -070085 SSHLibrary.Read Until Prompt
86 SSHLibrary.Write ssh root@${hostname}
87 SSHLibrary.Read Until \#
88 SSHLibrary.Write ${cmd}
89 ${output}= SSHLibrary.Read Until \#
90 SSHLibrary.Close Connection
91 [Return] ${output}
92
Kailash Khalasi3a4e0f52017-06-01 16:35:59 -070093Execute Command Locally
94 [Arguments] ${cmd}
95 ${output}= Run ${cmd}
96 [Return] ${output}
97
Kailash Khalasi2f567a42017-09-27 13:50:05 -070098Execute ONOS Command
99 [Arguments] ${onos} ${port} ${cmd} ${user}=karaf ${pass}=karaf
100 ${conn_id}= SSHLibrary.Open Connection ${onos} port=${port} prompt=onos> timeout=300s
101 SSHLibrary.Login ${user} ${pass}
102 ${output}= SSHLibrary.Execute Command ${cmd}
103 SSHLibrary.Close Connection
104 [Return] ${output}
105
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700106Get Docker Container ID
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700107 [Arguments] ${container_name}
108 [Documentation] Retrieves the id of the requested docker container running inside headnode
109 ${container_id}= Run docker ps | grep ${container_name} | awk '{print $1}'
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700110 Log ${container_id}
111 [Return] ${container_id}
112
113Get Docker Logs
114 [Arguments] ${system} ${container_id} ${user}=${USER} ${password}=${PASSWD} ${prompt}=prod:~$
115 [Documentation] Retrieves the id of the requested docker container running inside given ${HOST}
116 ##In Ciab, all containers are run in the prod vm so we must log into that
117 ${conn_id}= SSHLibrary.Open Connection ${system} prompt=$ timeout=300s
118 SSHLibrary.Login With Public Key ${USER} %{HOME}/.ssh/${SSH_KEY} any
119 #SSHLibrary.Login ${HOST_USER} ${HOST_PASSWORD}
Kailash Khalasif6de2592017-09-13 13:37:14 -0700120 SSHLibrary.Write ssh head1
Kailash Khalasib6e87fc2017-04-18 15:08:19 -0700121 SSHLibrary.Read Until ${prompt}
122 SSHLibrary.Write docker logs -t ${container_id}
123 ${container_logs}= SSHLibrary.Read Until ${prompt}
124 SSHLibrary.Close Connection
125 Log ${container_logs}
Kailash Khalasi2adbad82017-05-15 14:53:40 -0700126 [Return] ${container_logs}
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700127
128Remove Value From List
129 [Arguments] ${list} ${val}
130 ${length}= Get Length ${list}
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700131 : FOR ${INDEX} IN RANGE 0 ${length}
Kailash Khalasi86e231e2017-06-06 13:13:43 -0700132 \ Log ${list[${INDEX}]}
133 \ ${value}= Get Dictionary Values ${list[${INDEX}]}
134 \ Log ${value[0]}
135 \ Run Keyword If '${value[0]}' == '${val}' Remove From List ${list} ${INDEX}
Kailash Khalasi2f567a42017-09-27 13:50:05 -0700136 \ Run Keyword If '${value[0]}' == '${val}' Exit For Loop
137
138Test Ping
Kailash Khalasi422d1ab2018-09-18 09:54:26 -0700139 [Arguments] ${status} ${src} ${user} ${pass} ${dest} ${interface} ${prompt}=$ ${prompt_timeout}=60s
Kailash Khalasi55ca9f22018-09-17 17:36:55 -0700140 [Documentation] SSH's into src and attempts to ping dest. Status determines if ping should pass | fail
141 ${conn_id}= SSHLibrary.Open Connection ${src} prompt=${prompt} timeout=${prompt_timeout}
142 SSHLibrary.Login ${user} ${pass}
Kailash Khalasi422d1ab2018-09-18 09:54:26 -0700143 ${result}= SSHLibrary.Execute Command ping -I ${interface} -c 5 ${dest}
Kailash Khalasi55ca9f22018-09-17 17:36:55 -0700144 SSHLibrary.Close Connection
145 Log ${result}
146 Run Keyword If '${status}' == 'PASS' Should Contain ${result} 64 bytes
147 Run Keyword If '${status}' == 'PASS' Should Contain ${result} 0% packet loss
148 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 100% packet loss
149 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 80% packet loss
150 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 60% packet loss
151 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 40% packet loss
152 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} 20% packet loss
153 Run Keyword If '${status}' == 'PASS' Should Not Contain ${result} Destination Host Unreachable
154 Run Keyword If '${status}' == 'FAIL' Should Not Contain ${result} 64 bytes
155 Run Keyword If '${status}' == 'FAIL' Should Contain ${result} 100% packet loss
156 Log To Console \n ${result}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700157
158Clean Up Objects
159 [Arguments] ${model_api}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700160 @{ids}= Create List
161 ${resp}= CORD Get ${model_api}
162 ${jsondata}= To Json ${resp.content}
163 Log ${jsondata}
164 ${length}= Get Length ${jsondata['items']}
165 : FOR ${INDEX} IN RANGE 0 ${length}
166 \ ${value}= Get From List ${jsondata['items']} ${INDEX}
167 \ ${id}= Get From Dictionary ${value} id
168 \ Append To List ${ids} ${id}
169 : FOR ${i} IN @{ids}
170 \ CORD Delete ${model_api} ${i}
Kailash Khalasic930eac2018-09-05 12:18:23 -0700171
172CORD Get
173 [Documentation] Make a GET call to XOS
174 [Arguments] ${service}
175 ${resp}= Get Request ${server_ip} ${service}
176 Log ${resp.content}
177 Should Be Equal As Strings ${resp.status_code} 200
178 [Return] ${resp}
179
Kailash Khalasif3e0a9f2018-09-18 13:43:42 -0700180CORD Post
181 [Documentation] Make a POST call to XOS
182 [Arguments] ${service} ${data}
183 ${data}= Evaluate json.dumps(${data}) json
184 ${resp}= Post Request ${SERVER_IP} uri=${service} data=${data}
185 Log ${resp.content}
186 Should Be Equal As Strings ${resp.status_code} 200
187 [Return] ${resp}
188
Kailash Khalasi16d95c12018-09-21 14:17:28 -0700189CORD Put
190 [Documentation] Make a PUT call to XOS
191 [Arguments] ${service} ${data} ${data_id}
192 ${data}= Evaluate json.dumps(${data}) json
193 ${resp}= Put Request ${SERVER_IP} uri=${service}/${data_id} data=${data}
194 Log ${resp.content}
195 Should Be Equal As Strings ${resp.status_code} 200
196 ${id}= Get Json Value ${resp.content} /id
197 Set Suite Variable ${id}
198 [Return] ${resp}
199
Kailash Khalasic930eac2018-09-05 12:18:23 -0700200CORD Delete
201 [Documentation] Make a DELETE call to XOS
202 [Arguments] ${service} ${data_id}
203 ${resp}= Delete Request ${SERVER_IP} uri=${service}/${data_id}
204 Log ${resp.content}
205 Should Be Equal As Strings ${resp.status_code} 200
206 [Return] ${resp}
Kailash Khalasia0810ce2018-09-10 13:03:27 -0700207
Kailash Khalasif3e0a9f2018-09-18 13:43:42 -0700208Get Service Owner Id
209 [Arguments] ${service}
210 ${resp}= CORD Get ${service}
211 ${jsondata}= To Json ${resp.content}
212 log ${jsondata}
213 ${length}= Get Length ${jsondata['items']}
214 : for ${INDEX} IN RANGE 0 ${length}
215 \ ${value}= Get From List ${jsondata['items']} ${INDEX}
216 \ ${id}= Get From Dictionary ${value} id
217 [Return] ${id}
218
Kailash Khalasia0810ce2018-09-10 13:03:27 -0700219Kill Linux Process
You Wang88e1d852018-10-05 11:44:19 -0700220 [Arguments] ${process} ${ip} ${user} ${pass}=${EMPTY} ${key}=${EMPTY} ${container_name}=${EMPTY}
221 ${rc}= Login And Run Command On Remote System sudo kill $(ps aux | grep '${process}' | awk '{print $2}'); echo $? ${ip} ${user} ${pass} ${key} ${container_name}
222 Should Be Equal As Integers ${rc} 0
You Wangaee35112018-09-28 16:07:49 -0700223
You Wang0c2b3662018-10-01 16:56:17 -0700224Check Remote File Contents
You Wang88e1d852018-10-05 11:44:19 -0700225 [Arguments] ${file_should_exist} ${file} ${pattern} ${ip} ${user} ${pass}=${EMPTY} ${key}=${EMPTY} ${container_name}=${EMPTY} ${prompt}=~$
226 ${output}= Login And Run Command On Remote System cat ${file} | grep '${pattern}' ${ip} ${user} ${pass} ${key} ${container_name} ${prompt}
227 Run Keyword If '${file_should_exist}' == 'True' Should Contain ${output} ${pattern}
228 ... ELSE Should Not Contain ${output} ${pattern}
229
230Check Ping
231 [Arguments] ${ping_should_pass} ${dst_ip} ${iface} ${ip} ${user} ${pass}=${EMPTY} ${key}=${EMPTY} ${container_name}=${EMPTY}
232 ${result}= Login And Run Command On Remote System ping -I ${iface} -c 3 ${dst_ip} ${ip} ${user} ${pass} ${key} ${container_name}
233 Check Ping Result ${ping_should_pass} ${result}
You Wang0c2b3662018-10-01 16:56:17 -0700234
235Check Remote System Reachability
You Wang88e1d852018-10-05 11:44:19 -0700236 [Arguments] ${reachable} ${ip}
You Wang0c2b3662018-10-01 16:56:17 -0700237 [Documentation] Check if the specified IP address is reachable or not
238 ${result}= Run ping -c 3 -t 3 ${ip}
You Wang88e1d852018-10-05 11:44:19 -0700239 Check Ping Result ${reachable} ${result}
240
241Check Ping Result
242 [Arguments] ${reachable} ${result}
You Wang0c2b3662018-10-01 16:56:17 -0700243 Run Keyword If '${reachable}' == 'True' Should Contain ${result} 64 bytes
244 Run Keyword If '${reachable}' == 'True' Should Contain Any ${result} 0% packet loss 0.0% packet loss
245 Run Keyword If '${reachable}' == 'True' Should Not Contain Any ${result} 100% packet loss 100.0% packet loss
246 Run Keyword If '${reachable}' == 'False' Should Not Contain ${result} 64 bytes
247 Run Keyword If '${reachable}' == 'False' Should Contain Any ${result} 100% packet loss 100.0% packet loss