From 44efaa639138dc040e0850f56278337eacaf928b Mon Sep 17 00:00:00 2001 From: Ekaterina Kazakova Date: Thu, 29 Aug 2024 17:29:57 +0400 Subject: [PATCH] Add management validation tests --- internal/webhook/management_webhook_test.go | 77 +++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 internal/webhook/management_webhook_test.go diff --git a/internal/webhook/management_webhook_test.go b/internal/webhook/management_webhook_test.go new file mode 100644 index 000000000..9044c5127 --- /dev/null +++ b/internal/webhook/management_webhook_test.go @@ -0,0 +1,77 @@ +// Copyright 2024 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package webhook + +import ( + "context" + "testing" + + . "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + + "github.com/Mirantis/hmc/api/v1alpha1" + "github.com/Mirantis/hmc/test/objects/deployment" + "github.com/Mirantis/hmc/test/objects/management" + "github.com/Mirantis/hmc/test/scheme" +) + +func TestManagementValidateDelete(t *testing.T) { + g := NewWithT(t) + + ctx := context.Background() + + tests := []struct { + name string + management *v1alpha1.Management + existingObjects []runtime.Object + err string + warnings admission.Warnings + }{ + { + name: "should fail if Deployment objects exist", + management: management.NewManagement(), + existingObjects: []runtime.Object{deployment.NewDeployment()}, + warnings: admission.Warnings{"The Management object can't be removed if Deployment objects still exist"}, + err: "management deletion is forbidden", + }, + { + name: "should succeed", + management: management.NewManagement(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(tt.existingObjects...).Build() + validator := &ManagementValidator{Client: c} + warn, err := validator.ValidateDelete(ctx, tt.management) + if tt.err != "" { + g.Expect(err).To(HaveOccurred()) + if err.Error() != tt.err { + t.Fatalf("expected error '%s', got error: %s", tt.err, err.Error()) + } + } else { + g.Expect(err).To(Succeed()) + } + if len(tt.warnings) > 0 { + g.Expect(warn).To(Equal(tt.warnings)) + } else { + g.Expect(warn).To(BeEmpty()) + } + }) + } +}