Skip to content

Commit

Permalink
test: use T.Setenv to set env vars in tests
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Juneezee authored and tekton-robot committed Dec 15, 2022
1 parent b4a404d commit a1995aa
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 73 deletions.
7 changes: 1 addition & 6 deletions pkg/apis/config/feature_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package config_test

import (
"os"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -81,12 +80,8 @@ func TestGetFeatureFlagsConfigName(t *testing.T) {
expected: "feature-flags-test",
}} {
t.Run(tc.description, func(t *testing.T) {
original := os.Getenv("CONFIG_FEATURE_FLAGS_NAME")
defer t.Cleanup(func() {
os.Setenv("CONFIG_FEATURE_FLAGS_NAME", original)
})
if tc.featureFlagEnvValue != "" {
os.Setenv("CONFIG_FEATURE_FLAGS_NAME", tc.featureFlagEnvValue)
t.Setenv("CONFIG_FEATURE_FLAGS_NAME", tc.featureFlagEnvValue)
}
got := config.GetFeatureFlagsConfigName()
want := tc.expected
Expand Down
32 changes: 8 additions & 24 deletions pkg/interceptors/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,9 @@ func Test_SecretNotExist(t *testing.T) {
}

func createSecret(t *testing.T, noAfter time.Time) (v1.CoreV1Interface, []byte, []byte, error) {
if err := os.Setenv(interceptorTLSSvcKey, testsvc); err != nil {
return nil, []byte{}, []byte{}, err
}
if err := os.Setenv(interceptorTLSSecretKey, testsecrets); err != nil {
return nil, []byte{}, []byte{}, err
}
if err := os.Setenv("SYSTEM_NAMESPACE", testns); err != nil {
return nil, []byte{}, []byte{}, err
}
t.Setenv(interceptorTLSSvcKey, testsvc)
t.Setenv(interceptorTLSSecretKey, testsecrets)
t.Setenv("SYSTEM_NAMESPACE", testns)
namespace := system.Namespace()

logger := zaptest.NewLogger(t)
Expand Down Expand Up @@ -299,9 +293,7 @@ func Test_CreateAndValidateCerts(t *testing.T) {
logger := zaptest.NewLogger(t)
clientSet := fakekubeclient.Get(ctx).CoreV1()
tc := faketriggersclient.Get(ctx)
if err := os.Setenv(interceptorTLSSecretKey, testsecrets); err != nil {
t.Error(err)
}
t.Setenv(interceptorTLSSecretKey, testsecrets)

createSecretWithData(ctx, t, clientSet)

Expand Down Expand Up @@ -376,9 +368,7 @@ func Test_GetTLSData(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if err := os.Setenv(interceptorTLSSecretKey, tc.secretName); err != nil {
t.Error(err)
}
t.Setenv(interceptorTLSSecretKey, tc.secretName)
if err := clientSet.Secrets(namespace).Delete(ctx, tc.secretName, metav1.DeleteOptions{}); err != nil && !apiErrors.IsNotFound(err) {
t.Error(err)
}
Expand Down Expand Up @@ -411,9 +401,7 @@ func Test_UpdateCACertToClusterInterceptorCRD(t *testing.T) {
logger := zaptest.NewLogger(t)
secretInformer := fakesecretinformer.Get(ctx)
clientSet := fakekubeclient.Get(ctx).CoreV1()
if err := os.Setenv(interceptorTLSSecretKey, testsecrets); err != nil {
t.Error(err)
}
t.Setenv(interceptorTLSSecretKey, testsecrets)

s := createSecretWithData(ctx, t, clientSet)
if err := secretInformer.Informer().GetIndexer().Add(s); err != nil {
Expand All @@ -439,12 +427,8 @@ func Test_UpdateCACertToClusterInterceptorCRD(t *testing.T) {
}

func getCerts(ctx context.Context, t *testing.T) ([]byte, []byte, []byte, string) {
if err := os.Setenv(interceptorTLSSvcKey, testsvc); err != nil {
t.Error(err)
}
if err := os.Setenv("SYSTEM_NAMESPACE", testns); err != nil {
t.Error(err)
}
t.Setenv(interceptorTLSSvcKey, testsvc)
t.Setenv("SYSTEM_NAMESPACE", testns)
namespace := system.Namespace()

serverKey, serverCert, caCert, err := certresources.CreateCerts(ctx, os.Getenv(interceptorTLSSvcKey), namespace, time.Now().Add(second))
Expand Down
16 changes: 3 additions & 13 deletions pkg/reconciler/eventlistener/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package eventlistener
import (
"context"
"fmt"
"os"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -557,14 +556,8 @@ func withDeletionTimestamp(el *v1beta1.EventListener) {
}

func TestReconcile(t *testing.T) {
err := os.Setenv("METRICS_PROMETHEUS_PORT", "9000")
if err != nil {
t.Fatal(err)
}
err = os.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")
if err != nil {
t.Fatal(err)
}
t.Setenv("METRICS_PROMETHEUS_PORT", "9000")
t.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")

customPort := 80

Expand Down Expand Up @@ -1490,10 +1483,7 @@ func TestReconcile(t *testing.T) {
}

func TestReconcile_InvalidForCustomResource(t *testing.T) {
err := os.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")
if err != nil {
t.Fatal(err)
}
t.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")

elWithCustomResource := makeEL(withStatus, withKnativeStatus, func(el *v1beta1.EventListener) {
el.Spec.Resources.CustomResource = &v1beta1.CustomResource{
Expand Down
21 changes: 4 additions & 17 deletions pkg/reconciler/eventlistener/resources/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package resources

import (
"context"
"os"
"strconv"
"testing"

Expand All @@ -30,14 +29,8 @@ import (
)

func TestCustomObject(t *testing.T) {
err := os.Setenv("METRICS_PROMETHEUS_PORT", "9000")
if err != nil {
t.Fatal(err)
}
err = os.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")
if err != nil {
t.Fatal(err)
}
t.Setenv("METRICS_PROMETHEUS_PORT", "9000")
t.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")

config := *MakeConfig()
metadata := map[string]interface{}{
Expand Down Expand Up @@ -323,14 +316,8 @@ func TestCustomObject(t *testing.T) {
}

func TestCustomObjectError(t *testing.T) {
err := os.Setenv("METRICS_PROMETHEUS_PORT", "9000")
if err != nil {
t.Fatal(err)
}
err = os.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")
if err != nil {
t.Fatal(err)
}
t.Setenv("METRICS_PROMETHEUS_PORT", "9000")
t.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")

config := *MakeConfig()

Expand Down
16 changes: 3 additions & 13 deletions pkg/reconciler/eventlistener/resources/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package resources

import (
"context"
"os"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -33,14 +32,8 @@ import (
)

func TestDeployment(t *testing.T) {
err := os.Setenv("METRICS_PROMETHEUS_PORT", "9000")
if err != nil {
t.Fatal(err)
}
err = os.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")
if err != nil {
t.Fatal(err)
}
t.Setenv("METRICS_PROMETHEUS_PORT", "9000")
t.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")

config := *MakeConfig()
labels := map[string]string{
Expand Down Expand Up @@ -304,10 +297,7 @@ func TestDeployment(t *testing.T) {
}

func TestDeploymentError(t *testing.T) {
err := os.Setenv("METRICS_PROMETHEUS_PORT", "bad")
if err != nil {
t.Fatal(err)
}
t.Setenv("METRICS_PROMETHEUS_PORT", "bad")
got, err := MakeDeployment(context.Background(), makeEL(), &reconcilersource.EmptyVarsGenerator{}, *MakeConfig())
if err == nil {
t.Fatalf("MakeDeployment() = %v, wanted error", got)
Expand Down

0 comments on commit a1995aa

Please sign in to comment.