Skip to content

Commit e72162e

Browse files
committed
Add cohort webhook test to cover error message
Error message in the test is not ignored because there was a misleading error message when using hierarchical cohorts. This test makes sure the message is different when validating cohort object.
1 parent 63497b7 commit e72162e

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

pkg/webhooks/clusterqueue_webhook_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package webhooks
1818

1919
import (
20-
"fmt"
2120
"testing"
2221

2322
"github.com/google/go-cmp/cmp"
@@ -157,7 +156,7 @@ func TestValidateClusterQueue(t *testing.T) {
157156
*testingutil.MakeFlavorQuotas("x86").Resource("cpu", "1", "", "1").Obj()).
158157
Obj(),
159158
wantErr: field.ErrorList{
160-
field.Invalid(resourceGroupsPath.Index(0).Child("flavors").Index(0).Child("resources").Index(0).Child("lendingLimit"), "1", fmt.Sprintf(limitIsEmptyErrorMsgTemplate, "cohort")),
159+
field.Invalid(resourceGroupsPath.Index(0).Child("flavors").Index(0).Child("resources").Index(0).Child("lendingLimit"), "1", "must be nil when cohort is empty"),
161160
},
162161
},
163162
{

pkg/webhooks/cohort_webhook_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package webhooks
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
"github.com/google/go-cmp/cmp/cmpopts"
24+
"k8s.io/apimachinery/pkg/util/validation/field"
25+
26+
kueue "sigs.k8s.io/kueue/apis/kueue/v1alpha1"
27+
"sigs.k8s.io/kueue/pkg/features"
28+
testingutil "sigs.k8s.io/kueue/pkg/util/testing"
29+
)
30+
31+
func TestValidateCohort(t *testing.T) {
32+
specPath := field.NewPath("spec")
33+
resourceGroupsPath := specPath.Child("resourceGroups")
34+
35+
testcases := []struct {
36+
name string
37+
cohort *kueue.Cohort
38+
wantErr field.ErrorList
39+
disableLendingLimit bool
40+
}{
41+
{
42+
name: "flavor quota with lendingLimit and empty parent",
43+
cohort: testingutil.MakeCohort("cohort").
44+
ResourceGroup(
45+
*testingutil.MakeFlavorQuotas("x86").Resource("cpu", "1", "", "1").Obj()).
46+
Obj(),
47+
wantErr: field.ErrorList{
48+
field.Invalid(resourceGroupsPath.Index(0).Child("flavors").Index(0).Child("resources").Index(0).Child("lendingLimit"), "1", "must be nil when parent is empty"),
49+
},
50+
},
51+
}
52+
53+
for _, tc := range testcases {
54+
t.Run(tc.name, func(t *testing.T) {
55+
if tc.disableLendingLimit {
56+
features.SetFeatureGateDuringTest(t, features.LendingLimit, false)
57+
}
58+
gotErr := validateCohort(tc.cohort)
59+
if diff := cmp.Diff(tc.wantErr, gotErr, cmpopts.IgnoreFields(field.Error{}, "BadValue")); diff != "" {
60+
t.Errorf("ValidateResources() mismatch (-want +got):\n%s", diff)
61+
}
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)