@@ -2,6 +2,7 @@ package k8s
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"testing"
6
7
7
8
"github.com/kyma-project/nats-manager/testutils"
@@ -18,6 +19,8 @@ import (
18
19
19
20
const testFieldManager = "nats-manager"
20
21
22
+ var errPatchNotAllowed = errors .New ("apply patches are not supported in the fake client" )
23
+
21
24
func Test_GetStatefulSet (t * testing.T ) {
22
25
t .Parallel ()
23
26
@@ -180,20 +183,11 @@ func Test_Delete(t *testing.T) {
180
183
func Test_PatchApply (t * testing.T ) {
181
184
t .Parallel ()
182
185
183
- // NOTE: In real k8s client, the kubeClient.PatchApply creates the resource
184
- // if it does not exist on the cluster. But in the fake client the behaviour
185
- // is not properly replicated. As mentioned: "ObjectMeta's `Generation` and
186
- // `ResourceVersion` don't behave properly, Patch or Update operations that
187
- // rely on these fields will fail, or give false positives." in docs
188
- // https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client/fake
189
- // This scenario will be tested in integration tests with envTest pkg.
190
-
191
186
// define test cases
192
187
testCases := []struct {
193
188
name string
194
189
givenStatefulSet * unstructured.Unstructured
195
190
givenUpdateStatefulSet * unstructured.Unstructured
196
- wantReplicas int
197
191
}{
198
192
{
199
193
name : "should update resource when exists in k8s" ,
@@ -203,7 +197,6 @@ func Test_PatchApply(t *testing.T) {
203
197
givenUpdateStatefulSet : testutils .NewNATSStatefulSetUnStruct (
204
198
testutils .WithSpecReplicas (3 ),
205
199
),
206
- wantReplicas : 3 ,
207
200
},
208
201
}
209
202
@@ -226,14 +219,12 @@ func Test_PatchApply(t *testing.T) {
226
219
err := kubeClient .PatchApply (context .Background (), tc .givenUpdateStatefulSet )
227
220
228
221
// then
229
- require .NoError (t , err )
230
- // check that it should exist on k8s.
231
- gotSTS , err := kubeClient .GetStatefulSet (context .Background (),
232
- tc .givenStatefulSet .GetName (), tc .givenStatefulSet .GetNamespace ())
233
- require .NoError (t , err )
234
- require .Equal (t , tc .givenUpdateStatefulSet .GetName (), gotSTS .Name )
235
- require .Equal (t , tc .givenUpdateStatefulSet .GetNamespace (), gotSTS .Namespace )
236
- require .Equal (t , int32 (tc .wantReplicas ), * gotSTS .Spec .Replicas )
222
+ // NOTE: The kubeClient.PatchApply is not supported in the fake client.
223
+ // (https://github.com/kubernetes/kubernetes/issues/115598)
224
+ // So in unit test we only check that the client.Patch with client.Apply
225
+ // is called or not.
226
+ // The real behaviour will be tested in integration tests with envTest pkg.
227
+ require .ErrorContains (t , err , errPatchNotAllowed .Error ())
237
228
})
238
229
}
239
230
}
0 commit comments