blob: 557158efd6a10028e0738ef3eea99c560794a315 [file] [log] [blame]
David Bainbridge117d23e2019-09-30 20:37:51 +00001# 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.
David Bainbridge117d23e2019-09-30 20:37:51 +000014# voltctl common functions
15
16*** Settings ***
17Documentation Library for various utilities
18Library SSHLibrary
David Bainbridge117d23e2019-09-30 20:37:51 +000019Library String
20Library DateTime
21Library Process
22Library Collections
23Library RequestsLibrary
24Library OperatingSystem
25
26*** Keywords ***
27Lookup Service IP
28 [Arguments] ${namespace} ${name}
David Bainbridgef81cd642019-11-20 00:14:47 +000029 [Documentation] Uses kubectl to resolve a service name to an IP
Gilles Depatie675a2062019-10-22 12:44:42 -040030 ${rc} ${ip}= Run and Return Rc and Output
31 ... kubectl get svc -n ${namespace} ${name} -o jsonpath={.spec.clusterIP}
David Bainbridge117d23e2019-09-30 20:37:51 +000032 Should Be Equal as Integers ${rc} 0
33 [Return] ${ip}
34
35Lookup Service PORT
36 [Arguments] ${namespace} ${name}
David Bainbridgef81cd642019-11-20 00:14:47 +000037 [Documentation] Uses kubectl to resolve a service name to an PORT
Gilles Depatie675a2062019-10-22 12:44:42 -040038 ${rc} ${port}= Run and Return Rc and Output
39 ... kubectl get svc -n ${namespace} ${name} -o jsonpath={.spec.ports[0].port}
David Bainbridge117d23e2019-09-30 20:37:51 +000040 Should Be Equal as Integers ${rc} 0
41 [Return] ${port}
suraj gourd64356b2019-11-07 13:26:20 +000042
43Restart Pod
44 [Arguments] ${namespace} ${name}
45 [Documentation] Uses kubectl to force delete pod
suraj gour067451d2019-11-13 11:20:13 +000046 ${rc} ${restart_pod_name}= Run and Return Rc and Output
47 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
suraj gourd64356b2019-11-07 13:26:20 +000048 Log ${restart_pod_name}
suraj gour067451d2019-11-13 11:20:13 +000049 Should Not Be Empty ${restart_pod_name} Unable to parse pod name
Zack Williamsa8fe75a2020-01-10 14:25:27 -070050 ${rc} ${output}= Run and Return Rc and Output
51 ... kubectl delete pod ${restart_pod_name} -n ${namespace} --grace-period=0 --force
suraj gourd64356b2019-11-07 13:26:20 +000052 Log ${output}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +000053
Scott Baker60e570d2020-02-02 22:10:13 -080054Exec Pod
55 [Arguments] ${namespace} ${name} ${command}
56 [Documentation] Uses kubectl to execute a command in the pod and return the output
57 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +020058 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -080059 Log ${exec_pod_name}
60 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
61 ${rc} ${output}= Run and Return Rc and Output
62 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
63 Log ${output}
64 [return] ${output}
65
Holger Hildebrandt23147742020-11-16 10:13:21 +000066Exec Pod In Kube
TorstenThiemefe7099e2021-01-29 08:41:04 +000067 [Arguments] ${namespace} ${name} ${command} ${grep}=${EMPTY}
Holger Hildebrandt23147742020-11-16 10:13:21 +000068 [Documentation] Uses kubectl to execute a command in the pod and return the output
TorstenThiemefe7099e2021-01-29 08:41:04 +000069 ${rc} ${exec_pod_name}= Run Keyword If '${grep}'=='${EMPTY}'
70 ... Run and Return Rc and Output kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name
71 ... ELSE Run and Return Rc and Output
72 ... kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name \| grep ${grep}
Hardik Windlassec86c232021-02-02 13:56:12 +000073 Log ${exec_pod_name}
74 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
75 ${rc} ${output}= Run and Return Rc and Output
76 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
77 Log ${output}
78 [return] ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -070079
80Exec Pod And Return Output And RC
81 [Arguments] ${namespace} ${name} ${command}
82 [Documentation] Uses kubectl to execute a command in the pod and return the output
83 ${rc} ${exec_pod_name}= Run and Return Rc and Output
84 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
Holger Hildebrandt23147742020-11-16 10:13:21 +000085 Log ${exec_pod_name}
86 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
87 ${rc} ${output}= Run and Return Rc and Output
88 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
89 Log ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -070090 [return] ${output} ${rc}
Holger Hildebrandt23147742020-11-16 10:13:21 +000091
Scott Baker60e570d2020-02-02 22:10:13 -080092Exec Pod Separate Stderr
93 [Arguments] ${namespace} ${name} ${command}
94 [Documentation] Uses kubectl to execute a command in the pod and return the stderr and stdout
95 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +020096 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -080097 Log ${exec_pod_name}
98 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
99 @{args}= Split String ${command}
100 ${result}= Run Process
101 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- @{args}
102 ${stdout}= Set Variable ${result.stdout}
103 ${stderr}= Set Variable ${result.stderr}
104 Log ${stdout}
105 Log ${stderr}
106 [return] ${stdout} ${stderr}
107
Scott Baker0478bab2020-04-10 17:19:57 -0700108Copy File To Pod
109 [Arguments] ${namespace} ${name} ${src} ${dest}
110 [Documentation] Uses kubectl to copy a file to a pod
111 ${rc} ${exec_pod_name}= Run and Return Rc and Output
112 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
113 Log ${exec_pod_name}
114 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
115 ${rc} ${output}= Run and Return Rc and Output
116 ... kubectl cp -n ${namespace} ${src} ${exec_pod_name}:${dest}
117 Log ${output}
118 [return] ${output}
119
Scott Baker60e570d2020-02-02 22:10:13 -0800120Apply Kubernetes Resources
121 [Arguments] ${resource_yaml} ${namespace}
122 [Documentation] Use kubectl to create resources given a yaml file
123 ${rc} Run and Return Rc
124 ... kubectl apply -n ${namespace} -f ${resource_yaml}
125 Should Be Equal as Integers ${rc} 0
126
Scott Baker0478bab2020-04-10 17:19:57 -0700127Delete Kubernetes Resources
128 [Arguments] ${resource_yaml} ${namespace}
129 [Documentation] Use kubectl to delete resources given a yaml file
130 ${rc} Run and Return Rc
131 ... kubectl delete -n ${namespace} -f ${resource_yaml}
132 Should Be Equal as Integers ${rc} 0
133
suraj gour1ecfae92019-12-20 15:11:40 +0000134Validate Pod Status
135 [Arguments] ${pod_name} ${namespace} ${expectedStatus}
136 [Documentation] To run the kubectl command and check the status of the given pod matches the expected status
Andy Bavierb63f6d22020-03-12 15:34:37 -0700137 ${length}= Run kubectl get pod -n ${namespace} -o name | wc -l
138 ${matched}= Set Variable False
139 FOR ${index} IN RANGE ${length}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700140 ${currentPodName}= Run
141 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.containerStatuses[0].name}"
suraj gour1ecfae92019-12-20 15:11:40 +0000142 Log Required Pod : ${pod_name}
143 Log Current Pod: ${currentPodName}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700144 ${matched}= Set Variable If '${currentPodName}'=='${pod_name}' True False
145 Exit For Loop If ${matched}
suraj gour1ecfae92019-12-20 15:11:40 +0000146 END
Andy Bavierb63f6d22020-03-12 15:34:37 -0700147 Should Be True ${matched} No pod ${podname} found
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700148 ${currentStatusofPod}= Run
149 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.phase}"
suraj gour1ecfae92019-12-20 15:11:40 +0000150 Log ${currentStatusofPod}
151 Should Contain ${currentStatusofPod} ${expectedStatus}
152
Matteo Scandoloa80b4732020-09-04 13:51:10 -0700153Get Pod Name By Label
154 [Arguments] ${namespace} ${label_key} ${label_value}
155 [Documentation] Return a pod name from a given label
156 ${rc} ${pod_name}= Run and Return Rc and Output
157 ... kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
158 Should Not Be Empty ${pod_name} Pod not found
159 [return] ${pod_name}
160
Hung-Wei Chiu2bee4d42020-04-24 11:31:50 -0700161Validate Pods Status By Label
162 [Arguments] ${namespace} ${label_key} ${label_value} ${expectedStatus}
163 [Documentation] To run the kubectl command and check the status of all pods filter
164 ... by label matche the expected status
165 ${command}= Catenate
166 ... kubectl -n ${namespace} get pods -l ${label_key}=${label_value}
167 ... -o=jsonpath="{.items[?(.status.phase=='${expectedStatus}')].status.phase}"
168 ${pods_status}= Run ${command}
169 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
170
Andrea Campanella4c404632020-08-26 14:41:36 +0200171Validate Pods Status By Name
172 [Arguments] ${namespace} ${name} ${expectedStatus}
173 [Documentation] To run the kubectl command and check the status of all pods filter
174 ... by label matche the expected status
175 ${command}= Catenate
176 ... kubectl -n ${namespace} get pods ${name}
177 ... -o=jsonpath="{.status.phase}"
178 ${pods_status}= Run ${command}
179 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
180
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000181Verify All Voltha Pods For Any Error Logs
182 [Arguments] ${datetime}
183 [Documentation] This keyword checks for the error occurence in the voltha pods
184 &{errorPodDict} Create Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700185 &{containerDict} Get Container Dictionary voltha
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000186 FOR ${podName} IN @{PODLIST1}
187 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700188 ${rc} ${logOutput} Run And Return Rc And Output
189 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
190 Run Keyword And Ignore Error
191 ... Run Keyword If '${logOutput}'=='${EMPTY}'
192 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000193 ... AND Continue For Loop
194 ${errorDict} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
195 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
196 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700197 Run Keyword And Ignore Error
198 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
199 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000200 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700201 Run Keyword And Ignore Error
202 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
203 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000204 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
205 END
206 FOR ${podName} IN @{PODLIST2}
207 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700208 ${rc} ${logOutput} Run And Return Rc And Output
209 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
210 Run Keyword And Ignore Error
211 ... Run Keyword If '${logOutput}'=='${EMPTY}'
212 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000213 ... AND Continue For Loop
214 ${errorDict} Check For Error Logs in Pod Type2 Given the Log Output ${logOutput}
215 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
216 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700217 Run Keyword And Ignore Error
218 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
219 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000220 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700221 Run Keyword And Ignore Error
222 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
223 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000224 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
225 END
226 Print to Console Error Statement logged in the following pods : ${errorPodDict}
227 [Return] ${errorPodDict}
228
229Check For Error Logs in Pod Type1 Given the Log Output
230 [Arguments] ${logOutput} ${logLevel}=error ${errorMessage}=${EMPTY}
231 [Documentation] Checks for error message in the particular list of pods
232 Log ${logOutput}
233 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.* partial_match=true
234 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700235 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
236 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
237 ${linesContainingError} = Get Lines Matching Regexp
238 ... ${logOutput} .*\s\${logLevel}.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000239 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700240 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
241 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000242 Log {linesContainingError}
243 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
244 [Return] ${errorDict}
245
246Check For Error Logs in Pod Type2 Given the Log Output
247 [Arguments] ${logOutput} ${logLevel}=warn ${errorMessage}=${EMPTY}
248 [Documentation] Checks for error message in the particular set of pods
249 Log ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700250 ${linesContainingLog} = Get Lines Matching Regexp
251 ... ${logOutput} .*?\s.*level.*${logLevel}.* partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000252 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700253 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
254 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
255 ${linesContainingError} = Get Lines Matching Regexp
256 ... ${logOutput} .*?\s.*level.*${logLevel}.*msg.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000257 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700258 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
259 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000260 Log {linesContainingError}
261 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
262 [Return] ${errorDict}
263
264Get Container Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700265 [Arguments] ${namespace}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000266 [Documentation] Creates a mapping for pod name and container name and returns the same
267 &{containerDict} Create Dictionary
268 ${containerName} Set Variable ${EMPTY}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700269 ${podName} Run kubectl get deployment -n ${namespace} | awk 'NR>1 {print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000270 @{podNameList}= Split To Lines ${podName}
271 Append To List ${podNameList} voltha-etcd-cluster voltha-kafka voltha-ro-core voltha-zookeeper
272 Log ${podNameList}
273 #Creatiing dictionary to correspond pod name and container name
274 FOR ${pod} IN @{podNameList}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700275 ${containerName} Run kubectl get pod -n ${namespace} | grep ${pod} | awk '{print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000276 &{containerDict} Set To Dictionary ${containerDict} ${pod} ${containerName}
277 END
278 Log ${containerDict}
279 [Return] ${containerDict}
280
281Validate Error For Given Pods
282 [Arguments] ${datetime} ${podDict}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700283 [Documentation]
284 ... This keyword is used to get the list of pods if there is any unexpected error
285 ... in a particular pod(s) given the time-${datetime} from which the log needs to
286 ... be analysed and the dictionary of pods and the error in the dictionary format
287 ... ${podDict] .
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000288 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700289 ... Usage: ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict}
290 ...
291 ... Arguments:
292 ...
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000293 ... ${datetime} = time from which the log needs to be taken
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700294 ... ${podDict} = Key-value pair of the pod name and the error msg
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000295 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700296 ... Example: ${podDict} = Set Dictionary ${podDict} radius sample error message.
297 ...
298 ... In case the radius pod log has any other error than the expected
299 ... error, then the podname will be returned
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000300 ${podList} = Get Dictionary Keys ${podDict}
301 FOR ${podName} IN @{podList}
302 ${containerName} Get From Dictionary ${containerDict} ${podName}
303 ${expectedError} Get From Dictionary ${podDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700304 ${rc} ${logOutput} Run And Return Rc And Output
305 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
306 Run Keyword And Ignore Error
307 ... Run Keyword If '${logOutput}'=='${EMPTY}'
308 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000309 ... AND Continue For Loop
310 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700311 Run Keyword And Ignore Error
312 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
313 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000314 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700315 Run Keyword And Ignore Error
316 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
317 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000318 ... AND Append To List ${errorPodList} ${podName}
319 END
320 [Return] ${errorPodList}
321
David Bainbridgef81cd642019-11-20 00:14:47 +0000322Delete K8s Pod
323 [Arguments] ${namespace} ${name}
324 [Documentation] Uses kubectl to delete a named POD
325 ${rc} Run and Return Rc
326 ... kubectl delete -n ${namespace} pod/${name}
327 Should Be Equal as Integers ${rc} 0
328
hwchiu85695932019-12-18 08:05:25 +0000329Delete K8s Pods By Label
330 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700331 [Documentation] Uses kubectl to delete a PODs, filtering by label
hwchiu85695932019-12-18 08:05:25 +0000332 ${rc}= Run and Return Rc
333 ... kubectl -n ${namespace} delete pods -l${key}=${value}
334 Should Be Equal as Integers ${rc} 0
335
Andrea Campanella4c404632020-08-26 14:41:36 +0200336Delete K8s Pods By Name
337 [Arguments] ${namespace} ${value}
338 [Documentation] Uses kubectl to delete a PODs, filtering by label
339 ${rc}= Run and Return Rc
340 ... kubectl -n ${namespace} delete pods ${value}
341 Should Be Equal as Integers ${rc} 0
342
David Bainbridgef81cd642019-11-20 00:14:47 +0000343Scale K8s Deployment
344 [Arguments] ${namespace} ${name} ${count}
345 [Documentation] Uses kubectl to scale a named deployment
346 ${rc} Run and Return Rc
347 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
348 Should Be Equal as Integers ${rc} 0
349
Andrea Campanella3dcce272021-01-15 16:04:47 +0100350Get K8s Deployment by Pod Label
351 [Arguments] ${namespace} ${key} ${value}
352 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
353 ${rc} ${name} Run And Return Rc And Output
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000354 ... kubectl describe rs -n ${namespace} -l ${key}=${value} | grep "Controlled By" | awk -F'/' '{print $2}' | awk 'FNR == 1'
Andrea Campanella3dcce272021-01-15 16:04:47 +0100355 Should Be Equal as Integers ${rc} 0
356 [Return] ${name}
357
358Scale K8s Deployment by Pod Label
359 [Arguments] ${namespace} ${key} ${value} ${count}
360 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
361 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
362 Scale K8s Deployment ${namespace} ${name} ${count}
363
David Bainbridgef81cd642019-11-20 00:14:47 +0000364Pod Exists
365 [Arguments] ${namespace} ${name}
366 [Documentation] Succeeds it the named POD exists
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700367 ${rc} ${count} Run and Return Rc
368 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700369 Should Be True ${count}>0 Pod ${name} not found
David Bainbridgef81cd642019-11-20 00:14:47 +0000370
371Pod Does Not Exist
372 [Arguments] ${namespace} ${name}
373 [Documentation] Succeeds if the named POD does not exist
374 ${rc} ${count} Run and Return Rc And Output
375 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
376 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700377 Should Be True ${count}==0 Pod ${name} exists but should not
David Bainbridgef81cd642019-11-20 00:14:47 +0000378
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000379Wait For Pods Not Exist
380 [Arguments] ${namespace} ${list_names}
381 [Documentation] Checks the passed PODs are no longer existing
TorstenThiemefe7099e2021-01-29 08:41:04 +0000382 FOR ${pod_name} IN @{list_names}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000383 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000384 ... Pod Does Not Exist ${namespace} ${pod_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000385 END
386
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700387Pods Do Not Exist By Label
hwchiu85695932019-12-18 08:05:25 +0000388 [Arguments] ${namespace} ${key} ${value}
389 [Documentation] Succeeds if the named POD does not exist
390 ${rc} ${count} Run and Return Rc And Output
391 ... kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
392 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700393 Should Be True ${count}==0 Pod with label ${key}=${value} exists but should not
hwchiu85695932019-12-18 08:05:25 +0000394
David Bainbridgef81cd642019-11-20 00:14:47 +0000395Get Available Deployment Replicas
396 [Arguments] ${namespace} ${name}
397 [Documentation] Succeeds if the named POD exists and has a ready count > 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700398 ${rc} ${count} Run and Return Rc and Output
399 ... kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
David Bainbridgef81cd642019-11-20 00:14:47 +0000400 ${result}= Run Keyword If '${count}' == '' Set Variable 0
401 ... ELSE Set Variable ${count}
402 [Return] ${result}
403
Andrea Campanella3dcce272021-01-15 16:04:47 +0100404Check Expected Available Deployment Replicas By Pod Label
405 [Arguments] ${namespace} ${key} ${value} ${expected}
406 [Documentation] Succeeds if the named deployment has the expected number of available replicas
407 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
408 Check Expected Available Deployment Replicas ${namespace} ${name} ${expected}
409
David Bainbridgef81cd642019-11-20 00:14:47 +0000410Check Expected Available Deployment Replicas
411 [Arguments] ${namespace} ${name} ${expected}
412 [Documentation] Succeeds if the named deployment has the expected number of available replicas
413 ${count}= Get Available Deployment Replicas ${namespace} ${name}
414 Should Be Equal As Integers ${expected} ${count}
415
416Get Deployment Replica Count
417 [Arguments] ${namespace} ${name}
418 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
419 ${rc} ${value} Run and Return Rc and Output
420 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
421 Should Be Equal as Integers ${rc} 0
422 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
423 ... ELSE Set Variable ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700424 [Return] ${replicas}
David Bainbridgef81cd642019-11-20 00:14:47 +0000425
426Does Deployment Have Replicas
427 [Arguments] ${namespace} ${name} ${expected_count}
428 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
429 ${rc} ${value} Run and Return Rc and Output
430 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
431 Should Be Equal as Integers ${rc} 0
432 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
433 ... ELSE Set Variable ${value}
434 Should be Equal as Integers ${replicas} ${expected_count}
hwchiu85695932019-12-18 08:05:25 +0000435
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700436Pods Are Ready By Label
hwchiu85695932019-12-18 08:05:25 +0000437 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700438 [Documentation] Check that all pods with a label are ready
439 ${output}= Run
440 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[].status.containerStatuses[].ready}"
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000441 Should Not Contain ${output} false
442
443Wait For Pods Ready
444 [Arguments] ${namespace} ${list_apps}
445 [Documentation] Checks the passed PODs are ready
TorstenThiemefe7099e2021-01-29 08:41:04 +0000446 FOR ${app_name} IN @{list_apps}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000447 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000448 ... Pods Are Ready By Label ${namespace} app ${app_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000449 END
Gayathri.Selvan49398962020-01-13 07:19:12 +0000450
hwchiu58af72d2020-01-14 00:50:35 +0000451Check Expected Running Pods Number By Label
452 [Arguments] ${namespace} ${key} ${value} ${number}
453 [Documentation] Succeeds if the desired pod has expected number replicas
454 ${rc} ${count} Run and Return Rc and Output
455 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o json | jq -r ".items[].status.phase" | wc -l
456 Should Be Equal as Integers ${count} ${number}
Andrea Campanella962fe832020-09-21 10:41:47 +0200457
458Get Number of Running Pods Number By Label
459 [Arguments] ${namespace} ${key} ${value}
460 [Documentation] Returns the number of pods for a given label
461 ${rc} ${count} Run and Return Rc and Output
462 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o name | wc -l
463 [Return] ${count}
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000464
465Get Pod Restart Count
466 [Arguments] ${namespace} ${name}
467 [Documentation] Returns the restart count for the given Pod
468 ${rc} ${count}= Run and Return Rc and Output
469 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $4}'
470 [Return] ${count}
471
472Verify ONOS Pod Restart
473 [Arguments] ${restarted}=True
474 [Documentation] Verifies if any of the given ONOS instances restarted
475 ${num_onos}= Wait Until Keyword Succeeds 20s 5s Get Number of Running Pods Number By Label default
476 ... app onos-onos-classic
477 FOR ${I} IN RANGE 0 ${num_onos}
478 ${onos_pod}= Catenate SEPARATOR=- onos-onos-classic ${I}
479 ${count}= Get Pod Restart Count default ${onos_pod}
480 Run Keyword If ${restarted}
481 ... Should Not Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Not Restarted
482 ... ELSE
483 ... Should Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Restarted
484 END
485
486Deploy Pod New Image
487 [Arguments] ${namespace} ${deployment} ${container} ${image}
488 [Documentation] Deploys the Pod given image
489 ${rc} Run and Return Rc
490 ... kubectl -n ${namespace} set image deployment/${deployment} ${container}=${image}
491 Should Be Equal as Integers ${rc} 0
492
493Verify Pod Image
494 [Arguments] ${namespace} ${key} ${value} ${image}
495 [Documentation] Verifies the Pod Image
496 ${output}= Run
Hardik Windlass08451302021-03-09 12:14:36 +0000497 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
498 Should Be Equal '${output}' '${image}'
Hardik Windlassdc2610f2021-03-09 07:33:51 +0000499
500Get Pod Image And App Version And Helm Chart By Label
501 [Arguments] ${namespace} ${key} ${value}
502 [Documentation] Retrieves Pod Image and, App and Helm Chart Version details
503 ${image}= Run
504 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
505 ${cmd}= Catenate SEPARATOR=
506 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=
507 ... jsonpath="{.items[*].metadata.labels.\\app\\.kubernetes\\.io\\/version}"
508 ${app_version}= Run ${cmd}
509 ${helm_chart}= Run
510 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].metadata.labels.\\helm\\.sh\\/chart}"
511 [Return] ${image} ${app_version} ${helm_chart}