Skip to content

Commit

Permalink
tests: updated TestUpdateStatefulSet
Browse files Browse the repository at this point in the history
Signed-off-by: Mahesh Kumar <[email protected]>
  • Loading branch information
k3mahesh committed Apr 9, 2024
1 parent ea08d1c commit bb3b3c3
Showing 1 changed file with 84 additions and 52 deletions.
136 changes: 84 additions & 52 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 Down Expand Up @@ -284,82 +285,113 @@ func Test_createStatefulSet(t *testing.T) {
}
}

func TestUpdateStatefulSet(t *testing.T) {
func ptrInt32(i int32) *int32 {
return &i
}

func TestUpdateStatefulSet(t *testing.T) {
tests := []struct {
name string
sts appsv1.StatefulSet
recreatests bool
updated bool
name string
existingStsSpec appsv1.StatefulSetSpec
updatedStsSpec appsv1.StatefulSetSpec
recreateSts bool
stsPresent bool
}{
{
name: "StatefulSet Recreated and Updated",
sts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
},
name: "Update StatefulSet without recreate in existing Statefulset",
existingStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(3),
},
updatedStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(5),
},
recreatests: true,
updated: true,
recreateSts: false,
stsPresent: true,
},
{
name: "StatefulSet Updated",
sts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
},
name: "Update StatefulSet with recreate in existing Statefulset",
existingStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(2),
},
updatedStsSpec: appsv1.StatefulSetSpec{
Replicas: ptrInt32(4),
},
recreatests: false,
updated: true,
recreateSts: true,
stsPresent: true,
},
{
name: "StatefulSet Updated",
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: true,
updated: false,
recreateSts: false,
stsPresent: false,
},
{
name: "StatefulSet Updated",
sts: appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sts",
Namespace: "test-ns",
name: "Update StatefulSet without recreate StatefulSet",
existingStsSpec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"name": "redis",
},
},
},
},
recreatests: false,
updated: false,
updatedStsSpec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"name": "redis-standalone",
},
},
},
},
recreateSts: false,
stsPresent: true,
},
}

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.updated {
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.sts, test.recreatests, client)
if test.updated {
if test.recreatests {
assert.Nil(t, err)
} else {
assert.Nil(t, err)
}
if test.stsPresent {
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 {
if test.recreatests {
assert.NotNil(t, err)
} else {
assert.NotNil(t, err)
}
client = k8sClientFake.NewSimpleClientset()

Check failure on line 394 in k8sutils/statefulset_test.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `client` is never used (staticcheck)
}
})
}
Expand Down

0 comments on commit bb3b3c3

Please sign in to comment.