Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev committed Oct 3, 2023
1 parent f928da8 commit 4860bf3
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 33 deletions.
7 changes: 5 additions & 2 deletions controllers/admission/deployment_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
},
},
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions controllers/container_image/deployment_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion controllers/integration/integration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
25 changes: 17 additions & 8 deletions controllers/integration/integration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (s *IntegrationCheckInSuite) TestClearPreviousCondition() {
},
}

existingObjects := []runtime.Object{
existingObjects := []client.Object{
testMondooCredsSecret(),
mondooAuditConfig,
}
Expand All @@ -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,
Expand All @@ -180,7 +183,7 @@ func (s *IntegrationCheckInSuite) TestMissingIntegrationMRN() {
credsSecret := testMondooCredsSecret()
delete(credsSecret.Data, constants.MondooCredsSecretIntegrationMRNKey)

existingObjects := []runtime.Object{
existingObjects := []client.Object{
credsSecret,
mondooAuditConfig,
}
Expand All @@ -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,
Expand All @@ -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,
}
Expand All @@ -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,
Expand All @@ -254,7 +260,7 @@ func (s *IntegrationCheckInSuite) TestFailedCheckIn() {
mondooAuditConfig := testMondooAuditConfig()
mondooAuditConfig.Spec.ConsoleIntegration.Enable = true

existingObjects := []runtime.Object{
existingObjects := []client.Object{
testMondooCredsSecret(),
mondooAuditConfig,
}
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions controllers/k8s_scan/deployment_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
31 changes: 18 additions & 13 deletions controllers/mondooauditconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
},
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -177,7 +176,7 @@ func TestTokenRegistration(t *testing.T) {
},
{
name: "mondoo API error",
existingObjects: []runtime.Object{
existingObjects: []client.Object{
testTokenSecret(),
testMondooAuditConfig(),
},
Expand All @@ -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)
Expand All @@ -208,7 +207,7 @@ func TestTokenRegistration(t *testing.T) {
},
{
name: "generate service account via Integrations",
existingObjects: []runtime.Object{
existingObjects: []client.Object{
testIntegrationTokenSecret(),
testMondooAuditConfigWithIntegration(),
},
Expand Down Expand Up @@ -286,7 +285,7 @@ func TestTokenRegistration(t *testing.T) {
},
{
name: "missing owner claim error",
existingObjects: []runtime.Object{
existingObjects: []client.Object{
testTokenSecret(),
testMondooAuditConfigWithIntegration(),
},
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions controllers/nodes/deployment_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions pkg/client/scanapiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 4860bf3

Please sign in to comment.