|
| 1 | +/* |
| 2 | +Copyright 2024 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 client_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" |
| 28 | +) |
| 29 | + |
| 30 | +func TestWithStrictFieldValidation(t *testing.T) { |
| 31 | + calls := 0 |
| 32 | + fakeClient := testFieldValidationClient(t, metav1.FieldValidationStrict, func() { calls++ }) |
| 33 | + wrappedClient := client.WithFieldValidation(fakeClient, metav1.FieldValidationStrict) |
| 34 | + |
| 35 | + ctx := context.Background() |
| 36 | + dummyObj := &corev1.Namespace{} |
| 37 | + |
| 38 | + _ = wrappedClient.Create(ctx, dummyObj) |
| 39 | + _ = wrappedClient.Update(ctx, dummyObj) |
| 40 | + _ = wrappedClient.Patch(ctx, dummyObj, nil) |
| 41 | + _ = wrappedClient.Status().Create(ctx, dummyObj, dummyObj) |
| 42 | + _ = wrappedClient.Status().Update(ctx, dummyObj) |
| 43 | + _ = wrappedClient.Status().Patch(ctx, dummyObj, nil) |
| 44 | + _ = wrappedClient.SubResource("some-subresource").Create(ctx, dummyObj, dummyObj) |
| 45 | + _ = wrappedClient.SubResource("some-subresource").Update(ctx, dummyObj) |
| 46 | + _ = wrappedClient.SubResource("some-subresource").Patch(ctx, dummyObj, nil) |
| 47 | + |
| 48 | + if expectedCalls := 9; calls != expectedCalls { |
| 49 | + t.Fatalf("wrong number of calls to assertions: expected=%d; got=%d", expectedCalls, calls) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestWithStrictFieldValidationOverridden(t *testing.T) { |
| 54 | + calls := 0 |
| 55 | + |
| 56 | + fakeClient := testFieldValidationClient(t, metav1.FieldValidationWarn, func() { calls++ }) |
| 57 | + wrappedClient := client.WithFieldValidation(fakeClient, metav1.FieldValidationStrict) |
| 58 | + |
| 59 | + ctx := context.Background() |
| 60 | + dummyObj := &corev1.Namespace{} |
| 61 | + |
| 62 | + _ = wrappedClient.Create(ctx, dummyObj, client.FieldValidation(metav1.FieldValidationWarn)) |
| 63 | + _ = wrappedClient.Update(ctx, dummyObj, client.FieldValidation(metav1.FieldValidationWarn)) |
| 64 | + _ = wrappedClient.Patch(ctx, dummyObj, nil, client.FieldValidation(metav1.FieldValidationWarn)) |
| 65 | + _ = wrappedClient.Status().Create(ctx, dummyObj, dummyObj, client.FieldValidation(metav1.FieldValidationWarn)) |
| 66 | + _ = wrappedClient.Status().Update(ctx, dummyObj, client.FieldValidation(metav1.FieldValidationWarn)) |
| 67 | + _ = wrappedClient.Status().Patch(ctx, dummyObj, nil, client.FieldValidation(metav1.FieldValidationWarn)) |
| 68 | + _ = wrappedClient.SubResource("some-subresource").Create(ctx, dummyObj, dummyObj, client.FieldValidation(metav1.FieldValidationWarn)) |
| 69 | + _ = wrappedClient.SubResource("some-subresource").Update(ctx, dummyObj, client.FieldValidation(metav1.FieldValidationWarn)) |
| 70 | + _ = wrappedClient.SubResource("some-subresource").Patch(ctx, dummyObj, nil, client.FieldValidation(metav1.FieldValidationWarn)) |
| 71 | + |
| 72 | + if expectedCalls := 9; calls != expectedCalls { |
| 73 | + t.Fatalf("wrong number of calls to assertions: expected=%d; got=%d", expectedCalls, calls) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// testFieldValidationClient is a helper function that checks if calls have the expected field validation, |
| 78 | +// and calls the callback function on each intercepted call. |
| 79 | +func testFieldValidationClient(t *testing.T, expectedFieldValidation string, callback func()) client.Client { |
| 80 | + // TODO: we could use the dummyClient in interceptor pkg if we move it to an internal pkg |
| 81 | + return fake.NewClientBuilder().WithInterceptorFuncs(interceptor.Funcs{ |
| 82 | + Create: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.CreateOption) error { |
| 83 | + callback() |
| 84 | + out := &client.CreateOptions{} |
| 85 | + for _, f := range opts { |
| 86 | + f.ApplyToCreate(out) |
| 87 | + } |
| 88 | + if got := out.FieldValidation; expectedFieldValidation != got { |
| 89 | + t.Fatalf("wrong field validation: expected=%q; got=%q", expectedFieldValidation, got) |
| 90 | + } |
| 91 | + return nil |
| 92 | + }, |
| 93 | + Update: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { |
| 94 | + callback() |
| 95 | + out := &client.UpdateOptions{} |
| 96 | + for _, f := range opts { |
| 97 | + f.ApplyToUpdate(out) |
| 98 | + } |
| 99 | + if got := out.FieldValidation; expectedFieldValidation != got { |
| 100 | + t.Fatalf("wrong field validation: expected=%q; got=%q", expectedFieldValidation, got) |
| 101 | + } |
| 102 | + return nil |
| 103 | + }, |
| 104 | + Patch: func(ctx context.Context, c client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { |
| 105 | + callback() |
| 106 | + out := &client.PatchOptions{} |
| 107 | + for _, f := range opts { |
| 108 | + f.ApplyToPatch(out) |
| 109 | + } |
| 110 | + if got := out.FieldValidation; expectedFieldValidation != got { |
| 111 | + t.Fatalf("wrong field validation: expected=%q; got=%q", expectedFieldValidation, got) |
| 112 | + } |
| 113 | + return nil |
| 114 | + }, |
| 115 | + SubResourceCreate: func(ctx context.Context, c client.Client, subResourceName string, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error { |
| 116 | + callback() |
| 117 | + out := &client.SubResourceCreateOptions{} |
| 118 | + for _, f := range opts { |
| 119 | + f.ApplyToSubResourceCreate(out) |
| 120 | + } |
| 121 | + if got := out.FieldValidation; expectedFieldValidation != got { |
| 122 | + t.Fatalf("wrong field validation: expected=%q; got=%q", expectedFieldValidation, got) |
| 123 | + } |
| 124 | + return nil |
| 125 | + }, |
| 126 | + SubResourceUpdate: func(ctx context.Context, c client.Client, subResourceName string, obj client.Object, opts ...client.SubResourceUpdateOption) error { |
| 127 | + callback() |
| 128 | + out := &client.SubResourceUpdateOptions{} |
| 129 | + for _, f := range opts { |
| 130 | + f.ApplyToSubResourceUpdate(out) |
| 131 | + } |
| 132 | + if got := out.FieldValidation; expectedFieldValidation != got { |
| 133 | + t.Fatalf("wrong field validation: expected=%q; got=%q", expectedFieldValidation, got) |
| 134 | + } |
| 135 | + return nil |
| 136 | + }, |
| 137 | + SubResourcePatch: func(ctx context.Context, c client.Client, subResourceName string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error { |
| 138 | + callback() |
| 139 | + out := &client.SubResourcePatchOptions{} |
| 140 | + for _, f := range opts { |
| 141 | + f.ApplyToSubResourcePatch(out) |
| 142 | + } |
| 143 | + if got := out.FieldValidation; expectedFieldValidation != got { |
| 144 | + t.Fatalf("wrong field validation: expected=%q; got=%q", expectedFieldValidation, got) |
| 145 | + } |
| 146 | + return nil |
| 147 | + }, |
| 148 | + }).Build() |
| 149 | +} |
0 commit comments