blob: 38da3ee29f9926db0bb069ee0adaab090997c167 [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
Matteo Scandolo6f24ea92021-04-29 11:55:50 -070043Restart Pod By Label
44 [Arguments] ${namespace} ${label_key} ${label_value}
TorstenThieme37165402021-09-03 11:39:40 +000045 [Documentation] Uses kubectl to force delete pod(s)
Matteo Scandolo6f24ea92021-04-29 11:55:50 -070046 ${rc} ${restart_pod_name}= Run and Return Rc and Output
47 ... kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
48 Log ${restart_pod_name}
49 Should Not Be Empty ${restart_pod_name} Unable to parse pod name
TorstenThieme37165402021-09-03 11:39:40 +000050 @{pods}= Split String ${restart_pod_name} separator=${\n}
51 FOR ${pod_name} IN @{pods}
52 ${rc} ${output}= Run and Return Rc and Output
53 ... kubectl delete pod ${pod_name} -n ${namespace} --grace-period=0 --force
54 Log ${output}
55 END
Matteo Scandolo6f24ea92021-04-29 11:55:50 -070056
Scott Baker60e570d2020-02-02 22:10:13 -080057Exec Pod
58 [Arguments] ${namespace} ${name} ${command}
59 [Documentation] Uses kubectl to execute a command in the pod and return the output
60 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +020061 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -080062 Log ${exec_pod_name}
63 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
64 ${rc} ${output}= Run and Return Rc and Output
65 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
66 Log ${output}
67 [return] ${output}
68
Holger Hildebrandt23147742020-11-16 10:13:21 +000069Exec Pod In Kube
TorstenThiemefe7099e2021-01-29 08:41:04 +000070 [Arguments] ${namespace} ${name} ${command} ${grep}=${EMPTY}
Holger Hildebrandt23147742020-11-16 10:13:21 +000071 [Documentation] Uses kubectl to execute a command in the pod and return the output
TorstenThiemefe7099e2021-01-29 08:41:04 +000072 ${rc} ${exec_pod_name}= Run Keyword If '${grep}'=='${EMPTY}'
73 ... Run and Return Rc and Output kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name
74 ... ELSE Run and Return Rc and Output
75 ... kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name \| grep ${grep}
Hardik Windlassec86c232021-02-02 13:56:12 +000076 Log ${exec_pod_name}
77 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
78 ${rc} ${output}= Run and Return Rc and Output
79 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
80 Log ${output}
81 [return] ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -070082
83Exec Pod And Return Output And RC
84 [Arguments] ${namespace} ${name} ${command}
85 [Documentation] Uses kubectl to execute a command in the pod and return the output
86 ${rc} ${exec_pod_name}= Run and Return Rc and Output
87 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
Holger Hildebrandt23147742020-11-16 10:13:21 +000088 Log ${exec_pod_name}
89 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
90 ${rc} ${output}= Run and Return Rc and Output
91 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
92 Log ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -070093 [return] ${output} ${rc}
Holger Hildebrandt23147742020-11-16 10:13:21 +000094
Scott Baker60e570d2020-02-02 22:10:13 -080095Exec Pod Separate Stderr
96 [Arguments] ${namespace} ${name} ${command}
97 [Documentation] Uses kubectl to execute a command in the pod and return the stderr and stdout
98 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +020099 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -0800100 Log ${exec_pod_name}
101 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
102 @{args}= Split String ${command}
103 ${result}= Run Process
104 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- @{args}
105 ${stdout}= Set Variable ${result.stdout}
106 ${stderr}= Set Variable ${result.stderr}
107 Log ${stdout}
108 Log ${stderr}
109 [return] ${stdout} ${stderr}
110
Scott Baker0478bab2020-04-10 17:19:57 -0700111Copy File To Pod
Andrea Campanella0aa21d62021-07-22 10:44:32 +0200112 [Arguments] ${namespace} ${label} ${src} ${dest}
Scott Baker0478bab2020-04-10 17:19:57 -0700113 [Documentation] Uses kubectl to copy a file to a pod
114 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella0aa21d62021-07-22 10:44:32 +0200115 ... kubectl get pods -n ${namespace} -l ${label} --no-headers | awk 'NR==1{print $1}'
Scott Baker0478bab2020-04-10 17:19:57 -0700116 Log ${exec_pod_name}
117 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
118 ${rc} ${output}= Run and Return Rc and Output
119 ... kubectl cp -n ${namespace} ${src} ${exec_pod_name}:${dest}
120 Log ${output}
121 [return] ${output}
122
Scott Baker60e570d2020-02-02 22:10:13 -0800123Apply Kubernetes Resources
124 [Arguments] ${resource_yaml} ${namespace}
125 [Documentation] Use kubectl to create resources given a yaml file
126 ${rc} Run and Return Rc
127 ... kubectl apply -n ${namespace} -f ${resource_yaml}
128 Should Be Equal as Integers ${rc} 0
129
Scott Baker0478bab2020-04-10 17:19:57 -0700130Delete Kubernetes Resources
131 [Arguments] ${resource_yaml} ${namespace}
132 [Documentation] Use kubectl to delete resources given a yaml file
133 ${rc} Run and Return Rc
134 ... kubectl delete -n ${namespace} -f ${resource_yaml}
135 Should Be Equal as Integers ${rc} 0
136
suraj gour1ecfae92019-12-20 15:11:40 +0000137Validate Pod Status
138 [Arguments] ${pod_name} ${namespace} ${expectedStatus}
139 [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 -0700140 ${length}= Run kubectl get pod -n ${namespace} -o name | wc -l
141 ${matched}= Set Variable False
142 FOR ${index} IN RANGE ${length}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700143 ${currentPodName}= Run
144 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.containerStatuses[0].name}"
suraj gour1ecfae92019-12-20 15:11:40 +0000145 Log Required Pod : ${pod_name}
146 Log Current Pod: ${currentPodName}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700147 ${matched}= Set Variable If '${currentPodName}'=='${pod_name}' True False
148 Exit For Loop If ${matched}
suraj gour1ecfae92019-12-20 15:11:40 +0000149 END
Andy Bavierb63f6d22020-03-12 15:34:37 -0700150 Should Be True ${matched} No pod ${podname} found
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700151 ${currentStatusofPod}= Run
152 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.phase}"
suraj gour1ecfae92019-12-20 15:11:40 +0000153 Log ${currentStatusofPod}
154 Should Contain ${currentStatusofPod} ${expectedStatus}
155
Matteo Scandoloa80b4732020-09-04 13:51:10 -0700156Get Pod Name By Label
157 [Arguments] ${namespace} ${label_key} ${label_value}
158 [Documentation] Return a pod name from a given label
159 ${rc} ${pod_name}= Run and Return Rc and Output
160 ... kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
161 Should Not Be Empty ${pod_name} Pod not found
162 [return] ${pod_name}
163
Hung-Wei Chiu2bee4d42020-04-24 11:31:50 -0700164Validate Pods Status By Label
165 [Arguments] ${namespace} ${label_key} ${label_value} ${expectedStatus}
166 [Documentation] To run the kubectl command and check the status of all pods filter
167 ... by label matche the expected status
168 ${command}= Catenate
169 ... kubectl -n ${namespace} get pods -l ${label_key}=${label_value}
170 ... -o=jsonpath="{.items[?(.status.phase=='${expectedStatus}')].status.phase}"
171 ${pods_status}= Run ${command}
172 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
173
Andrea Campanella4c404632020-08-26 14:41:36 +0200174Validate Pods Status By Name
175 [Arguments] ${namespace} ${name} ${expectedStatus}
176 [Documentation] To run the kubectl command and check the status of all pods filter
177 ... by label matche the expected status
178 ${command}= Catenate
179 ... kubectl -n ${namespace} get pods ${name}
180 ... -o=jsonpath="{.status.phase}"
181 ${pods_status}= Run ${command}
182 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
183
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000184Verify All Voltha Pods For Any Error Logs
185 [Arguments] ${datetime}
186 [Documentation] This keyword checks for the error occurence in the voltha pods
187 &{errorPodDict} Create Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700188 &{containerDict} Get Container Dictionary voltha
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000189 FOR ${podName} IN @{PODLIST1}
190 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700191 ${rc} ${logOutput} Run And Return Rc And Output
192 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
193 Run Keyword And Ignore Error
194 ... Run Keyword If '${logOutput}'=='${EMPTY}'
195 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000196 ... AND Continue For Loop
197 ${errorDict} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
198 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
199 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700200 Run Keyword And Ignore Error
201 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
202 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000203 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700204 Run Keyword And Ignore Error
205 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
206 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000207 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
208 END
209 FOR ${podName} IN @{PODLIST2}
210 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700211 ${rc} ${logOutput} Run And Return Rc And Output
212 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
213 Run Keyword And Ignore Error
214 ... Run Keyword If '${logOutput}'=='${EMPTY}'
215 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000216 ... AND Continue For Loop
217 ${errorDict} Check For Error Logs in Pod Type2 Given the Log Output ${logOutput}
218 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
219 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700220 Run Keyword And Ignore Error
221 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
222 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000223 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700224 Run Keyword And Ignore Error
225 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
226 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000227 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
228 END
229 Print to Console Error Statement logged in the following pods : ${errorPodDict}
230 [Return] ${errorPodDict}
231
232Check For Error Logs in Pod Type1 Given the Log Output
233 [Arguments] ${logOutput} ${logLevel}=error ${errorMessage}=${EMPTY}
234 [Documentation] Checks for error message in the particular list of pods
235 Log ${logOutput}
236 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.* partial_match=true
237 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700238 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
239 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
240 ${linesContainingError} = Get Lines Matching Regexp
241 ... ${logOutput} .*\s\${logLevel}.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000242 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700243 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
244 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000245 Log {linesContainingError}
246 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
247 [Return] ${errorDict}
248
249Check For Error Logs in Pod Type2 Given the Log Output
250 [Arguments] ${logOutput} ${logLevel}=warn ${errorMessage}=${EMPTY}
251 [Documentation] Checks for error message in the particular set of pods
252 Log ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700253 ${linesContainingLog} = Get Lines Matching Regexp
254 ... ${logOutput} .*?\s.*level.*${logLevel}.* partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000255 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700256 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
257 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
258 ${linesContainingError} = Get Lines Matching Regexp
259 ... ${logOutput} .*?\s.*level.*${logLevel}.*msg.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000260 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700261 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
262 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000263 Log {linesContainingError}
264 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
265 [Return] ${errorDict}
266
267Get Container Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700268 [Arguments] ${namespace}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000269 [Documentation] Creates a mapping for pod name and container name and returns the same
270 &{containerDict} Create Dictionary
271 ${containerName} Set Variable ${EMPTY}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700272 ${podName} Run kubectl get deployment -n ${namespace} | awk 'NR>1 {print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000273 @{podNameList}= Split To Lines ${podName}
274 Append To List ${podNameList} voltha-etcd-cluster voltha-kafka voltha-ro-core voltha-zookeeper
275 Log ${podNameList}
276 #Creatiing dictionary to correspond pod name and container name
277 FOR ${pod} IN @{podNameList}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700278 ${containerName} Run kubectl get pod -n ${namespace} | grep ${pod} | awk '{print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000279 &{containerDict} Set To Dictionary ${containerDict} ${pod} ${containerName}
280 END
281 Log ${containerDict}
282 [Return] ${containerDict}
283
284Validate Error For Given Pods
285 [Arguments] ${datetime} ${podDict}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700286 [Documentation]
287 ... This keyword is used to get the list of pods if there is any unexpected error
288 ... in a particular pod(s) given the time-${datetime} from which the log needs to
289 ... be analysed and the dictionary of pods and the error in the dictionary format
290 ... ${podDict] .
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000291 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700292 ... Usage: ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict}
293 ...
294 ... Arguments:
295 ...
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000296 ... ${datetime} = time from which the log needs to be taken
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700297 ... ${podDict} = Key-value pair of the pod name and the error msg
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000298 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700299 ... Example: ${podDict} = Set Dictionary ${podDict} radius sample error message.
300 ...
301 ... In case the radius pod log has any other error than the expected
302 ... error, then the podname will be returned
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000303 ${podList} = Get Dictionary Keys ${podDict}
304 FOR ${podName} IN @{podList}
305 ${containerName} Get From Dictionary ${containerDict} ${podName}
306 ${expectedError} Get From Dictionary ${podDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700307 ${rc} ${logOutput} Run And Return Rc And Output
308 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
309 Run Keyword And Ignore Error
310 ... Run Keyword If '${logOutput}'=='${EMPTY}'
311 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000312 ... AND Continue For Loop
313 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700314 Run Keyword And Ignore Error
315 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
316 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000317 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700318 Run Keyword And Ignore Error
319 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
320 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000321 ... AND Append To List ${errorPodList} ${podName}
322 END
323 [Return] ${errorPodList}
324
David Bainbridgef81cd642019-11-20 00:14:47 +0000325Delete K8s Pod
326 [Arguments] ${namespace} ${name}
327 [Documentation] Uses kubectl to delete a named POD
328 ${rc} Run and Return Rc
329 ... kubectl delete -n ${namespace} pod/${name}
330 Should Be Equal as Integers ${rc} 0
331
hwchiu85695932019-12-18 08:05:25 +0000332Delete K8s Pods By Label
333 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700334 [Documentation] Uses kubectl to delete a PODs, filtering by label
hwchiu85695932019-12-18 08:05:25 +0000335 ${rc}= Run and Return Rc
336 ... kubectl -n ${namespace} delete pods -l${key}=${value}
337 Should Be Equal as Integers ${rc} 0
338
Andrea Campanella4c404632020-08-26 14:41:36 +0200339Delete K8s Pods By Name
340 [Arguments] ${namespace} ${value}
341 [Documentation] Uses kubectl to delete a PODs, filtering by label
342 ${rc}= Run and Return Rc
343 ... kubectl -n ${namespace} delete pods ${value}
344 Should Be Equal as Integers ${rc} 0
345
David Bainbridgef81cd642019-11-20 00:14:47 +0000346Scale K8s Deployment
347 [Arguments] ${namespace} ${name} ${count}
348 [Documentation] Uses kubectl to scale a named deployment
349 ${rc} Run and Return Rc
350 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
351 Should Be Equal as Integers ${rc} 0
352
Andrea Campanella3dcce272021-01-15 16:04:47 +0100353Get K8s Deployment by Pod Label
354 [Arguments] ${namespace} ${key} ${value}
355 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
356 ${rc} ${name} Run And Return Rc And Output
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000357 ... 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 +0100358 Should Be Equal as Integers ${rc} 0
359 [Return] ${name}
360
361Scale K8s Deployment by Pod Label
362 [Arguments] ${namespace} ${key} ${value} ${count}
363 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
364 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
365 Scale K8s Deployment ${namespace} ${name} ${count}
366
David Bainbridgef81cd642019-11-20 00:14:47 +0000367Pod Exists
368 [Arguments] ${namespace} ${name}
369 [Documentation] Succeeds it the named POD exists
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700370 ${rc} ${count} Run and Return Rc
371 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700372 Should Be True ${count}>0 Pod ${name} not found
David Bainbridgef81cd642019-11-20 00:14:47 +0000373
374Pod Does Not Exist
375 [Arguments] ${namespace} ${name}
376 [Documentation] Succeeds if the named POD does not exist
377 ${rc} ${count} Run and Return Rc And Output
378 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
379 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700380 Should Be True ${count}==0 Pod ${name} exists but should not
David Bainbridgef81cd642019-11-20 00:14:47 +0000381
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000382Wait For Pods Not Exist
383 [Arguments] ${namespace} ${list_names}
384 [Documentation] Checks the passed PODs are no longer existing
TorstenThiemefe7099e2021-01-29 08:41:04 +0000385 FOR ${pod_name} IN @{list_names}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000386 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000387 ... Pod Does Not Exist ${namespace} ${pod_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000388 END
389
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700390Pods Do Not Exist By Label
hwchiu85695932019-12-18 08:05:25 +0000391 [Arguments] ${namespace} ${key} ${value}
392 [Documentation] Succeeds if the named POD does not exist
393 ${rc} ${count} Run and Return Rc And Output
394 ... kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
395 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700396 Should Be True ${count}==0 Pod with label ${key}=${value} exists but should not
hwchiu85695932019-12-18 08:05:25 +0000397
David Bainbridgef81cd642019-11-20 00:14:47 +0000398Get Available Deployment Replicas
399 [Arguments] ${namespace} ${name}
400 [Documentation] Succeeds if the named POD exists and has a ready count > 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700401 ${rc} ${count} Run and Return Rc and Output
402 ... kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
David Bainbridgef81cd642019-11-20 00:14:47 +0000403 ${result}= Run Keyword If '${count}' == '' Set Variable 0
404 ... ELSE Set Variable ${count}
405 [Return] ${result}
406
Andrea Campanella3dcce272021-01-15 16:04:47 +0100407Check Expected Available Deployment Replicas By Pod Label
408 [Arguments] ${namespace} ${key} ${value} ${expected}
409 [Documentation] Succeeds if the named deployment has the expected number of available replicas
410 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
411 Check Expected Available Deployment Replicas ${namespace} ${name} ${expected}
412
David Bainbridgef81cd642019-11-20 00:14:47 +0000413Check Expected Available Deployment Replicas
414 [Arguments] ${namespace} ${name} ${expected}
415 [Documentation] Succeeds if the named deployment has the expected number of available replicas
416 ${count}= Get Available Deployment Replicas ${namespace} ${name}
417 Should Be Equal As Integers ${expected} ${count}
418
419Get Deployment Replica Count
420 [Arguments] ${namespace} ${name}
421 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
422 ${rc} ${value} Run and Return Rc and Output
423 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
424 Should Be Equal as Integers ${rc} 0
425 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
426 ... ELSE Set Variable ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700427 [Return] ${replicas}
David Bainbridgef81cd642019-11-20 00:14:47 +0000428
429Does Deployment Have Replicas
430 [Arguments] ${namespace} ${name} ${expected_count}
431 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
432 ${rc} ${value} Run and Return Rc and Output
433 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
434 Should Be Equal as Integers ${rc} 0
435 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
436 ... ELSE Set Variable ${value}
437 Should be Equal as Integers ${replicas} ${expected_count}
hwchiu85695932019-12-18 08:05:25 +0000438
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700439Pods Are Ready By Label
hwchiu85695932019-12-18 08:05:25 +0000440 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700441 [Documentation] Check that all pods with a label are ready
TorstenThieme37165402021-09-03 11:39:40 +0000442 ${pod_names}= Get Pod Name By Label ${namespace} ${key} ${value}
443 Should Not Be Empty ${pod_names} Unable to parse pod name
444 @{pods}= Split String ${pod_names} separator=${\n}
445 ${lenght}= Get Length ${pods}
446 FOR ${I} IN RANGE 0 ${lenght}
447 ${output}= Run
448 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[${I}].status.containerStatuses[].ready}"
449 Should Not Contain ${output} false
450 END
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000451
452Wait For Pods Ready
453 [Arguments] ${namespace} ${list_apps}
454 [Documentation] Checks the passed PODs are ready
TorstenThiemefe7099e2021-01-29 08:41:04 +0000455 FOR ${app_name} IN @{list_apps}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000456 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000457 ... Pods Are Ready By Label ${namespace} app ${app_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000458 END
Gayathri.Selvan49398962020-01-13 07:19:12 +0000459
hwchiu58af72d2020-01-14 00:50:35 +0000460Check Expected Running Pods Number By Label
461 [Arguments] ${namespace} ${key} ${value} ${number}
462 [Documentation] Succeeds if the desired pod has expected number replicas
463 ${rc} ${count} Run and Return Rc and Output
464 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o json | jq -r ".items[].status.phase" | wc -l
465 Should Be Equal as Integers ${count} ${number}
Andrea Campanella962fe832020-09-21 10:41:47 +0200466
467Get Number of Running Pods Number By Label
468 [Arguments] ${namespace} ${key} ${value}
469 [Documentation] Returns the number of pods for a given label
470 ${rc} ${count} Run and Return Rc and Output
471 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o name | wc -l
472 [Return] ${count}
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000473
474Get Pod Restart Count
475 [Arguments] ${namespace} ${name}
476 [Documentation] Returns the restart count for the given Pod
477 ${rc} ${count}= Run and Return Rc and Output
478 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $4}'
479 [Return] ${count}
480
481Verify ONOS Pod Restart
482 [Arguments] ${restarted}=True
483 [Documentation] Verifies if any of the given ONOS instances restarted
484 ${num_onos}= Wait Until Keyword Succeeds 20s 5s Get Number of Running Pods Number By Label default
485 ... app onos-onos-classic
486 FOR ${I} IN RANGE 0 ${num_onos}
487 ${onos_pod}= Catenate SEPARATOR=- onos-onos-classic ${I}
488 ${count}= Get Pod Restart Count default ${onos_pod}
489 Run Keyword If ${restarted}
490 ... Should Not Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Not Restarted
491 ... ELSE
492 ... Should Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Restarted
493 END
494
495Deploy Pod New Image
496 [Arguments] ${namespace} ${deployment} ${container} ${image}
497 [Documentation] Deploys the Pod given image
498 ${rc} Run and Return Rc
499 ... kubectl -n ${namespace} set image deployment/${deployment} ${container}=${image}
500 Should Be Equal as Integers ${rc} 0
501
502Verify Pod Image
503 [Arguments] ${namespace} ${key} ${value} ${image}
504 [Documentation] Verifies the Pod Image
505 ${output}= Run
Hardik Windlass08451302021-03-09 12:14:36 +0000506 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
507 Should Be Equal '${output}' '${image}'
Hardik Windlassdc2610f2021-03-09 07:33:51 +0000508
509Get Pod Image And App Version And Helm Chart By Label
510 [Arguments] ${namespace} ${key} ${value}
511 [Documentation] Retrieves Pod Image and, App and Helm Chart Version details
512 ${image}= Run
513 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
514 ${cmd}= Catenate SEPARATOR=
515 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=
516 ... jsonpath="{.items[*].metadata.labels.\\app\\.kubernetes\\.io\\/version}"
517 ${app_version}= Run ${cmd}
518 ${helm_chart}= Run
519 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].metadata.labels.\\helm\\.sh\\/chart}"
520 [Return] ${image} ${app_version} ${helm_chart}