Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.Setenv to set env vars in tests #1491

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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