Skip to content

Commit dffad0d

Browse files
authored
test: use T.Setenv to set env vars in tests (#1179)
This commit replaces `os.Setenv` with `t.Setenv` in tests. The environment variable is automatically restored to its original value when the test and all its subtests complete. Reference: https://pkg.go.dev/testing#T.Setenv Signed-off-by: Eng Zer Jun <[email protected]> Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 52a08d2 commit dffad0d

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

cmd/readiness/readiness_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,31 +141,27 @@ func TestNoDeadlockForImmediateWaitRs(t *testing.T) {
141141
// (as Agent doesn't marks all the step statuses finished when it reaches the goal) but this doesn't affect the result
142142
// as the whole plan is complete already
143143
func TestHeadlessAgentHasntReachedGoal(t *testing.T) {
144-
_ = os.Setenv(headlessAgent, "true")
144+
t.Setenv(headlessAgent, "true")
145145
c := testConfig("testdata/health-status-ok.json")
146146
c.ClientSet = fake.NewSimpleClientset(testdata.TestPod(c.Namespace, c.Hostname), testdata.TestSecret(c.Namespace, c.AutomationConfigSecretName, 6))
147147
ready, err := isPodReady(c)
148148
assert.False(t, ready)
149149
assert.NoError(t, err)
150150
thePod, _ := c.ClientSet.CoreV1().Pods(c.Namespace).Get(context.TODO(), c.Hostname, metav1.GetOptions{})
151151
assert.Equal(t, map[string]string{"agent.mongodb.com/version": "5"}, thePod.Annotations)
152-
153-
os.Unsetenv(headlessAgent)
154152
}
155153

156154
// TestHeadlessAgentReachedGoal verifies that the probe reports "true" if the config version is equal to the
157155
// last achieved version of the Agent
158156
func TestHeadlessAgentReachedGoal(t *testing.T) {
159-
_ = os.Setenv(headlessAgent, "true")
157+
t.Setenv(headlessAgent, "true")
160158
c := testConfig("testdata/health-status-ok.json")
161159
c.ClientSet = fake.NewSimpleClientset(testdata.TestPod(c.Namespace, c.Hostname), testdata.TestSecret(c.Namespace, c.AutomationConfigSecretName, 5))
162160
ready, err := isPodReady(c)
163161
assert.True(t, ready)
164162
assert.NoError(t, err)
165163
thePod, _ := c.ClientSet.CoreV1().Pods(c.Namespace).Get(context.TODO(), c.Hostname, metav1.GetOptions{})
166164
assert.Equal(t, map[string]string{"agent.mongodb.com/version": "5"}, thePod.Annotations)
167-
168-
os.Unsetenv(headlessAgent)
169165
}
170166

171167
func TestPodReadiness(t *testing.T) {

controllers/construct/build_statefulset_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package construct
22

33
import (
4-
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/podtemplatespec"
5-
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar"
64
"os"
75
"reflect"
86
"testing"
97

108
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/container"
9+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/podtemplatespec"
1110
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/resourcerequirements"
11+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar"
1212

1313
corev1 "k8s.io/api/core/v1"
1414

@@ -40,9 +40,9 @@ func newTestReplicaSet() mdbv1.MongoDBCommunity {
4040
}
4141

4242
func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
43-
_ = os.Setenv(MongodbRepoUrl, "repo")
44-
_ = os.Setenv(MongodbImageEnv, "mongo")
45-
_ = os.Setenv(AgentImageEnv, "agent-image")
43+
t.Setenv(MongodbRepoUrl, "repo")
44+
t.Setenv(MongodbImageEnv, "mongo")
45+
t.Setenv(AgentImageEnv, "agent-image")
4646

4747
mdb := newTestReplicaSet()
4848
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, mdb)
@@ -63,10 +63,10 @@ func TestMultipleCalls_DoNotCauseSideEffects(t *testing.T) {
6363
}
6464

6565
func TestManagedSecurityContext(t *testing.T) {
66-
_ = os.Setenv(MongodbRepoUrl, "repo")
67-
_ = os.Setenv(MongodbImageEnv, "mongo")
68-
_ = os.Setenv(AgentImageEnv, "agent-image")
69-
_ = os.Setenv(podtemplatespec.ManagedSecurityContextEnv, "true")
66+
t.Setenv(MongodbRepoUrl, "repo")
67+
t.Setenv(MongodbImageEnv, "mongo")
68+
t.Setenv(AgentImageEnv, "agent-image")
69+
t.Setenv(podtemplatespec.ManagedSecurityContextEnv, "true")
7070

7171
mdb := newTestReplicaSet()
7272
stsFunc := BuildMongoDBReplicaSetStatefulSetModificationFunction(&mdb, mdb)

controllers/replicaset_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func TestKubernetesResources_AreCreated(t *testing.T) {
142142
}
143143

144144
func TestStatefulSet_IsCorrectlyConfigured(t *testing.T) {
145-
_ = os.Setenv(construct.MongodbRepoUrl, "repo")
146-
_ = os.Setenv(construct.MongodbImageEnv, "mongo")
145+
t.Setenv(construct.MongodbRepoUrl, "repo")
146+
t.Setenv(construct.MongodbImageEnv, "mongo")
147147

148148
mdb := newTestReplicaSet()
149149
mgr := client.NewManager(&mdb)

pkg/util/envvar/envvars_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package envvar
22

33
import (
4-
"os"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
87
)
98

109
func TestGetEnvOrDefault(t *testing.T) {
11-
12-
err := os.Setenv("env1", "val1")
13-
assert.NoError(t, err)
10+
t.Setenv("env1", "val1")
1411

1512
val := GetEnvOrDefault("env1", "defaultVal1")
1613
assert.Equal(t, "val1", val)

0 commit comments

Comments
 (0)