Skip to content

Commit

Permalink
tests: use fake objects provided by the fake client
Browse files Browse the repository at this point in the history
The fake client stores the objects after adding a resource version to
them. This is a breaking change introduced in
kubernetes-sigs/controller-runtime#1306.
Therefore we cannot use the fake object that we provided as input to the
the fake client and should use the object obtained from the Get
operation.

The helper function returns the objects after obtaining them from the
fake client and therefore these objects cannot be used for create
functions.

Signed-off-by: Raghavendra Talur <raghavendra.talur@gmail.com>
  • Loading branch information
raghavendra-talur committed Apr 19, 2021
1 parent dee571f commit d9a5495
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions controllers/ocsinitialization/ocsinitialization_controller_test.go
Expand Up @@ -50,14 +50,17 @@ func getTestParams(mockNamespace bool, t *testing.T) (v1.OCSInitialization, reco
Namespace: request.Namespace,
},
}
ocsRecon := v1.OCSInitialization{
ObjectMeta: metav1.ObjectMeta{
Name: request.Name,
Namespace: request.Namespace,
},
}

return ocs, request, getReconciler(t, &ocsRecon)
reconciler := getReconciler(t, &ocs)
//The fake client stores the objects after adding a resource version to
//them. This is a breaking change introduced in
//https://github.com/kubernetes-sigs/controller-runtime/pull/1306.
//Therefore we cannot use the fake object that we provided as input to the
//the fake client and should use the object obtained from the Get
//operation.
_ = reconciler.Client.Get(context.TODO(), request.NamespacedName, &ocs)

return ocs, request, reconciler
}

func getReconciler(t *testing.T, objs ...client.Object) OCSInitializationReconciler {
Expand Down Expand Up @@ -158,11 +161,19 @@ func TestNonWatchedResourceFound(t *testing.T) {
}

for _, tc := range testcases {
ocs, request, reconciler := getTestParams(true, t)
request.Name = tc.name
request.Namespace = tc.namespace
ocs.Name = tc.name
ocs.Namespace = tc.namespace
_, _, reconciler := getTestParams(true, t)
request := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: tc.name,
Namespace: tc.namespace,
},
}
ocs := v1.OCSInitialization{
ObjectMeta: metav1.ObjectMeta{
Name: tc.name,
Namespace: tc.namespace,
},
}
err := reconciler.Client.Create(nil, &ocs)
assert.NoErrorf(t, err, "[%s]: failed CREATE of non watched resource", tc.label)
_, err = reconciler.Reconcile(context.TODO(), request)
Expand Down Expand Up @@ -230,9 +241,8 @@ func TestCreateSCCs(t *testing.T) {

if tc.sscCreated {
ocs.Status.SCCsCreated = false
// TODO: uncomment this!
//err := reconciler.Client.Update(context.TODO(), &ocs)
//assert.NoErrorf(t, err, "[%s]: failed to update ocsInit status", tc.label)
err := reconciler.Client.Update(context.TODO(), &ocs)
assert.NoErrorf(t, err, "[%s]: failed to update ocsInit status", tc.label)
}

_, err := reconciler.Reconcile(context.TODO(), request)
Expand Down

0 comments on commit d9a5495

Please sign in to comment.