Skip to content

Commit 8a6e8b7

Browse files
committed
fix: working unit tests
1 parent 914482f commit 8a6e8b7

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

internal/controller/serviceinstance/controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,11 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
273273
return managed.ExternalUpdate{}, errors.Wrap(err, errUpdate)
274274
}
275275

276-
if err := c.kube.Update(ctx, cr); err != nil {
277-
return managed.ExternalUpdate{}, errors.Wrap(err, errUpdateCR)
276+
if creds != nil {
277+
cr.Status.AtProvider.Credentials = iSha256(creds)
278+
if err := c.kube.Status().Update(ctx, cr); err != nil {
279+
return managed.ExternalUpdate{}, errors.Wrap(err, errUpdateCR)
280+
}
278281
}
279282

280283
return managed.ExternalUpdate{}, nil
@@ -408,6 +411,9 @@ func (s servicePlanInitializer) Initialize(ctx context.Context, mg resource.Mana
408411
// info: if creds == nil, it will result in a hash value anyway (e3b0c44298...).
409412
// This should not be a security problem.
410413
func iSha256(data []byte) []byte {
414+
if len(data) == 0 || string(data) == "{}" {
415+
return nil
416+
}
411417
s := sha256.Sum256(data)
412418
return s[:]
413419
}

internal/controller/serviceinstance/controller_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func TestObserve(t *testing.T) {
386386
mg: serviceInstance("managed",
387387
withExternalName(guid),
388388
withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}),
389-
withStatus(v1alpha1.ServiceInstanceObservation{ID: &guid, ServicePlan: &servicePlan}),
389+
withStatus(v1alpha1.ServiceInstanceObservation{ID: &guid, ServicePlan: &servicePlan, Credentials: iSha256(*fake.JSONRawMessage("{\"foo\":\"bar\"}"))}),
390390
withConditions(xpv1.Available()),
391391
withParameters("{\"foo\":\"bar\", \"baz\": 1}"),
392392
withDriftDetection(true),
@@ -413,13 +413,13 @@ func TestObserve(t *testing.T) {
413413
},
414414
"DriftDetectionBreak": {
415415
args: args{
416-
mg: serviceInstance("managed", withExternalName(guid), withSpace(spaceGUID), withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}), withParameters("{\"foo\":\"bar\", \"baz\": 1}"), withDriftDetection(false)),
416+
mg: serviceInstance("managed", withExternalName(guid), withSpace(spaceGUID), withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}), withParameters("{\"foo\":\"bar\", \"baz\": 1}"), withDriftDetection(false), withStatus(v1alpha1.ServiceInstanceObservation{Credentials: iSha256([]byte("{\"foo\":\"bar\", \"baz\": 1}"))})),
417417
},
418418
want: want{
419419
mg: serviceInstance("managed",
420420
withExternalName(guid),
421421
withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}),
422-
withStatus(v1alpha1.ServiceInstanceObservation{ID: &guid, ServicePlan: &servicePlan}),
422+
withStatus(v1alpha1.ServiceInstanceObservation{ID: &guid, ServicePlan: &servicePlan, Credentials: iSha256([]byte("{\"foo\":\"bar\", \"baz\": 1}"))}),
423423
withConditions(xpv1.Available()),
424424
withParameters("{\"foo\":\"bar\", \"baz\": 1}"),
425425
withDriftDetection(false),
@@ -532,7 +532,7 @@ func TestCreate(t *testing.T) {
532532
mg: serviceInstance("managed", withSpace(spaceGUID), withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}), withCredentials(&jsonCredentials)),
533533
},
534534
want: want{
535-
mg: serviceInstance("managed", withSpace(spaceGUID), withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}), withCredentials(&jsonCredentials), withConditions(xpv1.Creating()), withExternalName(guid), withStatus(v1alpha1.ServiceInstanceObservation{Credentials: []byte(jsonCredentials)})),
535+
mg: serviceInstance("managed", withSpace(spaceGUID), withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}), withCredentials(&jsonCredentials), withConditions(xpv1.Creating()), withExternalName(guid), withStatus(v1alpha1.ServiceInstanceObservation{Credentials: iSha256([]byte(jsonCredentials))})),
536536
obs: managed.ExternalCreation{},
537537
err: nil,
538538
},
@@ -713,7 +713,7 @@ func TestUpdate(t *testing.T) {
713713
mg: serviceInstance("managed", withSpace(spaceGUID), withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}), withExternalName(guid), withStatus(v1alpha1.ServiceInstanceObservation{ID: &guid}), withCredentials(&jsonCredentials)),
714714
},
715715
want: want{
716-
mg: serviceInstance("managed", withSpace(spaceGUID), withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}), withCredentials(&jsonCredentials), withExternalName(guid), withStatus(v1alpha1.ServiceInstanceObservation{ID: &guid, Credentials: *fake.JSONRawMessage(jsonCredentials)})),
716+
mg: serviceInstance("managed", withSpace(spaceGUID), withServicePlan(v1alpha1.ServicePlanParameters{ID: &servicePlan}), withCredentials(&jsonCredentials), withExternalName(guid), withStatus(v1alpha1.ServiceInstanceObservation{ID: &guid, Credentials: iSha256([]byte(jsonCredentials))})),
717717
obs: managed.ExternalUpdate{},
718718
err: nil,
719719
},
@@ -894,7 +894,7 @@ func TestJSONContain(t *testing.T) {
894894
"Superset": {
895895
args: args{
896896
a: `{ "bar": 1, "baz": "baz", "foo":"foo"}`,
897-
b: `{"foo":"foo",
897+
b: `{"foo":"foo",
898898
"bar": 1}`,
899899
},
900900
want: want{

0 commit comments

Comments
 (0)