Skip to content

Commit

Permalink
Add missing test UpdateResourceSpecSuccess and fix test UpdateResourc…
Browse files Browse the repository at this point in the history
…eSpecFail

Signed-off-by: danielinclouds <[email protected]>
  • Loading branch information
danielinclouds committed Sep 18, 2022
1 parent e143ffe commit 542f994
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions pkg/controller/dns/managed_zone_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Crossplane Authors.
Copyright 2022 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,6 +43,12 @@ const (
managedZoneProjectID = "myproject-id-1234"
)

var (
nonManagedZone resource.Managed
errManagedZoneBoom = errors.New("boom")
managedZoneLabels = map[string]string{"foo": "bar"}
)

type ManagedZoneOption func(*v1alpha1.ManagedZone)

func newManagedZone(opts ...ManagedZoneOption) *v1alpha1.ManagedZone {
Expand All @@ -55,6 +61,12 @@ func newManagedZone(opts ...ManagedZoneOption) *v1alpha1.ManagedZone {
return mz
}

func withLabels(l map[string]string) ManagedZoneOption {
return func(mz *v1alpha1.ManagedZone) {
mz.Spec.ForProvider.Labels = l
}
}

func managedZoneGError(code int, message string) *googleapi.Error {
return &googleapi.Error{
Code: code,
Expand Down Expand Up @@ -82,7 +94,7 @@ func TestManagedZoneObserve(t *testing.T) {
"NotManagedZone": {
reason: "Should return an error if the resource is not ManagedZone",
args: args{
mg: unexpectedObject,
mg: nonManagedZone,
},
want: want{
e: managed.ExternalObservation{},
Expand Down Expand Up @@ -136,23 +148,53 @@ func TestManagedZoneObserve(t *testing.T) {
},
want: want{
e: managed.ExternalObservation{},
err: errors.Wrap(errBoom, errUpdateManagedZone),
err: errors.Wrap(errManagedZoneBoom, errUpdateManagedZone),
},
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = r.Body.Close()
if diff := cmp.Diff(http.MethodGet, r.Method); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
w.WriteHeader(http.StatusOK)
cr := newManagedZone()
cr := newManagedZone(withLabels(managedZoneLabels))
mz := &dns.ManagedZone{}
mzclient.GenerateManagedZone(meta.GetExternalName(cr), cr.Spec.ForProvider, mz)
if err := json.NewEncoder(w).Encode(mz); err != nil {
t.Error(err)
}
}),
kube: &test.MockClient{
MockUpdate: test.NewMockUpdateFn(errBoom),
MockUpdate: test.NewMockUpdateFn(errManagedZoneBoom),
},
},
"UpdateResourceSpecSuccess": {
reason: "Should not return an error if the internal update succeeds",
args: args{
mg: newManagedZone(),
},
want: want{
e: managed.ExternalObservation{
ResourceLateInitialized: true,
ResourceExists: true,
ResourceUpToDate: true,
},
err: nil,
},
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = r.Body.Close()
if diff := cmp.Diff(http.MethodGet, r.Method); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
w.WriteHeader(http.StatusOK)
cr := newManagedZone(withLabels(managedZoneLabels))
mz := &dns.ManagedZone{}
mzclient.GenerateManagedZone(meta.GetExternalName(cr), cr.Spec.ForProvider, mz)
if err := json.NewEncoder(w).Encode(mz); err != nil {
t.Error(err)
}
}),
kube: &test.MockClient{
MockUpdate: test.NewMockUpdateFn(nil),
},
},
"ResourceNotUpToDate": {
Expand Down Expand Up @@ -191,6 +233,7 @@ func TestManagedZoneObserve(t *testing.T) {
dns: s.ManagedZones,
}
got, err := e.Observe(context.Background(), tc.args.mg)

if diff := cmp.Diff(tc.want.e, got); diff != "" {
t.Errorf("Observe(...): -want, +got:\n%s", diff)
}
Expand Down Expand Up @@ -219,7 +262,7 @@ func TestManagedZoneCreate(t *testing.T) {
"NotManagedZone": {
reason: "Should return an error if the resource is not ManagedZone",
args: args{
mg: unexpectedObject,
mg: nonManagedZone,
},
want: want{
e: managed.ExternalCreation{},
Expand Down Expand Up @@ -306,7 +349,7 @@ func TestManagedZoneUpdate(t *testing.T) {
"NotManagedZone": {
reason: "Should return an error if the resource is not ManagedZone",
args: args{
mg: unexpectedObject,
mg: nonManagedZone,
},
want: want{
e: managed.ExternalUpdate{},
Expand Down Expand Up @@ -392,7 +435,7 @@ func TestManagedZoneDelete(t *testing.T) {
"NotManagedZone": {
reason: "Should return an error if the resource is not ManagedZone",
args: args{
mg: unexpectedObject,
mg: nonManagedZone,
},
want: want{
err: errors.New(errNotManagedZone),
Expand Down

0 comments on commit 542f994

Please sign in to comment.