blob: 15822c2c787406aa35f81ac8887b4f080b6b1a26 [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
Andrea Speranza8b6f46f2022-08-23 14:22:15 +0200124Copy File From Pod
125 [Arguments] ${namespace} ${label} ${src} ${dest}
126 [Documentation] Uses kubectl to copy a file from a pod
127 ${rc} ${exec_pod_name}= Run and Return Rc and Output
128 ... kubectl get pods -n ${namespace} -l ${label} --no-headers | awk 'NR==1{print $1}'
129 Log ${exec_pod_name}
130 Should Not Be Empty ${exec_pod_name} Unable to parse pod name
131 ${rc} ${output}= Run and Return Rc and Output
132 ... kubectl cp -n ${namespace} ${exec_pod_name}:${src} ${dest}
133 Log ${output}
134 [return] ${output}
135
Scott Baker60e570d2020-02-02 22:10:13 -0800136Apply Kubernetes Resources
137 [Arguments] ${resource_yaml} ${namespace}
138 [Documentation] Use kubectl to create resources given a yaml file
139 ${rc} Run and Return Rc
140 ... kubectl apply -n ${namespace} -f ${resource_yaml}
141 Should Be Equal as Integers ${rc} 0
142
Scott Baker0478bab2020-04-10 17:19:57 -0700143Delete Kubernetes Resources
144 [Arguments] ${resource_yaml} ${namespace}
145 [Documentation] Use kubectl to delete resources given a yaml file
146 ${rc} Run and Return Rc
147 ... kubectl delete -n ${namespace} -f ${resource_yaml}
148 Should Be Equal as Integers ${rc} 0
149
suraj gour1ecfae92019-12-20 15:11:40 +0000150Validate Pod Status
151 [Arguments] ${pod_name} ${namespace} ${expectedStatus}
152 [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 -0700153 ${length}= Run kubectl get pod -n ${namespace} -o name | wc -l
154 ${matched}= Set Variable False
155 FOR ${index} IN RANGE ${length}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700156 ${currentPodName}= Run
157 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.containerStatuses[0].name}"
suraj gour1ecfae92019-12-20 15:11:40 +0000158 Log Required Pod : ${pod_name}
159 Log Current Pod: ${currentPodName}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700160 ${matched}= Set Variable If '${currentPodName}'=='${pod_name}' True False
161 Exit For Loop If ${matched}
suraj gour1ecfae92019-12-20 15:11:40 +0000162 END
Andy Bavierb63f6d22020-03-12 15:34:37 -0700163 Should Be True ${matched} No pod ${podname} found
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700164 ${currentStatusofPod}= Run
165 ... kubectl get pod -n ${namespace} -o=jsonpath="{.items[${index}].status.phase}"
suraj gour1ecfae92019-12-20 15:11:40 +0000166 Log ${currentStatusofPod}
167 Should Contain ${currentStatusofPod} ${expectedStatus}
168
Matteo Scandoloa80b4732020-09-04 13:51:10 -0700169Get Pod Name By Label
170 [Arguments] ${namespace} ${label_key} ${label_value}
171 [Documentation] Return a pod name from a given label
172 ${rc} ${pod_name}= Run and Return Rc and Output
173 ... kubectl get pods -n ${namespace} -l ${label_key}=${label_value} --no-headers | awk '{print $1}'
174 Should Not Be Empty ${pod_name} Pod not found
175 [return] ${pod_name}
176
Hung-Wei Chiu2bee4d42020-04-24 11:31:50 -0700177Validate Pods Status By Label
178 [Arguments] ${namespace} ${label_key} ${label_value} ${expectedStatus}
179 [Documentation] To run the kubectl command and check the status of all pods filter
180 ... by label matche the expected status
181 ${command}= Catenate
182 ... kubectl -n ${namespace} get pods -l ${label_key}=${label_value}
183 ... -o=jsonpath="{.items[?(.status.phase=='${expectedStatus}')].status.phase}"
184 ${pods_status}= Run ${command}
185 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
186
Andrea Campanella4c404632020-08-26 14:41:36 +0200187Validate Pods Status By Name
188 [Arguments] ${namespace} ${name} ${expectedStatus}
189 [Documentation] To run the kubectl command and check the status of all pods filter
190 ... by label matche the expected status
191 ${command}= Catenate
192 ... kubectl -n ${namespace} get pods ${name}
193 ... -o=jsonpath="{.status.phase}"
194 ${pods_status}= Run ${command}
195 Should Not Be Equal ${pods_status} ${EMPTY} Can't filter out Pods with exptected status ${expectedStatus}
196
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000197Verify All Voltha Pods For Any Error Logs
Matteo Scandolo6b524122021-10-22 14:34:29 -0700198 [Arguments] ${datetime} ${namespace}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000199 [Documentation] This keyword checks for the error occurence in the voltha pods
200 &{errorPodDict} Create Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700201 &{containerDict} Get Container Dictionary voltha
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000202 FOR ${podName} IN @{PODLIST1}
203 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700204 ${rc} ${logOutput} Run And Return Rc And Output
Matteo Scandolo6b524122021-10-22 14:34:29 -0700205 ... kubectl logs --timestamps -n ${namespace} --since-time=${datetime} ${containerName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700206 Run Keyword And Ignore Error
207 ... Run Keyword If '${logOutput}'=='${EMPTY}'
208 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000209 ... AND Continue For Loop
210 ${errorDict} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
211 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
212 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700213 Run Keyword And Ignore Error
214 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
215 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000216 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700217 Run Keyword And Ignore Error
218 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
219 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000220 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
221 END
222 FOR ${podName} IN @{PODLIST2}
223 ${containerName} Get From Dictionary ${containerDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700224 ${rc} ${logOutput} Run And Return Rc And Output
Matteo Scandolo6b524122021-10-22 14:34:29 -0700225 ... kubectl logs --timestamps -n ${namespace} --since-time=${datetime} ${containerName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700226 Run Keyword And Ignore Error
227 ... Run Keyword If '${logOutput}'=='${EMPTY}'
228 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000229 ... AND Continue For Loop
230 ${errorDict} Check For Error Logs in Pod Type2 Given the Log Output ${logOutput}
231 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
232 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700233 Run Keyword And Ignore Error
234 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
235 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000236 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700237 Run Keyword And Ignore Error
238 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
239 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000240 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
241 END
242 Print to Console Error Statement logged in the following pods : ${errorPodDict}
243 [Return] ${errorPodDict}
244
245Check For Error Logs in Pod Type1 Given the Log Output
246 [Arguments] ${logOutput} ${logLevel}=error ${errorMessage}=${EMPTY}
247 [Documentation] Checks for error message in the particular list of pods
248 Log ${logOutput}
249 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.* partial_match=true
250 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700251 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
252 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
253 ${linesContainingError} = Get Lines Matching Regexp
254 ... ${logOutput} .*\s\${logLevel}.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000255 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700256 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
257 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000258 Log {linesContainingError}
259 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
260 [Return] ${errorDict}
261
262Check For Error Logs in Pod Type2 Given the Log Output
263 [Arguments] ${logOutput} ${logLevel}=warn ${errorMessage}=${EMPTY}
264 [Documentation] Checks for error message in the particular set of pods
265 Log ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700266 ${linesContainingLog} = Get Lines Matching Regexp
267 ... ${logOutput} .*?\s.*level.*${logLevel}.* partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000268 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700269 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
270 ... Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
271 ${linesContainingError} = Get Lines Matching Regexp
272 ... ${logOutput} .*?\s.*level.*${logLevel}.*msg.*${errorMessage} partial_match=true
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000273 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700274 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS'
275 ... UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000276 Log {linesContainingError}
277 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
278 [Return] ${errorDict}
279
280Get Container Dictionary
Matteo Scandolo142e6272020-04-29 17:36:59 -0700281 [Arguments] ${namespace}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000282 [Documentation] Creates a mapping for pod name and container name and returns the same
283 &{containerDict} Create Dictionary
284 ${containerName} Set Variable ${EMPTY}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700285 ${podName} Run kubectl get deployment -n ${namespace} | awk 'NR>1 {print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000286 @{podNameList}= Split To Lines ${podName}
287 Append To List ${podNameList} voltha-etcd-cluster voltha-kafka voltha-ro-core voltha-zookeeper
288 Log ${podNameList}
289 #Creatiing dictionary to correspond pod name and container name
290 FOR ${pod} IN @{podNameList}
Matteo Scandolo142e6272020-04-29 17:36:59 -0700291 ${containerName} Run kubectl get pod -n ${namespace} | grep ${pod} | awk '{print $1}'
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000292 &{containerDict} Set To Dictionary ${containerDict} ${pod} ${containerName}
293 END
294 Log ${containerDict}
295 [Return] ${containerDict}
296
297Validate Error For Given Pods
Matteo Scandolo6b524122021-10-22 14:34:29 -0700298 [Arguments] ${datetime} ${podDict} ${namespace}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700299 [Documentation]
300 ... This keyword is used to get the list of pods if there is any unexpected error
301 ... in a particular pod(s) given the time-${datetime} from which the log needs to
302 ... be analysed and the dictionary of pods and the error in the dictionary format
303 ... ${podDict] .
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000304 ...
Matteo Scandolo6b524122021-10-22 14:34:29 -0700305 ... Usage: ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict} ${namespace}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700306 ...
307 ... Arguments:
308 ...
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000309 ... ${datetime} = time from which the log needs to be taken
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700310 ... ${podDict} = Key-value pair of the pod name and the error msg
Matteo Scandolo6b524122021-10-22 14:34:29 -0700311 ... ${namespace} = the namespace into which look for pods
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000312 ...
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700313 ... Example: ${podDict} = Set Dictionary ${podDict} radius sample error message.
314 ...
315 ... In case the radius pod log has any other error than the expected
316 ... error, then the podname will be returned
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000317 ${podList} = Get Dictionary Keys ${podDict}
318 FOR ${podName} IN @{podList}
319 ${containerName} Get From Dictionary ${containerDict} ${podName}
320 ${expectedError} Get From Dictionary ${podDict} ${podName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700321 ${rc} ${logOutput} Run And Return Rc And Output
Matteo Scandolo6b524122021-10-22 14:34:29 -0700322 ... kubectl logs --timestamps -n ${namespace} --since-time=${datetime} ${containerName}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700323 Run Keyword And Ignore Error
324 ... Run Keyword If '${logOutput}'=='${EMPTY}'
325 ... Run Keywords Log No Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000326 ... AND Continue For Loop
327 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700328 Run Keyword And Ignore Error
329 ... Run Keyword If '${returnStatusFlag}'=='Nologfound'
330 ... Run Keywords Log No Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000331 ... AND Continue For Loop
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700332 Run Keyword And Ignore Error
333 ... Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound'
334 ... Run Keywords Log Unexpected Error Log found in pod ${podName}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +0000335 ... AND Append To List ${errorPodList} ${podName}
336 END
337 [Return] ${errorPodList}
338
David Bainbridgef81cd642019-11-20 00:14:47 +0000339Delete K8s Pod
340 [Arguments] ${namespace} ${name}
341 [Documentation] Uses kubectl to delete a named POD
342 ${rc} Run and Return Rc
343 ... kubectl delete -n ${namespace} pod/${name}
344 Should Be Equal as Integers ${rc} 0
345
hwchiu85695932019-12-18 08:05:25 +0000346Delete K8s Pods By Label
347 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700348 [Documentation] Uses kubectl to delete a PODs, filtering by label
hwchiu85695932019-12-18 08:05:25 +0000349 ${rc}= Run and Return Rc
350 ... kubectl -n ${namespace} delete pods -l${key}=${value}
351 Should Be Equal as Integers ${rc} 0
352
Andrea Campanella4c404632020-08-26 14:41:36 +0200353Delete K8s Pods By Name
354 [Arguments] ${namespace} ${value}
355 [Documentation] Uses kubectl to delete a PODs, filtering by label
356 ${rc}= Run and Return Rc
357 ... kubectl -n ${namespace} delete pods ${value}
358 Should Be Equal as Integers ${rc} 0
359
David Bainbridgef81cd642019-11-20 00:14:47 +0000360Scale K8s Deployment
361 [Arguments] ${namespace} ${name} ${count}
362 [Documentation] Uses kubectl to scale a named deployment
363 ${rc} Run and Return Rc
364 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
365 Should Be Equal as Integers ${rc} 0
366
Andrea Campanella3dcce272021-01-15 16:04:47 +0100367Get K8s Deployment by Pod Label
368 [Arguments] ${namespace} ${key} ${value}
369 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
370 ${rc} ${name} Run And Return Rc And Output
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000371 ... 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 +0100372 Should Be Equal as Integers ${rc} 0
373 [Return] ${name}
374
375Scale K8s Deployment by Pod Label
376 [Arguments] ${namespace} ${key} ${value} ${count}
377 [Documentation] Uses kubectl to scale a deployment given the app name of the pod
378 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
379 Scale K8s Deployment ${namespace} ${name} ${count}
380
David Bainbridgef81cd642019-11-20 00:14:47 +0000381Pod Exists
382 [Arguments] ${namespace} ${name}
383 [Documentation] Succeeds it the named POD exists
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700384 ${rc} ${count} Run and Return Rc
385 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
Andy Bavierb63f6d22020-03-12 15:34:37 -0700386 Should Be True ${count}>0 Pod ${name} not found
David Bainbridgef81cd642019-11-20 00:14:47 +0000387
388Pod Does Not Exist
389 [Arguments] ${namespace} ${name}
390 [Documentation] Succeeds if the named POD does not exist
391 ${rc} ${count} Run and Return Rc And Output
392 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
393 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700394 Should Be True ${count}==0 Pod ${name} exists but should not
David Bainbridgef81cd642019-11-20 00:14:47 +0000395
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000396Wait For Pods Not Exist
397 [Arguments] ${namespace} ${list_names}
398 [Documentation] Checks the passed PODs are no longer existing
TorstenThiemefe7099e2021-01-29 08:41:04 +0000399 FOR ${pod_name} IN @{list_names}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000400 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000401 ... Pod Does Not Exist ${namespace} ${pod_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000402 END
403
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700404Pods Do Not Exist By Label
hwchiu85695932019-12-18 08:05:25 +0000405 [Arguments] ${namespace} ${key} ${value}
406 [Documentation] Succeeds if the named POD does not exist
407 ${rc} ${count} Run and Return Rc And Output
408 ... kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
409 Should Be Equal As Integers ${count} 0
Andy Bavierb63f6d22020-03-12 15:34:37 -0700410 Should Be True ${count}==0 Pod with label ${key}=${value} exists but should not
hwchiu85695932019-12-18 08:05:25 +0000411
David Bainbridgef81cd642019-11-20 00:14:47 +0000412Get Available Deployment Replicas
413 [Arguments] ${namespace} ${name}
414 [Documentation] Succeeds if the named POD exists and has a ready count > 0
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700415 ${rc} ${count} Run and Return Rc and Output
416 ... kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
David Bainbridgef81cd642019-11-20 00:14:47 +0000417 ${result}= Run Keyword If '${count}' == '' Set Variable 0
418 ... ELSE Set Variable ${count}
419 [Return] ${result}
420
Andrea Campanella3dcce272021-01-15 16:04:47 +0100421Check Expected Available Deployment Replicas By Pod Label
422 [Arguments] ${namespace} ${key} ${value} ${expected}
423 [Documentation] Succeeds if the named deployment has the expected number of available replicas
424 ${name} Get K8s Deployment by Pod Label ${namespace} ${key} ${value}
425 Check Expected Available Deployment Replicas ${namespace} ${name} ${expected}
426
David Bainbridgef81cd642019-11-20 00:14:47 +0000427Check Expected Available Deployment Replicas
428 [Arguments] ${namespace} ${name} ${expected}
429 [Documentation] Succeeds if the named deployment has the expected number of available replicas
430 ${count}= Get Available Deployment Replicas ${namespace} ${name}
431 Should Be Equal As Integers ${expected} ${count}
432
433Get Deployment Replica Count
434 [Arguments] ${namespace} ${name}
435 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
436 ${rc} ${value} Run and Return Rc and Output
437 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
438 Should Be Equal as Integers ${rc} 0
439 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
440 ... ELSE Set Variable ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700441 [Return] ${replicas}
David Bainbridgef81cd642019-11-20 00:14:47 +0000442
443Does Deployment Have Replicas
444 [Arguments] ${namespace} ${name} ${expected_count}
445 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
446 ${rc} ${value} Run and Return Rc and Output
447 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
448 Should Be Equal as Integers ${rc} 0
449 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
450 ... ELSE Set Variable ${value}
451 Should be Equal as Integers ${replicas} ${expected_count}
hwchiu85695932019-12-18 08:05:25 +0000452
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700453Pods Are Ready By Label
hwchiu85695932019-12-18 08:05:25 +0000454 [Arguments] ${namespace} ${key} ${value}
Zack Williamsa8fe75a2020-01-10 14:25:27 -0700455 [Documentation] Check that all pods with a label are ready
TorstenThieme37165402021-09-03 11:39:40 +0000456 ${pod_names}= Get Pod Name By Label ${namespace} ${key} ${value}
457 Should Not Be Empty ${pod_names} Unable to parse pod name
458 @{pods}= Split String ${pod_names} separator=${\n}
459 ${lenght}= Get Length ${pods}
460 FOR ${I} IN RANGE 0 ${lenght}
461 ${output}= Run
462 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[${I}].status.containerStatuses[].ready}"
463 Should Not Contain ${output} false
464 END
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000465
466Wait For Pods Ready
467 [Arguments] ${namespace} ${list_apps}
468 [Documentation] Checks the passed PODs are ready
TorstenThiemefe7099e2021-01-29 08:41:04 +0000469 FOR ${app_name} IN @{list_apps}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000470 Run Keyword And Continue On Failure Wait Until Keyword Succeeds ${timeout} 3s
TorstenThiemefe7099e2021-01-29 08:41:04 +0000471 ... Pods Are Ready By Label ${namespace} app ${app_name}
TorstenThiemed2cd91d2020-07-28 07:13:44 +0000472 END
Gayathri.Selvan49398962020-01-13 07:19:12 +0000473
TorstenThieme96fe9ee2021-10-21 10:24:08 +0000474Get Pod Ready Timestamp by Label
475 [Arguments] ${namespace} ${key} ${value}
476 [Documentation] delivers timestamp of pod was ready
477 ${cmd}= Catenate kubectl -n ${namespace} get pods -l ${key}=${value} -o=json | jq -r
478 ... ".items[].status.containerStatuses[].state.running.startedAt"
479 ${output}= Run ${cmd}
480 [Return] ${output}
481
hwchiu58af72d2020-01-14 00:50:35 +0000482Check Expected Running Pods Number By Label
483 [Arguments] ${namespace} ${key} ${value} ${number}
484 [Documentation] Succeeds if the desired pod has expected number replicas
485 ${rc} ${count} Run and Return Rc and Output
486 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o json | jq -r ".items[].status.phase" | wc -l
487 Should Be Equal as Integers ${count} ${number}
Andrea Campanella962fe832020-09-21 10:41:47 +0200488
489Get Number of Running Pods Number By Label
490 [Arguments] ${namespace} ${key} ${value}
491 [Documentation] Returns the number of pods for a given label
492 ${rc} ${count} Run and Return Rc and Output
493 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o name | wc -l
494 [Return] ${count}
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000495
496Get Pod Restart Count
497 [Arguments] ${namespace} ${name}
498 [Documentation] Returns the restart count for the given Pod
499 ${rc} ${count}= Run and Return Rc and Output
500 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $4}'
501 [Return] ${count}
502
TorstenThieme96fe9ee2021-10-21 10:24:08 +0000503Get Pod Age
504 [Arguments] ${namespace} ${name}
505 [Documentation] Returns the age for the given Pod
506 ${rc} ${age}= Run and Return Rc and Output
507 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $5}'
508 [Return] ${age}
509
Hardik Windlassdd1a9a12021-02-23 15:34:50 +0000510Verify ONOS Pod Restart
511 [Arguments] ${restarted}=True
512 [Documentation] Verifies if any of the given ONOS instances restarted
513 ${num_onos}= Wait Until Keyword Succeeds 20s 5s Get Number of Running Pods Number By Label default
514 ... app onos-onos-classic
515 FOR ${I} IN RANGE 0 ${num_onos}
516 ${onos_pod}= Catenate SEPARATOR=- onos-onos-classic ${I}
517 ${count}= Get Pod Restart Count default ${onos_pod}
518 Run Keyword If ${restarted}
519 ... Should Not Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Not Restarted
520 ... ELSE
521 ... Should Be Equal As Integers ${count} 0 ONOS Pod ${onos_pod} Restarted
522 END
523
524Deploy Pod New Image
525 [Arguments] ${namespace} ${deployment} ${container} ${image}
526 [Documentation] Deploys the Pod given image
527 ${rc} Run and Return Rc
528 ... kubectl -n ${namespace} set image deployment/${deployment} ${container}=${image}
529 Should Be Equal as Integers ${rc} 0
530
531Verify Pod Image
532 [Arguments] ${namespace} ${key} ${value} ${image}
533 [Documentation] Verifies the Pod Image
534 ${output}= Run
Hardik Windlass08451302021-03-09 12:14:36 +0000535 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
536 Should Be Equal '${output}' '${image}'
Hardik Windlassdc2610f2021-03-09 07:33:51 +0000537
538Get Pod Image And App Version And Helm Chart By Label
539 [Arguments] ${namespace} ${key} ${value}
540 [Documentation] Retrieves Pod Image and, App and Helm Chart Version details
541 ${image}= Run
542 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].spec.containers[*].image}"
543 ${cmd}= Catenate SEPARATOR=
544 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=
545 ... jsonpath="{.items[*].metadata.labels.\\app\\.kubernetes\\.io\\/version}"
546 ${app_version}= Run ${cmd}
547 ${helm_chart}= Run
548 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o=jsonpath="{.items[*].metadata.labels.\\helm\\.sh\\/chart}"
549 [Return] ${image} ${app_version} ${helm_chart}