generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Misc] Webhook workload name check and label check on CAPTenantOutput (…
- Loading branch information
1 parent
c84180b
commit f52a332
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -782,6 +782,7 @@ func TestCavInvalidity(t *testing.T) { | |
multipleContentJobsWithNoOrder bool | ||
missingContentJobinContentJobs bool | ||
invalidJobinContentJobs bool | ||
invalidWorkloadName bool | ||
backlogItems []string | ||
}{ | ||
{ | ||
|
@@ -874,6 +875,11 @@ func TestCavInvalidity(t *testing.T) { | |
invalidJobinContentJobs: true, | ||
backlogItems: []string{"ERP4SMEPREPWORKAPPPLAT-4351"}, | ||
}, | ||
{ | ||
operation: admissionv1.Create, | ||
invalidWorkloadName: true, | ||
backlogItems: []string{}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
nameParts := []string{"Testing CAPApplicationversion invalidity for operation " + string(test.operation) + "; "} | ||
|
@@ -1145,6 +1151,8 @@ func TestCavInvalidity(t *testing.T) { | |
}, | ||
}) | ||
crd.Spec.ContentJobs = append(crd.Spec.ContentJobs, "content", "content-2", "dummy") | ||
} else if test.invalidWorkloadName == true { | ||
crd.Spec.Workloads[0].Name = "WrongWorkloadName" | ||
} | ||
|
||
rawBytes, _ := json.Marshal(crd) | ||
|
@@ -1202,6 +1210,8 @@ func TestCavInvalidity(t *testing.T) { | |
errorMessage = fmt.Sprintf("%s %s content job content-2 is not specified as part of ContentJobs", InvalidationMessage, v1alpha1.CAPApplicationVersionKind) | ||
} else if test.invalidJobinContentJobs == true { | ||
errorMessage = fmt.Sprintf("%s %s job dummy specified as part of ContentJobs is not a valid content job", InvalidationMessage, v1alpha1.CAPApplicationVersionKind) | ||
} else if test.invalidWorkloadName == true { | ||
errorMessage = fmt.Sprintf("%s %s Invalid workload name: %s", InvalidationMessage, v1alpha1.CAPApplicationVersionKind, "WrongWorkloadName") | ||
} | ||
|
||
if admissionReviewRes.Response.Allowed || admissionReviewRes.Response.Result.Message != errorMessage { | ||
|
@@ -1210,3 +1220,85 @@ func TestCavInvalidity(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestCtoutInvalidity(t *testing.T) { | ||
tests := []struct { | ||
operation admissionv1.Operation | ||
labelPresent bool | ||
}{ | ||
{ | ||
operation: admissionv1.Create, | ||
labelPresent: true, | ||
}, | ||
{ | ||
operation: admissionv1.Create, | ||
labelPresent: false, | ||
}, | ||
{ | ||
operation: admissionv1.Update, | ||
labelPresent: true, | ||
}, | ||
{ | ||
operation: admissionv1.Update, | ||
labelPresent: false, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run("Testing CAPTenantOutput invalidity for operation "+string(test.operation), func(t *testing.T) { | ||
var crdObjects []runtime.Object | ||
|
||
wh := &WebhookHandler{ | ||
CrdClient: fakeCrdClient.NewSimpleClientset(crdObjects...), | ||
} | ||
|
||
ctout := &ResponseCtout{ | ||
Metadata: Metadata{ | ||
Name: "some-ctout", | ||
Namespace: metav1.NamespaceDefault, | ||
Labels: map[string]string{}, | ||
}, | ||
Spec: &v1alpha1.CAPTenantOutputSpec{ | ||
SubscriptionCallbackData: `{"supportUsers":[{"name":"user_t1", "email":"[email protected]"},{"name":"user_t2", "email":"[email protected]"}]}`, | ||
}, | ||
Kind: v1alpha1.CAPApplicationVersionKind, | ||
} | ||
|
||
if test.labelPresent { | ||
ctout.Labels[LabelTenantId] = "some-tenant-id" | ||
} | ||
|
||
admissionReview, err := createAdmissionRequest(test.operation, v1alpha1.CAPTenantOutputKind, ctout.Name, noUpdate) | ||
if err != nil { | ||
t.Fatal("admission review error") | ||
} | ||
|
||
rawBytes, _ := json.Marshal(ctout) | ||
admissionReview.Request.Object.Raw = rawBytes | ||
bytesRequest, err := json.Marshal(admissionReview) | ||
if err != nil { | ||
t.Fatal("marshal error") | ||
} | ||
request := httptest.NewRequest(http.MethodGet, "/validate", bytes.NewBuffer(bytesRequest)) | ||
recorder := httptest.NewRecorder() | ||
|
||
wh.Validate(recorder, request) | ||
|
||
admissionReviewRes := admissionv1.AdmissionReview{} | ||
bytes, err := io.ReadAll(recorder.Body) | ||
if err != nil { | ||
t.Fatal("io read error") | ||
} | ||
universalDeserializer.Decode(bytes, nil, &admissionReviewRes) | ||
|
||
if test.labelPresent { | ||
if admissionReviewRes.Response.Allowed || admissionReviewRes.Response.Result.Message != fmt.Sprintf("%s %s label %s on CAP tenant output %s does not contain a valid tenant ID", InvalidationMessage, v1alpha1.CAPTenantOutputKind, LabelTenantId, "some-ctout") { | ||
t.Fatal("validation response error") | ||
} | ||
} else { | ||
if admissionReviewRes.Response.Allowed || admissionReviewRes.Response.Result.Message != fmt.Sprintf("%s %s label %s missing on CAP tenant output %s", InvalidationMessage, v1alpha1.CAPTenantOutputKind, LabelTenantId, "some-ctout") { | ||
t.Fatal("validation response error") | ||
} | ||
} | ||
}) | ||
} | ||
} |