Skip to content

Commit

Permalink
Add management validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eromanova committed Aug 29, 2024
1 parent 86b5de0 commit 44efaa6
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions internal/webhook/management_webhook_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
})
}
}

0 comments on commit 44efaa6

Please sign in to comment.