Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/webhook: refactor Multi-Cluster Service test #5539

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions pkg/webhook/multiclusterservice/mutating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestMutatingAdmission_Handle_FullCoverage(t *testing.T) {
},
}

// Create the initial mcs with default values for testing.
// Create the initial mcs object with default values for testing.
mcsObj := &networkingv1alpha1.MultiClusterService{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -174,10 +174,10 @@ func TestMutatingAdmission_Handle_FullCoverage(t *testing.T) {
obj: mcsObj,
}

// Marshal the expected policy to simulate the final mutated object.
// Marshal the expected mcs object to simulate the final mutated object.
wantBytes, err := json.Marshal(wantMCSObj)
if err != nil {
t.Fatalf("Failed to marshal expected policy: %v", err)
t.Fatalf("Failed to marshal expected mcs object: %v", err)
}
req.Object.Raw = wantBytes

Expand All @@ -189,12 +189,16 @@ func TestMutatingAdmission_Handle_FullCoverage(t *testing.T) {
// Call the Handle function.
got := mutatingHandler.Handle(context.Background(), req)

// Verify that the only patch applied is for the UUID label. If any other patches are present, it indicates that the mcs object was not handled as expected.
if len(got.Patches) > 0 {
firstPatch := got.Patches[0]
if firstPatch.Operation != "replace" || firstPatch.Path != "/metadata/labels/multiclusterservice.karmada.io~1permanent-id" {
t.Errorf("Handle() returned unexpected patches. Only the UUID patch was expected. Received patches: %v", got.Patches)
}
// Check if exactly one patch is applied.
if len(got.Patches) != 1 {
t.Errorf("Handle() returned an unexpected number of patches. Expected one patch, received: %v", got.Patches)
}

// Verify that the only patch applied is for the UUID label.
// If any other patches are present, it indicates that the mcs object was not handled as expected.
firstPatch := got.Patches[0]
if firstPatch.Operation != "replace" || firstPatch.Path != "/metadata/labels/multiclusterservice.karmada.io~1permanent-id" {
t.Errorf("Handle() returned unexpected patches. Only the UUID patch was expected. Received patches: %v", got.Patches)
}

// Check if the admission request was allowed.
Expand Down