blob: fc5e0e7f2beb449d211ed86ad2f21b912a07e20b [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}
56 [Documentation] Uses kubectl to force delete pod
57 ${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
61 ${rc} ${output}= Run and Return Rc and Output
62 ... kubectl delete pod ${restart_pod_name} -n ${namespace} --grace-period=0 --force
63 Log ${output}
64
Scott Baker60e570d2020-02-02 22:10:13 -080065Exec Pod
66 [Arguments] ${namespace} ${name} ${command}
67 [Documentation] Uses kubectl to execute a command in the pod and return the output
68 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +020069 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -080070 Log ${exec_pod_name}
71 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
72 ${rc} ${output}= Run and Return Rc and Output
73 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
74 Log ${output}
75 [return] ${output}
76
Holger Hildebrandt23147742020-11-16 10:13:21 +000077Exec Pod In Kube
TorstenThiemefe7099e2021-01-29 08:41:04 +000078 [Arguments] ${namespace} ${name} ${command} ${grep}=${EMPTY}
Holger Hildebrandt23147742020-11-16 10:13:21 +000079 [Documentation] Uses kubectl to execute a command in the pod and return the output
TorstenThiemefe7099e2021-01-29 08:41:04 +000080 ${rc} ${exec_pod_name}= Run Keyword If '${grep}'=='${EMPTY}'
Hardik Windlassa9b38262021-10-27 08:14:22 +000081 ... Run and Return Rc and Output
82 ... kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name | awk 'NR==1{print $1}'
TorstenThiemefe7099e2021-01-29 08:41:04 +000083 ... ELSE Run and Return Rc and Output
84 ... kubectl -n ${namespace} get pods -l app.kubernetes.io/name=${name} -o name \| grep ${grep}
Hardik Windlassec86c232021-02-02 13:56:12 +000085 Log ${exec_pod_name}
86 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
87 ${rc} ${output}= Run and Return Rc and Output
88 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
89 Log ${output}
90 [return] ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -070091
92Exec Pod And Return Output And RC
93 [Arguments] ${namespace} ${name} ${command}
94 [Documentation] Uses kubectl to execute a command in the pod and return the output
95 ${rc} ${exec_pod_name}= Run and Return Rc and Output
96 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
Holger Hildebrandt23147742020-11-16 10:13:21 +000097 Log ${exec_pod_name}
98 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
99 ${rc} ${output}= Run and Return Rc and Output
100 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- ${command}
101 Log ${output}
Matteo Scandoloa80b4732020-09-04 13:51:10 -0700102 [return] ${output} ${rc}
Holger Hildebrandt23147742020-11-16 10:13:21 +0000103
Scott Baker60e570d2020-02-02 22:10:13 -0800104Exec Pod Separate Stderr
105 [Arguments] ${namespace} ${name} ${command}
106 [Documentation] Uses kubectl to execute a command in the pod and return the stderr and stdout
107 ${rc} ${exec_pod_name}= Run and Return Rc and Output
Andrea Campanella1b25ac52020-09-09 14:34:31 +0200108 ... kubectl -n ${namespace} get pods -l app=${name} -o name
Scott Baker60e570d2020-02-02 22:10:13 -0800109 Log ${exec_pod_name}
110 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
111 @{args}= Split String ${command}
112 ${result}= Run Process
113 ... kubectl exec -i ${exec_pod_name} -n ${namespace} -- @{args}
114 ${stdout}= Set Variable ${result.stdout}
115 ${stderr}= Set Variable ${result.stderr}
116 Log ${stdout}
117 Log ${stderr}
118 [return] ${stdout} ${stderr}
119
Scott Baker0478bab2020-04-10 17:19:57 -0700120Copy File To Pod
121 [Arguments] ${namespace} ${name} ${src} ${dest}
122 [Documentation] Uses kubectl to copy a file to a pod
123 ${rc} ${exec_pod_name}= Run and Return Rc and Output
124 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
125 Log ${exec_pod_name}
126 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
127 ${rc} ${output}= Run and Return Rc and Output
128 ... kubectl cp -n ${namespace} ${src} ${exec_pod_name}:${dest}
129 Log ${output}
130 [return] ${output}
131
Scott Baker60e570d2020-02-02 22:10:13 -0800132Apply Kubernetes Resources
133 [Arguments] ${resource_yaml} ${namespace}
134 [Documentation] Use kubectl to create resources given a yaml file
135 ${rc} Run and Return Rc
136 ... kubectl apply -n ${namespace} -f ${resource_yaml}
137 Should Be Equal as Integers ${rc} 0
138
Scott Baker0478bab2020-04-10 17:19:57 -0700139Delete Kubernetes Resources
140 [Arguments] ${resource_yaml} ${namespace}
141 [Documentation] Use kubectl to delete resources given a yaml file
142 ${rc} Run and Return Rc
143 ... kubectl delete -n ${namespace} -f ${resource_yaml}
144 Should Be Equal as Integers ${rc} 0
145
suraj gour1ecfae92019-12-20 15:11:40 +0000146Validate Pod Status
147 [Arguments] ${pod_name} ${namespace} ${expectedStatus}
148 [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 -0700149 ${length}= Run kubectl get pod -n ${namespace} -o name | wc -l
150 ${matched}= Set Variable False
151 FOR ${index} IN RANGE ${length}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700152 ${currentPodName}= Run
153 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.containerStatuses[0].name}"
suraj gour1ecfae92019-12-20 15:11:40 +0000154 Log Required Pod : ${pod_name}
155 Log Current Pod: ${currentPodName}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700156 ${matched}= Set Variable If '${currentPodName}'=='${pod_name}' True False
157 Exit For Loop If ${matched}
suraj gour1ecfae92019-12-20 15:11:40 +0000158 END
Andy Bavierb63f6d22020-03-12 15:34:37 -0700159 Should Be True ${matched} No pod ${podname} found
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700160 ${currentStatusofPod}= Run
161 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.phase}"
suraj gour1ecfae92019-12-20 15:11:40 +0000162 Log ${currentStatusofPod}
163 Should Contain ${currentStatusofPod} ${expectedStatus}
164
Matteo Scandoloa80b4732020-09-04 13:51:10 -0700165Get Pod Name By Label
166 [Arguments] ${namespace} ${label_key} ${label_value}
167 [Documentation] Return a pod name from a given label
168 ${rc} ${pod_name}= Run and Return Rc and Output
169 ... kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
170 Should Not Be Empty ${pod_name} Pod not found
171 [return] ${pod_name}
172
Hung-Wei Chiu2bee4d42020-04-24 11:31:50 -0700173Validate Pods Status By Label
174 [Arguments] ${namespace} ${label_key} ${label_value} ${expectedStatus}
175 [Documentation] To run the kubectl command and check the status of all pods filter
176 ... by label matche the expected status
177 ${command}= Catenate
178 ... kubectl -n ${namespace} get pods -l ${label_key}=${label_value}
179 ... -o=jsonpath="{.items[?(.status.phase=='${expectedStatus}')].status.phase}"
180 ${pods_status}= Run ${command}
181 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
182
Andrea Campanella4c404632020-08-26 14:41:36 +0200183Validate Pods Status By Name
184 [Arguments] ${namespace} ${name} ${expectedStatus}
185 [Documentation] To run the kubectl command and check the status of all pods filter
186 ... by label matche the expected status
187 ${command}= Catenate
188 ... kubectl -n ${namespace} get pods ${name}
189 ... -o=jsonpath="{.status.phase}"
190 ${pods_status}= Run ${command}
191 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
192
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000193Verify All Voltha Pods For Any Error Logs
194 [Arguments] ${datetime}
195 [Documentation] This keyword checks for the error occurence in the voltha pods
196 &{errorPodDict} Create Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700197 &{containerDict} Get Container Dictionary voltha
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000198 FOR ${podName} IN @{PODLIST1}
199 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700200 ${rc} ${logOutput} Run And Return Rc And Output
201 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
202 Run Keyword And Ignore Error
203 ... Run Keyword If '${logOutput}'=='${EMPTY}'
204 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000205 ... AND Continue For Loop
206 ${errorDict} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
207 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
208 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700209 Run Keyword And Ignore Error
210 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
211 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000212 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700213 Run Keyword And Ignore Error
214 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
215 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000216 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
217 END
218 FOR ${podName} IN @{PODLIST2}
219 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700220 ${rc} ${logOutput} Run And Return Rc And Output
221 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
222 Run Keyword And Ignore Error
223 ... Run Keyword If '${logOutput}'=='${EMPTY}'
224 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000225 ... AND Continue For Loop
226 ${errorDict} Check For Error Logs in Pod Type2 Given the Log Output ${logOutput}
227 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
228 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700229 Run Keyword And Ignore Error
230 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
231 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000232 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700233 Run Keyword And Ignore Error
234 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
235 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000236 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
237 END
238 Print to Console Error Statement logged in the following pods : ${errorPodDict}
239 [Return] ${errorPodDict}
240
241Check For Error Logs in Pod Type1 Given the Log Output
242 [Arguments] ${logOutput} ${logLevel}=error ${errorMessage}=${EMPTY}
243 [Documentation] Checks for error message in the particular list of pods
244 Log ${logOutput}
245 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.* partial_match=true
246 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700247 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
248 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
249 ${linesContainingError} = Get Lines Matching Regexp
250 ... ${logOutput} .*\s\${logLevel}.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000251 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700252 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
253 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000254 Log {linesContainingError}
255 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
256 [Return] ${errorDict}
257
258Check For Error Logs in Pod Type2 Given the Log Output
259 [Arguments] ${logOutput} ${logLevel}=warn ${errorMessage}=${EMPTY}
260 [Documentation] Checks for error message in the particular set of pods
261 Log ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700262 ${linesContainingLog} = Get Lines Matching Regexp
263 ... ${logOutput} .*?\s.*level.*${logLevel}.* partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000264 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700265 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
266 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
267 ${linesContainingError} = Get Lines Matching Regexp
268 ... ${logOutput} .*?\s.*level.*${logLevel}.*msg.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000269 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700270 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
271 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000272 Log {linesContainingError}
273 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
274 [Return] ${errorDict}
275
276Get Container Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700277 [Arguments] ${namespace}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000278 [Documentation] Creates a mapping for pod name and container name and returns the same
279 &{containerDict} Create Dictionary
280 ${containerName} Set Variable ${EMPTY}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700281 ${podName} Run kubectl get deployment -n ${namespace} | awk 'NR>1 {print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000282 @{podNameList}= Split To Lines ${podName}
283 Append To List ${podNameList} voltha-etcd-cluster voltha-kafka voltha-ro-core voltha-zookeeper
284 Log ${podNameList}
285 #Creatiing dictionary to correspond pod name and container name
286 FOR ${pod} IN @{podNameList}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700287 ${containerName} Run kubectl get pod -n ${namespace} | grep ${pod} | awk '{print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000288 &{containerDict} Set To Dictionary ${containerDict} ${pod} ${containerName}
289 END
290 Log ${containerDict}
291 [Return] ${containerDict}
292
293Validate Error For Given Pods
294 [Arguments] ${datetime} ${podDict}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700295 [Documentation]
296 ... This keyword is used to get the list of pods if there is any unexpected error
297 ... in a particular pod(s) given the time-${datetime} from which the log needs to
298 ... be analysed and the dictionary of pods and the error in the dictionary format
299 ... ${podDict] .
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000300 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700301 ... Usage: ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict}
302 ...
303 ... Arguments:
304 ...
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000305 ... ${datetime} = time from which the log needs to be taken
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700306 ... ${podDict} = Key-value pair of the pod name and the error msg
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000307 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700308 ... Example: ${podDict} = Set Dictionary ${podDict} radius sample error message.
309 ...
310 ... In case the radius pod log has any other error than the expected
311 ... error, then the podname will be returned
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000312 ${podList} = Get Dictionary Keys ${podDict}
313 FOR ${podName} IN @{podList}
314 ${containerName} Get From Dictionary ${containerDict} ${podName}
315 ${expectedError} Get From Dictionary ${podDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700316 ${rc} ${logOutput} Run And Return Rc And Output
317 ... kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
318 Run Keyword And Ignore Error
319 ... Run Keyword If '${logOutput}'=='${EMPTY}'
320 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000321 ... AND Continue For Loop
322 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700323 Run Keyword And Ignore Error
324 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
325 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000326 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700327 Run Keyword And Ignore Error
328 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
329 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000330 ... AND Append To List ${errorPodList} ${podName}
331 END
332 [Return] ${errorPodList}
333
David Bainbridgef81cd642019-11-20 00:14:47 +0000334Delete K8s Pod
335 [Arguments] ${namespace} ${name}
336 [Documentation] Uses kubectl to delete a named POD
337 ${rc} Run and Return Rc
338 ... kubectl delete -n ${namespace} pod/${name}
339 Should Be Equal as Integers ${rc} 0
340
hwchiu85695932019-12-18 08:05:25 +0000341Delete K8s Pods By Label
342 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700343 [Documentation] Uses kubectl to delete a PODs, filtering by label
hwchiu85695932019-12-18 08:05:25 +0000344 ${rc}= Run and Return Rc
345 ... kubectl -n ${namespace} delete pods -l${key}=${value}
346 Should Be Equal as Integers ${rc} 0
347
Andrea Campanella4c404632020-08-26 14:41:36 +0200348Delete K8s Pods By Name
349 [Arguments] ${namespace} ${value}
350 [Documentation] Uses kubectl to delete a PODs, filtering by label
351 ${rc}= Run and Return Rc
352 ... kubectl -n ${namespace} delete pods ${value}
353 Should Be Equal as Integers ${rc} 0
354
David Bainbridgef81cd642019-11-20 00:14:47 +0000355Scale K8s Deployment
356 [Arguments] ${namespace} ${name} ${count}
357 [Documentation] Uses kubectl to scale a named deployment
358 ${rc} Run and Return Rc
359 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
360 Should Be Equal as Integers ${rc} 0
361
Andrea Campanella3dcce272021-01-15 16:04:47 +0100362Get K8s Deployment by Pod Label
363 [Arguments] ${namespace} ${key} ${value}
364 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
365 ${rc} ${name} Run And Return Rc And Output
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000366 ... 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 +0100367 Should Be Equal as Integers ${rc} 0
368 [Return] ${name}
369
370Scale K8s Deployment by Pod Label
371 [Arguments] ${namespace} ${key} ${value} ${count}
372 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
373 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
374 Scale K8s Deployment ${namespace} ${name} ${count}
375
David Bainbridgef81cd642019-11-20 00:14:47 +0000376Pod Exists
377 [Arguments] ${namespace} ${name}
378 [Documentation] Succeeds it the named POD exists
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700379 ${rc} ${count} Run and Return Rc
380 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700381 Should Be True ${count}>0 Pod ${name} not found
David Bainbridgef81cd642019-11-20 00:14:47 +0000382
383Pod Does Not Exist
384 [Arguments] ${namespace} ${name}
385 [Documentation] Succeeds if the named POD does not exist
386 ${rc} ${count} Run and Return Rc And Output
387 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
388 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700389 Should Be True ${count}==0 Pod ${name} exists but should not
David Bainbridgef81cd642019-11-20 00:14:47 +0000390
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000391Wait For Pods Not Exist
392 [Arguments] ${namespace} ${list_names}
393 [Documentation] Checks the passed PODs are no longer existing
TorstenThiemefe7099e2021-01-29 08:41:04 +0000394 FOR ${pod_name} IN @{list_names}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000395 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000396 ... Pod Does Not Exist ${namespace} ${pod_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000397 END
398
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700399Pods Do Not Exist By Label
hwchiu85695932019-12-18 08:05:25 +0000400 [Arguments] ${namespace} ${key} ${value}
401 [Documentation] Succeeds if the named POD does not exist
402 ${rc} ${count} Run and Return Rc And Output
403 ... kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
404 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700405 Should Be True ${count}==0 Pod with label ${key}=${value} exists but should not
hwchiu85695932019-12-18 08:05:25 +0000406
David Bainbridgef81cd642019-11-20 00:14:47 +0000407Get Available Deployment Replicas
408 [Arguments] ${namespace} ${name}
409 [Documentation] Succeeds if the named POD exists and has a ready count > 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700410 ${rc} ${count} Run and Return Rc and Output
411 ... kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
David Bainbridgef81cd642019-11-20 00:14:47 +0000412 ${result}= Run Keyword If '${count}' == '' Set Variable 0
413 ... ELSE Set Variable ${count}
414 [Return] ${result}
415
Andrea Campanella3dcce272021-01-15 16:04:47 +0100416Check Expected Available Deployment Replicas By Pod Label
417 [Arguments] ${namespace} ${key} ${value} ${expected}
418 [Documentation] Succeeds if the named deployment has the expected number of available replicas
419 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
420 Check Expected Available Deployment Replicas ${namespace} ${name} ${expected}
421
David Bainbridgef81cd642019-11-20 00:14:47 +0000422Check Expected Available Deployment Replicas
423 [Arguments] ${namespace} ${name} ${expected}
424 [Documentation] Succeeds if the named deployment has the expected number of available replicas
425 ${count}= Get Available Deployment Replicas ${namespace} ${name}
426 Should Be Equal As Integers ${expected} ${count}
427
428Get Deployment Replica Count
429 [Arguments] ${namespace} ${name}
430 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
431 ${rc} ${value} Run and Return Rc and Output
432 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
433 Should Be Equal as Integers ${rc} 0
434 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
435 ... ELSE Set Variable ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700436 [Return] ${replicas}
David Bainbridgef81cd642019-11-20 00:14:47 +0000437
438Does Deployment Have Replicas
439 [Arguments] ${namespace} ${name} ${expected_count}
440 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
441 ${rc} ${value} Run and Return Rc and Output
442 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
443 Should Be Equal as Integers ${rc} 0
444 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
445 ... ELSE Set Variable ${value}
446 Should be Equal as Integers ${replicas} ${expected_count}
hwchiu85695932019-12-18 08:05:25 +0000447
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700448Pods Are Ready By Label
hwchiu85695932019-12-18 08:05:25 +0000449 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700450 [Documentation] Check that all pods with a label are ready
451 ${output}= Run
452 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[].status.containerStatuses[].ready}"
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000453 Should Not Contain ${output} false
454
455Wait For Pods Ready
456 [Arguments] ${namespace} ${list_apps}
457 [Documentation] Checks the passed PODs are ready
TorstenThiemefe7099e2021-01-29 08:41:04 +0000458 FOR ${app_name} IN @{list_apps}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000459 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000460 ... Pods Are Ready By Label ${namespace} app ${app_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000461 END
Gayathri.Selvan49398962020-01-13 07:19:12 +0000462
hwchiu58af72d2020-01-14 00:50:35 +0000463Check Expected Running Pods Number By Label
464 [Arguments] ${namespace} ${key} ${value} ${number}
465 [Documentation] Succeeds if the desired pod has expected number replicas
466 ${rc} ${count} Run and Return Rc and Output
467 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o json | jq -r ".items[].status.phase" | wc -l
468 Should Be Equal as Integers ${count} ${number}
Andrea Campanella962fe832020-09-21 10:41:47 +0200469
470Get Number of Running Pods Number By Label
471 [Arguments] ${namespace} ${key} ${value}
472 [Documentation] Returns the number of pods for a given label
473 ${rc} ${count} Run and Return Rc and Output
474 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o name | wc -l
475 [Return] ${count}
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000476
477Get Pod Restart Count
478 [Arguments] ${namespace} ${name}
479 [Documentation] Returns the restart count for the given Pod
480 ${rc} ${count}= Run and Return Rc and Output
481 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $4}'
482 [Return] ${count}
483
484Verify ONOS Pod Restart
485 [Arguments] ${restarted}=True
486 [Documentation] Verifies if any of the given ONOS instances restarted
487 ${num_onos}= Wait Until Keyword Succeeds 20s 5s Get Number of Running Pods Number By Label default
488 ... app onos-onos-classic
489 FOR ${I} IN RANGE 0 ${num_onos}
490 ${onos_pod}= Catenate SEPARATOR=- onos-onos-classic ${I}
491 ${count}= Get Pod Restart Count default ${onos_pod}
492 Run Keyword If ${restarted}
493 ... Should Not Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Not Restarted
494 ... ELSE
495 ... Should Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Restarted
496 END
497
498Deploy Pod New Image
499 [Arguments] ${namespace} ${deployment} ${container} ${image}
500 [Documentation] Deploys the Pod given image
501 ${rc} Run and Return Rc
502 ... kubectl -n ${namespace} set image deployment/${deployment} ${container}=${image}
503 Should Be Equal as Integers ${rc} 0
504
505Verify Pod Image
506 [Arguments] ${namespace} ${key} ${value} ${image}
507 [Documentation] Verifies the Pod Image
508 ${output}= Run
Hardik Windlass08451302021-03-09 12:14:36 +0000509 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
510 Should Be Equal '${output}' '${image}'
Hardik Windlassdc2610f2021-03-09 07:33:51 +0000511
512Get Pod Image And App Version And Helm Chart By Label
513 [Arguments] ${namespace} ${key} ${value}
514 [Documentation] Retrieves Pod Image and, App and Helm Chart Version details
515 ${image}= Run
516 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
517 ${cmd}= Catenate SEPARATOR=
518 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=
519 ... jsonpath="{.items[*].metadata.labels.\\app\\.kubernetes\\.io\\/version}"
520 ${app_version}= Run ${cmd}
521 ${helm_chart}= Run
522 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].metadata.labels.\\helm\\.sh\\/chart}"
523 [Return] ${image} ${app_version} ${helm_chart}