From 74df6d138b87fa53218dc0fea7a12f0a51bda7ba Mon Sep 17 00:00:00 2001 From: Matt Moyer Date: Wed, 17 Mar 2021 12:47:38 -0500 Subject: [PATCH] Memoize library.IntegrationEnv so it's only constructed once per test. This is probably a good idea regardless, but it also avoids an infinite recursion from IntegrationEnv() -> assertNoRestartsDuringTest() -> NewKubeclient() -> IntegrationEnv() -> ... Signed-off-by: Matt Moyer --- test/library/env.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/test/library/env.go b/test/library/env.go index 28caf52f..97b24c22 100644 --- a/test/library/env.go +++ b/test/library/env.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "strings" + "sync" "testing" "github.com/stretchr/testify/require" @@ -73,9 +74,17 @@ func (e *TestEnv) ProxyEnv() []string { return []string{"http_proxy=" + e.Proxy, "https_proxy=" + e.Proxy, "no_proxy=127.0.0.1"} } +// memoizedTestEnvsByTest maps *testing.T pointers to *TestEnv. It exists so that we don't do all the +// environment parsing N times per test and so that any implicit assertions happen only once. +var memoizedTestEnvsByTest sync.Map //nolint: gochecknoglobals + // IntegrationEnv gets the integration test environment from OS environment variables. This // method also implies SkipUnlessIntegration(). func IntegrationEnv(t *testing.T) *TestEnv { + if existing, exists := memoizedTestEnvsByTest.Load(t); exists { + return existing.(*TestEnv) + } + t.Helper() SkipUnlessIntegration(t) @@ -96,12 +105,12 @@ func IntegrationEnv(t *testing.T) *TestEnv { require.NoErrorf(t, err, "capabilities specification was invalid YAML") loadEnvVars(t, &result) + result.t = t + memoizedTestEnvsByTest.Store(t, &result) // In every integration test, assert that no pods in our namespaces restart during the test. assertNoRestartsDuringTest(t, result.ConciergeNamespace, "") assertNoRestartsDuringTest(t, result.SupervisorNamespace, "") - - result.t = t return &result }