@@ -11,6 +11,7 @@ import (
11
11
12
12
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
13
"k8s.io/apimachinery/pkg/util/wait"
14
+ "k8s.io/klog/v2"
14
15
admissionapi "k8s.io/pod-security-admission/api"
15
16
16
17
exutil "github.com/openshift/origin/test/extended/util"
@@ -155,3 +156,131 @@ var _ = g.Describe("[sig-cli] oc idle [apigroup:apps.openshift.io][apigroup:rout
155
156
o .Expect (out ).To (o .Equal (scaledReplicaCount ))
156
157
})
157
158
})
159
+
160
+ var _ = g .Describe ("[sig-cli] oc idle Deployments [apigroup:route.openshift.io][apigroup:project.openshift.io][apigroup:image.openshift.io]" , func () {
161
+ defer g .GinkgoRecover ()
162
+
163
+ var (
164
+ oc = exutil .NewCLIWithPodSecurityLevel ("oc-idle" , admissionapi .LevelBaseline )
165
+ cmdTestData = exutil .FixturePath ("testdata" , "cmd" , "test" , "cmd" , "testdata" )
166
+ idleSVCRoute = filepath .Join (cmdTestData , "idling-svc-route.yaml" )
167
+ idleDeployment = filepath .Join (cmdTestData , "idling-deployment.yaml" )
168
+ idledTemplate = fmt .Sprintf ("--template={{index .metadata.annotations \" %s\" }}" , idledAnnotation )
169
+ )
170
+
171
+ var deploymentName , expectedOutput string
172
+ g .JustBeforeEach (func () {
173
+ projectName , err := oc .Run ("project" ).Args ("-q" ).Output ()
174
+ o .Expect (err ).NotTo (o .HaveOccurred ())
175
+
176
+ g .By ("create required service and routers" )
177
+ err = oc .Run ("create" ).Args ("-f" , idleSVCRoute ).Execute ()
178
+ o .Expect (err ).NotTo (o .HaveOccurred ())
179
+
180
+ g .By ("create deployment and get deployment name" )
181
+ _ , err = oc .Run ("create" ).Args ("-f" , idleDeployment ).Output ()
182
+ o .Expect (err ).NotTo (o .HaveOccurred ())
183
+
184
+ dcList , err := oc .AdminKubeClient ().AppsV1 ().Deployments (projectName ).List (context .TODO (), metav1.ListOptions {LabelSelector : "app=idling-echo,deployment=idling-echo" })
185
+ o .Expect (dcList .Items ).Should (o .HaveLen (1 ))
186
+ deploymentName = dcList .Items [0 ].Name
187
+
188
+ expectedOutput = fmt .Sprintf ("The service will unidle Deployment \" %s/%s\" to %s replicas once it receives traffic" , projectName , deploymentName , scaledReplicaCount )
189
+
190
+ err = oc .Run ("describe" ).Args ("deployments" , deploymentName ).Execute ()
191
+ o .Expect (err ).NotTo (o .HaveOccurred ())
192
+
193
+ g .By ("wait until idling-echo endpoint is ready" )
194
+ err = wait .PollImmediate (time .Second , 60 * time .Second , func () (done bool , err error ) {
195
+ err = oc .Run ("describe" ).Args ("endpoints" , "idling-echo" ).Execute ()
196
+ if err != nil {
197
+ return false , nil
198
+ }
199
+
200
+ return true , nil
201
+ })
202
+ o .Expect (err ).NotTo (o .HaveOccurred ())
203
+
204
+ g .By ("wait until replicaset is ready" )
205
+ var rsName string
206
+ err = wait .PollImmediate (time .Second , 60 * time .Second , func () (done bool , err error ) {
207
+ rsList , err := oc .AdminKubeClient ().AppsV1 ().ReplicaSets (projectName ).List (context .TODO (), metav1.ListOptions {LabelSelector : "app=idling-echo,deployment=idling-echo" })
208
+ o .Expect (err ).NotTo (o .HaveOccurred ())
209
+ if len (rsList .Items ) != 1 {
210
+ klog .Infof ("Expected only a single replicaset, got %d instead" , len (rsList .Items ))
211
+ return false , nil
212
+ }
213
+ rsName = rsList .Items [0 ].Name
214
+ err = oc .Run ("get" ).Args ("replicaset" , rsName ).Execute ()
215
+ if err != nil {
216
+ return false , nil
217
+ }
218
+
219
+ return true , nil
220
+ })
221
+ o .Expect (err ).NotTo (o .HaveOccurred ())
222
+
223
+ g .By (fmt .Sprintf ("scale deployment to %s replicas" , scaledReplicaCount ))
224
+ err = oc .Run ("scale" ).Args ("replicaset" , rsName , fmt .Sprintf ("--replicas=%s" , scaledReplicaCount )).Execute ()
225
+ o .Expect (err ).NotTo (o .HaveOccurred ())
226
+
227
+ g .By (fmt .Sprintf ("wait until pod is scaled to %s" , scaledReplicaCount ))
228
+ err = wait .PollImmediate (time .Second , 60 * time .Second , func () (done bool , err error ) {
229
+ out , err := oc .Run ("get" ).Args ("pods" , "-l" , "app=idling-echo" , "--template={{ len .items }}" , "--output=go-template" ).Output ()
230
+ if err != nil {
231
+ return false , err
232
+ }
233
+
234
+ if out != scaledReplicaCount {
235
+ return false , nil
236
+ }
237
+
238
+ return true , nil
239
+ })
240
+ o .Expect (err ).NotTo (o .HaveOccurred ())
241
+
242
+ g .By (fmt .Sprintf ("wait until endpoint addresses are scaled to %s" , scaledReplicaCount ))
243
+ err = wait .PollImmediate (time .Second , 60 * time .Second , func () (done bool , err error ) {
244
+ out , err := oc .Run ("get" ).Args ("endpoints" , "idling-echo" , "--template={{ len (index .subsets 0).addresses }}" , "--output=go-template" ).Output ()
245
+ if err != nil || out != scaledReplicaCount {
246
+ return false , nil
247
+ }
248
+
249
+ return true , nil
250
+ })
251
+ o .Expect (err ).NotTo (o .HaveOccurred ())
252
+ })
253
+
254
+ g .It ("by name" , func () {
255
+ err := oc .Run ("idle" ).Args (fmt .Sprintf ("deployment/%s" , deploymentName )).Execute ()
256
+ o .Expect (err ).To (o .HaveOccurred ())
257
+
258
+ out , err := oc .Run ("idle" ).Args ("idling-echo" ).Output ()
259
+ o .Expect (err ).NotTo (o .HaveOccurred ())
260
+ o .Expect (out ).To (o .ContainSubstring (expectedOutput ))
261
+
262
+ out , err = oc .Run ("get" ).Args ("service" , "idling-echo" , idledTemplate , "--output=go-template" ).Output ()
263
+ o .Expect (err ).NotTo (o .HaveOccurred ())
264
+ o .Expect (out ).NotTo (o .BeEmpty ())
265
+ })
266
+
267
+ g .It ("by label" , func () {
268
+ out , err := oc .Run ("idle" ).Args ("-l" , "app=idling-echo" ).Output ()
269
+ o .Expect (err ).NotTo (o .HaveOccurred ())
270
+ o .Expect (out ).To (o .ContainSubstring (expectedOutput ))
271
+
272
+ out , err = oc .Run ("get" ).Args ("service" , "idling-echo" , idledTemplate , "--output=go-template" ).Output ()
273
+ o .Expect (err ).NotTo (o .HaveOccurred ())
274
+ o .Expect (out ).NotTo (o .BeEmpty ())
275
+ })
276
+
277
+ g .It ("by all" , func () {
278
+ out , err := oc .Run ("idle" ).Args ("--all" ).Output ()
279
+ o .Expect (err ).NotTo (o .HaveOccurred ())
280
+ o .Expect (out ).To (o .ContainSubstring (expectedOutput ))
281
+
282
+ out , err = oc .Run ("get" ).Args ("service" , "idling-echo" , idledTemplate , "--output=go-template" ).Output ()
283
+ o .Expect (err ).NotTo (o .HaveOccurred ())
284
+ o .Expect (out ).NotTo (o .BeEmpty ())
285
+ })
286
+ })
0 commit comments