|
| 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