Skip to content

Commit ecbc288

Browse files
authored
Merge pull request #3171 from fabriziopandini/fix-test-NewFakeClientWithScheme
🌱 fix test/helpers.NewFakeClientWithScheme
2 parents b596268 + bfc6e20 commit ecbc288

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

test/helpers/client.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,25 @@ import (
2525
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2626
)
2727

28-
// NewFakeClientWithScheme creates a new fake client with the given scheme
29-
// for testing.
28+
// NewFakeClientWithScheme creates a new fake client with the given scheme for testing.
3029
// You can choose to initialize it with a slice of runtime.Object; all the objects with be given
31-
// a fake ResourceVersion="1" so it will be possible to use optimistic lock when patching conditions.
30+
// a fake ResourceVersion="1" so it will be possible to use optimistic lock.
3231
func NewFakeClientWithScheme(clientScheme *runtime.Scheme, initObjs ...runtime.Object) client.Client {
33-
for _, obj := range initObjs {
34-
accessor, err := meta.Accessor(obj)
32+
// NOTE: for consistency with the NewFakeClientWithScheme func in controller runtime, this func
33+
// should not have side effects on initObjs. So it creates a copy of each object and
34+
// set the resourceVersion on the copy only.
35+
initObjsWithResourceVersion := make([]runtime.Object, len(initObjs))
36+
for i := range initObjs {
37+
objsWithResourceVersion := initObjs[i].DeepCopyObject()
38+
accessor, err := meta.Accessor(objsWithResourceVersion)
3539
if err != nil {
3640
panic(fmt.Errorf("failed to get accessor for object: %v", err))
3741
}
3842

3943
if accessor.GetResourceVersion() == "" {
4044
accessor.SetResourceVersion("1")
4145
}
46+
initObjsWithResourceVersion[i] = objsWithResourceVersion
4247
}
43-
return fake.NewFakeClientWithScheme(clientScheme, initObjs...)
48+
return fake.NewFakeClientWithScheme(clientScheme, initObjsWithResourceVersion...)
4449
}

0 commit comments

Comments
 (0)