Skip to content

Commit 4967f87

Browse files
authored
Merge pull request #233 from jkyros/make-loglevel-test-work
Fix functionality (audit, loglevel) tests to actually wait for changes
2 parents 81ff7b1 + c926be8 commit 4967f87

File tree

1 file changed

+132
-34
lines changed

1 file changed

+132
-34
lines changed

controllers/keda/kedacontroller_controller_test.go

+132-34
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var _ = Describe("Deploying KedaController manifest", func() {
6969
})
7070

7171
AfterEach(func() {
72-
manifest, err = changeAttribute(manifest, "namespace", namespace, scheme)
72+
manifest, err = changeAttribute(manifest, "namespace", namespace, scheme, "")
7373
Expect(err).To(BeNil())
7474

7575
Expect(manifest.Delete()).Should(Succeed())
@@ -97,7 +97,7 @@ var _ = Describe("Deploying KedaController manifest", func() {
9797

9898
It("Should not deploy KedaController", func() {
9999

100-
manifest, err = changeAttribute(manifest, "namespace", changedNamespace, scheme)
100+
manifest, err = changeAttribute(manifest, "namespace", changedNamespace, scheme, "")
101101
Expect(err).To(BeNil())
102102

103103
Expect(manifest.Apply()).Should(Succeed())
@@ -137,10 +137,28 @@ var _ = Describe("Testing functionality", func() {
137137
)
138138

139139
BeforeEach(func() {
140+
By("Applying a manifest that defaults operator logLevel back to info")
140141
scheme = k8sManager.GetScheme()
141142
manifest, err = createManifest(kedaManifestFilepath, k8sClient)
142143
Expect(err).To(BeNil())
144+
manifest, err = changeAttribute(manifest, "logLevel", "info", scheme, "default")
145+
Expect(err).To(BeNil())
143146
Expect(manifest.Apply()).Should(Succeed())
147+
148+
By("Waiting for the operator deployment to reflect the changes")
149+
Eventually(func() error {
150+
return deploymentHasRolledOut(deploymentName, namespace, "default")
151+
}, timeout, interval).Should(Succeed())
152+
153+
By("Checking to make sure loglevel is set back to info")
154+
u, err := getObject(ctx, "Deployment", deploymentName, namespace, k8sClient)
155+
Expect(err).To(BeNil())
156+
err = scheme.Convert(u, dep, nil)
157+
Expect(err).To(BeNil())
158+
159+
arg, err = getDepArg(dep, logLevelPrefix, containerName)
160+
Expect(err).To(BeNil())
161+
Expect(arg).To(Equal("info"))
144162
})
145163

146164
Context("When changing \"--zap-log-level\"", func() {
@@ -160,6 +178,8 @@ var _ = Describe("Testing functionality", func() {
160178
initialLogLevel: "error",
161179
actualLogLevel: "error",
162180
},
181+
// the default in the sample kedacontroller manifest is "info", so "info" in this list can also mean "make sure it doesn't change",
182+
// it is supposed to ignore these two cases because the values are invalid.
163183
{
164184
initialLogLevel: "",
165185
actualLogLevel: "info",
@@ -171,27 +191,33 @@ var _ = Describe("Testing functionality", func() {
171191
}
172192

173193
for _, variant := range variants {
174-
It(fmt.Sprintf("Should change it, initialLoglevel='%s', actualLoglevel='%s'",
175-
variant.initialLogLevel, variant.actualLogLevel), func() {
176-
177-
manifest, err = changeAttribute(manifest, "logLevel", variant.initialLogLevel, scheme)
178-
_ = manifest.Apply()
179-
180-
Eventually(func() error {
181-
_, err = getObject(ctx, "Deployment", deploymentName, namespace, k8sClient)
182-
return err
183-
}, timeout, interval).Should(Succeed())
184-
185-
u, err := getObject(ctx, "Deployment", deploymentName, namespace, k8sClient)
186-
Expect(err).To(BeNil())
187-
err = scheme.Convert(u, dep, nil)
188-
Expect(err).To(BeNil())
189-
190-
arg, err = getDepArg(dep, logLevelPrefix, containerName)
191-
Expect(err).To(BeNil())
192-
Expect(arg).To(Equal(variant.actualLogLevel))
193-
})
194+
caseName := fmt.Sprintf("Should change it, initialLoglevel='%s', actualLoglevel='%s'", variant.initialLogLevel, variant.actualLogLevel)
195+
It(caseName,
196+
func() {
197+
By(fmt.Sprintf("Setting operator loglevel to %s in kedaController manifest", variant.initialLogLevel))
198+
manifest, err = changeAttribute(manifest, "logLevel", variant.initialLogLevel, scheme, caseName)
199+
Expect(err).To(BeNil())
200+
err = manifest.Apply()
201+
Expect(err).To(BeNil())
202+
203+
By("Waiting for the operator deployment to reflect the changes")
204+
Eventually(func() error {
205+
return deploymentHasRolledOut(deploymentName, namespace, caseName)
206+
}, timeout, interval).Should(Succeed())
207+
208+
By("Checking to make sure the log level is " + variant.actualLogLevel)
209+
u, err := getObject(ctx, "Deployment", deploymentName, namespace, k8sClient)
210+
Expect(err).To(BeNil())
211+
err = scheme.Convert(u, dep, nil)
212+
Expect(err).To(BeNil())
213+
214+
arg, err = getDepArg(dep, logLevelPrefix, containerName)
215+
Expect(err).To(BeNil())
216+
Expect(arg).To(Equal(variant.actualLogLevel))
217+
218+
})
194219
}
220+
195221
})
196222
})
197223

@@ -219,10 +245,28 @@ var _ = Describe("Testing functionality", func() {
219245
)
220246

221247
BeforeEach(func() {
248+
By("Applying a manifest that defaults operator logLevel back to info")
222249
scheme = k8sManager.GetScheme()
223250
manifest, err = createManifest(kedaManifestFilepath, k8sClient)
224251
Expect(err).To(BeNil())
252+
manifest, err = changeAttribute(manifest, "logLevel", "info", scheme, "default")
253+
Expect(err).To(BeNil())
225254
Expect(manifest.Apply()).Should(Succeed())
255+
256+
By("Waiting for the operator deployment to reflect the changes")
257+
Eventually(func() error {
258+
return deploymentHasRolledOut(deploymentName, namespace, "default")
259+
}, timeout, interval).Should(Succeed())
260+
261+
By("Checking to make sure loglevel is set back to info")
262+
u, err := getObject(ctx, "Deployment", deploymentName, namespace, k8sClient)
263+
Expect(err).To(BeNil())
264+
err = scheme.Convert(u, dep, nil)
265+
Expect(err).To(BeNil())
266+
267+
arg, err = getDepArg(dep, logLevelPrefix, containerName)
268+
Expect(err).To(BeNil())
269+
Expect(arg).To(Equal("info"))
226270
})
227271

228272
Context("When changing \"--zap-log-level\"", func() {
@@ -242,6 +286,8 @@ var _ = Describe("Testing functionality", func() {
242286
initialLogLevel: "error",
243287
actualLogLevel: "error",
244288
},
289+
// the default in the sample kedacontroller manifest is "info", so "info" in this list can also mean "make sure it doesn't change",
290+
// it is supposed to ignore these two cases because the values are invalid.
245291
{
246292
initialLogLevel: "",
247293
actualLogLevel: "info",
@@ -253,17 +299,21 @@ var _ = Describe("Testing functionality", func() {
253299
}
254300

255301
for _, variant := range variants {
256-
It(fmt.Sprintf("Should change it, initialLoglevel='%s', actualLoglevel='%s'",
257-
variant.initialLogLevel, variant.actualLogLevel), func() {
302+
caseName := fmt.Sprintf("Should change it, initialLoglevel='%s', actualLoglevel='%s'", variant.initialLogLevel, variant.actualLogLevel)
258303

259-
manifest, err = changeAttribute(manifest, "logLevel", variant.initialLogLevel, scheme)
260-
_ = manifest.Apply()
304+
It(caseName, func() {
305+
By(fmt.Sprintf("Setting admission loglevel to %s in kedaController manifest", variant.initialLogLevel))
306+
manifest, err = changeAttribute(manifest, "logLevel-admission", variant.initialLogLevel, scheme, caseName)
307+
Expect(err).To(BeNil())
308+
err = manifest.Apply()
309+
Expect(err).To(BeNil())
261310

311+
By("Waiting for the admission deployment to reflect the changes")
262312
Eventually(func() error {
263-
_, err = getObject(ctx, "Deployment", deploymentName, namespace, k8sClient)
264-
return err
313+
return deploymentHasRolledOut(deploymentName, namespace, caseName)
265314
}, timeout, interval).Should(Succeed())
266315

316+
By("Checking to make sure the log level is " + variant.actualLogLevel)
267317
u, err := getObject(ctx, "Deployment", deploymentName, namespace, k8sClient)
268318
Expect(err).To(BeNil())
269319
err = scheme.Convert(u, dep, nil)
@@ -298,6 +348,10 @@ var _ = Describe("Testing audit flags", func() {
298348
)
299349

300350
When("Manipulating parameters", func() {
351+
352+
// TODO(jkyros): come back to refactor this test, it doesn't proeprly reset between test cases, and
353+
// if it did, it would be failing, because it's currently impossible to deconfigure audit because of
354+
// how our manifestival code only operates if the settings are different from default/empty.
301355
BeforeEach(func() {
302356
scheme = k8sManager.GetScheme()
303357
dep = &appsv1.Deployment{}
@@ -317,7 +371,7 @@ var _ = Describe("Testing audit flags", func() {
317371
value: "json",
318372
},
319373
{
320-
argument: "auditMaxAge=",
374+
argument: "auditMaxAge",
321375
prefix: "--audit-log-maxage=",
322376
value: "1",
323377
},
@@ -333,13 +387,15 @@ var _ = Describe("Testing audit flags", func() {
333387
},
334388
}
335389
for _, variant := range vars {
390+
caseName := fmt.Sprintf("adds '%s' with value '%s'", variant.argument, variant.value)
391+
It(caseName, func() {
392+
manifest, err := changeAttribute(manifest, variant.argument, variant.value, scheme, caseName)
393+
Expect(err).To(BeNil())
336394

337-
It(fmt.Sprintf("adds '%s' with value '%s'", variant.argument, variant.value), func() {
338-
manifest, err := changeAttribute(manifest, variant.argument, variant.value, scheme)
339395
Expect(manifest.Apply()).To(Succeed())
340396
Eventually(func() error {
341-
_, err = getObject(ctx, "Deployment", metricsServerName, namespace, k8sClient)
342-
return err
397+
return deploymentHasRolledOut(metricsServerName, namespace, caseName)
398+
343399
}, timeout, interval).Should(Succeed())
344400
u, err := getObject(ctx, "Deployment", metricsServerName, namespace, k8sClient)
345401
Expect(err).To(BeNil())
@@ -369,18 +425,41 @@ func getDepArg(dep *appsv1.Deployment, prefix string, containerName string) (str
369425
return "", errors.New("Could not find a container: " + containerName)
370426
}
371427

372-
func changeAttribute(manifest mf.Manifest, attr string, value string, scheme *runtime.Scheme) (mf.Manifest, error) {
428+
func changeAttribute(manifest mf.Manifest, attr string, value string, scheme *runtime.Scheme, annotation string) (mf.Manifest, error) {
373429
transformer := func(u *unstructured.Unstructured) error {
374430
kedaControllerInstance := &kedav1alpha1.KedaController{}
375431
if err := scheme.Convert(u, kedaControllerInstance, nil); err != nil {
376432
return err
377433
}
378434

435+
// Annotations might be nil, so we need to make sure we account for that
436+
if kedaControllerInstance.Spec.Operator.DeploymentAnnotations == nil {
437+
kedaControllerInstance.Spec.Operator.DeploymentAnnotations = make(map[string]string)
438+
}
439+
440+
if kedaControllerInstance.Spec.AdmissionWebhooks.DeploymentAnnotations == nil {
441+
kedaControllerInstance.Spec.AdmissionWebhooks.DeploymentAnnotations = make(map[string]string)
442+
}
443+
if kedaControllerInstance.Spec.MetricsServer.DeploymentAnnotations == nil {
444+
kedaControllerInstance.Spec.MetricsServer.DeploymentAnnotations = make(map[string]string)
445+
}
446+
// When we push through an attribute change, we also set an annotation matching the test case
447+
// so we can be sure the controller reacted to our kedaControllerInstance updates and we have "our"
448+
// changes, not just some changes that might be from a previous test case
449+
kedaControllerInstance.Spec.Operator.DeploymentAnnotations["testCase"] = annotation
450+
kedaControllerInstance.Spec.AdmissionWebhooks.DeploymentAnnotations["testCase"] = annotation
451+
kedaControllerInstance.Spec.MetricsServer.DeploymentAnnotations["testCase"] = annotation
452+
379453
switch attr {
380454
case "namespace":
381455
kedaControllerInstance.Namespace = value
382456
case "logLevel":
383457
kedaControllerInstance.Spec.Operator.LogLevel = value
458+
// TODO(jkyros): this breaks pattern with the rest of these cases but multiple operands have
459+
// the same field, but we kind of bolted the admission tests on here without doing a refactor and
460+
// this makes it work for now
461+
case "logLevel-admission":
462+
kedaControllerInstance.Spec.AdmissionWebhooks.LogLevel = value
384463
// metricsServer audit arguments
385464
case "auditLogFormat":
386465
kedaControllerInstance.Spec.MetricsServer.AuditConfig.LogFormat = value
@@ -398,3 +477,22 @@ func changeAttribute(manifest mf.Manifest, attr string, value string, scheme *ru
398477

399478
return manifest.Transform(transformer)
400479
}
480+
481+
// deploymentHasRolledOut waits for the specified deployment to possess the specified annotation
482+
//
483+
//nolint:unparam
484+
func deploymentHasRolledOut(deploymentName string, namespace string, deploymentAnnotation string) error {
485+
u, err := getObject(ctx, "Deployment", deploymentName, namespace, k8sClient)
486+
if err != nil {
487+
return err
488+
}
489+
// The default manifest has no annotation, so I'm not checking whether it's present, only the value,
490+
// because the default case will not have the annotation, and we want that to be okay in the case where we
491+
// apply the default.
492+
testcase := u.GetAnnotations()["testCase"]
493+
if deploymentAnnotation == testcase {
494+
By("Observing that the test case annotation is now: " + testcase)
495+
return nil
496+
}
497+
return fmt.Errorf("Deployment has not rolled out, annotation is still '%s'", testcase)
498+
}

0 commit comments

Comments
 (0)