blob: dfc072707c0bad5918eb42c698a12200fe6086b3 [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
55Verify All Voltha Pods For Any Error Logs
56 [Arguments] ${datetime}
57 [Documentation] This keyword checks for the error occurence in the voltha pods
58 &{errorPodDict} Create Dictionary
59 &{containerDict} Get Container Dictionary
60 FOR ${podName} IN @{PODLIST1}
61 ${containerName} Get From Dictionary ${containerDict} ${podName}
62 ${rc} ${logOutput} Run And Return Rc And Output ${KUBECTL_CONFIG};kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
63 Run Keyword And Ignore Error Run Keyword If '${logOutput}'=='${EMPTY}' Run Keywords Log No Log found in pod ${podName}
64 ... AND Continue For Loop
65 ${errorDict} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
66 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
67 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
68 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='Nologfound' Run Keywords Log No Error Log found in pod ${podName}
69 ... AND Continue For Loop
70 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound' Run Keywords Log Unexpected Error Log found in pod ${podName}
71 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
72 END
73 FOR ${podName} IN @{PODLIST2}
74 ${containerName} Get From Dictionary ${containerDict} ${podName}
75 ${rc} ${logOutput} Run And Return Rc And Output ${KUBECTL_CONFIG};kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
76 Run Keyword And Ignore Error Run Keyword If '${logOutput}'=='${EMPTY}' Run Keywords Log No Log found in pod ${podName}
77 ... AND Continue For Loop
78 ${errorDict} Check For Error Logs in Pod Type2 Given the Log Output ${logOutput}
79 ${returnStatusFlagList} Get Dictionary Keys ${errorDict}
80 ${returnStatusFlag} Get From List ${returnStatusFlagList} 0
81 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='Nologfound' Run Keywords Log No Error Log found in pod ${podName}
82 ... AND Continue For Loop
83 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound' Run Keywords Log Unexpected Error Log found in pod ${podName}
84 ... AND Set to Dictionary ${errorPodDict} ${podName} ${errorDict}
85 END
86 Print to Console Error Statement logged in the following pods : ${errorPodDict}
87 [Return] ${errorPodDict}
88
89Check For Error Logs in Pod Type1 Given the Log Output
90 [Arguments] ${logOutput} ${logLevel}=error ${errorMessage}=${EMPTY}
91 [Documentation] Checks for error message in the particular list of pods
92 Log ${logOutput}
93 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.* partial_match=true
94 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingLog}
95 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS' Nologfound '${is_exec_status}'=='FAIL' Errorlogfound
96 ${linesContainingError} = Get Lines Matching Regexp ${logOutput} .*\s\${logLevel}.*${errorMessage} partial_match=true
97 ${is_exec_status} ${output} Run Keyword And Ignore Error Should Be Empty ${linesContainingError}
98 ${returnStatusFlag} Set Variable If '${is_exec_status}'=='PASS' UnexpectedErrorfound '${is_exec_status}'=='FAIL' MatchingErrorlogfound
99 Log {linesContainingError}
100 &{errorDict} Create Dictionary ${returnStatusFlag} ${linesContainingLog}
101 [Return] ${errorDict}
102
103Check For Error Logs in Pod Type2 Given the Log Output
104 [Arguments] ${logOutput} ${logLevel}=warn ${errorMessage}=${EMPTY}
105 [Documentation] Checks for error message in the particular set of pods
106 Log ${logOutput}
107 ${linesContainingLog} = Get Lines Matching Regexp ${logOutput} .*?\s.*level.*${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.*level.*${logLevel}.*msg.*${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
117Get Container Dictionary
118 [Documentation] Creates a mapping for pod name and container name and returns the same
119 &{containerDict} Create Dictionary
120 ${containerName} Set Variable ${EMPTY}
121 ${podName} Run ${KUBECTL_CONFIG};kubectl get deployment -n voltha | awk 'NR>1 {print $1}'
122 @{podNameList}= Split To Lines ${podName}
123 Append To List ${podNameList} voltha-etcd-cluster voltha-kafka voltha-ro-core voltha-zookeeper
124 Log ${podNameList}
125 #Creatiing dictionary to correspond pod name and container name
126 FOR ${pod} IN @{podNameList}
127 ${containerName} Run ${KUBECTL_CONFIG};kubectl get pod -n voltha | grep ${pod} | awk '{print $1}'
128 &{containerDict} Set To Dictionary ${containerDict} ${pod} ${containerName}
129 END
130 Log ${containerDict}
131 [Return] ${containerDict}
132
133Validate Error For Given Pods
134 [Arguments] ${datetime} ${podDict}
135 [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] .
136 ...
137 ... Usage : ${returnStatusFlag} Validate Error For Given Pods ${datetime} ${podDict}
138 ... where,
139 ... ${datetime} = time from which the log needs to be taken
140 ... ${podDict} = Key-value pair of the pod name and the error msg expected like ${podDict} = Set Dictionary ${podDict} radius sample error message.
141 ...
142 ... In case the radius pod log has any other error than the expected error, then the podname will be returned
143 ${podList} = Get Dictionary Keys ${podDict}
144 FOR ${podName} IN @{podList}
145 ${containerName} Get From Dictionary ${containerDict} ${podName}
146 ${expectedError} Get From Dictionary ${podDict} ${podName}
147 ${rc} ${logOutput} Run And Return Rc And Output ${KUBECTL_CONFIG};kubectl logs --timestamps -n voltha --since-time=${datetime} ${containerName}
148 Run Keyword And Ignore Error Run Keyword If '${logOutput}'=='${EMPTY}' Run Keywords Log No Log found in pod ${podName}
149 ... AND Continue For Loop
150 ${returnStatusFlag} Check For Error Logs in Pod Type1 Given the Log Output ${logOutput}
151 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='Nologfound' Run Keywords Log No Error Log found in pod ${podName}
152 ... AND Continue For Loop
153 Run Keyword And Ignore Error Run Keyword If '${returnStatusFlag}'=='UnexpectedErrorfound' Run Keywords Log Unexpected Error Log found in pod ${podName}
154 ... AND Append To List ${errorPodList} ${podName}
155 END
156 [Return] ${errorPodList}
157
David Bainbridgef81cd642019-11-20 00:14:47 +0000158Delete K8s Pod
159 [Arguments] ${namespace} ${name}
160 [Documentation] Uses kubectl to delete a named POD
161 ${rc} Run and Return Rc
162 ... kubectl delete -n ${namespace} pod/${name}
163 Should Be Equal as Integers ${rc} 0
164
165Scale K8s Deployment
166 [Arguments] ${namespace} ${name} ${count}
167 [Documentation] Uses kubectl to scale a named deployment
168 ${rc} Run and Return Rc
169 ... kubectl scale --replicas=${count} -n ${namespace} deploy/${name}
170 Should Be Equal as Integers ${rc} 0
171
172Pod Exists
173 [Arguments] ${namespace} ${name}
174 [Documentation] Succeeds it the named POD exists
175 ${rc} ${count} Run and Return Rc kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep ${name}
176 Should Be True ${count}>0
177
178Pod Does Not Exist
179 [Arguments] ${namespace} ${name}
180 [Documentation] Succeeds if the named POD does not exist
181 ${rc} ${count} Run and Return Rc And Output
182 ... kubectl get -n ${namespace} pod -o json | jq -r ".items[].metadata.name" | grep -c ${name}
183 Should Be Equal As Integers ${count} 0
184 Should Be True ${count}==0
185
186Get Available Deployment Replicas
187 [Arguments] ${namespace} ${name}
188 [Documentation] Succeeds if the named POD exists and has a ready count > 0
189 ${rc} ${count} Run and Return Rc and Output ${KUBECTL_CONFIG};kubectl get -n ${namespace} deploy/${name} -o jsonpath='{.status.availableReplicas}'
190 ${result}= Run Keyword If '${count}' == '' Set Variable 0
191 ... ELSE Set Variable ${count}
192 [Return] ${result}
193
194Check Expected Available Deployment Replicas
195 [Arguments] ${namespace} ${name} ${expected}
196 [Documentation] Succeeds if the named deployment has the expected number of available replicas
197 ${count}= Get Available Deployment Replicas ${namespace} ${name}
198 Should Be Equal As Integers ${expected} ${count}
199
200Get Deployment Replica Count
201 [Arguments] ${namespace} ${name}
202 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
203 ${rc} ${value} Run and Return Rc and Output
204 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
205 Should Be Equal as Integers ${rc} 0
206 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
207 ... ELSE Set Variable ${value}
208 [Return] ${replicas}
209
210Does Deployment Have Replicas
211 [Arguments] ${namespace} ${name} ${expected_count}
212 [Documentation] Uses kubectl to fetch the number of configured replicas on a deployment
213 ${rc} ${value} Run and Return Rc and Output
214 ... kubectl -n ${namespace} get deploy/${name} -o 'jsonpath={.status.replicas}'
215 Should Be Equal as Integers ${rc} 0
216 ${replicas}= Run Keyword If '${value}' == '' Set Variable 0
217 ... ELSE Set Variable ${value}
218 Should be Equal as Integers ${replicas} ${expected_count}