Skip to content

Commit

Permalink
Fix broken tests as a result of updating controller-runtime 0.16.2
Browse files Browse the repository at this point in the history
The fake client we use in our unit tests was recently updated to be more
inline with what the actual kubernetes API does:

  kubernetes-sigs/controller-runtime#2259

As a result, some of our tests broken because we were lumping Status and
other updates into the same Update() request.

This commit refactors those tests so that Status updates are implemented
separately from the rest of the runtime objects. We need to do this
before we can upgrade controller-runtime to 0.16.3.
  • Loading branch information
rhmdnd committed Oct 25, 2023
1 parent 9d21958 commit 976bc36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,11 @@ var _ = Describe("Testing compliancescan controller phases", func() {

// Set state to DONE
compliancescaninstance.Status.Phase = compv1alpha1.PhaseDone
compliancescaninstance.Spec.Debug = true
err := reconciler.Client.Status().Update(context.TODO(), compliancescaninstance)
Expect(err).To(BeNil())
compliancescaninstance.Spec.Debug = true
err = reconciler.Client.Update(context.TODO(), compliancescaninstance)
Expect(err).To(BeNil())
})
It("Should return success & not delete the scan pods or secrets (doDelete=false)", func() {
result, err := reconciler.phaseDoneHandler(handler, compliancescaninstance, logger, dontDelete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compliancesuite
import (
"context"
"encoding/json"

"github.com/ComplianceAsCode/compliance-operator/pkg/controller/metrics"
"github.com/ComplianceAsCode/compliance-operator/pkg/controller/metrics/metricsfakes"

Expand Down Expand Up @@ -207,7 +208,7 @@ var _ = Describe("ComplianceSuiteController", func() {

By("The remediation controller setting the applied status")
rem.Status.ApplicationState = compv1alpha1.RemediationApplied
err := reconciler.Client.Update(ctx, rem)
err := reconciler.Client.Status().Update(ctx, rem)
Expect(err).To(BeNil())

By("Running a second reconcile loop")
Expand All @@ -222,7 +223,7 @@ var _ = Describe("ComplianceSuiteController", func() {
Context("With spec.AutoApplyRemediations = true", func() {
BeforeEach(func() {
suite.Spec.AutoApplyRemediations = true
err := reconciler.Client.Status().Update(ctx, suite)
err := reconciler.Client.Update(ctx, suite)
Expect(err).To(BeNil())
})
Context("With ComplianceSuite and Scans not done", func() {
Expand Down Expand Up @@ -351,7 +352,7 @@ var _ = Describe("ComplianceSuiteController", func() {

By("The remediation controller setting the applied status")
rem.Status.ApplicationState = compv1alpha1.RemediationApplied
err := reconciler.Client.Update(ctx, rem)
err := reconciler.Client.Status().Update(ctx, rem)
Expect(err).To(BeNil())

By("the pool should be paused")
Expand All @@ -374,7 +375,7 @@ var _ = Describe("ComplianceSuiteController", func() {
Context("With spec.AutoApplyRemediations = true", func() {
BeforeEach(func() {
suite.Spec.AutoApplyRemediations = true
err := reconciler.Client.Status().Update(ctx, suite)
err := reconciler.Client.Update(ctx, suite)
Expect(err).To(BeNil())
})
Context("With ComplianceSuite and Scans not done", func() {
Expand Down Expand Up @@ -596,7 +597,7 @@ var _ = Describe("ComplianceSuiteController", func() {

By("The remediation controller setting the applied status")
rem.Status.ApplicationState = compv1alpha1.RemediationApplied
err := reconciler.Client.Update(ctx, rem)
err := reconciler.Client.Status().Update(ctx, rem)
Expect(err).To(BeNil())

By("the pool should be paused")
Expand Down Expand Up @@ -651,7 +652,7 @@ var _ = Describe("ComplianceSuiteController", func() {
Context("With spec.AutoApplyRemediations = true", func() {
BeforeEach(func() {
suite.Spec.AutoApplyRemediations = true
err := reconciler.Client.Status().Update(ctx, suite)
err := reconciler.Client.Update(ctx, suite)
Expect(err).To(BeNil())
})
Context("With ComplianceSuite and Scans not done", func() {
Expand Down Expand Up @@ -875,7 +876,7 @@ var _ = Describe("ComplianceSuiteController", func() {

By("The remediation controller setting the applied status")
rem.Status.ApplicationState = compv1alpha1.RemediationApplied
err := reconciler.Client.Update(ctx, rem)
err := reconciler.Client.Status().Update(ctx, rem)
Expect(err).To(BeNil())

By("the pool should be paused")
Expand Down Expand Up @@ -930,7 +931,7 @@ var _ = Describe("ComplianceSuiteController", func() {
Context("With spec.AutoApplyRemediations = true", func() {
BeforeEach(func() {
suite.Spec.AutoApplyRemediations = true
err := reconciler.Client.Status().Update(ctx, suite)
err := reconciler.Client.Update(ctx, suite)
Expect(err).To(BeNil())
})
Context("With ComplianceSuite and Scans not done", func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ var _ = Describe("Testing scansettingbinding controller", func() {
pBundleRhcos *compv1alpha1.ProfileBundle
profRhcosE8 *compv1alpha1.Profile
tpRhcosE8 *compv1alpha1.TailoredProfile
scratchTP *compv1alpha1.TailoredProfile

setting *compv1alpha1.ScanSetting
ssb *compv1alpha1.ScanSettingBinding
Expand All @@ -43,6 +42,12 @@ var _ = Describe("Testing scansettingbinding controller", func() {

suite *compv1alpha1.ComplianceSuite
)
scratchTP := &compv1alpha1.TailoredProfile{
TypeMeta: v1.TypeMeta{
Kind: "TailoredProfile",
APIVersion: compv1alpha1.SchemeGroupVersion.String(),
},
}

BeforeEach(func() {
// Uncomment these lines if you need to debug the controller's output.
Expand Down Expand Up @@ -115,6 +120,10 @@ var _ = Describe("Testing scansettingbinding controller", func() {
}

scratchTP = &compv1alpha1.TailoredProfile{
TypeMeta: v1.TypeMeta{
Kind: "TailoredProfile",
APIVersion: compv1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: v1.ObjectMeta{
Name: "scratch-tp",
Namespace: common.GetComplianceOperatorNamespace(),
Expand Down Expand Up @@ -591,6 +600,10 @@ var _ = Describe("Testing scansettingbinding controller", func() {
Expect(updateErr).To(BeNil())

ssb = &compv1alpha1.ScanSettingBinding{
TypeMeta: v1.TypeMeta{
Kind: "ScanSettingBinding",
APIVersion: compv1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: v1.ObjectMeta{
Name: "tp-not-ready",
Namespace: common.GetComplianceOperatorNamespace(),
Expand Down Expand Up @@ -642,6 +655,10 @@ var _ = Describe("Testing scansettingbinding controller", func() {
Expect(updateErr).To(BeNil())

ssb = &compv1alpha1.ScanSettingBinding{
TypeMeta: v1.TypeMeta{
Kind: "ScanSettingBinding",
APIVersion: compv1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: v1.ObjectMeta{
Name: "tp-errored",
Namespace: common.GetComplianceOperatorNamespace(),
Expand Down

0 comments on commit 976bc36

Please sign in to comment.