blob: 62a5b115159a0b1a6771bc07ca3c4146547aba9c [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.
14
15# voltctl common functions
16
17*** Settings ***
18Documentation Library for various utilities
19Library SSHLibrary
20Library HttpLibrary.HTTP
21Library String
22Library DateTime
23Library Process
24Library Collections
25Library RequestsLibrary
26Library OperatingSystem
27
28*** Keywords ***
29Lookup Service IP
30 [Arguments] ${namespace} ${name}
David Bainbridgef81cd642019-11-20 00:14:47 +000031 [Documentation] Uses kubectl to resolve a service name to an IP
Gilles Depatie675a2062019-10-22 12:44:42 -040032 ${rc} ${ip}= Run and Return Rc and Output
33 ... kubectl get svc -n ${namespace} ${name} -o jsonpath={.spec.clusterIP}
David Bainbridge117d23e2019-09-30 20:37:51 +000034 Should Be Equal as Integers ${rc} 0
35 [Return] ${ip}
36
37Lookup Service PORT
38 [Arguments] ${namespace} ${name}
David Bainbridgef81cd642019-11-20 00:14:47 +000039 [Documentation] Uses kubectl to resolve a service name to an PORT
Gilles Depatie675a2062019-10-22 12:44:42 -040040 ${rc} ${port}= Run and Return Rc and Output
41 ... kubectl get svc -n ${namespace} ${name} -o jsonpath={.spec.ports[0].port}
David Bainbridge117d23e2019-09-30 20:37:51 +000042 Should Be Equal as Integers ${rc} 0
43 [Return] ${port}
suraj gourd64356b2019-11-07 13:26:20 +000044
45Restart Pod
46 [Arguments] ${namespace} ${name}
47 [Documentation] Uses kubectl to force delete pod
suraj gour067451d2019-11-13 11:20:13 +000048 ${rc} ${restart_pod_name}= Run and Return Rc and Output
49 ... kubectl get pods -n ${namespace} | grep ${name} | awk 'NR==1{print $1}'
suraj gourd64356b2019-11-07 13:26:20 +000050 Log ${restart_pod_name}
suraj gour067451d2019-11-13 11:20:13 +000051 Should Not Be Empty ${restart_pod_name} Unable to parse pod name
suraj gourd64356b2019-11-07 13:26:20 +000052 ${rc} ${output}= Run and Return Rc and Output kubectl delete pod ${restart_pod_name} -n ${namespace} --grace-period=0 --force
53 Log ${output}
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +000054
suraj gour1ecfae92019-12-20 15:11:40 +000055Validate Pod Status
56 [Arguments] ${pod_name} ${namespace} ${expectedStatus}
57 [Documentation] To run the kubectl command and check the status of the given pod matches the expected status
58 ${length}= Run ${KUBECTL_CONFIG}; kubectl get pod -n ${namespace} | wc -l
59 FOR ${index} IN RANGE ${length}-1
60 ${currentPodName}= Run ${KUBECTL_CONFIG}; kubectl get pod -n ${namespace} -o jsonpath={.items[${index}].status.containerStatuses[0].name}
61 Log Required Pod : ${pod_name}
62 Log Current Pod: ${currentPodName}
63 Run Keyword and Ignore Error Run Keyword If '${currentPodName}'=='${pod_name}' Exit For Loop
64 END
65 ${currentStatusofPod}= Run ${KUBECTL_CONFIG}; kubectl get pod -n ${namespace} -o jsonpath={.items[${index}].status.phase}
66 Log ${currentStatusofPod}
67 Should Contain ${currentStatusofPod} ${expectedStatus}
68
Gayathri.Selvan61fbcdb2019-11-14 05:08:19 +000069Verify All Voltha Pods For Any Error Logs
70 [Arguments] ${datetime}
71 [Documentation] This keyword checks for the error occurence in the voltha pods
72 &{errorPodDict} Create Dictionary
73 &{containerDict} Get Container Dictionary
74 FOR ${podName} IN @{PODLIST1}
75 ${containerName} Get From Dictionary ${containerDict} ${podName}
76 ${rc} ${logOutput} Run And Return Rc And Output ${KUBECTL_CONFIG};kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
77 Run Keyword And Ignore Error Run Keyword If '${logOutput}'=='${EMPTY}' Run Keywords Log No Log found in pod ${podName}
78 ... AND Continue For Loop
79 ${errorDict} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
80 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
81 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
82 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='Nologfound' Run Keywords Log No Error Log found in pod ${podName}
83 ... AND Continue For Loop
84 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound' Run Keywords Log Unexpected Error Log found in pod ${podName}
85 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
86 END
87 FOR ${podName} IN @{PODLIST2}
88 ${containerName} Get From Dictionary ${containerDict} ${podName}
89 ${rc} ${logOutput} Run And Return Rc And Output ${KUBECTL_CONFIG};kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
90 Run Keyword And Ignore Error Run Keyword If '${logOutput}'=='${EMPTY}' Run Keywords Log No Log found in pod ${podName}
91 ... AND Continue For Loop
92 ${errorDict} Check For Error Logs in Pod Type2 Given the Log Output ${logOutput}
93 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
94 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
95 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='Nologfound' Run Keywords Log No Error Log found in pod ${podName}
96 ... AND Continue For Loop
97 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound' Run Keywords Log Unexpected Error Log found in pod ${podName}
98 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
99 END
100 Print to Console Error Statement logged in the following pods : ${errorPodDict}
101 [Return] ${errorPodDict}
102
103Check For Error Logs in Pod Type1 Given the Log Output
104 [Arguments] ${logOutput} ${logLevel}=error ${errorMessage}=${EMPTY}
105 [Documentation] Checks for error message in the particular list of pods
106 Log ${logOutput}
107 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.* partial_match=true
108 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
109 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS' Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
110 ${linesContainingError} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.*${errorMessage} partial_match=true
111 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
112 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS' UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
113 Log {linesContainingError}
114 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
115 [Return] ${errorDict}
116
117Check For Error Logs in Pod Type2 Given the Log Output
118 [Arguments] ${logOutput} ${logLevel}=warn ${errorMessage}=${EMPTY}
119 [Documentation] Checks for error message in the particular set of pods
120 Log ${logOutput}
121 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*?\s.*level.*${logLevel}.* partial_match=true
122 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
123 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS' Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
124 ${linesContainingError} = Get Lines Matching Regexp ${logOutput} .*?\s.*level.*${logLevel}.*msg.*${errorMessage} partial_match=true
125 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
126 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS' UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
127 Log {linesContainingError}
128 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
129 [Return] ${errorDict}
130
131Get Container Dictionary
132 [Documentation] Creates a mapping for pod name and container name and returns the same
133 &{containerDict} Create Dictionary
134 ${containerName} Set Variable ${EMPTY}
135 ${podName} Run ${KUBECTL_CONFIG};kubectl get deployment -n voltha | awk 'NR>1 {print $1}'
136 @{podNameList}= Split To Lines ${podName}
137 Append To List ${podNameList} voltha-etcd-cluster voltha-kafka voltha-ro-core voltha-zookeeper
138 Log ${podNameList}
139 #Creatiing dictionary to correspond pod name and container name
140 FOR ${pod} IN @{podNameList}
141 ${containerName} Run ${KUBECTL_CONFIG};kubectl get pod -n voltha | grep ${pod} | awk '{print $1}'
142 &{containerDict} Set To Dictionary ${containerDict} ${pod} ${containerName}
143 END
144 Log ${containerDict}
145 [Return] ${containerDict}
146
147Validate Error For Given Pods
148 [Arguments] ${datetime} ${podDict}
149 [Documentation] This keyword is used to get the list of pods if there is any unexpected error in a particular pod(s) given the time-${datetime} from which the log needs to be analysed and the dictionary of pods and the error in the dictionary format ${podDict] .
150 ...
151 ... Usage : ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict}
152 ... where,
153 ... ${datetime} = time from which the log needs to be taken
154 ... ${podDict} = Key-value pair of the pod name and the error msg expected like ${podDict} = Set Dictionary ${podDict} radius sample error message.
155 ...
156 ... In case the radius pod log has any other error than the expected error, then the podname will be returned
157 ${podList} = Get Dictionary Keys ${podDict}
158 FOR ${podName} IN @{podList}
159 ${containerName} Get From Dictionary ${containerDict} ${podName}
160 ${expectedError} Get From Dictionary ${podDict} ${podName}
161 ${rc} ${logOutput} Run And Return Rc And Output ${KUBECTL_CONFIG};kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
162 Run Keyword And Ignore Error Run Keyword If '${logOutput}'=='${EMPTY}' Run Keywords Log No Log found in pod ${podName}
163 ... AND Continue For Loop
164 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
165 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='Nologfound' Run Keywords Log No Error Log found in pod ${podName}
166 ... AND Continue For Loop
167 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound' Run Keywords Log Unexpected Error Log found in pod ${podName}
168 ... AND Append To List ${errorPodList} ${podName}
169 END
170 [Return] ${errorPodList}
171
David Bainbridgef81cd642019-11-20 00:14:47 +0000172Delete K8s Pod
173 [Arguments] ${namespace} ${name}
174 [Documentation] Uses kubectl to delete a named POD
175 ${rc} Run and Return Rc
176 ... kubectl delete -n ${namespace} pod/${name}
177 Should Be Equal as Integers ${rc} 0
178
hwchiu85695932019-12-18 08:05:25 +0000179Delete K8s Pods By Label
180 [Arguments] ${namespace} ${key} ${value}
181 [Documentation] Uses kubectl to delete a PODs, filtering by label
182 ${rc}= Run and Return Rc
183 ... kubectl -n ${namespace} delete pods -l${key}=${value}
184 Should Be Equal as Integers ${rc} 0
185
David Bainbridgef81cd642019-11-20 00:14:47 +0000186Scale K8s Deployment
187 [Arguments] ${namespace} ${name} ${count}
188 [Documentation] Uses kubectl to scale a named deployment
189 ${rc} Run and Return Rc
190 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
191 Should Be Equal as Integers ${rc} 0
192
193Pod Exists
194 [Arguments] ${namespace} ${name}
195 [Documentation] Succeeds it the named POD exists
196 ${rc} ${count} Run and Return Rc kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
197 Should Be True ${count}>0
198
199Pod Does Not Exist
200 [Arguments] ${namespace} ${name}
201 [Documentation] Succeeds if the named POD does not exist
202 ${rc} ${count} Run and Return Rc And Output
203 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
204 Should Be Equal As Integers ${count} 0
205 Should Be True ${count}==0
206
hwchiu85695932019-12-18 08:05:25 +0000207Pods Does Not Exist By Label
208 [Arguments] ${namespace} ${key} ${value}
209 [Documentation] Succeeds if the named POD does not exist
210 ${rc} ${count} Run and Return Rc And Output
211 ... kubectl get -n ${namespace} pod -l${key}=${value} -o json | jq -r ".items[].metadata.name" | wc -l
212 Should Be Equal As Integers ${count} 0
213 Should Be True ${count}==0
214
David Bainbridgef81cd642019-11-20 00:14:47 +0000215Get Available Deployment Replicas
216 [Arguments] ${namespace} ${name}
217 [Documentation] Succeeds if the named POD exists and has a ready count > 0
218 ${rc} ${count} Run and Return Rc and Output ${KUBECTL_CONFIG};kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
219 ${result}= Run Keyword If '${count}' == '' Set Variable 0
220 ... ELSE Set Variable ${count}
221 [Return] ${result}
222
223Check Expected Available Deployment Replicas
224 [Arguments] ${namespace} ${name} ${expected}
225 [Documentation] Succeeds if the named deployment has the expected number of available replicas
226 ${count}= Get Available Deployment Replicas ${namespace} ${name}
227 Should Be Equal As Integers ${expected} ${count}
228
229Get Deployment Replica Count
230 [Arguments] ${namespace} ${name}
231 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
232 ${rc} ${value} Run and Return Rc and Output
233 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
234 Should Be Equal as Integers ${rc} 0
235 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
236 ... ELSE Set Variable ${value}
237 [Return] ${replicas}
238
239Does Deployment Have Replicas
240 [Arguments] ${namespace} ${name} ${expected_count}
241 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
242 ${rc} ${value} Run and Return Rc and Output
243 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
244 Should Be Equal as Integers ${rc} 0
245 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
246 ... ELSE Set Variable ${value}
247 Should be Equal as Integers ${replicas} ${expected_count}
hwchiu85695932019-12-18 08:05:25 +0000248
249Pods Does Not Ready By Label
250 [Arguments] ${namespace} ${key} ${value}
251 [Documentation] Check PODs Ready Status
252 ${rc} ${count} Run and Return Rc and Output
253 ... kubectl -n ${namespace} get pods -l ${key}=${value} -o json | jq -r ".items[].status.containerStatuses[].ready" | grep -c false
254 Should Be Equal as Integers ${rc} 0