diff --git a/pkg/apis/config/feature_flags_test.go b/pkg/apis/config/feature_flags_test.go index 7d83b9c40..abfad4b53 100644 --- a/pkg/apis/config/feature_flags_test.go +++ b/pkg/apis/config/feature_flags_test.go @@ -17,7 +17,6 @@ limitations under the License. package config_test import ( - "os" "testing" "github.com/google/go-cmp/cmp" @@ -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 diff --git a/pkg/interceptors/server/server_test.go b/pkg/interceptors/server/server_test.go index 16b3d252c..3cad62287 100644 --- a/pkg/interceptors/server/server_test.go +++ b/pkg/interceptors/server/server_test.go @@ -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) @@ -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) @@ -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) } @@ -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 { @@ -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)) diff --git a/pkg/reconciler/eventlistener/eventlistener_test.go b/pkg/reconciler/eventlistener/eventlistener_test.go index 6416215d9..e3c80700c 100644 --- a/pkg/reconciler/eventlistener/eventlistener_test.go +++ b/pkg/reconciler/eventlistener/eventlistener_test.go @@ -19,7 +19,6 @@ package eventlistener import ( "context" "fmt" - "os" "strconv" "testing" "time" @@ -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 @@ -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{ diff --git a/pkg/reconciler/eventlistener/resources/custom_test.go b/pkg/reconciler/eventlistener/resources/custom_test.go index d1c945612..1c6a37309 100644 --- a/pkg/reconciler/eventlistener/resources/custom_test.go +++ b/pkg/reconciler/eventlistener/resources/custom_test.go @@ -18,7 +18,6 @@ package resources import ( "context" - "os" "strconv" "testing" @@ -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{}{ @@ -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() diff --git a/pkg/reconciler/eventlistener/resources/deployment_test.go b/pkg/reconciler/eventlistener/resources/deployment_test.go index 32c9a671f..9d5c4b726 100644 --- a/pkg/reconciler/eventlistener/resources/deployment_test.go +++ b/pkg/reconciler/eventlistener/resources/deployment_test.go @@ -18,7 +18,6 @@ package resources import ( "context" - "os" "testing" "github.com/google/go-cmp/cmp" @@ -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{ @@ -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)