blob: f9ce0f3528a8589cf36b1cd19c54cea4d8311d95 [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}'
Hardik Windlass801eaa02021-10-27 13:22:10 +000073 ... Run and Return Rc and Output
74 ... kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name | awk 'NR==1{print $1}'
TorstenThiemefe7099e2021-01-29 08:41:04 +000075 ... ELSE Run and Return Rc and Output
76 ... kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name \| grep ${grep}
Hardik Windlassec86c232021-02-02 13:56:12 +000077 Log ${exec_pod_name}
78 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
79 ${rc} ${output}= Run and Return Rc and Output
80 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
81 Log ${output}
82 [return] ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -070083
84Exec Pod And Return Output And RC
85 [Arguments] ${namespace} ${name} ${command}
86 [Documentation] Uses kubectl to execute a command in the pod and return the output
87 ${rc} ${exec_pod_name}= Run and Return Rc and Output
88 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
Holger Hildebrandt23147742020-11-16 10:13:21 +000089 Log ${exec_pod_name}
90 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
91 ${rc} ${output}= Run and Return Rc and Output
92 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
93 Log ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -070094 [return] ${output} ${rc}
Holger Hildebrandt23147742020-11-16 10:13:21 +000095
Scott Baker60e570d2020-02-02 22:10:13 -080096Exec Pod Separate Stderr
97 [Arguments] ${namespace} ${name} ${command}
98 [Documentation] Uses kubectl to execute a command in the pod and return the stderr and stdout
99 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +0200100 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -0800101 Log ${exec_pod_name}
102 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
103 @{args}= Split String ${command}
104 ${result}= Run Process
105 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- @{args}
106 ${stdout}= Set Variable ${result.stdout}
107 ${stderr}= Set Variable ${result.stderr}
108 Log ${stdout}
109 Log ${stderr}
110 [return] ${stdout} ${stderr}
111
Scott Baker0478bab2020-04-10 17:19:57 -0700112Copy File To Pod
Andrea Campanella0aa21d62021-07-22 10:44:32 +0200113 [Arguments] ${namespace} ${label} ${src} ${dest}
Scott Baker0478bab2020-04-10 17:19:57 -0700114 [Documentation] Uses kubectl to copy a file to a pod
115 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella0aa21d62021-07-22 10:44:32 +0200116 ... kubectl get pods -n ${namespace} -l ${label} --no-headers | awk 'NR==1{print $1}'
Scott Baker0478bab2020-04-10 17:19:57 -0700117 Log ${exec_pod_name}
118 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
119 ${rc} ${output}= Run and Return Rc and Output
120 ... kubectl cp -n ${namespace} ${src} ${exec_pod_name}:${dest}
121 Log ${output}
122 [return] ${output}
123
Scott Baker60e570d2020-02-02 22:10:13 -0800124Apply Kubernetes Resources
125 [Arguments] ${resource_yaml} ${namespace}
126 [Documentation] Use kubectl to create resources given a yaml file
127 ${rc} Run and Return Rc
128 ... kubectl apply -n ${namespace} -f ${resource_yaml}
129 Should Be Equal as Integers ${rc} 0
130
Scott Baker0478bab2020-04-10 17:19:57 -0700131Delete Kubernetes Resources
132 [Arguments] ${resource_yaml} ${namespace}
133 [Documentation] Use kubectl to delete resources given a yaml file
134 ${rc} Run and Return Rc
135 ... kubectl delete -n ${namespace} -f ${resource_yaml}
136 Should Be Equal as Integers ${rc} 0
137
suraj gour1ecfae92019-12-20 15:11:40 +0000138Validate Pod Status
139 [Arguments] ${pod_name} ${namespace} ${expectedStatus}
140 [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 -0700141 ${length}= Run kubectl get pod -n ${namespace} -o name | wc -l
142 ${matched}= Set Variable False
143 FOR ${index} IN RANGE ${length}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700144 ${currentPodName}= Run
145 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.containerStatuses[0].name}"
suraj gour1ecfae92019-12-20 15:11:40 +0000146 Log Required Pod : ${pod_name}
147 Log Current Pod: ${currentPodName}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700148 ${matched}= Set Variable If '${currentPodName}'=='${pod_name}' True False
149 Exit For Loop If ${matched}
suraj gour1ecfae92019-12-20 15:11:40 +0000150 END
Andy Bavierb63f6d22020-03-12 15:34:37 -0700151 Should Be True ${matched} No pod ${podname} found
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700152 ${currentStatusofPod}= Run
153 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.phase}"
suraj gour1ecfae92019-12-20 15:11:40 +0000154 Log ${currentStatusofPod}
155 Should Contain ${currentStatusofPod} ${expectedStatus}
156
Matteo Scandoloa80b4732020-09-04 13:51:10 -0700157Get Pod Name By Label
158 [Arguments] ${namespace} ${label_key} ${label_value}
159 [Documentation] Return a pod name from a given label
160 ${rc} ${pod_name}= Run and Return Rc and Output
161 ... kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
162 Should Not Be Empty ${pod_name} Pod not found
163 [return] ${pod_name}
164
Hung-Wei Chiu2bee4d42020-04-24 11:31:50 -0700165Validate Pods Status By Label
166 [Arguments] ${namespace} ${label_key} ${label_value} ${expectedStatus}
167 [Documentation] To run the kubectl command and check the status of all pods filter
168 ... by label matche the expected status
169 ${command}= Catenate
170 ... kubectl -n ${namespace} get pods -l ${label_key}=${label_value}
171 ... -o=jsonpath="{.items[?(.status.phase=='${expectedStatus}')].status.phase}"
172 ${pods_status}= Run ${command}
173 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
174
Andrea Campanella4c404632020-08-26 14:41:36 +0200175Validate Pods Status By Name
176 [Arguments] ${namespace} ${name} ${expectedStatus}
177 [Documentation] To run the kubectl command and check the status of all pods filter
178 ... by label matche the expected status
179 ${command}= Catenate
180 ... kubectl -n ${namespace} get pods ${name}
181 ... -o=jsonpath="{.status.phase}"
182 ${pods_status}= Run ${command}
183 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
184
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000185Verify All Voltha Pods For Any Error Logs
186 [Arguments] ${datetime}
187 [Documentation] This keyword checks for the error occurence in the voltha pods
188 &{errorPodDict} Create Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700189 &{containerDict} Get Container Dictionary voltha
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000190 FOR ${podName} IN @{PODLIST1}
191 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700192 ${rc} ${logOutput} Run And Return Rc And Output
193 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
194 Run Keyword And Ignore Error
195 ... Run Keyword If '${logOutput}'=='${EMPTY}'
196 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000197 ... AND Continue For Loop
198 ${errorDict} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
199 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
200 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700201 Run Keyword And Ignore Error
202 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
203 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000204 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700205 Run Keyword And Ignore Error
206 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
207 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000208 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
209 END
210 FOR ${podName} IN @{PODLIST2}
211 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700212 ${rc} ${logOutput} Run And Return Rc And Output
213 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
214 Run Keyword And Ignore Error
215 ... Run Keyword If '${logOutput}'=='${EMPTY}'
216 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000217 ... AND Continue For Loop
218 ${errorDict} Check For Error Logs in Pod Type2 Given the Log Output ${logOutput}
219 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
220 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700221 Run Keyword And Ignore Error
222 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
223 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000224 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700225 Run Keyword And Ignore Error
226 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
227 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000228 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
229 END
230 Print to Console Error Statement logged in the following pods : ${errorPodDict}
231 [Return] ${errorPodDict}
232
233Check For Error Logs in Pod Type1 Given the Log Output
234 [Arguments] ${logOutput} ${logLevel}=error ${errorMessage}=${EMPTY}
235 [Documentation] Checks for error message in the particular list of pods
236 Log ${logOutput}
237 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.* partial_match=true
238 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700239 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
240 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
241 ${linesContainingError} = Get Lines Matching Regexp
242 ... ${logOutput} .*\s\${logLevel}.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000243 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700244 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
245 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000246 Log {linesContainingError}
247 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
248 [Return] ${errorDict}
249
250Check For Error Logs in Pod Type2 Given the Log Output
251 [Arguments] ${logOutput} ${logLevel}=warn ${errorMessage}=${EMPTY}
252 [Documentation] Checks for error message in the particular set of pods
253 Log ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700254 ${linesContainingLog} = Get Lines Matching Regexp
255 ... ${logOutput} .*?\s.*level.*${logLevel}.* partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000256 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700257 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
258 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
259 ${linesContainingError} = Get Lines Matching Regexp
260 ... ${logOutput} .*?\s.*level.*${logLevel}.*msg.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000261 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700262 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
263 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000264 Log {linesContainingError}
265 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
266 [Return] ${errorDict}
267
268Get Container Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700269 [Arguments] ${namespace}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000270 [Documentation] Creates a mapping for pod name and container name and returns the same
271 &{containerDict} Create Dictionary
272 ${containerName} Set Variable ${EMPTY}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700273 ${podName} Run kubectl get deployment -n ${namespace} | awk 'NR>1 {print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000274 @{podNameList}= Split To Lines ${podName}
275 Append To List ${podNameList} voltha-etcd-cluster voltha-kafka voltha-ro-core voltha-zookeeper
276 Log ${podNameList}
277 #Creatiing dictionary to correspond pod name and container name
278 FOR ${pod} IN @{podNameList}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700279 ${containerName} Run kubectl get pod -n ${namespace} | grep ${pod} | awk '{print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000280 &{containerDict} Set To Dictionary ${containerDict} ${pod} ${containerName}
281 END
282 Log ${containerDict}
283 [Return] ${containerDict}
284
285Validate Error For Given Pods
286 [Arguments] ${datetime} ${podDict}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700287 [Documentation]
288 ... This keyword is used to get the list of pods if there is any unexpected error
289 ... in a particular pod(s) given the time-${datetime} from which the log needs to
290 ... be analysed and the dictionary of pods and the error in the dictionary format
291 ... ${podDict] .
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000292 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700293 ... Usage: ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict}
294 ...
295 ... Arguments:
296 ...
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000297 ... ${datetime} = time from which the log needs to be taken
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700298 ... ${podDict} = Key-value pair of the pod name and the error msg
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000299 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700300 ... Example: ${podDict} = Set Dictionary ${podDict} radius sample error message.
301 ...
302 ... In case the radius pod log has any other error than the expected
303 ... error, then the podname will be returned
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000304 ${podList} = Get Dictionary Keys ${podDict}
305 FOR ${podName} IN @{podList}
306 ${containerName} Get From Dictionary ${containerDict} ${podName}
307 ${expectedError} Get From Dictionary ${podDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700308 ${rc} ${logOutput} Run And Return Rc And Output
309 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
310 Run Keyword And Ignore Error
311 ... Run Keyword If '${logOutput}'=='${EMPTY}'
312 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000313 ... AND Continue For Loop
314 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700315 Run Keyword And Ignore Error
316 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
317 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000318 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700319 Run Keyword And Ignore Error
320 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
321 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000322 ... AND Append To List ${errorPodList} ${podName}
323 END
324 [Return] ${errorPodList}
325
David Bainbridgef81cd642019-11-20 00:14:47 +0000326Delete K8s Pod
327 [Arguments] ${namespace} ${name}
328 [Documentation] Uses kubectl to delete a named POD
329 ${rc} Run and Return Rc
330 ... kubectl delete -n ${namespace} pod/${name}
331 Should Be Equal as Integers ${rc} 0
332
hwchiu85695932019-12-18 08:05:25 +0000333Delete K8s Pods By Label
334 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700335 [Documentation] Uses kubectl to delete a PODs, filtering by label
hwchiu85695932019-12-18 08:05:25 +0000336 ${rc}= Run and Return Rc
337 ... kubectl -n ${namespace} delete pods -l${key}=${value}
338 Should Be Equal as Integers ${rc} 0
339
Andrea Campanella4c404632020-08-26 14:41:36 +0200340Delete K8s Pods By Name
341 [Arguments] ${namespace} ${value}
342 [Documentation] Uses kubectl to delete a PODs, filtering by label
343 ${rc}= Run and Return Rc
344 ... kubectl -n ${namespace} delete pods ${value}
345 Should Be Equal as Integers ${rc} 0
346
David Bainbridgef81cd642019-11-20 00:14:47 +0000347Scale K8s Deployment
348 [Arguments] ${namespace} ${name} ${count}
349 [Documentation] Uses kubectl to scale a named deployment
350 ${rc} Run and Return Rc
351 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
352 Should Be Equal as Integers ${rc} 0
353
Andrea Campanella3dcce272021-01-15 16:04:47 +0100354Get K8s Deployment by Pod Label
355 [Arguments] ${namespace} ${key} ${value}
356 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
357 ${rc} ${name} Run And Return Rc And Output
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000358 ... 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 +0100359 Should Be Equal as Integers ${rc} 0
360 [Return] ${name}
361
362Scale K8s Deployment by Pod Label
363 [Arguments] ${namespace} ${key} ${value} ${count}
364 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
365 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
366 Scale K8s Deployment ${namespace} ${name} ${count}
367
David Bainbridgef81cd642019-11-20 00:14:47 +0000368Pod Exists
369 [Arguments] ${namespace} ${name}
370 [Documentation] Succeeds it the named POD exists
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700371 ${rc} ${count} Run and Return Rc
372 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700373 Should Be True ${count}>0 Pod ${name} not found
David Bainbridgef81cd642019-11-20 00:14:47 +0000374
375Pod Does Not Exist
376 [Arguments] ${namespace} ${name}
377 [Documentation] Succeeds if the named POD does not exist
378 ${rc} ${count} Run and Return Rc And Output
379 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
380 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700381 Should Be True ${count}==0 Pod ${name} exists but should not
David Bainbridgef81cd642019-11-20 00:14:47 +0000382
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000383Wait For Pods Not Exist
384 [Arguments] ${namespace} ${list_names}
385 [Documentation] Checks the passed PODs are no longer existing
TorstenThiemefe7099e2021-01-29 08:41:04 +0000386 FOR ${pod_name} IN @{list_names}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000387 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000388 ... Pod Does Not Exist ${namespace} ${pod_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000389 END
390
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700391Pods Do Not Exist By Label
hwchiu85695932019-12-18 08:05:25 +0000392 [Arguments] ${namespace} ${key} ${value}
393 [Documentation] Succeeds if the named POD does not exist
394 ${rc} ${count} Run and Return Rc And Output
395 ... kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
396 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700397 Should Be True ${count}==0 Pod with label ${key}=${value} exists but should not
hwchiu85695932019-12-18 08:05:25 +0000398
David Bainbridgef81cd642019-11-20 00:14:47 +0000399Get Available Deployment Replicas
400 [Arguments] ${namespace} ${name}
401 [Documentation] Succeeds if the named POD exists and has a ready count > 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700402 ${rc} ${count} Run and Return Rc and Output
403 ... kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
David Bainbridgef81cd642019-11-20 00:14:47 +0000404 ${result}= Run Keyword If '${count}' == '' Set Variable 0
405 ... ELSE Set Variable ${count}
406 [Return] ${result}
407
Andrea Campanella3dcce272021-01-15 16:04:47 +0100408Check Expected Available Deployment Replicas By Pod Label
409 [Arguments] ${namespace} ${key} ${value} ${expected}
410 [Documentation] Succeeds if the named deployment has the expected number of available replicas
411 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
412 Check Expected Available Deployment Replicas ${namespace} ${name} ${expected}
413
David Bainbridgef81cd642019-11-20 00:14:47 +0000414Check Expected Available Deployment Replicas
415 [Arguments] ${namespace} ${name} ${expected}
416 [Documentation] Succeeds if the named deployment has the expected number of available replicas
417 ${count}= Get Available Deployment Replicas ${namespace} ${name}
418 Should Be Equal As Integers ${expected} ${count}
419
420Get Deployment Replica Count
421 [Arguments] ${namespace} ${name}
422 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
423 ${rc} ${value} Run and Return Rc and Output
424 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
425 Should Be Equal as Integers ${rc} 0
426 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
427 ... ELSE Set Variable ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700428 [Return] ${replicas}
David Bainbridgef81cd642019-11-20 00:14:47 +0000429
430Does Deployment Have Replicas
431 [Arguments] ${namespace} ${name} ${expected_count}
432 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
433 ${rc} ${value} Run and Return Rc and Output
434 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
435 Should Be Equal as Integers ${rc} 0
436 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
437 ... ELSE Set Variable ${value}
438 Should be Equal as Integers ${replicas} ${expected_count}
hwchiu85695932019-12-18 08:05:25 +0000439
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700440Pods Are Ready By Label
hwchiu85695932019-12-18 08:05:25 +0000441 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700442 [Documentation] Check that all pods with a label are ready
TorstenThieme37165402021-09-03 11:39:40 +0000443 ${pod_names}= Get Pod Name By Label ${namespace} ${key} ${value}
444 Should Not Be Empty ${pod_names} Unable to parse pod name
445 @{pods}= Split String ${pod_names} separator=${\n}
446 ${lenght}= Get Length ${pods}
447 FOR ${I} IN RANGE 0 ${lenght}
448 ${output}= Run
449 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[${I}].status.containerStatuses[].ready}"
450 Should Not Contain ${output} false
451 END
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000452
453Wait For Pods Ready
454 [Arguments] ${namespace} ${list_apps}
455 [Documentation] Checks the passed PODs are ready
TorstenThiemefe7099e2021-01-29 08:41:04 +0000456 FOR ${app_name} IN @{list_apps}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000457 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000458 ... Pods Are Ready By Label ${namespace} app ${app_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000459 END
Gayathri.Selvan49398962020-01-13 07:19:12 +0000460
TorstenThieme96fe9ee2021-10-21 10:24:08 +0000461Get Pod Ready Timestamp by Label
462 [Arguments] ${namespace} ${key} ${value}
463 [Documentation] delivers timestamp of pod was ready
464 ${cmd}= Catenate kubectl -n ${namespace} get pods -l ${key}=${value} -o=json | jq -r
465 ... ".items[].status.containerStatuses[].state.running.startedAt"
466 ${output}= Run ${cmd}
467 [Return] ${output}
468
hwchiu58af72d2020-01-14 00:50:35 +0000469Check Expected Running Pods Number By Label
470 [Arguments] ${namespace} ${key} ${value} ${number}
471 [Documentation] Succeeds if the desired pod has expected number replicas
472 ${rc} ${count} Run and Return Rc and Output
473 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o json | jq -r ".items[].status.phase" | wc -l
474 Should Be Equal as Integers ${count} ${number}
Andrea Campanella962fe832020-09-21 10:41:47 +0200475
476Get Number of Running Pods Number By Label
477 [Arguments] ${namespace} ${key} ${value}
478 [Documentation] Returns the number of pods for a given label
479 ${rc} ${count} Run and Return Rc and Output
480 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o name | wc -l
481 [Return] ${count}
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000482
483Get Pod Restart Count
484 [Arguments] ${namespace} ${name}
485 [Documentation] Returns the restart count for the given Pod
486 ${rc} ${count}= Run and Return Rc and Output
487 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $4}'
488 [Return] ${count}
489
TorstenThieme96fe9ee2021-10-21 10:24:08 +0000490Get Pod Age
491 [Arguments] ${namespace} ${name}
492 [Documentation] Returns the age for the given Pod
493 ${rc} ${age}= Run and Return Rc and Output
494 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $5}'
495 [Return] ${age}
496
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000497Verify ONOS Pod Restart
498 [Arguments] ${restarted}=True
499 [Documentation] Verifies if any of the given ONOS instances restarted
500 ${num_onos}= Wait Until Keyword Succeeds 20s 5s Get Number of Running Pods Number By Label default
501 ... app onos-onos-classic
502 FOR ${I} IN RANGE 0 ${num_onos}
503 ${onos_pod}= Catenate SEPARATOR=- onos-onos-classic ${I}
504 ${count}= Get Pod Restart Count default ${onos_pod}
505 Run Keyword If ${restarted}
506 ... Should Not Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Not Restarted
507 ... ELSE
508 ... Should Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Restarted
509 END
510
511Deploy Pod New Image
512 [Arguments] ${namespace} ${deployment} ${container} ${image}
513 [Documentation] Deploys the Pod given image
514 ${rc} Run and Return Rc
515 ... kubectl -n ${namespace} set image deployment/${deployment} ${container}=${image}
516 Should Be Equal as Integers ${rc} 0
517
518Verify Pod Image
519 [Arguments] ${namespace} ${key} ${value} ${image}
520 [Documentation] Verifies the Pod Image
521 ${output}= Run
Hardik Windlass08451302021-03-09 12:14:36 +0000522 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
523 Should Be Equal '${output}' '${image}'
Hardik Windlassdc2610f2021-03-09 07:33:51 +0000524
525Get Pod Image And App Version And Helm Chart By Label
526 [Arguments] ${namespace} ${key} ${value}
527 [Documentation] Retrieves Pod Image and, App and Helm Chart Version details
528 ${image}= Run
529 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
530 ${cmd}= Catenate SEPARATOR=
531 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=
532 ... jsonpath="{.items[*].metadata.labels.\\app\\.kubernetes\\.io\\/version}"
533 ${app_version}= Run ${cmd}
534 ${helm_chart}= Run
535 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].metadata.labels.\\helm\\.sh\\/chart}"
536 [Return] ${image} ${app_version} ${helm_chart}