Skip to content

Commit

Permalink
test: TestUpdateStatefulSet updated
Browse files Browse the repository at this point in the history
Signed-off-by: Mahesh Kumar <k.mahesh.9779@gmail.com>
  • Loading branch information
k3mahesh committed Apr 8, 2024
1 parent 0d4315c commit 928e521
Showing 1 changed file with 87 additions and 51 deletions.
138 changes: 87 additions & 51 deletions k8sutils/statefulset_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package k8sutils

import (
"context"
"path"
"testing"

Expand All @@ -10,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sClientFake "k8s.io/client-go/kubernetes/fake"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -284,84 +286,118 @@ func Test_createStatefulSet(t *testing.T) {
}
}

func ptrInt32(i int32) *int32 {
return &i
}

func TestUpdateStatefulSet(t *testing.T) {
tests := []struct {
name string
sts appsv1.StatefulSet
updatedSts appsv1.StatefulSet
recreateSts bool
stsPresent bool
name string
existingStsSpec appsv1.StatefulSetSpec
updatedStsSpec appsv1.StatefulSetSpec
recreateSts bool
stsPresent bool
errorExpected bool
}{
{
name: "Update StatefulSet without recreate in existing Statefulset",
sts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
},
existingStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(3),
},
updatedSts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
},
updatedStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(5),
},
recreateSts: false,
stsPresent: true,
recreateSts: false,
stsPresent: true,
errorExpected: false,
},
{
name: "Update StatefulSet with recreate in existing Statefulset",
sts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
},
existingStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(2),
},
updatedSts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
},
updatedStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(4),
},
recreateSts: true,
stsPresent: true,
recreateSts: true,
stsPresent: true,
errorExpected: false,
},
{
name: "Update StatefulSet, StatefulSet is not present",
sts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
name: "Update StatefulSet without recreate StatefulSet is not present",
existingStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(2),
},
updatedStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(4),
},
recreateSts: false,
stsPresent: false,
errorExpected: false,
},
{
name: "Update StatefulSet without recreate StatefulSet",
existingStsSpec: appsv1.StatefulSetSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"name": "redis",
},
},
},
},
updatedSts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
updatedStsSpec: appsv1.StatefulSetSpec{
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"name": "redis-standalone",
},
},
},
},
recreateSts: false,
stsPresent: false,
recreateSts: false,
stsPresent: true,
errorExpected: false,
},
}

assert := assert.New(t)

for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
var client *k8sClientFake.Clientset
if test.stsPresent {
client = k8sClientFake.NewSimpleClientset(test.sts.DeepCopy())
} else {
client = k8sClientFake.NewSimpleClientset()
existingSts := appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
},
Spec: test.existingStsSpec,
}
updatedSts := appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
},
Spec: test.updatedStsSpec,
}
err := updateStatefulSet(test.sts.Namespace, &test.updatedSts, test.recreateSts, client)
if test.stsPresent {
if test.recreateSts {
assert.Nil(t, err)
} else {
assert.Nil(t, err)
}
existingStsCopy := existingSts.DeepCopy()

client = k8sClientFake.NewSimpleClientset()
_, errExistingSts := client.AppsV1().StatefulSets(existingSts.Namespace).Create(context.TODO(), &existingSts, metav1.CreateOptions{})
assert.NoError(errExistingSts, "Error while creating Stateful Set")

errUpdatedSts := updateStatefulSet(updatedSts.Namespace, &updatedSts, test.recreateSts, client)
assert.NoError(errUpdatedSts, "Error while updating Statefulset")

getUpdatedSts, errGetUpdatedSts := client.AppsV1().StatefulSets(updatedSts.Namespace).Get(context.TODO(), updatedSts.Name, metav1.GetOptions{})
assert.NoError(errGetUpdatedSts, "Error getting Updted StatefulSet")

// assert.Equal(test.updatedStsSpec, getUpdatedSts.Spec, "StatefulSet spec Match")
assert.NotEqual(getUpdatedSts.Spec, existingStsCopy.Spec, "StatefulSet spec Mismatch")
} else {
assert.NotNil(t, err)
client = k8sClientFake.NewSimpleClientset()
}
})
}
Expand Down

0 comments on commit 928e521

Please sign in to comment.