diff --git a/internal/controller/eventing/subscription/eventmesh/test/utils.go b/internal/controller/eventing/subscription/eventmesh/test/utils.go index d0fafa71..8189a7ff 100644 --- a/internal/controller/eventing/subscription/eventmesh/test/utils.go +++ b/internal/controller/eventing/subscription/eventmesh/test/utils.go @@ -84,7 +84,6 @@ func setupSuite() error { emTestEnsemble = &eventMeshTestEnsemble{} // define logger - var err error defaultLogger, err := logger.New(string(kymalogger.JSON), string(kymalogger.INFO)) if err != nil { return err @@ -127,8 +126,7 @@ func setupSuite() error { eventMesh, credentials := setupEventMesh(defaultLogger) // Init the Subscription validator. - sinkValidator := validator.NewSinkValidator(k8sManager.GetClient()) - subscriptionValidator := validator.NewSubscriptionValidator(sinkValidator) + subscriptionValidator := validator.NewSubscriptionValidator(validator.NewSinkValidator(k8sManager.GetClient())) col := metrics.NewCollector() testReconciler := subscriptioncontrollereventmesh.NewReconciler( diff --git a/internal/controller/eventing/subscription/eventmesh/testwebhookauth/utils.go b/internal/controller/eventing/subscription/eventmesh/testwebhookauth/utils.go index 509f64b1..fc810bcd 100644 --- a/internal/controller/eventing/subscription/eventmesh/testwebhookauth/utils.go +++ b/internal/controller/eventing/subscription/eventmesh/testwebhookauth/utils.go @@ -86,7 +86,6 @@ func setupSuite() (*eventMeshTestEnsemble, error) { emTestEnsemble := &eventMeshTestEnsemble{} // define logger - var err error defaultLogger, err := logger.New(string(kymalogger.JSON), string(kymalogger.INFO)) if err != nil { return nil, err @@ -129,8 +128,7 @@ func setupSuite() (*eventMeshTestEnsemble, error) { col := metrics.NewCollector() // Init the Subscription validator. - sinkValidator := validator.NewSinkValidator(k8sManager.GetClient()) - subscriptionValidator := validator.NewSubscriptionValidator(sinkValidator) + subscriptionValidator := validator.NewSubscriptionValidator(validator.NewSinkValidator(k8sManager.GetClient())) testReconciler = subscriptioncontrollereventmesh.NewReconciler( k8sManager.GetClient(), diff --git a/internal/controller/eventing/subscription/validator/sink.go b/internal/controller/eventing/subscription/validator/sink.go index 26fe2629..9dc24134 100644 --- a/internal/controller/eventing/subscription/validator/sink.go +++ b/internal/controller/eventing/subscription/validator/sink.go @@ -35,7 +35,8 @@ func (sv sinkValidator) Validate(ctx context.Context, sink string) error { return fmt.Errorf("%w: %w", ErrSinkValidationFailed, err) } - if len(subDomains) < 2 { + const minSubDomains = 2 + if len(subDomains) < minSubDomains { return fmt.Errorf("%w: sink format should contain the service name.namespace", ErrSinkValidationFailed) } diff --git a/internal/controller/eventing/subscription/validator/subscription_test.go b/internal/controller/eventing/subscription/validator/subscription_test.go index b53c73ea..47f2da9c 100644 --- a/internal/controller/eventing/subscription/validator/subscription_test.go +++ b/internal/controller/eventing/subscription/validator/subscription_test.go @@ -1,3 +1 @@ package validator - -// TODO(marcobebway): Add tests for the subscription validator. diff --git a/internal/webhook/cleanup.go b/internal/webhook/cleanup.go index a17a9b54..b1ac2336 100644 --- a/internal/webhook/cleanup.go +++ b/internal/webhook/cleanup.go @@ -20,12 +20,13 @@ func CleanupResources(ctx context.Context, client kctrlclient.Client) []error { mutatingWebhookConfiguration = "subscription-mutating-webhook-configuration" validatingWebhookConfiguration = "subscription-validating-webhook-configuration" ) - var errList = make([]error, 0, 5) - appendIfError(errList, deleteService(ctx, client, namespace, service)) - appendIfError(errList, deleteCronJob(ctx, client, namespace, cronjob)) - appendIfError(errList, deleteJob(ctx, client, namespace, job)) - appendIfError(errList, deleteMutatingWebhookConfiguration(ctx, client, namespace, mutatingWebhookConfiguration)) - appendIfError(errList, deleteValidatingWebhookConfiguration(ctx, client, namespace, validatingWebhookConfiguration)) + const capacity = 5 + errList := make([]error, 0, capacity) + errList = appendIfError(errList, deleteService(ctx, client, namespace, service)) + errList = appendIfError(errList, deleteCronJob(ctx, client, namespace, cronjob)) + errList = appendIfError(errList, deleteJob(ctx, client, namespace, job)) + errList = appendIfError(errList, deleteMutatingWebhookConfiguration(ctx, client, namespace, mutatingWebhookConfiguration)) + errList = appendIfError(errList, deleteValidatingWebhookConfiguration(ctx, client, namespace, validatingWebhookConfiguration)) return errList } @@ -104,8 +105,9 @@ func deleteValidatingWebhookConfiguration(ctx context.Context, client kctrlclien ) } -func appendIfError(errList []error, err error) { +func appendIfError(errList []error, err error) []error { if err != nil { - errList = append(errList, err) + return append(errList, err) } + return errList } diff --git a/internal/webhook/cleanup_test.go b/internal/webhook/cleanup_test.go new file mode 100644 index 00000000..5fc542d4 --- /dev/null +++ b/internal/webhook/cleanup_test.go @@ -0,0 +1,25 @@ +package webhook + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_appendIfError(t *testing.T) { + // given + var errList []error + + // when + errList = appendIfError(errList, nil) + errList = appendIfError(errList, nil) + // then + require.Empty(t, errList) + + // when + errList = appendIfError(errList, errors.New("error1")) //nolint:goerr113 + errList = appendIfError(errList, errors.New("error2")) //nolint:goerr113 + // then + require.Len(t, errList, 2) +}