Skip to content
This repository was archived by the owner on May 30, 2025. It is now read-only.

Commit 542f994

Browse files
Add missing test UpdateResourceSpecSuccess and fix test UpdateResourceSpecFail
Signed-off-by: danielinclouds <[email protected]>
1 parent e143ffe commit 542f994

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

pkg/controller/dns/managed_zone_test.go

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2021 The Crossplane Authors.
2+
Copyright 2022 The Crossplane Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -43,6 +43,12 @@ const (
4343
managedZoneProjectID = "myproject-id-1234"
4444
)
4545

46+
var (
47+
nonManagedZone resource.Managed
48+
errManagedZoneBoom = errors.New("boom")
49+
managedZoneLabels = map[string]string{"foo": "bar"}
50+
)
51+
4652
type ManagedZoneOption func(*v1alpha1.ManagedZone)
4753

4854
func newManagedZone(opts ...ManagedZoneOption) *v1alpha1.ManagedZone {
@@ -55,6 +61,12 @@ func newManagedZone(opts ...ManagedZoneOption) *v1alpha1.ManagedZone {
5561
return mz
5662
}
5763

64+
func withLabels(l map[string]string) ManagedZoneOption {
65+
return func(mz *v1alpha1.ManagedZone) {
66+
mz.Spec.ForProvider.Labels = l
67+
}
68+
}
69+
5870
func managedZoneGError(code int, message string) *googleapi.Error {
5971
return &googleapi.Error{
6072
Code: code,
@@ -82,7 +94,7 @@ func TestManagedZoneObserve(t *testing.T) {
8294
"NotManagedZone": {
8395
reason: "Should return an error if the resource is not ManagedZone",
8496
args: args{
85-
mg: unexpectedObject,
97+
mg: nonManagedZone,
8698
},
8799
want: want{
88100
e: managed.ExternalObservation{},
@@ -136,23 +148,53 @@ func TestManagedZoneObserve(t *testing.T) {
136148
},
137149
want: want{
138150
e: managed.ExternalObservation{},
139-
err: errors.Wrap(errBoom, errUpdateManagedZone),
151+
err: errors.Wrap(errManagedZoneBoom, errUpdateManagedZone),
140152
},
141153
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
142154
_ = r.Body.Close()
143155
if diff := cmp.Diff(http.MethodGet, r.Method); diff != "" {
144156
t.Errorf("r: -want, +got:\n%s", diff)
145157
}
146158
w.WriteHeader(http.StatusOK)
147-
cr := newManagedZone()
159+
cr := newManagedZone(withLabels(managedZoneLabels))
148160
mz := &dns.ManagedZone{}
149161
mzclient.GenerateManagedZone(meta.GetExternalName(cr), cr.Spec.ForProvider, mz)
150162
if err := json.NewEncoder(w).Encode(mz); err != nil {
151163
t.Error(err)
152164
}
153165
}),
154166
kube: &test.MockClient{
155-
MockUpdate: test.NewMockUpdateFn(errBoom),
167+
MockUpdate: test.NewMockUpdateFn(errManagedZoneBoom),
168+
},
169+
},
170+
"UpdateResourceSpecSuccess": {
171+
reason: "Should not return an error if the internal update succeeds",
172+
args: args{
173+
mg: newManagedZone(),
174+
},
175+
want: want{
176+
e: managed.ExternalObservation{
177+
ResourceLateInitialized: true,
178+
ResourceExists: true,
179+
ResourceUpToDate: true,
180+
},
181+
err: nil,
182+
},
183+
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
184+
_ = r.Body.Close()
185+
if diff := cmp.Diff(http.MethodGet, r.Method); diff != "" {
186+
t.Errorf("r: -want, +got:\n%s", diff)
187+
}
188+
w.WriteHeader(http.StatusOK)
189+
cr := newManagedZone(withLabels(managedZoneLabels))
190+
mz := &dns.ManagedZone{}
191+
mzclient.GenerateManagedZone(meta.GetExternalName(cr), cr.Spec.ForProvider, mz)
192+
if err := json.NewEncoder(w).Encode(mz); err != nil {
193+
t.Error(err)
194+
}
195+
}),
196+
kube: &test.MockClient{
197+
MockUpdate: test.NewMockUpdateFn(nil),
156198
},
157199
},
158200
"ResourceNotUpToDate": {
@@ -191,6 +233,7 @@ func TestManagedZoneObserve(t *testing.T) {
191233
dns: s.ManagedZones,
192234
}
193235
got, err := e.Observe(context.Background(), tc.args.mg)
236+
194237
if diff := cmp.Diff(tc.want.e, got); diff != "" {
195238
t.Errorf("Observe(...): -want, +got:\n%s", diff)
196239
}
@@ -219,7 +262,7 @@ func TestManagedZoneCreate(t *testing.T) {
219262
"NotManagedZone": {
220263
reason: "Should return an error if the resource is not ManagedZone",
221264
args: args{
222-
mg: unexpectedObject,
265+
mg: nonManagedZone,
223266
},
224267
want: want{
225268
e: managed.ExternalCreation{},
@@ -306,7 +349,7 @@ func TestManagedZoneUpdate(t *testing.T) {
306349
"NotManagedZone": {
307350
reason: "Should return an error if the resource is not ManagedZone",
308351
args: args{
309-
mg: unexpectedObject,
352+
mg: nonManagedZone,
310353
},
311354
want: want{
312355
e: managed.ExternalUpdate{},
@@ -392,7 +435,7 @@ func TestManagedZoneDelete(t *testing.T) {
392435
"NotManagedZone": {
393436
reason: "Should return an error if the resource is not ManagedZone",
394437
args: args{
395-
mg: unexpectedObject,
438+
mg: nonManagedZone,
396439
},
397440
want: want{
398441
err: errors.New(errNotManagedZone),

0 commit comments

Comments
 (0)