From 4860bf39954022bd1f63341d922d7dbe1f29305a Mon Sep 17 00:00:00 2001 From: Ivan Milchev Date: Tue, 3 Oct 2023 15:13:03 +0300 Subject: [PATCH] fix tests Signed-off-by: Ivan Milchev --- .../admission/deployment_handler_test.go | 7 +++-- .../deployment_handler_test.go | 4 +-- .../integration/integration_controller.go | 2 +- .../integration_controller_test.go | 25 ++++++++++----- .../k8s_scan/deployment_handler_test.go | 4 +-- .../mondooauditconfig_controller_test.go | 31 +++++++++++-------- controllers/nodes/deployment_handler_test.go | 4 +-- pkg/client/scanapiclient/client.go | 3 -- 8 files changed, 47 insertions(+), 33 deletions(-) diff --git a/controllers/admission/deployment_handler_test.go b/controllers/admission/deployment_handler_test.go index 7d6669754..40ebe58cd 100644 --- a/controllers/admission/deployment_handler_test.go +++ b/controllers/admission/deployment_handler_test.go @@ -316,7 +316,7 @@ func TestReconcile(t *testing.T) { err := kubeClient.Get(context.TODO(), deploymentKey, deployment) require.NoError(t, err, "expected Admission Deployment to exist") - assert.Equal(t, deployment.Spec.Replicas, int32((3))) + assert.Equal(t, ptr.To(int32(3)), deployment.Spec.Replicas) assert.Contains(t, deployment.Spec.Template.Spec.Containers[0].Args, string(mondoov1alpha2.Permissive), "expected Webhook mode to be set to 'permissive'") }, }, @@ -478,7 +478,10 @@ func TestReconcile(t *testing.T) { if test.existingObjects != nil { existingObj = append(existingObj, test.existingObjects(*auditConfig)...) } - fakeClient := fake.NewClientBuilder().WithObjects(existingObj...).Build() + fakeClient := fake.NewClientBuilder(). + WithStatusSubresource(existingObj...). + WithObjects(existingObj...). + Build() webhooks := &DeploymentHandler{ Mondoo: auditConfig, diff --git a/controllers/container_image/deployment_handler_test.go b/controllers/container_image/deployment_handler_test.go index d4edb65b0..9ae364266 100644 --- a/controllers/container_image/deployment_handler_test.go +++ b/controllers/container_image/deployment_handler_test.go @@ -225,7 +225,7 @@ func (s *DeploymentHandlerSuite) TestReconcile_K8sContainerImageScanningStatus() cronJobs.Items[0].Status.LastScheduleTime = &metaNow cronJobs.Items[0].Status.LastSuccessfulTime = &metaHourAgo - s.NoError(d.KubeClient.Update(s.ctx, &cronJobs.Items[0])) + s.NoError(d.KubeClient.Status().Update(s.ctx, &cronJobs.Items[0])) // Reconcile to update the audit config status result, err = d.Reconcile(s.ctx) @@ -241,7 +241,7 @@ func (s *DeploymentHandlerSuite) TestReconcile_K8sContainerImageScanningStatus() // Make the jobs successful again cronJobs.Items[0].Status.LastScheduleTime = nil cronJobs.Items[0].Status.LastSuccessfulTime = nil - s.NoError(d.KubeClient.Update(s.ctx, &cronJobs.Items[0])) + s.NoError(d.KubeClient.Status().Update(s.ctx, &cronJobs.Items[0])) // Reconcile to update the audit config status result, err = d.Reconcile(s.ctx) diff --git a/controllers/integration/integration_controller.go b/controllers/integration/integration_controller.go index a4c67b1ec..c72742fb6 100644 --- a/controllers/integration/integration_controller.go +++ b/controllers/integration/integration_controller.go @@ -158,7 +158,7 @@ func updateIntegrationCondition(config *v1alpha2.MondooAuditConfig, degradedStat status = corev1.ConditionFalse } else if degradedStatus { msg = "Mondoo integration not working" - reason = "IntegrationUnvailable" + reason = "IntegrationUnavailable" status = corev1.ConditionTrue } diff --git a/controllers/integration/integration_controller_test.go b/controllers/integration/integration_controller_test.go index 7f2718553..27565f7fc 100644 --- a/controllers/integration/integration_controller_test.go +++ b/controllers/integration/integration_controller_test.go @@ -138,7 +138,7 @@ func (s *IntegrationCheckInSuite) TestClearPreviousCondition() { }, } - existingObjects := []runtime.Object{ + existingObjects := []client.Object{ testMondooCredsSecret(), mondooAuditConfig, } @@ -156,7 +156,10 @@ func (s *IntegrationCheckInSuite) TestClearPreviousCondition() { return mClient, nil } - fakeClient := fake.NewClientBuilder().WithRuntimeObjects(existingObjects...).Build() + fakeClient := fake.NewClientBuilder(). + WithStatusSubresource(existingObjects...). + WithObjects(existingObjects...). + Build() r := &IntegrationReconciler{ Client: fakeClient, @@ -180,7 +183,7 @@ func (s *IntegrationCheckInSuite) TestMissingIntegrationMRN() { credsSecret := testMondooCredsSecret() delete(credsSecret.Data, constants.MondooCredsSecretIntegrationMRNKey) - existingObjects := []runtime.Object{ + existingObjects := []client.Object{ credsSecret, mondooAuditConfig, } @@ -194,7 +197,10 @@ func (s *IntegrationCheckInSuite) TestMissingIntegrationMRN() { return mClient, nil } - fakeClient := fake.NewClientBuilder().WithRuntimeObjects(existingObjects...).Build() + fakeClient := fake.NewClientBuilder(). + WithStatusSubresource(existingObjects...). + WithObjects(existingObjects...). + Build() r := &IntegrationReconciler{ Client: fakeClient, @@ -218,7 +224,7 @@ func (s *IntegrationCheckInSuite) TestBadServiceAccountData() { credsSecret := testMondooCredsSecret() credsSecret.Data[constants.MondooCredsSecretServiceAccountKey] = []byte("NOT VALID JWT") - existingObjects := []runtime.Object{ + existingObjects := []client.Object{ credsSecret, mondooAuditConfig, } @@ -232,7 +238,7 @@ func (s *IntegrationCheckInSuite) TestBadServiceAccountData() { return mClient, nil } - fakeClient := fake.NewClientBuilder().WithRuntimeObjects(existingObjects...).Build() + fakeClient := fake.NewClientBuilder().WithObjects(existingObjects...).WithStatusSubresource(existingObjects...).Build() r := &IntegrationReconciler{ Client: fakeClient, @@ -254,7 +260,7 @@ func (s *IntegrationCheckInSuite) TestFailedCheckIn() { mondooAuditConfig := testMondooAuditConfig() mondooAuditConfig.Spec.ConsoleIntegration.Enable = true - existingObjects := []runtime.Object{ + existingObjects := []client.Object{ testMondooCredsSecret(), mondooAuditConfig, } @@ -270,7 +276,10 @@ func (s *IntegrationCheckInSuite) TestFailedCheckIn() { return mClient, nil } - fakeClient := fake.NewClientBuilder().WithRuntimeObjects(existingObjects...).Build() + fakeClient := fake.NewClientBuilder(). + WithStatusSubresource(existingObjects...). + WithObjects(existingObjects...). + Build() r := &IntegrationReconciler{ Client: fakeClient, diff --git a/controllers/k8s_scan/deployment_handler_test.go b/controllers/k8s_scan/deployment_handler_test.go index 6d317723e..238e1bb28 100644 --- a/controllers/k8s_scan/deployment_handler_test.go +++ b/controllers/k8s_scan/deployment_handler_test.go @@ -231,7 +231,7 @@ func (s *DeploymentHandlerSuite) TestReconcile_K8sResourceScanningStatus() { cronJobs.Items[0].Status.LastScheduleTime = &metaNow cronJobs.Items[0].Status.LastSuccessfulTime = &metaHourAgo - s.NoError(d.KubeClient.Update(s.ctx, &cronJobs.Items[0])) + s.NoError(d.KubeClient.Status().Update(s.ctx, &cronJobs.Items[0])) // Reconcile to update the audit config status result, err = d.Reconcile(s.ctx) @@ -247,7 +247,7 @@ func (s *DeploymentHandlerSuite) TestReconcile_K8sResourceScanningStatus() { // Make the jobs successful again cronJobs.Items[0].Status.LastScheduleTime = nil cronJobs.Items[0].Status.LastSuccessfulTime = nil - s.NoError(d.KubeClient.Update(s.ctx, &cronJobs.Items[0])) + s.NoError(d.KubeClient.Status().Update(s.ctx, &cronJobs.Items[0])) // Reconcile to update the audit config status result, err = d.Reconcile(s.ctx) diff --git a/controllers/mondooauditconfig_controller_test.go b/controllers/mondooauditconfig_controller_test.go index a7a8c987d..53fbdfdf2 100644 --- a/controllers/mondooauditconfig_controller_test.go +++ b/controllers/mondooauditconfig_controller_test.go @@ -18,7 +18,6 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" utilruntime "k8s.io/apimachinery/pkg/util/runtime" scheme "k8s.io/client-go/kubernetes/scheme" @@ -82,14 +81,14 @@ func TestTokenRegistration(t *testing.T) { tests := []struct { name string - existingObjects []runtime.Object + existingObjects []client.Object mockMondooClient func(*gomock.Controller) *mockmondoo.MockMondooClient verify func(*testing.T, client.Client) expectError bool }{ { name: "generate service account from token secret", - existingObjects: []runtime.Object{ + existingObjects: []client.Object{ testTokenSecret(), testMondooAuditConfig(), }, @@ -119,7 +118,7 @@ func TestTokenRegistration(t *testing.T) { }, { name: "no token, no service account", - existingObjects: []runtime.Object{ + existingObjects: []client.Object{ testMondooAuditConfig(), }, mockMondooClient: func(mockCtrl *gomock.Controller) *mockmondoo.MockMondooClient { @@ -141,8 +140,8 @@ func TestTokenRegistration(t *testing.T) { }, { name: "already a Mondoo creds secret", - existingObjects: func() []runtime.Object { - objs := []runtime.Object{testMondooAuditConfig()} + existingObjects: func() []client.Object { + objs := []client.Object{testMondooAuditConfig()} objs = append(objs, &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ @@ -177,7 +176,7 @@ func TestTokenRegistration(t *testing.T) { }, { name: "mondoo API error", - existingObjects: []runtime.Object{ + existingObjects: []client.Object{ testTokenSecret(), testMondooAuditConfig(), }, @@ -192,8 +191,8 @@ func TestTokenRegistration(t *testing.T) { }, { name: "malformed JWT", - existingObjects: func() []runtime.Object { - objs := []runtime.Object{testMondooAuditConfig()} + existingObjects: func() []client.Object { + objs := []client.Object{testMondooAuditConfig()} sec := testTokenSecret() sec.Data["token"] = []byte("NOT JWT DATA") objs = append(objs, sec) @@ -208,7 +207,7 @@ func TestTokenRegistration(t *testing.T) { }, { name: "generate service account via Integrations", - existingObjects: []runtime.Object{ + existingObjects: []client.Object{ testIntegrationTokenSecret(), testMondooAuditConfigWithIntegration(), }, @@ -286,7 +285,7 @@ func TestTokenRegistration(t *testing.T) { }, { name: "missing owner claim error", - existingObjects: []runtime.Object{ + existingObjects: []client.Object{ testTokenSecret(), testMondooAuditConfigWithIntegration(), }, @@ -311,7 +310,10 @@ func TestTokenRegistration(t *testing.T) { return mClient, nil } - fakeClient := fake.NewClientBuilder().WithRuntimeObjects(test.existingObjects...).Build() + fakeClient := fake.NewClientBuilder(). + WithStatusSubresource(test.existingObjects...). + WithObjects(test.existingObjects...). + Build() scanApiStore := scan_api_store.NewScanApiStore(context.Background()) go scanApiStore.Start() @@ -365,7 +367,10 @@ func TestMondooAuditConfigStatus(t *testing.T) { mondooAuditConfig := testMondooAuditConfig() testToken := testTokenSecret() - fakeClient := fake.NewClientBuilder().WithRuntimeObjects(mondooAuditConfig, testToken).Build() + fakeClient := fake.NewClientBuilder(). + WithStatusSubresource(mondooAuditConfig, testToken). + WithObjects(mondooAuditConfig, testToken). + Build() scanApiStore := scan_api_store.NewScanApiStore(context.Background()) go scanApiStore.Start() diff --git a/controllers/nodes/deployment_handler_test.go b/controllers/nodes/deployment_handler_test.go index 09aa8dfb5..daa2ef260 100644 --- a/controllers/nodes/deployment_handler_test.go +++ b/controllers/nodes/deployment_handler_test.go @@ -390,7 +390,7 @@ func (s *DeploymentHandlerSuite) TestReconcile_NodeScanningStatus() { cronJobs.Items[0].Status.LastScheduleTime = &metaNow cronJobs.Items[0].Status.LastSuccessfulTime = &metaHourAgo - s.NoError(d.KubeClient.Update(s.ctx, &cronJobs.Items[0])) + s.NoError(d.KubeClient.Status().Update(s.ctx, &cronJobs.Items[0])) // Reconcile to update the audit config status result, err = d.Reconcile(s.ctx) @@ -406,7 +406,7 @@ func (s *DeploymentHandlerSuite) TestReconcile_NodeScanningStatus() { // Make the jobs successful again cronJobs.Items[0].Status.LastScheduleTime = nil cronJobs.Items[0].Status.LastSuccessfulTime = nil - s.NoError(d.KubeClient.Update(s.ctx, &cronJobs.Items[0])) + s.NoError(d.KubeClient.Status().Update(s.ctx, &cronJobs.Items[0])) // Reconcile to update the audit config status result, err = d.Reconcile(s.ctx) diff --git a/pkg/client/scanapiclient/client.go b/pkg/client/scanapiclient/client.go index 6367560c4..9fade059d 100644 --- a/pkg/client/scanapiclient/client.go +++ b/pkg/client/scanapiclient/client.go @@ -16,7 +16,6 @@ import ( "go.mondoo.com/mondoo-operator/pkg/client/common" "go.mondoo.com/mondoo-operator/pkg/constants" "go.mondoo.com/mondoo-operator/pkg/feature_flags" - "go.uber.org/zap" ) const ( @@ -120,10 +119,8 @@ func (s *scanApiClient) ScanKubernetesResources(ctx context.Context, scanOpts *S } if feature_flags.GetEnableV9() { - zap.S().Info("using v9 inventory") scanJob.Inventory.Spec.Assets[0].Connections[0].Type = "k8s" } else { - zap.S().Info("using v8 inventory") scanJob.Inventory.Spec.Assets[0].Connections[0].Backend = inventory.ProviderType_K8S }