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 26, 2023
1 parent 468f5f4 commit c7e9a30
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,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 @@ -3,9 +3,10 @@ package scansettingbinding
import (
"context"
"regexp"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"strings"

runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/go-logr/zapr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
Expand Down Expand Up @@ -35,7 +36,6 @@ var _ = Describe("Testing scansettingbinding controller", func() {
profOcpCis *compv1alpha1.Profile
tpRhcosE8 *compv1alpha1.TailoredProfile
tpOcpCis *compv1alpha1.TailoredProfile
scratchTP *compv1alpha1.TailoredProfile

setting *compv1alpha1.ScanSetting
ssb *compv1alpha1.ScanSettingBinding
Expand All @@ -45,6 +45,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 @@ -187,6 +193,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 @@ -698,6 +708,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 @@ -749,6 +763,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 c7e9a30

Please sign in to comment.