blob: 5b0029adf512cd3dd7273b40944598e3a43a1880 [file] [log] [blame]
David Bainbridge117d23e2019-09-30 20:37:51 +00001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
David Bainbridge117d23e2019-09-30 20:37:51 +000014# voltctl common functions
15
16*** Settings ***
17Documentation Library for various utilities
18Library SSHLibrary
David Bainbridge117d23e2019-09-30 20:37:51 +000019Library String
20Library DateTime
21Library Process
22Library Collections
23Library RequestsLibrary
24Library OperatingSystem
25
26*** Keywords ***
27Lookup Service IP
28 [Arguments] ${namespace} ${name}
David Bainbridgef81cd642019-11-20 00:14:47 +000029 [Documentation] Uses kubectl to resolve a service name to an IP
Gilles Depatie675a2062019-10-22 12:44:42 -040030 ${rc} ${ip}= Run and Return Rc and Output
31 ... kubectl get svc -n ${namespace} ${name} -o jsonpath={.spec.clusterIP}
David Bainbridge117d23e2019-09-30 20:37:51 +000032 Should Be Equal as Integers ${rc} 0
33 [Return] ${ip}
34
35Lookup Service PORT
36 [Arguments] ${namespace} ${name}
David Bainbridgef81cd642019-11-20 00:14:47 +000037 [Documentation] Uses kubectl to resolve a service name to an PORT
Gilles Depatie675a2062019-10-22 12:44:42 -040038 ${rc} ${port}= Run and Return Rc and Output
39 ... kubectl get svc -n ${namespace} ${name} -o jsonpath={.spec.ports[0].port}
David Bainbridge117d23e2019-09-30 20:37:51 +000040 Should Be Equal as Integers ${rc} 0
41 [Return] ${port}
suraj gourd64356b2019-11-07 13:26:20 +000042
43Restart Pod
44 [Arguments] ${namespace} ${name}
Matteo Scandolo6f24ea92021-04-29 11:55:50 -070045 [Documentation] *DEPRECATED* Use Restart Pod By Label instead
suraj gour067451d2019-11-13 11:20:13 +000046 ${rc} ${restart_pod_name}= Run and Return Rc and Output
47 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
suraj gourd64356b2019-11-07 13:26:20 +000048 Log ${restart_pod_name}
suraj gour067451d2019-11-13 11:20:13 +000049 Should Not Be Empty ${restart_pod_name} Unable to parse pod name
Zack Williamsa8fe75a2020-01-10 14:25:27 -070050 ${rc} ${output}= Run and Return Rc and Output
51 ... kubectl delete pod ${restart_pod_name} -n ${namespace} --grace-period=0 --force
suraj gourd64356b2019-11-07 13:26:20 +000052 Log ${output}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +000053
Matteo Scandolo6f24ea92021-04-29 11:55:50 -070054Restart Pod By Label
55 [Arguments] ${namespace} ${label_key} ${label_value}
TorstenThiemeceb9a202022-03-04 08:27:54 +000056 [Documentation] Uses kubectl to force delete pod(s)
Matteo Scandolo6f24ea92021-04-29 11:55:50 -070057 ${rc} ${restart_pod_name}= Run and Return Rc and Output
58 ... kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
59 Log ${restart_pod_name}
60 Should Not Be Empty ${restart_pod_name} Unable to parse pod name
TorstenThiemeceb9a202022-03-04 08:27:54 +000061 @{pods}= Split String ${restart_pod_name} separator=${\n}
62 FOR ${pod_name} IN @{pods}
63 ${rc} ${output}= Run and Return Rc and Output
64 ... kubectl delete pod ${pod_name} -n ${namespace} --grace-period=0 --force
65 Log ${output}
66 END
Matteo Scandolo6f24ea92021-04-29 11:55:50 -070067
Scott Baker60e570d2020-02-02 22:10:13 -080068Exec Pod
69 [Arguments] ${namespace} ${name} ${command}
70 [Documentation] Uses kubectl to execute a command in the pod and return the output
71 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +020072 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -080073 Log ${exec_pod_name}
74 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
75 ${rc} ${output}= Run and Return Rc and Output
76 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
77 Log ${output}
78 [return] ${output}
79
Holger Hildebrandt23147742020-11-16 10:13:21 +000080Exec Pod In Kube
TorstenThiemefe7099e2021-01-29 08:41:04 +000081 [Arguments] ${namespace} ${name} ${command} ${grep}=${EMPTY}
Holger Hildebrandt23147742020-11-16 10:13:21 +000082 [Documentation] Uses kubectl to execute a command in the pod and return the output
TorstenThiemefe7099e2021-01-29 08:41:04 +000083 ${rc} ${exec_pod_name}= Run Keyword If '${grep}'=='${EMPTY}'
Hardik Windlassa9b38262021-10-27 08:14:22 +000084 ... Run and Return Rc and Output
85 ... kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name | awk 'NR==1{print $1}'
TorstenThiemefe7099e2021-01-29 08:41:04 +000086 ... ELSE Run and Return Rc and Output
87 ... kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name \| grep ${grep}
Hardik Windlassec86c232021-02-02 13:56:12 +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}
93 [return] ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -070094
95Exec Pod And Return Output And RC
96 [Arguments] ${namespace} ${name} ${command}
97 [Documentation] Uses kubectl to execute a command in the pod and return the output
98 ${rc} ${exec_pod_name}= Run and Return Rc and Output
99 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
Holger Hildebrandt23147742020-11-16 10:13:21 +0000100 Log ${exec_pod_name}
101 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
102 ${rc} ${output}= Run and Return Rc and Output
103 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
104 Log ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -0700105 [return] ${output} ${rc}
Holger Hildebrandt23147742020-11-16 10:13:21 +0000106
Scott Baker60e570d2020-02-02 22:10:13 -0800107Exec Pod Separate Stderr
108 [Arguments] ${namespace} ${name} ${command}
109 [Documentation] Uses kubectl to execute a command in the pod and return the stderr and stdout
110 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +0200111 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -0800112 Log ${exec_pod_name}
113 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
114 @{args}= Split String ${command}
115 ${result}= Run Process
116 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- @{args}
117 ${stdout}= Set Variable ${result.stdout}
118 ${stderr}= Set Variable ${result.stderr}
119 Log ${stdout}
120 Log ${stderr}
121 [return] ${stdout} ${stderr}
122
Scott Baker0478bab2020-04-10 17:19:57 -0700123Copy File To Pod
124 [Arguments] ${namespace} ${name} ${src} ${dest}
125 [Documentation] Uses kubectl to copy a file to a pod
126 ${rc} ${exec_pod_name}= Run and Return Rc and Output
127 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
128 Log ${exec_pod_name}
129 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
130 ${rc} ${output}= Run and Return Rc and Output
131 ... kubectl cp -n ${namespace} ${src} ${exec_pod_name}:${dest}
132 Log ${output}
133 [return] ${output}
134
Scott Baker60e570d2020-02-02 22:10:13 -0800135Apply Kubernetes Resources
136 [Arguments] ${resource_yaml} ${namespace}
137 [Documentation] Use kubectl to create resources given a yaml file
138 ${rc} Run and Return Rc
139 ... kubectl apply -n ${namespace} -f ${resource_yaml}
140 Should Be Equal as Integers ${rc} 0
141
Scott Baker0478bab2020-04-10 17:19:57 -0700142Delete Kubernetes Resources
143 [Arguments] ${resource_yaml} ${namespace}
144 [Documentation] Use kubectl to delete resources given a yaml file
145 ${rc} Run and Return Rc
146 ... kubectl delete -n ${namespace} -f ${resource_yaml}
147 Should Be Equal as Integers ${rc} 0
148
suraj gour1ecfae92019-12-20 15:11:40 +0000149Validate Pod Status
150 [Arguments] ${pod_name} ${namespace} ${expectedStatus}
151 [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 -0700152 ${length}= Run kubectl get pod -n ${namespace} -o name | wc -l
153 ${matched}= Set Variable False
154 FOR ${index} IN RANGE ${length}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700155 ${currentPodName}= Run
156 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.containerStatuses[0].name}"
suraj gour1ecfae92019-12-20 15:11:40 +0000157 Log Required Pod : ${pod_name}
158 Log Current Pod: ${currentPodName}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700159 ${matched}= Set Variable If '${currentPodName}'=='${pod_name}' True False
160 Exit For Loop If ${matched}
suraj gour1ecfae92019-12-20 15:11:40 +0000161 END
Andy Bavierb63f6d22020-03-12 15:34:37 -0700162 Should Be True ${matched} No pod ${podname} found
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700163 ${currentStatusofPod}= Run
164 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.phase}"
suraj gour1ecfae92019-12-20 15:11:40 +0000165 Log ${currentStatusofPod}
166 Should Contain ${currentStatusofPod} ${expectedStatus}
167
Matteo Scandoloa80b4732020-09-04 13:51:10 -0700168Get Pod Name By Label
169 [Arguments] ${namespace} ${label_key} ${label_value}
170 [Documentation] Return a pod name from a given label
171 ${rc} ${pod_name}= Run and Return Rc and Output
172 ... kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
173 Should Not Be Empty ${pod_name} Pod not found
174 [return] ${pod_name}
175
Hung-Wei Chiu2bee4d42020-04-24 11:31:50 -0700176Validate Pods Status By Label
177 [Arguments] ${namespace} ${label_key} ${label_value} ${expectedStatus}
178 [Documentation] To run the kubectl command and check the status of all pods filter
179 ... by label matche the expected status
180 ${command}= Catenate
181 ... kubectl -n ${namespace} get pods -l ${label_key}=${label_value}
182 ... -o=jsonpath="{.items[?(.status.phase=='${expectedStatus}')].status.phase}"
183 ${pods_status}= Run ${command}
184 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
185
Andrea Campanella4c404632020-08-26 14:41:36 +0200186Validate Pods Status By Name
187 [Arguments] ${namespace} ${name} ${expectedStatus}
188 [Documentation] To run the kubectl command and check the status of all pods filter
189 ... by label matche the expected status
190 ${command}= Catenate
191 ... kubectl -n ${namespace} get pods ${name}
192 ... -o=jsonpath="{.status.phase}"
193 ${pods_status}= Run ${command}
194 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
195
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000196Verify All Voltha Pods For Any Error Logs
197 [Arguments] ${datetime}
198 [Documentation] This keyword checks for the error occurence in the voltha pods
199 &{errorPodDict} Create Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700200 &{containerDict} Get Container Dictionary voltha
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000201 FOR ${podName} IN @{PODLIST1}
202 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700203 ${rc} ${logOutput} Run And Return Rc And Output
204 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
205 Run Keyword And Ignore Error
206 ... Run Keyword If '${logOutput}'=='${EMPTY}'
207 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000208 ... AND Continue For Loop
209 ${errorDict} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
210 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
211 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700212 Run Keyword And Ignore Error
213 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
214 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000215 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700216 Run Keyword And Ignore Error
217 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
218 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000219 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
220 END
221 FOR ${podName} IN @{PODLIST2}
222 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700223 ${rc} ${logOutput} Run And Return Rc And Output
224 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
225 Run Keyword And Ignore Error
226 ... Run Keyword If '${logOutput}'=='${EMPTY}'
227 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000228 ... AND Continue For Loop
229 ${errorDict} Check For Error Logs in Pod Type2 Given the Log Output ${logOutput}
230 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
231 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700232 Run Keyword And Ignore Error
233 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
234 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000235 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700236 Run Keyword And Ignore Error
237 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
238 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000239 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
240 END
241 Print to Console Error Statement logged in the following pods : ${errorPodDict}
242 [Return] ${errorPodDict}
243
244Check For Error Logs in Pod Type1 Given the Log Output
245 [Arguments] ${logOutput} ${logLevel}=error ${errorMessage}=${EMPTY}
246 [Documentation] Checks for error message in the particular list of pods
247 Log ${logOutput}
248 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.* partial_match=true
249 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700250 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
251 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
252 ${linesContainingError} = Get Lines Matching Regexp
253 ... ${logOutput} .*\s\${logLevel}.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000254 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700255 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
256 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000257 Log {linesContainingError}
258 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
259 [Return] ${errorDict}
260
261Check For Error Logs in Pod Type2 Given the Log Output
262 [Arguments] ${logOutput} ${logLevel}=warn ${errorMessage}=${EMPTY}
263 [Documentation] Checks for error message in the particular set of pods
264 Log ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700265 ${linesContainingLog} = Get Lines Matching Regexp
266 ... ${logOutput} .*?\s.*level.*${logLevel}.* partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000267 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700268 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
269 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
270 ${linesContainingError} = Get Lines Matching Regexp
271 ... ${logOutput} .*?\s.*level.*${logLevel}.*msg.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000272 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700273 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
274 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000275 Log {linesContainingError}
276 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
277 [Return] ${errorDict}
278
279Get Container Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700280 [Arguments] ${namespace}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000281 [Documentation] Creates a mapping for pod name and container name and returns the same
282 &{containerDict} Create Dictionary
283 ${containerName} Set Variable ${EMPTY}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700284 ${podName} Run kubectl get deployment -n ${namespace} | awk 'NR>1 {print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000285 @{podNameList}= Split To Lines ${podName}
286 Append To List ${podNameList} voltha-etcd-cluster voltha-kafka voltha-ro-core voltha-zookeeper
287 Log ${podNameList}
288 #Creatiing dictionary to correspond pod name and container name
289 FOR ${pod} IN @{podNameList}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700290 ${containerName} Run kubectl get pod -n ${namespace} | grep ${pod} | awk '{print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000291 &{containerDict} Set To Dictionary ${containerDict} ${pod} ${containerName}
292 END
293 Log ${containerDict}
294 [Return] ${containerDict}
295
296Validate Error For Given Pods
297 [Arguments] ${datetime} ${podDict}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700298 [Documentation]
299 ... This keyword is used to get the list of pods if there is any unexpected error
300 ... in a particular pod(s) given the time-${datetime} from which the log needs to
301 ... be analysed and the dictionary of pods and the error in the dictionary format
302 ... ${podDict] .
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000303 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700304 ... Usage: ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict}
305 ...
306 ... Arguments:
307 ...
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000308 ... ${datetime} = time from which the log needs to be taken
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700309 ... ${podDict} = Key-value pair of the pod name and the error msg
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000310 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700311 ... Example: ${podDict} = Set Dictionary ${podDict} radius sample error message.
312 ...
313 ... In case the radius pod log has any other error than the expected
314 ... error, then the podname will be returned
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000315 ${podList} = Get Dictionary Keys ${podDict}
316 FOR ${podName} IN @{podList}
317 ${containerName} Get From Dictionary ${containerDict} ${podName}
318 ${expectedError} Get From Dictionary ${podDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700319 ${rc} ${logOutput} Run And Return Rc And Output
320 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
321 Run Keyword And Ignore Error
322 ... Run Keyword If '${logOutput}'=='${EMPTY}'
323 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000324 ... AND Continue For Loop
325 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700326 Run Keyword And Ignore Error
327 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
328 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000329 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700330 Run Keyword And Ignore Error
331 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
332 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000333 ... AND Append To List ${errorPodList} ${podName}
334 END
335 [Return] ${errorPodList}
336
David Bainbridgef81cd642019-11-20 00:14:47 +0000337Delete K8s Pod
338 [Arguments] ${namespace} ${name}
339 [Documentation] Uses kubectl to delete a named POD
340 ${rc} Run and Return Rc
341 ... kubectl delete -n ${namespace} pod/${name}
342 Should Be Equal as Integers ${rc} 0
343
hwchiu85695932019-12-18 08:05:25 +0000344Delete K8s Pods By Label
345 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700346 [Documentation] Uses kubectl to delete a PODs, filtering by label
hwchiu85695932019-12-18 08:05:25 +0000347 ${rc}= Run and Return Rc
348 ... kubectl -n ${namespace} delete pods -l${key}=${value}
349 Should Be Equal as Integers ${rc} 0
350
Andrea Campanella4c404632020-08-26 14:41:36 +0200351Delete K8s Pods By Name
352 [Arguments] ${namespace} ${value}
353 [Documentation] Uses kubectl to delete a PODs, filtering by label
354 ${rc}= Run and Return Rc
355 ... kubectl -n ${namespace} delete pods ${value}
356 Should Be Equal as Integers ${rc} 0
357
David Bainbridgef81cd642019-11-20 00:14:47 +0000358Scale K8s Deployment
359 [Arguments] ${namespace} ${name} ${count}
360 [Documentation] Uses kubectl to scale a named deployment
361 ${rc} Run and Return Rc
362 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
363 Should Be Equal as Integers ${rc} 0
364
Andrea Campanella3dcce272021-01-15 16:04:47 +0100365Get K8s Deployment by Pod Label
366 [Arguments] ${namespace} ${key} ${value}
367 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
368 ${rc} ${name} Run And Return Rc And Output
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000369 ... 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 +0100370 Should Be Equal as Integers ${rc} 0
371 [Return] ${name}
372
373Scale K8s Deployment by Pod Label
374 [Arguments] ${namespace} ${key} ${value} ${count}
375 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
376 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
377 Scale K8s Deployment ${namespace} ${name} ${count}
378
David Bainbridgef81cd642019-11-20 00:14:47 +0000379Pod Exists
380 [Arguments] ${namespace} ${name}
381 [Documentation] Succeeds it the named POD exists
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700382 ${rc} ${count} Run and Return Rc
383 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700384 Should Be True ${count}>0 Pod ${name} not found
David Bainbridgef81cd642019-11-20 00:14:47 +0000385
386Pod Does Not Exist
387 [Arguments] ${namespace} ${name}
388 [Documentation] Succeeds if the named POD does not exist
389 ${rc} ${count} Run and Return Rc And Output
390 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
391 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700392 Should Be True ${count}==0 Pod ${name} exists but should not
David Bainbridgef81cd642019-11-20 00:14:47 +0000393
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000394Wait For Pods Not Exist
395 [Arguments] ${namespace} ${list_names}
396 [Documentation] Checks the passed PODs are no longer existing
TorstenThiemefe7099e2021-01-29 08:41:04 +0000397 FOR ${pod_name} IN @{list_names}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000398 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000399 ... Pod Does Not Exist ${namespace} ${pod_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000400 END
401
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700402Pods Do Not Exist By Label
hwchiu85695932019-12-18 08:05:25 +0000403 [Arguments] ${namespace} ${key} ${value}
404 [Documentation] Succeeds if the named POD does not exist
405 ${rc} ${count} Run and Return Rc And Output
406 ... kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
407 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700408 Should Be True ${count}==0 Pod with label ${key}=${value} exists but should not
hwchiu85695932019-12-18 08:05:25 +0000409
David Bainbridgef81cd642019-11-20 00:14:47 +0000410Get Available Deployment Replicas
411 [Arguments] ${namespace} ${name}
412 [Documentation] Succeeds if the named POD exists and has a ready count > 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700413 ${rc} ${count} Run and Return Rc and Output
414 ... kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
David Bainbridgef81cd642019-11-20 00:14:47 +0000415 ${result}= Run Keyword If '${count}' == '' Set Variable 0
416 ... ELSE Set Variable ${count}
417 [Return] ${result}
418
Andrea Campanella3dcce272021-01-15 16:04:47 +0100419Check Expected Available Deployment Replicas By Pod Label
420 [Arguments] ${namespace} ${key} ${value} ${expected}
421 [Documentation] Succeeds if the named deployment has the expected number of available replicas
422 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
423 Check Expected Available Deployment Replicas ${namespace} ${name} ${expected}
424
David Bainbridgef81cd642019-11-20 00:14:47 +0000425Check Expected Available Deployment Replicas
426 [Arguments] ${namespace} ${name} ${expected}
427 [Documentation] Succeeds if the named deployment has the expected number of available replicas
428 ${count}= Get Available Deployment Replicas ${namespace} ${name}
429 Should Be Equal As Integers ${expected} ${count}
430
431Get Deployment Replica Count
432 [Arguments] ${namespace} ${name}
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}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700439 [Return] ${replicas}
David Bainbridgef81cd642019-11-20 00:14:47 +0000440
441Does Deployment Have Replicas
442 [Arguments] ${namespace} ${name} ${expected_count}
443 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
444 ${rc} ${value} Run and Return Rc and Output
445 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
446 Should Be Equal as Integers ${rc} 0
447 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
448 ... ELSE Set Variable ${value}
449 Should be Equal as Integers ${replicas} ${expected_count}
hwchiu85695932019-12-18 08:05:25 +0000450
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700451Pods Are Ready By Label
hwchiu85695932019-12-18 08:05:25 +0000452 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700453 [Documentation] Check that all pods with a label are ready
454 ${output}= Run
455 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[].status.containerStatuses[].ready}"
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000456 Should Not Contain ${output} false
457
458Wait For Pods Ready
459 [Arguments] ${namespace} ${list_apps}
460 [Documentation] Checks the passed PODs are ready
TorstenThiemefe7099e2021-01-29 08:41:04 +0000461 FOR ${app_name} IN @{list_apps}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000462 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000463 ... Pods Are Ready By Label ${namespace} app ${app_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000464 END
Gayathri.Selvan49398962020-01-13 07:19:12 +0000465
hwchiu58af72d2020-01-14 00:50:35 +0000466Check Expected Running Pods Number By Label
467 [Arguments] ${namespace} ${key} ${value} ${number}
468 [Documentation] Succeeds if the desired pod has expected number replicas
469 ${rc} ${count} Run and Return Rc and Output
470 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o json | jq -r ".items[].status.phase" | wc -l
471 Should Be Equal as Integers ${count} ${number}
Andrea Campanella962fe832020-09-21 10:41:47 +0200472
473Get Number of Running Pods Number By Label
474 [Arguments] ${namespace} ${key} ${value}
475 [Documentation] Returns the number of pods for a given label
476 ${rc} ${count} Run and Return Rc and Output
477 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o name | wc -l
478 [Return] ${count}
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000479
480Get Pod Restart Count
481 [Arguments] ${namespace} ${name}
482 [Documentation] Returns the restart count for the given Pod
483 ${rc} ${count}= Run and Return Rc and Output
484 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $4}'
485 [Return] ${count}
486
487Verify ONOS Pod Restart
488 [Arguments] ${restarted}=True
489 [Documentation] Verifies if any of the given ONOS instances restarted
490 ${num_onos}= Wait Until Keyword Succeeds 20s 5s Get Number of Running Pods Number By Label default
491 ... app onos-onos-classic
492 FOR ${I} IN RANGE 0 ${num_onos}
493 ${onos_pod}= Catenate SEPARATOR=- onos-onos-classic ${I}
494 ${count}= Get Pod Restart Count default ${onos_pod}
495 Run Keyword If ${restarted}
496 ... Should Not Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Not Restarted
497 ... ELSE
498 ... Should Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Restarted
499 END
500
501Deploy Pod New Image
502 [Arguments] ${namespace} ${deployment} ${container} ${image}
503 [Documentation] Deploys the Pod given image
504 ${rc} Run and Return Rc
505 ... kubectl -n ${namespace} set image deployment/${deployment} ${container}=${image}
506 Should Be Equal as Integers ${rc} 0
507
508Verify Pod Image
509 [Arguments] ${namespace} ${key} ${value} ${image}
510 [Documentation] Verifies the Pod Image
511 ${output}= Run
Hardik Windlass08451302021-03-09 12:14:36 +0000512 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
513 Should Be Equal '${output}' '${image}'
Hardik Windlassdc2610f2021-03-09 07:33:51 +0000514
515Get Pod Image And App Version And Helm Chart By Label
516 [Arguments] ${namespace} ${key} ${value}
517 [Documentation] Retrieves Pod Image and, App and Helm Chart Version details
518 ${image}= Run
519 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
520 ${cmd}= Catenate SEPARATOR=
521 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=
522 ... jsonpath="{.items[*].metadata.labels.\\app\\.kubernetes\\.io\\/version}"
523 ${app_version}= Run ${cmd}
524 ${helm_chart}= Run
525 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].metadata.labels.\\helm\\.sh\\/chart}"
526 [Return] ${image} ${app_version} ${helm_chart}