blob: 876a5f675e372d7595003078e43725ba5638b1da [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
Matteo Scandolo6b524122021-10-22 14:34:29 -0700186 [Arguments] ${datetime} ${namespace}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000187 [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
Matteo Scandolo6b524122021-10-22 14:34:29 -0700193 ... kubectl logs --timestamps -n ${namespace} --since-time=${datetime} ${containerName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700194 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
Matteo Scandolo6b524122021-10-22 14:34:29 -0700213 ... kubectl logs --timestamps -n ${namespace} --since-time=${datetime} ${containerName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700214 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
Matteo Scandolo6b524122021-10-22 14:34:29 -0700286 [Arguments] ${datetime} ${podDict} ${namespace}
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 ...
Matteo Scandolo6b524122021-10-22 14:34:29 -0700293 ... Usage: ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict} ${namespace}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700294 ...
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
Matteo Scandolo6b524122021-10-22 14:34:29 -0700299 ... ${namespace} = the namespace into which look for pods
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000300 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700301 ... Example: ${podDict} = Set Dictionary ${podDict} radius sample error message.
302 ...
303 ... In case the radius pod log has any other error than the expected
304 ... error, then the podname will be returned
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000305 ${podList} = Get Dictionary Keys ${podDict}
306 FOR ${podName} IN @{podList}
307 ${containerName} Get From Dictionary ${containerDict} ${podName}
308 ${expectedError} Get From Dictionary ${podDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700309 ${rc} ${logOutput} Run And Return Rc And Output
Matteo Scandolo6b524122021-10-22 14:34:29 -0700310 ... kubectl logs --timestamps -n ${namespace} --since-time=${datetime} ${containerName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700311 Run Keyword And Ignore Error
312 ... Run Keyword If '${logOutput}'=='${EMPTY}'
313 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000314 ... AND Continue For Loop
315 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700316 Run Keyword And Ignore Error
317 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
318 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000319 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700320 Run Keyword And Ignore Error
321 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
322 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000323 ... AND Append To List ${errorPodList} ${podName}
324 END
325 [Return] ${errorPodList}
326
David Bainbridgef81cd642019-11-20 00:14:47 +0000327Delete K8s Pod
328 [Arguments] ${namespace} ${name}
329 [Documentation] Uses kubectl to delete a named POD
330 ${rc} Run and Return Rc
331 ... kubectl delete -n ${namespace} pod/${name}
332 Should Be Equal as Integers ${rc} 0
333
hwchiu85695932019-12-18 08:05:25 +0000334Delete K8s Pods By Label
335 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700336 [Documentation] Uses kubectl to delete a PODs, filtering by label
hwchiu85695932019-12-18 08:05:25 +0000337 ${rc}= Run and Return Rc
338 ... kubectl -n ${namespace} delete pods -l${key}=${value}
339 Should Be Equal as Integers ${rc} 0
340
Andrea Campanella4c404632020-08-26 14:41:36 +0200341Delete K8s Pods By Name
342 [Arguments] ${namespace} ${value}
343 [Documentation] Uses kubectl to delete a PODs, filtering by label
344 ${rc}= Run and Return Rc
345 ... kubectl -n ${namespace} delete pods ${value}
346 Should Be Equal as Integers ${rc} 0
347
David Bainbridgef81cd642019-11-20 00:14:47 +0000348Scale K8s Deployment
349 [Arguments] ${namespace} ${name} ${count}
350 [Documentation] Uses kubectl to scale a named deployment
351 ${rc} Run and Return Rc
352 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
353 Should Be Equal as Integers ${rc} 0
354
Andrea Campanella3dcce272021-01-15 16:04:47 +0100355Get K8s Deployment by Pod Label
356 [Arguments] ${namespace} ${key} ${value}
357 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
358 ${rc} ${name} Run And Return Rc And Output
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000359 ... 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 +0100360 Should Be Equal as Integers ${rc} 0
361 [Return] ${name}
362
363Scale K8s Deployment by Pod Label
364 [Arguments] ${namespace} ${key} ${value} ${count}
365 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
366 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
367 Scale K8s Deployment ${namespace} ${name} ${count}
368
David Bainbridgef81cd642019-11-20 00:14:47 +0000369Pod Exists
370 [Arguments] ${namespace} ${name}
371 [Documentation] Succeeds it the named POD exists
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700372 ${rc} ${count} Run and Return Rc
373 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700374 Should Be True ${count}>0 Pod ${name} not found
David Bainbridgef81cd642019-11-20 00:14:47 +0000375
376Pod Does Not Exist
377 [Arguments] ${namespace} ${name}
378 [Documentation] Succeeds if the named POD does not exist
379 ${rc} ${count} Run and Return Rc And Output
380 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
381 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700382 Should Be True ${count}==0 Pod ${name} exists but should not
David Bainbridgef81cd642019-11-20 00:14:47 +0000383
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000384Wait For Pods Not Exist
385 [Arguments] ${namespace} ${list_names}
386 [Documentation] Checks the passed PODs are no longer existing
TorstenThiemefe7099e2021-01-29 08:41:04 +0000387 FOR ${pod_name} IN @{list_names}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000388 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000389 ... Pod Does Not Exist ${namespace} ${pod_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000390 END
391
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700392Pods Do Not Exist By Label
hwchiu85695932019-12-18 08:05:25 +0000393 [Arguments] ${namespace} ${key} ${value}
394 [Documentation] Succeeds if the named POD does not exist
395 ${rc} ${count} Run and Return Rc And Output
396 ... kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
397 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700398 Should Be True ${count}==0 Pod with label ${key}=${value} exists but should not
hwchiu85695932019-12-18 08:05:25 +0000399
David Bainbridgef81cd642019-11-20 00:14:47 +0000400Get Available Deployment Replicas
401 [Arguments] ${namespace} ${name}
402 [Documentation] Succeeds if the named POD exists and has a ready count > 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700403 ${rc} ${count} Run and Return Rc and Output
404 ... kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
David Bainbridgef81cd642019-11-20 00:14:47 +0000405 ${result}= Run Keyword If '${count}' == '' Set Variable 0
406 ... ELSE Set Variable ${count}
407 [Return] ${result}
408
Andrea Campanella3dcce272021-01-15 16:04:47 +0100409Check Expected Available Deployment Replicas By Pod Label
410 [Arguments] ${namespace} ${key} ${value} ${expected}
411 [Documentation] Succeeds if the named deployment has the expected number of available replicas
412 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
413 Check Expected Available Deployment Replicas ${namespace} ${name} ${expected}
414
David Bainbridgef81cd642019-11-20 00:14:47 +0000415Check Expected Available Deployment Replicas
416 [Arguments] ${namespace} ${name} ${expected}
417 [Documentation] Succeeds if the named deployment has the expected number of available replicas
418 ${count}= Get Available Deployment Replicas ${namespace} ${name}
419 Should Be Equal As Integers ${expected} ${count}
420
421Get Deployment Replica Count
422 [Arguments] ${namespace} ${name}
423 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
424 ${rc} ${value} Run and Return Rc and Output
425 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
426 Should Be Equal as Integers ${rc} 0
427 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
428 ... ELSE Set Variable ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700429 [Return] ${replicas}
David Bainbridgef81cd642019-11-20 00:14:47 +0000430
431Does Deployment Have Replicas
432 [Arguments] ${namespace} ${name} ${expected_count}
433 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
434 ${rc} ${value} Run and Return Rc and Output
435 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
436 Should Be Equal as Integers ${rc} 0
437 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
438 ... ELSE Set Variable ${value}
439 Should be Equal as Integers ${replicas} ${expected_count}
hwchiu85695932019-12-18 08:05:25 +0000440
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700441Pods Are Ready By Label
hwchiu85695932019-12-18 08:05:25 +0000442 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700443 [Documentation] Check that all pods with a label are ready
TorstenThieme37165402021-09-03 11:39:40 +0000444 ${pod_names}= Get Pod Name By Label ${namespace} ${key} ${value}
445 Should Not Be Empty ${pod_names} Unable to parse pod name
446 @{pods}= Split String ${pod_names} separator=${\n}
447 ${lenght}= Get Length ${pods}
448 FOR ${I} IN RANGE 0 ${lenght}
449 ${output}= Run
450 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[${I}].status.containerStatuses[].ready}"
451 Should Not Contain ${output} false
452 END
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000453
454Wait For Pods Ready
455 [Arguments] ${namespace} ${list_apps}
456 [Documentation] Checks the passed PODs are ready
TorstenThiemefe7099e2021-01-29 08:41:04 +0000457 FOR ${app_name} IN @{list_apps}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000458 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000459 ... Pods Are Ready By Label ${namespace} app ${app_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000460 END
Gayathri.Selvan49398962020-01-13 07:19:12 +0000461
TorstenThieme96fe9ee2021-10-21 10:24:08 +0000462Get Pod Ready Timestamp by Label
463 [Arguments] ${namespace} ${key} ${value}
464 [Documentation] delivers timestamp of pod was ready
465 ${cmd}= Catenate kubectl -n ${namespace} get pods -l ${key}=${value} -o=json | jq -r
466 ... ".items[].status.containerStatuses[].state.running.startedAt"
467 ${output}= Run ${cmd}
468 [Return] ${output}
469
hwchiu58af72d2020-01-14 00:50:35 +0000470Check Expected Running Pods Number By Label
471 [Arguments] ${namespace} ${key} ${value} ${number}
472 [Documentation] Succeeds if the desired pod has expected number replicas
473 ${rc} ${count} Run and Return Rc and Output
474 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o json | jq -r ".items[].status.phase" | wc -l
475 Should Be Equal as Integers ${count} ${number}
Andrea Campanella962fe832020-09-21 10:41:47 +0200476
477Get Number of Running Pods Number By Label
478 [Arguments] ${namespace} ${key} ${value}
479 [Documentation] Returns the number of pods for a given label
480 ${rc} ${count} Run and Return Rc and Output
481 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o name | wc -l
482 [Return] ${count}
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000483
484Get Pod Restart Count
485 [Arguments] ${namespace} ${name}
486 [Documentation] Returns the restart count for the given Pod
487 ${rc} ${count}= Run and Return Rc and Output
488 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $4}'
489 [Return] ${count}
490
TorstenThieme96fe9ee2021-10-21 10:24:08 +0000491Get Pod Age
492 [Arguments] ${namespace} ${name}
493 [Documentation] Returns the age for the given Pod
494 ${rc} ${age}= Run and Return Rc and Output
495 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $5}'
496 [Return] ${age}
497
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000498Verify ONOS Pod Restart
499 [Arguments] ${restarted}=True
500 [Documentation] Verifies if any of the given ONOS instances restarted
501 ${num_onos}= Wait Until Keyword Succeeds 20s 5s Get Number of Running Pods Number By Label default
502 ... app onos-onos-classic
503 FOR ${I} IN RANGE 0 ${num_onos}
504 ${onos_pod}= Catenate SEPARATOR=- onos-onos-classic ${I}
505 ${count}= Get Pod Restart Count default ${onos_pod}
506 Run Keyword If ${restarted}
507 ... Should Not Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Not Restarted
508 ... ELSE
509 ... Should Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Restarted
510 END
511
512Deploy Pod New Image
513 [Arguments] ${namespace} ${deployment} ${container} ${image}
514 [Documentation] Deploys the Pod given image
515 ${rc} Run and Return Rc
516 ... kubectl -n ${namespace} set image deployment/${deployment} ${container}=${image}
517 Should Be Equal as Integers ${rc} 0
518
519Verify Pod Image
520 [Arguments] ${namespace} ${key} ${value} ${image}
521 [Documentation] Verifies the Pod Image
522 ${output}= Run
Hardik Windlass08451302021-03-09 12:14:36 +0000523 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
524 Should Be Equal '${output}' '${image}'
Hardik Windlassdc2610f2021-03-09 07:33:51 +0000525
526Get Pod Image And App Version And Helm Chart By Label
527 [Arguments] ${namespace} ${key} ${value}
528 [Documentation] Retrieves Pod Image and, App and Helm Chart Version details
529 ${image}= Run
530 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
531 ${cmd}= Catenate SEPARATOR=
532 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=
533 ... jsonpath="{.items[*].metadata.labels.\\app\\.kubernetes\\.io\\/version}"
534 ${app_version}= Run ${cmd}
535 ${helm_chart}= Run
536 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].metadata.labels.\\helm\\.sh\\/chart}"
537 [Return] ${image} ${app_version} ${helm_chart}