Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bumped controller-runtime and fixed tests #342

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
k8s.io/cli-runtime v0.29.2
k8s.io/client-go v0.29.2
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
sigs.k8s.io/controller-runtime v0.17.1
sigs.k8s.io/controller-runtime v0.17.2
sigs.k8s.io/yaml v1.4.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,8 @@ oras.land/oras-go v1.2.4/go.mod h1:DYcGfb3YF1nKjcezfX2SNlDAeQFKSXmf+qrFmrh4324=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/controller-runtime v0.17.1 h1:V1dQELMGVk46YVXXQUbTFujU7u4DQj6YUj9Rb6cuzz8=
sigs.k8s.io/controller-runtime v0.17.1/go.mod h1:+MngTvIQQQhfXtwfdGw/UOQ/aIaqsYywfCINOtwMO/s=
sigs.k8s.io/controller-runtime v0.17.2 h1:FwHwD1CTUemg0pW2otk7/U5/i5m2ymzvOXdbeGOUvw0=
sigs.k8s.io/controller-runtime v0.17.2/go.mod h1:+MngTvIQQQhfXtwfdGw/UOQ/aIaqsYywfCINOtwMO/s=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 h1:XX3Ajgzov2RKUdc5jW3t5jwY7Bo7dcRm+tFxT+NfgY0=
Expand Down
27 changes: 9 additions & 18 deletions pkg/k8s/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k8s

import (
"context"
"errors"
"testing"

"github.com/kyma-project/nats-manager/testutils"
Expand All @@ -18,6 +19,8 @@ import (

const testFieldManager = "nats-manager"

var errPatchNotAllowed = errors.New("apply patches are not supported in the fake client")

func Test_GetStatefulSet(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -180,20 +183,11 @@ func Test_Delete(t *testing.T) {
func Test_PatchApply(t *testing.T) {
t.Parallel()

// NOTE: In real k8s client, the kubeClient.PatchApply creates the resource
// if it does not exist on the cluster. But in the fake client the behaviour
// is not properly replicated. As mentioned: "ObjectMeta's `Generation` and
// `ResourceVersion` don't behave properly, Patch or Update operations that
// rely on these fields will fail, or give false positives." in docs
// https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client/fake
// This scenario will be tested in integration tests with envTest pkg.

// define test cases
testCases := []struct {
name string
givenStatefulSet *unstructured.Unstructured
givenUpdateStatefulSet *unstructured.Unstructured
wantReplicas int
}{
{
name: "should update resource when exists in k8s",
Expand All @@ -203,7 +197,6 @@ func Test_PatchApply(t *testing.T) {
givenUpdateStatefulSet: testutils.NewNATSStatefulSetUnStruct(
testutils.WithSpecReplicas(3),
),
wantReplicas: 3,
},
}

Expand All @@ -226,14 +219,12 @@ func Test_PatchApply(t *testing.T) {
err := kubeClient.PatchApply(context.Background(), tc.givenUpdateStatefulSet)

// then
require.NoError(t, err)
// check that it should exist on k8s.
gotSTS, err := kubeClient.GetStatefulSet(context.Background(),
tc.givenStatefulSet.GetName(), tc.givenStatefulSet.GetNamespace())
require.NoError(t, err)
require.Equal(t, tc.givenUpdateStatefulSet.GetName(), gotSTS.Name)
require.Equal(t, tc.givenUpdateStatefulSet.GetNamespace(), gotSTS.Namespace)
require.Equal(t, int32(tc.wantReplicas), *gotSTS.Spec.Replicas)
// NOTE: The kubeClient.PatchApply is not supported in the fake client.
// (https://github.com/kubernetes/kubernetes/issues/115598)
// So in unit test we only check that the client.Patch with client.Apply
// is called or not.
// The real behaviour will be tested in integration tests with envTest pkg.
require.ErrorContains(t, err, errPatchNotAllowed.Error())
})
}
}
Expand Down
Loading