|
| 1 | +/* |
| 2 | +Copyright 2020 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 nonstructuralschema |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "reflect" |
| 22 | + "testing" |
| 23 | + |
| 24 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 25 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 26 | +) |
| 27 | + |
| 28 | +func Test_calculateCondition(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + args *apiextensionsv1.CustomResourceDefinition |
| 32 | + want *apiextensionsv1.CustomResourceDefinitionCondition |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "preserve unknown fields is false", |
| 36 | + args: &apiextensionsv1.CustomResourceDefinition{ |
| 37 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 38 | + PreserveUnknownFields: false, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "preserve unknown fields is true", |
| 44 | + args: &apiextensionsv1.CustomResourceDefinition{ |
| 45 | + Spec: apiextensionsv1.CustomResourceDefinitionSpec{ |
| 46 | + PreserveUnknownFields: true, |
| 47 | + }, |
| 48 | + }, |
| 49 | + want: &apiextensionsv1.CustomResourceDefinitionCondition{ |
| 50 | + Type: apiextensionsv1.NonStructuralSchema, |
| 51 | + Status: apiextensionsv1.ConditionTrue, |
| 52 | + Reason: "Violations", |
| 53 | + Message: field.Invalid(field.NewPath("spec", "preserveUnknownFields"), |
| 54 | + true, |
| 55 | + fmt.Sprint("must be false")).Error(), |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + for _, tt := range tests { |
| 60 | + t.Run(tt.name, func(t *testing.T) { |
| 61 | + if got := calculateCondition(tt.args); !reflect.DeepEqual(got, tt.want) { |
| 62 | + t.Errorf("calculateCondition() = %v, want %v", got, tt.want) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
0 commit comments