|
| 1 | +package mongodb |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/probes" |
| 9 | + |
| 10 | + mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + appsv1 "k8s.io/api/apps/v1" |
| 14 | +) |
| 15 | + |
| 16 | +func init() { |
| 17 | + |
| 18 | + os.Setenv(preStopHookImageEnv, "pre-stop-hook-image") |
| 19 | +} |
| 20 | + |
| 21 | +func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) { |
| 22 | + mdb := newTestReplicaSet() |
| 23 | + stsFunc := buildStatefulSetModificationFunction(mdb) |
| 24 | + sts := &appsv1.StatefulSet{} |
| 25 | + |
| 26 | + t.Run("1st Call", func(t *testing.T) { |
| 27 | + stsFunc(sts) |
| 28 | + assertStatefulSetIsBuiltCorrectly(t, mdb, sts) |
| 29 | + }) |
| 30 | + t.Run("2nd Call", func(t *testing.T) { |
| 31 | + stsFunc(sts) |
| 32 | + assertStatefulSetIsBuiltCorrectly(t, mdb, sts) |
| 33 | + }) |
| 34 | + t.Run("3rd Call", func(t *testing.T) { |
| 35 | + stsFunc(sts) |
| 36 | + assertStatefulSetIsBuiltCorrectly(t, mdb, sts) |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +func assertStatefulSetIsBuiltCorrectly(t *testing.T, mdb mdbv1.MongoDB, sts *appsv1.StatefulSet) { |
| 41 | + assert.Len(t, sts.Spec.Template.Spec.Containers, 2) |
| 42 | + assert.Len(t, sts.Spec.Template.Spec.InitContainers, 1) |
| 43 | + assert.Equal(t, mdb.ServiceName(), sts.Spec.ServiceName) |
| 44 | + assert.Equal(t, mdb.Name, sts.Name) |
| 45 | + assert.Equal(t, mdb.Namespace, sts.Namespace) |
| 46 | + assert.Equal(t, appsv1.RollingUpdateStatefulSetStrategyType, sts.Spec.UpdateStrategy.Type) |
| 47 | + assert.Equal(t, operatorServiceAccountName, sts.Spec.Template.Spec.ServiceAccountName) |
| 48 | + assert.Len(t, sts.Spec.Template.Spec.Containers[0].Env, 1) |
| 49 | + assert.Len(t, sts.Spec.Template.Spec.Containers[1].Env, 2) |
| 50 | + |
| 51 | + agentContainer := sts.Spec.Template.Spec.Containers[0] |
| 52 | + assert.Equal(t, "agent-image", agentContainer.Image) |
| 53 | + probe := agentContainer.ReadinessProbe |
| 54 | + assert.True(t, reflect.DeepEqual(probes.New(defaultReadiness()), *probe)) |
| 55 | + assert.Equal(t, int32(240), probe.FailureThreshold) |
| 56 | + assert.Equal(t, int32(5), probe.InitialDelaySeconds) |
| 57 | + assert.Len(t, agentContainer.VolumeMounts, 3) |
| 58 | + |
| 59 | + mongodContainer := sts.Spec.Template.Spec.Containers[1] |
| 60 | + assert.Equal(t, "mongo:4.2.2", mongodContainer.Image) |
| 61 | + assert.NotNil(t, sts.Spec.Template.Spec.Containers[0].ReadinessProbe) |
| 62 | + assert.Len(t, mongodContainer.VolumeMounts, 3) |
| 63 | + |
| 64 | + initContainer := sts.Spec.Template.Spec.InitContainers[0] |
| 65 | + assert.Equal(t, preStopHookName, initContainer.Name) |
| 66 | + assert.Equal(t, "pre-stop-hook-image", initContainer.Image) |
| 67 | + assert.Len(t, initContainer.VolumeMounts, 1) |
| 68 | +} |
0 commit comments