@@ -4,15 +4,13 @@ package tenantsecret
44
55import (
66 "context"
7- "errors"
87 "reflect"
98 "testing"
109
1110 corev1 "k8s.io/api/core/v1"
1211 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1312 "k8s.io/apimachinery/pkg/runtime"
1413 "k8s.io/apimachinery/pkg/runtime/schema"
15- "k8s.io/apimachinery/pkg/types"
1614 "k8s.io/apiserver/pkg/endpoints/request"
1715 "k8s.io/apiserver/pkg/registry/rest"
1816 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -46,11 +44,6 @@ func tenantSecretInput(name string) *corev1alpha1.TenantSecret {
4644 return & corev1alpha1.TenantSecret {ObjectMeta : metav1.ObjectMeta {Name : name , Namespace : testNamespace }}
4745}
4846
49- func capturePatchOptions (opts ... client.PatchOption ) * metav1.PatchOptions {
50- converted := (& client.PatchOptions {}).ApplyOptions (opts )
51- return converted .AsPatchOptions ().DeepCopy ()
52- }
53-
5447func captureTenantCreateOptions (opts ... client.CreateOption ) * metav1.CreateOptions {
5548 converted := (& client.CreateOptions {}).ApplyOptions (opts )
5649 return converted .AsCreateOptions ().DeepCopy ()
@@ -71,6 +64,7 @@ func captureTenantDeleteOptions(opts ...client.DeleteOption) *metav1.DeleteOptio
7164
7265func TestTenantSecretCreatePropagatesWriteOptions (t * testing.T ) {
7366 want := & metav1.CreateOptions {DryRun : []string {metav1 .DryRunAll }, FieldManager : "creator" , FieldValidation : "Strict" }
67+ expected := want .DeepCopy ()
7468 var got * metav1.CreateOptions
7569 r := newWriteOptionsREST (t , interceptor.Funcs {
7670 Create : func (_ context.Context , _ client.WithWatch , _ client.Object , opts ... client.CreateOption ) error {
@@ -81,13 +75,14 @@ func TestTenantSecretCreatePropagatesWriteOptions(t *testing.T) {
8175 if _ , err := r .Create (request .WithNamespace (context .Background (), testNamespace ), tenantSecretInput ("new" ), nil , want ); err != nil {
8276 t .Fatalf ("Create returned error: %v" , err )
8377 }
84- if ! reflect .DeepEqual (got , want ) {
85- t .Fatalf ("Create options lost: got %#v, want %#v" , got , want )
78+ if ! reflect .DeepEqual (got , expected ) {
79+ t .Fatalf ("Create options lost: got %#v, want %#v" , got , expected )
8680 }
8781}
8882
8983func TestTenantSecretUpdatePropagatesWriteOptions (t * testing.T ) {
9084 want := & metav1.UpdateOptions {DryRun : []string {metav1 .DryRunAll }, FieldManager : "updater" , FieldValidation : "Warn" }
85+ expected := want .DeepCopy ()
9186 var got * metav1.UpdateOptions
9287 r := newWriteOptionsREST (t , interceptor.Funcs {
9388 Update : func (_ context.Context , _ client.WithWatch , _ client.Object , opts ... client.UpdateOption ) error {
@@ -99,8 +94,8 @@ func TestTenantSecretUpdatePropagatesWriteOptions(t *testing.T) {
9994 if _ , _ , err := r .Update (ctx , "existing" , rest .DefaultUpdatedObjectInfo (tenantSecretInput ("existing" )), nil , nil , false , want ); err != nil {
10095 t .Fatalf ("Update returned error: %v" , err )
10196 }
102- if ! reflect .DeepEqual (got , want ) {
103- t .Fatalf ("Update options lost: got %#v, want %#v" , got , want )
97+ if ! reflect .DeepEqual (got , expected ) {
98+ t .Fatalf ("Update options lost: got %#v, want %#v" , got , expected )
10499 }
105100}
106101
@@ -123,67 +118,10 @@ func TestTenantSecretForceCreatePropagatesWriteOptions(t *testing.T) {
123118 }
124119}
125120
126- func TestTenantSecretPatchPropagatesWriteOptions (t * testing.T ) {
127- want := & metav1.PatchOptions {DryRun : []string {metav1 .DryRunAll }, FieldManager : "patcher" , FieldValidation : "Warn" }
128- var got * metav1.PatchOptions
129- r := newWriteOptionsREST (t , interceptor.Funcs {
130- Patch : func (ctx context.Context , c client.WithWatch , obj client.Object , patch client.Patch , opts ... client.PatchOption ) error {
131- got = capturePatchOptions (opts ... )
132- return c .Patch (ctx , obj , patch , opts ... )
133- },
134- }, makeTenantSecret ("existing" , nil ))
135- ctx := request .WithNamespace (context .Background (), testNamespace )
136- if _ , err := r .Patch (ctx , "existing" , types .MergePatchType , []byte (`{"metadata":{"annotations":{"test":"value"}}}` ), want ); err != nil {
137- t .Fatalf ("Patch returned error: %v" , err )
138- }
139- if ! reflect .DeepEqual (got , want ) {
140- t .Fatalf ("Patch options lost: got %#v, want %#v" , got , want )
141- }
142- }
143-
144- func TestTenantSecretPatchRepairPreservesDryRun (t * testing.T ) {
145- want := & metav1.PatchOptions {DryRun : []string {metav1 .DryRunAll }, FieldManager : "patcher" , FieldValidation : "Strict" }
146- var got * metav1.UpdateOptions
147- r := newWriteOptionsREST (t , interceptor.Funcs {
148- Patch : func (ctx context.Context , c client.WithWatch , obj client.Object , patch client.Patch , opts ... client.PatchOption ) error {
149- return c .Patch (ctx , obj , patch , opts ... )
150- },
151- Update : func (_ context.Context , _ client.WithWatch , _ client.Object , opts ... client.UpdateOption ) error {
152- got = captureTenantUpdateOptions (opts ... )
153- return nil
154- },
155- }, makeTenantSecret ("existing" , nil ))
156- data := []byte (`{"metadata":{"labels":{"internal.cozystack.io/tenantresource":null}}}` )
157- ctx := request .WithNamespace (context .Background (), testNamespace )
158- if _ , err := r .Patch (ctx , "existing" , types .MergePatchType , data , want ); err != nil {
159- t .Fatalf ("Patch returned error: %v" , err )
160- }
161- expected := & metav1.UpdateOptions {DryRun : want .DryRun , FieldManager : want .FieldManager , FieldValidation : want .FieldValidation }
162- if ! reflect .DeepEqual (got , expected ) {
163- t .Fatalf ("patch repair options lost: got %#v, want %#v" , got , expected )
164- }
165- }
166-
167- func TestTenantSecretPatchReturnsRepairFailure (t * testing.T ) {
168- sentinel := errors .New ("repair failed" )
169- r := newWriteOptionsREST (t , interceptor.Funcs {
170- Patch : func (ctx context.Context , c client.WithWatch , obj client.Object , patch client.Patch , opts ... client.PatchOption ) error {
171- return c .Patch (ctx , obj , patch , opts ... )
172- },
173- Update : func (_ context.Context , _ client.WithWatch , _ client.Object , _ ... client.UpdateOption ) error {
174- return sentinel
175- },
176- }, makeTenantSecret ("existing" , nil ))
177- data := []byte (`{"metadata":{"labels":{"internal.cozystack.io/tenantresource":null}}}` )
178- ctx := request .WithNamespace (context .Background (), testNamespace )
179- if _ , err := r .Patch (ctx , "existing" , types .MergePatchType , data , & metav1.PatchOptions {}); ! errors .Is (err , sentinel ) {
180- t .Fatalf ("Patch returned %v, want repair error %v" , err , sentinel )
181- }
182- }
183-
184121func TestTenantSecretDeletePropagatesWriteOptions (t * testing.T ) {
185122 propagation := metav1 .DeletePropagationForeground
186123 want := & metav1.DeleteOptions {PropagationPolicy : & propagation , DryRun : []string {metav1 .DryRunAll }}
124+ expected := want .DeepCopy ()
187125 var got * metav1.DeleteOptions
188126 r := newWriteOptionsREST (t , interceptor.Funcs {
189127 Delete : func (_ context.Context , _ client.WithWatch , _ client.Object , opts ... client.DeleteOption ) error {
@@ -195,7 +133,7 @@ func TestTenantSecretDeletePropagatesWriteOptions(t *testing.T) {
195133 if _ , _ , err := r .Delete (ctx , "existing" , nil , want ); err != nil {
196134 t .Fatalf ("Delete returned error: %v" , err )
197135 }
198- if ! reflect .DeepEqual (got , want ) {
199- t .Fatalf ("Delete options lost: got %#v, want %#v" , got , want )
136+ if ! reflect .DeepEqual (got , expected ) {
137+ t .Fatalf ("Delete options lost: got %#v, want %#v" , got , expected )
200138 }
201139}
0 commit comments