diff --git a/broker/broker_test.go b/broker/broker_test.go index 708859dde..cada222d0 100644 --- a/broker/broker_test.go +++ b/broker/broker_test.go @@ -76,7 +76,7 @@ func getHelloWorldHandler(t *testing.T) func(resp http.ResponseWriter, req *http func Setup(t *testing.T, ctx context.Context, egrp *errgroup.Group) { dirpath := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("Logging.Level", "Debug") viper.Set("ConfigDir", filepath.Join(dirpath, "config")) config.InitConfig() diff --git a/broker/token_utils_test.go b/broker/token_utils_test.go index fc85cf9ce..b6fbd9d24 100644 --- a/broker/token_utils_test.go +++ b/broker/token_utils_test.go @@ -28,10 +28,11 @@ import ( "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/config" + "github.com/pelicanplatform/pelican/server_utils" ) func TestGetCacheHostnameFromToken(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", t.TempDir()) config.InitConfig() require.NoError(t, config.InitClient()) diff --git a/cache/advertise_test.go b/cache/advertise_test.go index 604af910c..4660b6194 100644 --- a/cache/advertise_test.go +++ b/cache/advertise_test.go @@ -29,6 +29,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" ) func TestFilterNsAdsForCache(t *testing.T) { @@ -82,8 +83,8 @@ func TestFilterNsAdsForCache(t *testing.T) { expectedNumNS: 2, }, } - viper.Reset() - defer viper.Reset() + server_utils.ResetTestState() + defer server_utils.ResetTestState() nsAds := []server_structs.NamespaceAdV2{ { @@ -130,7 +131,7 @@ func TestFilterNsAdsForCache(t *testing.T) { if testInput.permittedNS != nil { viper.Set("Cache.PermittedNamespaces", testInput.permittedNS) } - defer viper.Reset() + defer server_utils.ResetTestState() cacheServer.SetFilters() err = cacheServer.GetNamespaceAdsFromDirector() diff --git a/cache/cache_api_test.go b/cache/cache_api_test.go index 37ef878d7..22694622f 100644 --- a/cache/cache_api_test.go +++ b/cache/cache_api_test.go @@ -28,17 +28,18 @@ import ( "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/param" + "github.com/pelicanplatform/pelican/server_utils" ) func TestCheckCacheSentinelLocation(t *testing.T) { t.Run("sentinel-not-set", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() err := CheckCacheSentinelLocation() assert.NoError(t, err) }) t.Run("sentinel-contains-dir", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set(param.Cache_SentinelLocation.GetName(), "/test.txt") err := CheckCacheSentinelLocation() require.Error(t, err) @@ -47,7 +48,7 @@ func TestCheckCacheSentinelLocation(t *testing.T) { t.Run("sentinel-dne", func(t *testing.T) { tmpDir := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set(param.Cache_SentinelLocation.GetName(), "test.txt") viper.Set(param.Cache_LocalRoot.GetName(), tmpDir) err := CheckCacheSentinelLocation() @@ -57,7 +58,7 @@ func TestCheckCacheSentinelLocation(t *testing.T) { t.Run("sentinel-exists", func(t *testing.T) { tmpDir := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set(param.Cache_SentinelLocation.GetName(), "test.txt") viper.Set(param.Cache_LocalRoot.GetName(), tmpDir) diff --git a/client/fed_linux_test.go b/client/fed_linux_test.go index 4d12523ca..468b40ef6 100644 --- a/client/fed_linux_test.go +++ b/client/fed_linux_test.go @@ -47,8 +47,7 @@ import ( func TestRecursiveUploadsAndDownloads(t *testing.T) { // Create instance of test federation - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() fed := fed_test_utils.NewFedTest(t, mixedAuthOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) @@ -221,9 +220,9 @@ func TestRecursiveUploadsAndDownloads(t *testing.T) { if err := te.Shutdown(); err != nil { log.Errorln("Failure when shutting down transfer engine:", err) } - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() - server_utils.ResetOriginExports() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() + }) } @@ -243,8 +242,7 @@ func verifySuccessfulTransfer(t *testing.T, transferResults []client.TransferRes // Test that recursive uploads and downloads work with the ?recursive query func TestRecursiveUploadsAndDownloadsWithQuery(t *testing.T) { // Create instance of test federation - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() fed := fed_test_utils.NewFedTest(t, mixedAuthOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) @@ -257,9 +255,9 @@ func TestRecursiveUploadsAndDownloadsWithQuery(t *testing.T) { if err := te.Shutdown(); err != nil { log.Errorln("Failure when shutting down transfer engine:", err) } - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() - server_utils.ResetOriginExports() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() + }) // Create a token file @@ -436,8 +434,7 @@ func TestRecursiveUploadsAndDownloadsWithQuery(t *testing.T) { // This tests that is origins disable listings, we should fail the download // Note: origins disabling listings override the existence of dirlisthost, causing a failure func TestFailureOnOriginDisablingListings(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() viper.Set("Logging.Level", "debug") viper.Set("Origin.StorageType", "posix") @@ -461,8 +458,7 @@ func TestFailureOnOriginDisablingListings(t *testing.T) { func TestSyncUpload(t *testing.T) { // Create instance of test federation - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() fed := fed_test_utils.NewFedTest(t, bothAuthOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) @@ -592,8 +588,7 @@ func TestSyncUpload(t *testing.T) { func TestSyncDownload(t *testing.T) { // Create instance of test federation - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() fed := fed_test_utils.NewFedTest(t, bothAuthOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) diff --git a/client/fed_test.go b/client/fed_test.go index 03cf2f3da..83c0faa93 100644 --- a/client/fed_test.go +++ b/client/fed_test.go @@ -105,8 +105,8 @@ func getTempToken(t *testing.T) (tempToken *os.File, tkn string) { // A test that spins up a federation, and tests object get and put func TestGetAndPutAuth(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + fed := fed_test_utils.NewFedTest(t, bothAuthOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) assert.NoError(t, err) @@ -282,15 +282,15 @@ func TestGetAndPutAuth(t *testing.T) { } }) t.Cleanup(func() { - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() }) } // A test that spins up a federation, and tests object get and put func TestCopyAuth(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + fed := fed_test_utils.NewFedTest(t, bothAuthOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) assert.NoError(t, err) @@ -443,15 +443,15 @@ func TestCopyAuth(t *testing.T) { if err := te.Shutdown(); err != nil { log.Errorln("Failure when shutting down transfer engine:", err) } - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() }) } // A test that spins up the federation, where the origin is in EnablePublicReads mode. Then GET a file from the origin without a token func TestGetPublicRead(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + fed := fed_test_utils.NewFedTest(t, bothPublicOriginCfg) t.Run("testPubObjGet", func(t *testing.T) { @@ -480,17 +480,16 @@ func TestGetPublicRead(t *testing.T) { } }) t.Cleanup(func() { - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() }) } // A test that spins up a federation, and tests object stat func TestObjectStat(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() - defer server_utils.ResetOriginExports() - defer viper.Reset() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() fed := fed_test_utils.NewFedTest(t, mixedAuthOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) require.NoError(t, err) @@ -593,10 +592,10 @@ func TestObjectStat(t *testing.T) { // Test the functionality of the direct reads feature (?directread) func TestDirectReads(t *testing.T) { - defer viper.Reset() + defer server_utils.ResetTestState() t.Run("testDirectReadsSuccess", func(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + viper.Set("Origin.EnableDirectReads", true) fed := fed_test_utils.NewFedTest(t, bothPublicOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) @@ -637,8 +636,8 @@ func TestDirectReads(t *testing.T) { // Test that direct reads fail if DirectReads=false is set for origin config but true for namespace/export t.Run("testDirectReadsDirectReadFalseByOrigin", func(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + fed := fed_test_utils.NewFedTest(t, pubOriginNoDirectRead) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) require.NoError(t, err) @@ -667,8 +666,8 @@ func TestDirectReads(t *testing.T) { // Test that direct reads fail if DirectReads=false is set for namespace/export config but true for origin t.Run("testDirectReadsDirectReadFalseByNamespace", func(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + fed := fed_test_utils.NewFedTest(t, pubExportNoDirectRead) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) require.NoError(t, err) @@ -699,10 +698,9 @@ func TestDirectReads(t *testing.T) { // Test the functionality of NewTransferJob, checking we return at the correct locations for certain errors func TestNewTransferJob(t *testing.T) { - viper.Reset() - defer viper.Reset() - server_utils.ResetOriginExports() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + defer server_utils.ResetTestState() + fed := fed_test_utils.NewFedTest(t, mixedAuthOriginCfg) discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) require.NoError(t, err) @@ -748,10 +746,9 @@ func TestNewTransferJob(t *testing.T) { // A test that spins up a federation, and tests object list func TestObjectList(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() - defer server_utils.ResetOriginExports() - defer viper.Reset() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() fed := fed_test_utils.NewFedTest(t, mixedAuthOriginCfg) // Other set-up items: @@ -845,8 +842,7 @@ func TestObjectList(t *testing.T) { // We should get a 405 returned. This is a separate test since we need a completely different origin func TestObjectList405Error(t *testing.T) { test_utils.InitClient(t, nil) - server_utils.ResetOriginExports() - defer server_utils.ResetOriginExports() + err := config.InitClient() require.NoError(t, err) @@ -882,9 +878,8 @@ func TestObjectList405Error(t *testing.T) { // Startup a mini-federation and ensure the "pack=auto" functionality works // end-to-end func TestClientUnpack(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + err := config.InitClient() require.NoError(t, err) @@ -942,8 +937,8 @@ func TestClientUnpack(t *testing.T) { // A test that generates a token locally from the private key func TestTokenGenerate(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + fed := fed_test_utils.NewFedTest(t, bothAuthOriginCfg) // Other set-up items: diff --git a/client/handle_http_test.go b/client/handle_http_test.go index cb68c14a6..a1ec2c307 100644 --- a/client/handle_http_test.go +++ b/client/handle_http_test.go @@ -37,18 +37,18 @@ import ( "github.com/pkg/errors" log "github.com/sirupsen/logrus" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/error_codes" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) func TestMain(m *testing.M) { - viper.Reset() + server_utils.ResetTestState() if err := config.InitClient(); err != nil { os.Exit(1) } @@ -146,7 +146,7 @@ func TestNewTransferDetailsEnv(t *testing.T) { assert.Equal(t, "https", transfers[0].Url.Scheme) assert.Equal(t, false, transfers[0].Proxy) os.Unsetenv("OSG_DISABLE_PROXY_FALLBACK") - viper.Reset() + server_utils.ResetTestState() err := config.InitClient() assert.Nil(t, err) } @@ -509,7 +509,7 @@ func TestTimeoutHeaderSetForDownload(t *testing.T) { assert.NoError(t, err) _, _, _, _, err = downloadHTTP(ctx, nil, nil, transferAttemptDetails{Url: serverURL, Proxy: false}, filepath.Join(t.TempDir(), "test.txt"), -1, "", "") assert.NoError(t, err) - viper.Reset() + server_utils.ResetTestState() } func TestJobIdHeaderSetForDownload(t *testing.T) { @@ -548,7 +548,7 @@ func TestJobIdHeaderSetForDownload(t *testing.T) { assert.NoError(t, err) _, _, _, _, err = downloadHTTP(ctx, nil, nil, transferAttemptDetails{Url: serverURL, Proxy: false}, filepath.Join(t.TempDir(), "test.txt"), -1, "", "") assert.NoError(t, err) - viper.Reset() + server_utils.ResetTestState() os.Unsetenv("_CONDOR_JOB_AD") } @@ -911,8 +911,8 @@ func TestFailedLargeUploadError(t *testing.T) { } func TestNewTransferEngine(t *testing.T) { - viper.Reset() - defer viper.Reset() + server_utils.ResetTestState() + defer server_utils.ResetTestState() // Test we fail if we do not call initclient() before t.Run("TestInitClientNotCalled", func(t *testing.T) { config.ResetClientInitialized() diff --git a/client/main_test.go b/client/main_test.go index 489c09016..daacc05a7 100644 --- a/client/main_test.go +++ b/client/main_test.go @@ -31,7 +31,6 @@ import ( "strings" "testing" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -39,6 +38,7 @@ import ( "github.com/pelicanplatform/pelican/mock" "github.com/pelicanplatform/pelican/pelican_url" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) @@ -139,7 +139,7 @@ func TestGetToken(t *testing.T) { // Need a namespace for token acquisition defer os.Unsetenv("PELICAN_FEDERATION_TOPOLOGYNAMESPACEURL") os.Setenv("PELICAN_TOPOLOGY_NAMESPACE_URL", "https://topology.opensciencegrid.org/osdf/namespaces") - viper.Reset() + server_utils.ResetTestState() err := config.InitClient() assert.Nil(t, err) diff --git a/cmd/fed_serve_cache_test.go b/cmd/fed_serve_cache_test.go index 663c39c0f..abe45ed7b 100644 --- a/cmd/fed_serve_cache_test.go +++ b/cmd/fed_serve_cache_test.go @@ -44,10 +44,9 @@ func TestFedServeCache(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() modules := server_structs.ServerType(0) modules.Set(server_structs.CacheType) diff --git a/cmd/fed_serve_test.go b/cmd/fed_serve_test.go index b1ea831a7..a5f39c646 100644 --- a/cmd/fed_serve_test.go +++ b/cmd/fed_serve_test.go @@ -47,10 +47,9 @@ func TestFedServePosixOrigin(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() modules := server_structs.ServerType(0) modules.Set(server_structs.OriginType) diff --git a/cmd/generate_keygen_test.go b/cmd/generate_keygen_test.go index fa7a2f265..3eb111509 100644 --- a/cmd/generate_keygen_test.go +++ b/cmd/generate_keygen_test.go @@ -24,11 +24,11 @@ import ( "testing" "github.com/lestrrat-go/jwx/v2/jwk" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/config" + "github.com/pelicanplatform/pelican/server_utils" ) // Create tmpdir, change cwd, and setup clean up functions @@ -44,7 +44,7 @@ func setupTestRun(t *testing.T) string { t.Cleanup(func() { err := os.Chdir(wd) require.NoError(t, err) - viper.Reset() + server_utils.ResetTestState() config.ResetIssuerJWKPtr() }) return tmpDir @@ -67,7 +67,7 @@ func TestKeygenMain(t *testing.T) { config.ResetIssuerJWKPtr() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() config.ResetIssuerJWKPtr() }) diff --git a/cmd/origin_reset_password_test.go b/cmd/origin_reset_password_test.go index 5140c9396..07db18808 100644 --- a/cmd/origin_reset_password_test.go +++ b/cmd/origin_reset_password_test.go @@ -33,6 +33,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/param" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) @@ -42,7 +43,7 @@ func TestResetPassword(t *testing.T) { defer cancel() dirName := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", dirName) config.InitConfig() viper.Set("Server.WebPort", 8444) diff --git a/cmd/plugin_test.go b/cmd/plugin_test.go index 6e1cbd56b..582f0c0f4 100644 --- a/cmd/plugin_test.go +++ b/cmd/plugin_test.go @@ -232,13 +232,12 @@ func (f *FedTest) Teardown() { f.Cancel() f.FedCancel() assert.NoError(f.T, f.ErrGroup.Wait()) - viper.Reset() + server_utils.ResetTestState() } // Test the main function for the pelican plugin func TestStashPluginMain(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() oldPrefix, err := config.SetPreferredPrefix(config.StashPrefix) defer func() { @@ -314,8 +313,7 @@ func TestStashPluginMain(t *testing.T) { // Test multiple downloads from the plugin func TestPluginMulti(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() dirName := t.TempDir() @@ -379,10 +377,8 @@ func TestPluginMulti(t *testing.T) { // Test multiple downloads from the plugin func TestPluginDirectRead(t *testing.T) { - viper.Reset() - defer viper.Reset() - defer server_utils.ResetOriginExports() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + defer server_utils.ResetTestState() dirName := t.TempDir() @@ -457,8 +453,7 @@ func TestPluginDirectRead(t *testing.T) { func TestPluginCorrectStartAndEndTime(t *testing.T) { test_utils.InitClient(t, nil) server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + defer server_utils.ResetTestState() // Set up our http backend so that we can sleep during transfer body := "Hello, World!" @@ -670,10 +665,8 @@ func TestFailTransfer(t *testing.T) { // Test recursive downloads from the plugin func TestPluginRecursiveDownload(t *testing.T) { - viper.Reset() - defer viper.Reset() - defer server_utils.ResetOriginExports() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + defer server_utils.ResetTestState() dirName := t.TempDir() diff --git a/config/config.go b/config/config.go index 180ae0887..e12075851 100644 --- a/config/config.go +++ b/config/config.go @@ -1503,3 +1503,25 @@ func InitClient() error { return nil } + +// This function resets most states for test cases, including 1. viper settings, 2. preferred prefix, 3. transport object, 4. Federation metadata back to their default +func ResetConfig() { + viper.Reset() + + // Clear cached preferred prefix + testingPreferredPrefix = "" + + // Clear cached transport object + onceTransport = sync.Once{} + transport = nil + + // Reset federation metadata + fedDiscoveryOnce = &sync.Once{} + globalFedInfo = pelican_url.FederationDiscovery{} + globalFedErr = nil + + ResetIssuerJWKPtr() + ResetClientInitialized() + + // other than what's above, resetting Origin exports will be done by ResetTestState() in server_utils pkg +} diff --git a/config/config_test.go b/config/config_test.go index 97f4eae8a..5d4af9207 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -136,9 +136,9 @@ func TestDialerTimeout(t *testing.T) { } func TestInitConfig(t *testing.T) { - viper.Reset() + ResetConfig() t.Cleanup(func() { - viper.Reset() + ResetConfig() }) // Set prefix to OSDF to ensure that config is being set testingPreferredPrefix = "OSDF" @@ -161,13 +161,13 @@ func TestInitConfig(t *testing.T) { if err := viper.WriteConfigAs(tempCfgFile.Name()); err != nil { t.Fatalf("Failed to write to config file: %v", err) } - viper.Reset() + ResetConfig() viper.Set("config", tempCfgFile.Name()) // Set the temp file as the new 'pelican.yaml' InitConfig() // Check if server address overrides the default assert.Equal(t, "1.1.1.1", param.Server_WebHost.GetString()) - viper.Reset() + ResetConfig() //Test if prefix is not set, should not be able to find osdfYaml configuration testingPreferredPrefix = "" @@ -207,9 +207,9 @@ func setupConfigLocations(t *testing.T, continueDirs []string) { // Test that the `ConfigLocations` key works as expected func TestExtraCfg(t *testing.T) { - viper.Reset() + ResetConfig() t.Cleanup(func() { - viper.Reset() + ResetConfig() }) t.Run("test-no-continue", func(t *testing.T) { @@ -218,7 +218,7 @@ func TestExtraCfg(t *testing.T) { }) t.Run("test-one-dir-no-files", func(t *testing.T) { - viper.Reset() + ResetConfig() dir1 := t.TempDir() setupConfigLocations(t, []string{dir1}) @@ -231,7 +231,7 @@ func TestExtraCfg(t *testing.T) { }) t.Run("test-one-dir-one-file", func(t *testing.T) { - viper.Reset() + ResetConfig() dir1 := t.TempDir() setupConfigLocations(t, []string{dir1}) @@ -248,7 +248,7 @@ func TestExtraCfg(t *testing.T) { }) t.Run("test-two-dirs-one-file-each", func(t *testing.T) { - viper.Reset() + ResetConfig() dir1 := t.TempDir() dir2 := t.TempDir() setupConfigLocations(t, []string{dir1, dir2}) @@ -272,7 +272,7 @@ func TestExtraCfg(t *testing.T) { }) t.Run("test-two-dirs-two-files-each", func(t *testing.T) { - viper.Reset() + ResetConfig() dir1 := t.TempDir() dir2 := t.TempDir() setupConfigLocations(t, []string{dir1, dir2}) @@ -297,7 +297,7 @@ func TestExtraCfg(t *testing.T) { }) t.Run("test-bad-directory", func(t *testing.T) { - viper.Reset() + ResetConfig() continueDir := t.TempDir() setupConfigLocations(t, []string{continueDir + "-dne"}) @@ -322,11 +322,11 @@ func TestDeprecateLogMessage(t *testing.T) { require.NoError(t, err) viper.Set("ConfigDir", tmpPath) - viper.Reset() + ResetConfig() t.Run("expect-deprecated-message-if-namespace-is-set", func(t *testing.T) { hook := test.NewGlobal() - viper.Reset() - defer viper.Reset() + ResetConfig() + defer ResetConfig() // The default value is set to Error, but this is a warning message viper.Set("Logging.Level", "Warning") viper.Set("Origin.NamespacePrefix", "/a/prefix") @@ -584,11 +584,11 @@ func TestInitServerUrl(t *testing.T) { mockWebUrlWNon443Port := "https://example.com:8444" t.Cleanup(func() { - viper.Reset() + ResetConfig() }) initConfig := func() { - viper.Reset() + ResetConfig() tempDir := t.TempDir() viper.Set("ConfigDir", tempDir) } @@ -600,7 +600,7 @@ func TestInitServerUrl(t *testing.T) { } t.Run("web-url-defaults-to-hostname-port", func(t *testing.T) { - viper.Reset() + ResetConfig() viper.Set("Server.Hostname", mockHostname) viper.Set("Server.WebPort", mockNon443Port) err := InitConfigDir() @@ -611,7 +611,7 @@ func TestInitServerUrl(t *testing.T) { }) t.Run("default-web-url-removes-443-port", func(t *testing.T) { - viper.Reset() + ResetConfig() viper.Set("Server.Hostname", mockHostname) viper.Set("Server.WebPort", mock443Port) err := InitConfigDir() @@ -623,7 +623,7 @@ func TestInitServerUrl(t *testing.T) { t.Run("remove-443-port-for-set-web-url", func(t *testing.T) { // We respect the URL value set directly by others. Won't remove 443 port - viper.Reset() + ResetConfig() viper.Set("Server.ExternalWebUrl", mockWebUrlW443Port) err := InitConfigDir() require.NoError(t, err) diff --git a/config/config_validation_test.go b/config/config_validation_test.go index 3a1aa1f5a..3a186f578 100644 --- a/config/config_validation_test.go +++ b/config/config_validation_test.go @@ -31,10 +31,10 @@ import ( // Test that Pelican notifies users about unrecognized configuration keys. func TestBadConfigKeys(t *testing.T) { - t.Cleanup(func() { viper.Reset() }) + t.Cleanup(func() { ResetConfig() }) setupFunc := func() *test.Hook { - viper.Reset() + ResetConfig() viper.Set("ConfigDir", t.TempDir()) viper.Set("Debug", true) hook := test.NewLocal(logrus.StandardLogger()) diff --git a/config/encrypted_test.go b/config/encrypted_test.go index 6705fb434..bd100ea64 100644 --- a/config/encrypted_test.go +++ b/config/encrypted_test.go @@ -30,10 +30,10 @@ import ( ) func TestGetSecret(t *testing.T) { - viper.Reset() + ResetConfig() t.Cleanup(func() { - viper.Reset() + ResetConfig() }) t.Run("generate-32B-hash", func(t *testing.T) { tmp := t.TempDir() @@ -47,10 +47,10 @@ func TestGetSecret(t *testing.T) { } func TestEncryptString(t *testing.T) { - viper.Reset() + ResetConfig() t.Cleanup(func() { - viper.Reset() + ResetConfig() }) t.Run("encrypt-without-err", func(t *testing.T) { @@ -65,10 +65,10 @@ func TestEncryptString(t *testing.T) { } func TestDecryptString(t *testing.T) { - viper.Reset() + ResetConfig() t.Cleanup(func() { - viper.Reset() + ResetConfig() }) t.Run("decrypt-without-err", func(t *testing.T) { tmp := t.TempDir() diff --git a/config/env_test.go b/config/env_test.go index 5ccf3cebf..d37816184 100644 --- a/config/env_test.go +++ b/config/env_test.go @@ -35,7 +35,7 @@ func TestOsdfEnvToPelican(t *testing.T) { hook := test.NewGlobal() t.Run("non-osdf-prefix-does-nothing", func(t *testing.T) { - viper.Reset() + ResetConfig() testingPreferredPrefix = PelicanPrefix os.Setenv("OSDF_MOCK", "randomStr") @@ -49,7 +49,7 @@ func TestOsdfEnvToPelican(t *testing.T) { }) t.Run("one-osdf-env", func(t *testing.T) { - viper.Reset() + ResetConfig() hook.Reset() testingPreferredPrefix = OsdfPrefix @@ -67,7 +67,7 @@ func TestOsdfEnvToPelican(t *testing.T) { }) t.Run("one-stash-env", func(t *testing.T) { - viper.Reset() + ResetConfig() hook.Reset() testingPreferredPrefix = StashPrefix @@ -85,7 +85,7 @@ func TestOsdfEnvToPelican(t *testing.T) { }) t.Run("complex-osdf-env", func(t *testing.T) { - viper.Reset() + ResetConfig() hook.Reset() testingPreferredPrefix = OsdfPrefix @@ -102,7 +102,7 @@ func TestOsdfEnvToPelican(t *testing.T) { }) t.Run("pelican-env-still-works", func(t *testing.T) { - viper.Reset() + ResetConfig() hook.Reset() testingPreferredPrefix = OsdfPrefix @@ -129,7 +129,7 @@ func TestOsdfEnvToPelican(t *testing.T) { }) t.Run("pelican-env-overwrites-osdf", func(t *testing.T) { - viper.Reset() + ResetConfig() hook.Reset() testingPreferredPrefix = OsdfPrefix diff --git a/config/oidc_metadata_test.go b/config/oidc_metadata_test.go index c307670c9..48bac8f51 100644 --- a/config/oidc_metadata_test.go +++ b/config/oidc_metadata_test.go @@ -30,10 +30,10 @@ import ( func TestGetOIDCProvider(t *testing.T) { t.Cleanup(func() { - viper.Reset() + ResetConfig() }) t.Run("empty-endpoints-gives-error", func(t *testing.T) { - viper.Reset() + ResetConfig() get, err := GetOIDCProdiver() require.Error(t, err) assert.Contains(t, err.Error(), "nothing set for config parameter OIDC.IssuerUrl or OIDC.AuthorizationEndpoint") @@ -41,7 +41,7 @@ func TestGetOIDCProvider(t *testing.T) { }) t.Run("auth-endpoint-gives-correct-result", func(t *testing.T) { - viper.Reset() + ResetConfig() viper.Set(param.OIDC_AuthorizationEndpoint.GetName(), "https://example.com/authorization") get, err := GetOIDCProdiver() require.NoError(t, err) @@ -61,7 +61,7 @@ func TestGetOIDCProvider(t *testing.T) { }) t.Run("issuer-endpoint-gives-correct-result", func(t *testing.T) { - viper.Reset() + ResetConfig() viper.Set(param.OIDC_Issuer.GetName(), "https://example.com") get, err := GetOIDCProdiver() require.NoError(t, err) diff --git a/director/advertise_test.go b/director/advertise_test.go index 5a2e05c3a..4b4e0784f 100644 --- a/director/advertise_test.go +++ b/director/advertise_test.go @@ -36,6 +36,7 @@ import ( "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" ) var ( @@ -199,10 +200,10 @@ func multiExportsTopoJSONHandler(w http.ResponseWriter, r *http.Request) { func TestAdvertiseOSDF(t *testing.T) { t.Run("mock-topology-parse-correctly", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() serverAds.DeleteAll() defer func() { - viper.Reset() + server_utils.ResetTestState() serverAds.DeleteAll() }() @@ -251,10 +252,10 @@ func TestAdvertiseOSDF(t *testing.T) { }) t.Run("multiple-ns-single-origin", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() serverAds.DeleteAll() defer func() { - viper.Reset() + server_utils.ResetTestState() serverAds.DeleteAll() }() @@ -279,10 +280,10 @@ func TestAdvertiseOSDF(t *testing.T) { }) t.Run("caches-serving-multiple-nss", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() serverAds.DeleteAll() defer func() { - viper.Reset() + server_utils.ResetTestState() serverAds.DeleteAll() }() diff --git a/director/cache_ads_test.go b/director/cache_ads_test.go index fc6b5f0a4..15699834c 100644 --- a/director/cache_ads_test.go +++ b/director/cache_ads_test.go @@ -31,6 +31,7 @@ import ( "golang.org/x/sync/errgroup" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" ) func hasServerAdWithName(serverAds []server_structs.ServerAd, name string) bool { @@ -431,7 +432,7 @@ func TestRecordAd(t *testing.T) { t.Run("recorded-sad-should-match-health-test-utils-one", func(t *testing.T) { t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() healthTestUtilsMutex.Lock() statUtilsMutex.Lock() defer statUtilsMutex.Unlock() @@ -442,7 +443,7 @@ func TestRecordAd(t *testing.T) { serverAds.DeleteAll() geoIPOverrides = nil }) - viper.Reset() + server_utils.ResetTestState() func() { geoIPOverrides = nil diff --git a/director/director_test.go b/director/director_test.go index 674c8f110..adc00de0d 100644 --- a/director/director_test.go +++ b/director/director_test.go @@ -141,7 +141,7 @@ func TestDirectorRegistration(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() // Mock registry server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { @@ -1077,7 +1077,7 @@ func TestDiscoverOriginCache(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() // Direcor SD will only be used for director's Prometheus scraper to get available origins, // so the token issuer is issentially the director server itself // There's no need to rely on Federation.DirectorUrl as token issuer in this case @@ -1369,7 +1369,7 @@ func TestRedirects(t *testing.T) { expectedPath = "/api/v1.0/director/object/foo/bar" assert.Equal(t, expectedPath, c.Request.URL.Path) - viper.Reset() + server_utils.ResetTestState() }) t.Run("redirect-middleware", func(t *testing.T) { @@ -1459,7 +1459,7 @@ func TestRedirects(t *testing.T) { expectedPath = "/api/v1.0/director/origin/foo/bar" assert.Equal(t, expectedPath, c.Request.URL.Path) - viper.Reset() + server_utils.ResetTestState() }) t.Run("cache-test-file-redirect", func(t *testing.T) { @@ -1476,9 +1476,9 @@ func TestRedirects(t *testing.T) { }) t.Run("redirect-link-header-length", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() }) viper.Set("Director.CacheSortMethod", "random") @@ -1511,9 +1511,9 @@ func TestRedirects(t *testing.T) { // Make sure collections-url is correctly populated when the ns/origin comes from topology t.Run("collections-url-from-topology", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() }) viper.Set("Director.CacheSortMethod", "random") @@ -1538,9 +1538,9 @@ func TestRedirects(t *testing.T) { }) t.Run("object-endpoint-returns-all-headers", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() }) viper.Set("Director.CacheSortMethod", "random") @@ -1561,9 +1561,9 @@ func TestRedirects(t *testing.T) { }) t.Run("origin-endpoint-returns-all-headers", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() }) viper.Set("Director.CacheSortMethod", "random") diff --git a/director/discovery_test.go b/director/discovery_test.go index b83783972..013b87270 100644 --- a/director/discovery_test.go +++ b/director/discovery_test.go @@ -139,7 +139,7 @@ func TestFederationDiscoveryHandler(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", t.TempDir()) viper.Set("Federation.DirectorUrl", tc.dirUrl) viper.Set("Federation.RegistryUrl", tc.regUrl) @@ -212,7 +212,7 @@ func TestOidcDiscoveryHandler(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", t.TempDir()) viper.Set("Federation.DirectorUrl", tc.dirUrl) config.InitConfig() diff --git a/director/origin_api_test.go b/director/origin_api_test.go index 1ba9cc44c..1c3c350d7 100644 --- a/director/origin_api_test.go +++ b/director/origin_api_test.go @@ -47,7 +47,7 @@ func TestVerifyAdvertiseToken(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() tDir := t.TempDir() kfile := filepath.Join(tDir, "t-key") diff --git a/director/prom_query_test.go b/director/prom_query_test.go index 356b6e8a0..9cb26654d 100644 --- a/director/prom_query_test.go +++ b/director/prom_query_test.go @@ -31,6 +31,7 @@ import ( "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/param" + "github.com/pelicanplatform/pelican/server_utils" ) var ( @@ -202,8 +203,8 @@ func TestParsePromRes(t *testing.T) { } func TestQueryPrometheus(t *testing.T) { - viper.Reset() - t.Cleanup(viper.Reset) + server_utils.ResetTestState() + t.Cleanup(server_utils.ResetTestState) handler := func(w http.ResponseWriter, r *http.Request) { query := r.URL.Query().Get("query") diff --git a/director/sort_test.go b/director/sort_test.go index a89ca8639..9a82f7a5f 100644 --- a/director/sort_test.go +++ b/director/sort_test.go @@ -36,6 +36,7 @@ import ( "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" ) // Geo Override Yaml mockup @@ -44,9 +45,9 @@ import ( var yamlMockup string func TestCheckOverrides(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() geoIPOverrides = nil }) @@ -191,9 +192,9 @@ func TestSortServerAdsByTopo(t *testing.T) { } func TestSortServerAds(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() geoIPOverrides = nil }) diff --git a/director/stat_test.go b/director/stat_test.go index 83f68d314..7f74755c5 100644 --- a/director/stat_test.go +++ b/director/stat_test.go @@ -37,10 +37,11 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" ) func TestQueryServersForObject(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("Director.MinStatResponse", 1) viper.Set("Director.MaxStatResponse", 1) viper.Set("Director.StatTimeout", time.Microsecond*200) @@ -216,7 +217,7 @@ func TestQueryServersForObject(t *testing.T) { cleanupMock() // Restore the old serverAds at the end of this test func serverAds = oldAds - viper.Reset() + server_utils.ResetTestState() }) t.Run("empty-server-ads-returns", func(t *testing.T) { @@ -698,7 +699,7 @@ func TestQueryServersForObject(t *testing.T) { } func TestSendHeadReq(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() // Start a local HTTP server server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { diff --git a/fed_test_utils/fed.go b/fed_test_utils/fed.go index b0e5a7683..2dc26416b 100644 --- a/fed_test_utils/fed.go +++ b/fed_test_utils/fed.go @@ -225,8 +225,8 @@ func NewFedTest(t *testing.T, originConfig string) (ft *FedTest) { } err := os.RemoveAll(tmpPath) require.NoError(t, err) - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() }) return diff --git a/launcher_utils/advertise_test.go b/launcher_utils/advertise_test.go index 4d8831900..0b1b304ca 100644 --- a/launcher_utils/advertise_test.go +++ b/launcher_utils/advertise_test.go @@ -26,18 +26,18 @@ import ( "strings" "testing" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/pelican_url" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" ) func TestGetSitenameFromReg(t *testing.T) { t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() }) t.Run("no-registry-url", func(t *testing.T) { @@ -50,7 +50,7 @@ func TestGetSitenameFromReg(t *testing.T) { }) t.Run("registry-returns-404", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusNotFound) })) @@ -64,7 +64,7 @@ func TestGetSitenameFromReg(t *testing.T) { }) t.Run("registry-returns-correct-object", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if strings.HasPrefix(req.URL.Path, "/api/v1.0/registry") { prefix := strings.TrimPrefix(req.URL.Path, "/api/v1.0/registry") diff --git a/launcher_utils/register_namespace_test.go b/launcher_utils/register_namespace_test.go index 2baf76afc..f6e273b63 100644 --- a/launcher_utils/register_namespace_test.go +++ b/launcher_utils/register_namespace_test.go @@ -41,6 +41,7 @@ import ( "github.com/pelicanplatform/pelican/param" "github.com/pelicanplatform/pelican/registry" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) @@ -56,7 +57,7 @@ func TestRegistration(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", tempConfigDir) config.InitConfig() diff --git a/local_cache/cache_linux_test.go b/local_cache/cache_linux_test.go index e8349bc19..8f85ad68d 100644 --- a/local_cache/cache_linux_test.go +++ b/local_cache/cache_linux_test.go @@ -40,6 +40,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/fed_test_utils" "github.com/pelicanplatform/pelican/param" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" "github.com/pelicanplatform/pelican/token" "github.com/pelicanplatform/pelican/token_scopes" @@ -51,7 +52,7 @@ import ( func TestPurge(t *testing.T) { tmpDir := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("LocalCache.Size", "5MB") ft := fed_test_utils.NewFedTest(t, pubOriginCfg) @@ -103,8 +104,8 @@ func TestPurge(t *testing.T) { if err := te.Shutdown(); err != nil { log.Errorln("Failure when shutting down transfer engine:", err) } - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() }) } @@ -113,7 +114,7 @@ func TestPurge(t *testing.T) { func TestForcePurge(t *testing.T) { tmpDir := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("LocalCache.Size", "5MB") // Decrease the low water mark so invoking purge will result in 3 files in the cache. viper.Set("LocalCache.LowWaterMarkPercentage", "80") @@ -195,7 +196,7 @@ func TestForcePurge(t *testing.T) { if err := te.Shutdown(); err != nil { log.Errorln("Failure when shutting down transfer engine:", err) } - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() }) } diff --git a/local_cache/cache_test.go b/local_cache/cache_test.go index ea9cf0cfe..8401a4bd9 100644 --- a/local_cache/cache_test.go +++ b/local_cache/cache_test.go @@ -46,6 +46,7 @@ import ( "github.com/pelicanplatform/pelican/fed_test_utils" local_cache "github.com/pelicanplatform/pelican/local_cache" "github.com/pelicanplatform/pelican/param" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" "github.com/pelicanplatform/pelican/token" "github.com/pelicanplatform/pelican/token_scopes" @@ -64,7 +65,7 @@ var ( // The download is done twice -- once to verify functionality and once // as a cache hit. func TestFedPublicGet(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ft := fed_test_utils.NewFedTest(t, pubOriginCfg) lc, err := local_cache.NewLocalCache(ft.Ctx, ft.Egrp) @@ -89,7 +90,7 @@ func TestFedPublicGet(t *testing.T) { // Test the local cache library on an authenticated GET. func TestFedAuthGet(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ft := fed_test_utils.NewFedTest(t, authOriginCfg) lc, err := local_cache.NewLocalCache(ft.Ctx, ft.Egrp) @@ -121,7 +122,7 @@ func TestFedAuthGet(t *testing.T) { // Test a raw HTTP request (no Pelican client) works with the local cache func TestHttpReq(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ft := fed_test_utils.NewFedTest(t, authOriginCfg) transport := config.GetTransport().Clone() @@ -144,7 +145,7 @@ func TestHttpReq(t *testing.T) { // Test a raw HTTP request (no Pelican client) returns a 404 for an unknown object func TestHttpFailures(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() fed_test_utils.NewFedTest(t, authOriginCfg) transport := config.GetTransport().Clone() @@ -188,7 +189,7 @@ func TestHttpFailures(t *testing.T) { // Test that the client library (with authentication) works with the local cache func TestClient(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ft := fed_test_utils.NewFedTest(t, authOriginCfg) ctx, cancel, egrp := test_utils.TestContext(context.Background(), t) @@ -337,14 +338,14 @@ func TestClient(t *testing.T) { if err := egrp.Wait(); err != nil && err != context.Canceled && err != http.ErrServerClosed { require.NoError(t, err) } - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() }) } // Test that HEAD requests to the local cache return the correct result func TestStat(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ft := fed_test_utils.NewFedTest(t, pubOriginCfg) lc, err := local_cache.NewLocalCache(ft.Ctx, ft.Egrp) @@ -371,7 +372,7 @@ func TestStat(t *testing.T) { func TestLargeFile(t *testing.T) { tmpDir := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("Client.MaximumDownloadSpeed", 40*1024*1024) ft := fed_test_utils.NewFedTest(t, pubOriginCfg) @@ -404,8 +405,8 @@ func TestLargeFile(t *testing.T) { if err := te.Shutdown(); err != nil { log.Errorln("Failure when shutting down transfer engine:", err) } - // Throw in a viper.Reset for good measure. Keeps our env squeaky clean! - viper.Reset() + // Throw in a config.Reset for good measure. Keeps our env squeaky clean! + server_utils.ResetTestState() }) } @@ -415,7 +416,7 @@ func TestLargeFile(t *testing.T) { func TestOriginUnresponsive(t *testing.T) { tmpDir := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("Transport.ResponseHeaderTimeout", "3s") viper.Set("Logging.Level", "debug") ft := fed_test_utils.NewFedTest(t, pubOriginCfg) diff --git a/lotman/lotman_test.go b/lotman/lotman_test.go index 6e907311c..61975f6b1 100644 --- a/lotman/lotman_test.go +++ b/lotman/lotman_test.go @@ -30,6 +30,7 @@ import ( "strings" "testing" + "github.com/pelicanplatform/pelican/server_utils" log "github.com/sirupsen/logrus" "github.com/spf13/viper" "github.com/stretchr/testify/require" @@ -58,13 +59,13 @@ func setupLotmanFromConf(t *testing.T, readConfig bool, name string) (bool, func success := InitLotman() //reset func return success, func() { - viper.Reset() + server_utils.ResetTestState() } } // Test the library initializer. NOTE: this also tests CreateLot, which is a part of initialization. func TestLotmanInit(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Run("TestBadInit", func(t *testing.T) { // We haven't set various bits needed to create the lots, like discovery URL @@ -118,7 +119,7 @@ func TestLotmanInit(t *testing.T) { } func TestLotmanInitFromConfig(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() success, cleanup := setupLotmanFromConf(t, true, "LotmanInitConf") defer cleanup() @@ -218,7 +219,7 @@ func TestGetLotmanLib(t *testing.T) { } func TestGetAuthzCallers(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() success, cleanup := setupLotmanFromConf(t, true, "LotmanGetAuthzCalleres") defer cleanup() require.True(t, success) @@ -237,7 +238,7 @@ func TestGetAuthzCallers(t *testing.T) { } func TestGetLot(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() success, cleanup := setupLotmanFromConf(t, true, "LotmanGetLot") defer cleanup() require.True(t, success) @@ -260,7 +261,7 @@ func TestGetLot(t *testing.T) { } func TestUpdateLot(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() success, cleanup := setupLotmanFromConf(t, true, "LotmanInitConf") defer cleanup() require.True(t, success) @@ -298,7 +299,7 @@ func TestUpdateLot(t *testing.T) { } func TestDeleteLotsRec(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() success, cleanup := setupLotmanFromConf(t, true, "LotmanInitConf") defer cleanup() require.True(t, success) diff --git a/oauth2/oidc_client_test.go b/oauth2/oidc_client_test.go index 71b0fec92..6b7fc7305 100644 --- a/oauth2/oidc_client_test.go +++ b/oauth2/oidc_client_test.go @@ -26,15 +26,16 @@ import ( "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/param" + "github.com/pelicanplatform/pelican/server_utils" ) func TestGetRedirectURL(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() }) t.Run("no-redirect-host-no-cb-path-set", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set(param.Server_ExternalWebUrl.GetName(), "https://localhost:8888") get, err := GetRedirectURL("") require.NoError(t, err) @@ -42,7 +43,7 @@ func TestGetRedirectURL(t *testing.T) { }) t.Run("no-redirect-host-cp-path-set", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set(param.Server_ExternalWebUrl.GetName(), "https://localhost:8888") get, err := GetRedirectURL("/new/url") require.NoError(t, err) @@ -50,7 +51,7 @@ func TestGetRedirectURL(t *testing.T) { }) t.Run("redirect-host-cp-path-set", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set(param.Server_ExternalWebUrl.GetName(), "https://ea123fsac:8888") viper.Set("Server.WebPort", 8888) viper.Set(param.OIDC_ClientRedirectHostname.GetName(), "localhost") diff --git a/origin/origin_db_test.go b/origin/origin_db_test.go index 0c741b629..2e2a4d19f 100644 --- a/origin/origin_db_test.go +++ b/origin/origin_db_test.go @@ -33,6 +33,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/param" + "github.com/pelicanplatform/pelican/server_utils" ) const ( @@ -131,10 +132,10 @@ func TestCollectionExistsByUUID(t *testing.T) { } func TestGetCollectionByUUID(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() setupMockOriginDB(t) t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() teardownMockOriginDB(t) }) err := insertMockDBData(mockGC) @@ -164,10 +165,10 @@ func TestGetCollectionByUUID(t *testing.T) { } func TestCreateCollection(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() setupMockOriginDB(t) t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() teardownMockOriginDB(t) }) @@ -204,10 +205,10 @@ func TestCreateCollection(t *testing.T) { } func TestUpdateCollection(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() setupMockOriginDB(t) t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() teardownMockOriginDB(t) }) @@ -234,10 +235,10 @@ func TestUpdateCollection(t *testing.T) { } func TestDeleteCollectionByUUID(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() setupMockOriginDB(t) t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() teardownMockOriginDB(t) }) diff --git a/origin/reg_status_test.go b/origin/reg_status_test.go index 7ced7ab0b..2fb65c888 100644 --- a/origin/reg_status_test.go +++ b/origin/reg_status_test.go @@ -26,7 +26,6 @@ import ( "testing" "github.com/jellydator/ttlcache/v3" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -74,14 +73,14 @@ func mockRegistryCheck(t *testing.T) *httptest.Server { func TestFetchRegStatus(t *testing.T) { t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() }) t.Run("successful-fetch", func(t *testing.T) { ts := mockRegistryCheck(t) defer ts.Close() - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() config.SetFederation(pelican_url.FederationDiscovery{ RegistryEndpoint: ts.URL, @@ -105,7 +104,7 @@ func TestFetchRegStatus(t *testing.T) { w.WriteHeader(http.StatusNotFound) })) defer ts.Close() - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() config.SetFederation(pelican_url.FederationDiscovery{ RegistryEndpoint: ts.URL, @@ -125,7 +124,7 @@ func TestFetchRegStatus(t *testing.T) { w.WriteHeader(http.StatusInternalServerError) })) defer ts.Close() - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() config.SetFederation(pelican_url.FederationDiscovery{ RegistryEndpoint: ts.URL, @@ -139,11 +138,11 @@ func TestFetchRegStatus(t *testing.T) { func TestWrapExportsByStatus(t *testing.T) { t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() }) - viper.Reset() + server_utils.ResetTestState() config.SetFederation(pelican_url.FederationDiscovery{ RegistryEndpoint: "https://mock-registry.org", }) @@ -191,7 +190,7 @@ func TestWrapExportsByStatus(t *testing.T) { }) t.Run("partial-cached", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ts := mockRegistryCheck(t) defer ts.Close() config.ResetFederationForTest() @@ -237,6 +236,6 @@ func TestWrapExportsByStatus(t *testing.T) { assert.EqualValues(t, expected, got) registrationsStatus.DeleteAll() - viper.Reset() + server_utils.ResetTestState() }) } diff --git a/registry/client_commands_test.go b/registry/client_commands_test.go index b2449f594..400e9eb8d 100644 --- a/registry/client_commands_test.go +++ b/registry/client_commands_test.go @@ -35,6 +35,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) @@ -71,7 +72,7 @@ func TestServeNamespaceRegistry(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() svr := registryMockup(ctx, t, "serveregistry") defer func() { @@ -140,7 +141,7 @@ func TestServeNamespaceRegistry(t *testing.T) { stdoutCapture = string(capturedOutput[:n]) assert.Equal(t, "[]\n", stdoutCapture) }) - viper.Reset() + server_utils.ResetTestState() } func TestRegistryKeyChainingOSDF(t *testing.T) { @@ -148,7 +149,7 @@ func TestRegistryKeyChainingOSDF(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() _, err := config.SetPreferredPrefix(config.OsdfPrefix) assert.NoError(t, err) viper.Set("Federation.DirectorUrl", "https://osdf-director.osg-htc.org") @@ -238,7 +239,7 @@ func TestRegistryKeyChainingOSDF(t *testing.T) { _, err = config.SetPreferredPrefix(config.PelicanPrefix) assert.NoError(t, err) - viper.Reset() + server_utils.ResetTestState() } func TestRegistryKeyChaining(t *testing.T) { @@ -246,7 +247,7 @@ func TestRegistryKeyChaining(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() // On by default, but just to make things explicit viper.Set("Registry.RequireKeyChaining", true) @@ -297,5 +298,5 @@ func TestRegistryKeyChaining(t *testing.T) { err = NamespaceRegister(privKey, registrySvr.URL+"/api/v1.0/registry", "", "/foo", "") require.NoError(t, err) - viper.Reset() + server_utils.ResetTestState() } diff --git a/registry/registry_db_test.go b/registry/registry_db_test.go index ccb38334e..402887f0e 100644 --- a/registry/registry_db_test.go +++ b/registry/registry_db_test.go @@ -39,6 +39,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) @@ -805,7 +806,7 @@ func topologyMockup(t *testing.T, namespaces []string) *httptest.Server { } func TestRegistryTopology(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() topoNamespaces := []string{"/topo/foo", "/topo/bar"} svr := topologyMockup(t, topoNamespaces) @@ -894,7 +895,7 @@ func TestRegistryTopology(t *testing.T) { require.NoError(t, err) require.False(t, exists) - viper.Reset() + server_utils.ResetTestState() } func TestGetTopoPrefixString(t *testing.T) { diff --git a/registry/registry_test.go b/registry/registry_test.go index e3e47d6f5..43ad3d7e0 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -36,6 +36,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/pelican_url" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) @@ -81,7 +82,7 @@ func TestHandleWildcard(t *testing.T) { }) t.Run("match-wildcard-metadataHandler", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() mockPrefix := "/testnamespace/foo" setupMockRegistryDB(t) @@ -151,7 +152,7 @@ func TestHandleWildcard(t *testing.T) { for _, tc := range mockApprovalTcs { t.Run(tc.Name, func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("Registry.RequireCacheApproval", tc.CacheApprovedOnly) viper.Set("Registry.RequireOriginApproval", tc.OriginApprovedOnly) @@ -197,7 +198,7 @@ func TestCheckNamespaceCompleteHandler(t *testing.T) { router.POST("/namespaces/check/status", checkStatusHandler) t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() }) @@ -253,7 +254,7 @@ func TestCheckNamespaceCompleteHandler(t *testing.T) { t.Run("incomplete-registration", func(t *testing.T) { resetNamespaceDB(t) - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() config.SetFederation(pelican_url.FederationDiscovery{ RegistryEndpoint: "https://registry.org", @@ -291,7 +292,7 @@ func TestCheckNamespaceCompleteHandler(t *testing.T) { t.Run("complete-registration", func(t *testing.T) { resetNamespaceDB(t) - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() config.SetFederation(pelican_url.FederationDiscovery{ RegistryEndpoint: "https://registry.org", @@ -337,7 +338,7 @@ func TestCheckNamespaceCompleteHandler(t *testing.T) { t.Run("multiple-complete-registrations", func(t *testing.T) { resetNamespaceDB(t) - viper.Reset() + server_utils.ResetTestState() config.ResetFederationForTest() config.SetFederation(pelican_url.FederationDiscovery{ RegistryEndpoint: "https://registry.org", diff --git a/registry/registry_ui_test.go b/registry/registry_ui_test.go index 5790ec27c..d680b9210 100644 --- a/registry/registry_ui_test.go +++ b/registry/registry_ui_test.go @@ -40,6 +40,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/param" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" "github.com/pelicanplatform/pelican/token" "github.com/pelicanplatform/pelican/token_scopes" @@ -56,7 +57,7 @@ func mockAdminToken() (string, error) { } func TestListNamespaces(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() ctx, cancel, egrp := test_utils.TestContext(context.Background(), t) defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() @@ -310,7 +311,7 @@ func TestListNamespaces(t *testing.T) { } func TestListNamespacesForUser(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() _, cancel, egrp := test_utils.TestContext(context.Background(), t) defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() @@ -720,10 +721,10 @@ func TestUpdateNamespaceStatus(t *testing.T) { } func TestCreateNamespace(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() customRegFieldsConfigs = []customRegFieldsConfig{} }) @@ -805,7 +806,7 @@ func TestCreateNamespace(t *testing.T) { }) t.Run("missing-institution-returns-400", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("Registry.Institutions", []map[string]string{{"name": "Mock School", "id": "123"}}) resetNamespaceDB(t) jwks, err := test_utils.GenerateJWKS() @@ -825,7 +826,7 @@ func TestCreateNamespace(t *testing.T) { require.NoError(t, err) assert.Equal(t, http.StatusBadRequest, w.Result().StatusCode) assert.Contains(t, string(body), "Validation for Institution failed:") - viper.Reset() + server_utils.ResetTestState() }) t.Run("invalid-prefix-returns-400", func(t *testing.T) { @@ -914,7 +915,7 @@ func TestCreateNamespace(t *testing.T) { }) t.Run("key-chaining-failure-returns-400", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("Registry.RequireKeyChaining", true) resetNamespaceDB(t) @@ -941,7 +942,7 @@ func TestCreateNamespace(t *testing.T) { require.NoError(t, err) assert.Equal(t, http.StatusBadRequest, w.Result().StatusCode) assert.Contains(t, string(body), "Cannot register a namespace that is suffixed or prefixed by an already-registered namespace unless the incoming public key matches a registered key") - viper.Reset() + server_utils.ResetTestState() }) t.Run("inst-failure-returns-400", func(t *testing.T) { @@ -1094,7 +1095,7 @@ func TestCreateNamespace(t *testing.T) { assert.Equal(t, "admin", nss[0].AdminMetadata.UserID) assert.Equal(t, server_structs.RegPending, nss[0].AdminMetadata.Status) assert.NotEqual(t, time.Time{}, nss[0].AdminMetadata.CreatedAt) - viper.Reset() + server_utils.ResetTestState() }) t.Run("osdf-topology-same-prefix-request-gives-200", func(t *testing.T) { @@ -1136,14 +1137,14 @@ func TestCreateNamespace(t *testing.T) { assert.Equal(t, "admin", nss[0].AdminMetadata.UserID) assert.Equal(t, server_structs.RegPending, nss[0].AdminMetadata.Status) assert.NotEqual(t, time.Time{}, nss[0].AdminMetadata.CreatedAt) - viper.Reset() + server_utils.ResetTestState() }) } func TestUpdateNamespaceHandler(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() }) _, cancel, egrp := test_utils.TestContext(context.Background(), t) defer func() { require.NoError(t, egrp.Wait()) }() @@ -1388,7 +1389,7 @@ func TestUpdateNamespaceHandler(t *testing.T) { } func TestListInsitutions(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() router := gin.Default() router.GET("/institutions", listInstitutions) @@ -1408,7 +1409,7 @@ func TestListInsitutions(t *testing.T) { }) t.Run("cache-hit-returns", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() mockUrl := url.URL{Scheme: "https", Host: "example.com"} viper.Set("Registry.InstitutionsUrl", mockUrl.String()) mockInsts := []registrationFieldOption{{Name: "Foo", ID: "001"}} @@ -1432,7 +1433,7 @@ func TestListInsitutions(t *testing.T) { }) t.Run("nil-cache-with-nonnil-config-returns", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() optionsCache.DeleteAll() mockInstsConfig := []registrationFieldOption{{Name: "foo", ID: "bar"}} @@ -1456,7 +1457,7 @@ func TestListInsitutions(t *testing.T) { }) t.Run("non-nil-cache-with-nonnil-config-return-config", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() mockUrl := url.URL{Scheme: "https", Host: "example.com"} viper.Set("Registry.InstitutionsUrl", mockUrl.String()) mockInsts := []registrationFieldOption{{Name: "Foo", ID: "001"}} diff --git a/registry/registry_validation_test.go b/registry/registry_validation_test.go index 5f3d83349..469844e99 100644 --- a/registry/registry_validation_test.go +++ b/registry/registry_validation_test.go @@ -27,6 +27,7 @@ import ( "github.com/stretchr/testify/require" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) @@ -200,12 +201,12 @@ func TestValidateCustomFields(t *testing.T) { } func TestValidateKeyChaining(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() setupMockRegistryDB(t) defer func() { resetNamespaceDB(t) teardownMockNamespaceDB(t) - viper.Reset() + server_utils.ResetTestState() }() _, jwksFoo, jwksStrFoo, err := test_utils.GenerateJWK() diff --git a/server_utils/origin_test.go b/server_utils/origin_test.go index 383b35085..f31d9687a 100644 --- a/server_utils/origin_test.go +++ b/server_utils/origin_test.go @@ -82,13 +82,11 @@ func setup(t *testing.T, config string) []OriginExport { // tests an origin configuration that mimics what you could do with env vars due to the // fact that we don't use a yaml list func TestGetExports(t *testing.T) { - viper.Reset() - ResetOriginExports() + ResetTestState() // Posix tests t.Run("testSingleExportValid", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, envVarMimicConfig) assert.Len(t, exports, 1, "expected 1 export") @@ -101,8 +99,7 @@ func TestGetExports(t *testing.T) { }) t.Run("testMultiExportValid", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, multiExportValidConfig) assert.Len(t, exports, 2, "expected 2 exports") @@ -134,8 +131,7 @@ func TestGetExports(t *testing.T) { }) t.Run("testExportVolumesValid", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, exportVolumesValidConfig) assert.Len(t, exports, 2, "expected 2 exports") @@ -169,8 +165,7 @@ func TestGetExports(t *testing.T) { // When we have a single export volume, we also set a few viper variables that can be // used by sections of code that assume a single export. Test that those are set properly t.Run("testExportVolumesSingle", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, exportSingleVolumeConfig) assert.Len(t, exports, 1, "expected 1 export") @@ -198,8 +193,7 @@ func TestGetExports(t *testing.T) { }) t.Run("testSingleExportBlock", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, singleExportBlockConfig) assert.Len(t, exports, 1, "expected 1 export") @@ -227,8 +221,7 @@ func TestGetExports(t *testing.T) { }) t.Run("testInvalidExport", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() viper.Set("Origin.StorageType", "posix") viper.Set("Origin.ExportVolumes", "") @@ -236,14 +229,14 @@ func TestGetExports(t *testing.T) { assert.Error(t, err) assert.ErrorIs(t, err, ErrInvalidOriginConfig) - viper.Reset() + ResetTestState() viper.Set("Origin.StorageType", "posix") viper.Set("Origin.ExportVolumes", "foo") _, err = GetOriginExports() require.Error(t, err) assert.ErrorIs(t, err, ErrInvalidOriginConfig) - viper.Reset() + ResetTestState() viper.Set("Origin.StorageType", "blah") _, err = GetOriginExports() assert.Error(t, err) @@ -252,8 +245,7 @@ func TestGetExports(t *testing.T) { // S3 tests t.Run("testSingleExportValidS3", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, s3envVarMimicConfig) @@ -270,8 +262,7 @@ func TestGetExports(t *testing.T) { }) t.Run("testMultiExportValidS3", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, s3multiExportValidConfig) assert.Len(t, exports, 2, "expected 2 exports") @@ -305,8 +296,7 @@ func TestGetExports(t *testing.T) { }) t.Run("testExportVolumesValidS3", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, s3exportVolumesValidConfig) assert.Len(t, exports, 2, "expected 2 exports") @@ -340,8 +330,7 @@ func TestGetExports(t *testing.T) { }) t.Run("testExportVolumesSingleS3", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, s3exportSingleVolumeConfig) assert.Len(t, exports, 1, "expected 1 export") @@ -374,8 +363,7 @@ func TestGetExports(t *testing.T) { }) t.Run("testSingleExportBlockS3", func(t *testing.T) { - defer viper.Reset() - defer ResetOriginExports() + defer ResetTestState() exports := setup(t, s3singleExportBlockConfig) assert.Len(t, exports, 1, "expected 1 export") diff --git a/server_utils/registry_test.go b/server_utils/registry_test.go index 413a8be6a..7cd777a88 100644 --- a/server_utils/registry_test.go +++ b/server_utils/registry_test.go @@ -30,7 +30,7 @@ import ( ) func TestGetNSIssuerURL(t *testing.T) { - viper.Reset() + ResetTestState() viper.Set("ConfigDir", t.TempDir()) config.InitConfig() require.NoError(t, config.InitClient()) @@ -39,11 +39,11 @@ func TestGetNSIssuerURL(t *testing.T) { url, err := GetNSIssuerURL("/test-prefix") assert.Equal(t, nil, err) assert.Equal(t, "https://registry.com:8446/api/v1.0/registry/test-prefix", url) - viper.Reset() + ResetTestState() } func TestGetJWKSURLFromIssuerURL(t *testing.T) { - viper.Reset() + ResetTestState() viper.Set("ConfigDir", t.TempDir()) config.InitConfig() require.NoError(t, config.InitClient()) diff --git a/server_utils/server_utils.go b/server_utils/server_utils.go index 45447d4c9..15fa6b58f 100644 --- a/server_utils/server_utils.go +++ b/server_utils/server_utils.go @@ -271,3 +271,10 @@ func LaunchWatcherMaintenance(ctx context.Context, dirPaths []string, descriptio } }) } + +// Reset the testing state, including: +// 1. viper settings, 2. preferred prefix, 3. transport object, 4. Federation metadata, 5. origin exports +func ResetTestState() { + config.ResetConfig() + ResetOriginExports() +} diff --git a/test_utils/utils.go b/test_utils/utils.go index 8312d5423..14dac3b47 100644 --- a/test_utils/utils.go +++ b/test_utils/utils.go @@ -191,8 +191,8 @@ func RegistryMockup(t *testing.T, prefix string) *httptest.Server { // avoid pulling in global configuration) and set some arbitrary // viper configurations func InitClient(t *testing.T, initCfg map[string]any) { - viper.Reset() - t.Cleanup(viper.Reset) + config.ResetConfig() + t.Cleanup(config.ResetConfig) viper.Set("ConfigDir", t.TempDir()) for key, val := range initCfg { viper.Set(key, val) diff --git a/token/token_create_test.go b/token/token_create_test.go index 52aa7b1ee..eb55c61e3 100644 --- a/token/token_create_test.go +++ b/token/token_create_test.go @@ -205,7 +205,7 @@ func TestCreateToken(t *testing.T) { defer cancel() // Some viper pre-requisites - viper.Reset() + config.ResetConfig() viper.Set("IssuerUrl", "https://my-issuer.com") tDir := t.TempDir() kfile := filepath.Join(tDir, "testKey") diff --git a/web_ui/authentication_test.go b/web_ui/authentication_test.go index e6b33f15e..d743721b1 100644 --- a/web_ui/authentication_test.go +++ b/web_ui/authentication_test.go @@ -42,6 +42,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/param" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" "github.com/pelicanplatform/pelican/token" "github.com/pelicanplatform/pelican/token_scopes" @@ -53,7 +54,7 @@ func TestWaitUntilLogin(t *testing.T) { defer cancel() dirName := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", dirName) config.InitConfig() err := config.InitServer(ctx, server_structs.OriginType) @@ -102,7 +103,7 @@ func TestCodeBasedLogin(t *testing.T) { defer cancel() dirName := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", dirName) config.InitConfig() err := config.InitServer(ctx, server_structs.OriginType) @@ -158,7 +159,7 @@ func TestPasswordResetAPI(t *testing.T) { defer cancel() dirName := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", dirName) viper.Set("Origin.Port", 8443) viper.Set("Server.UIPasswordFile", tempPasswdFile.Name()) @@ -300,7 +301,7 @@ func TestPasswordBasedLoginAPI(t *testing.T) { defer cancel() dirName := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", dirName) config.InitConfig() viper.Set("Server.UIPasswordFile", tempPasswdFile.Name()) @@ -418,7 +419,7 @@ func TestWhoamiAPI(t *testing.T) { defer cancel() dirName := t.TempDir() - viper.Reset() + server_utils.ResetTestState() config.InitConfig() viper.Set("ConfigDir", dirName) viper.Set("Server.UIPasswordFile", tempPasswdFile.Name()) @@ -585,7 +586,7 @@ func TestAdminAuthHandler(t *testing.T) { if tc.expectedError != "" { assert.Contains(t, w.Body.String(), tc.expectedError) } - viper.Reset() + server_utils.ResetTestState() }) } } @@ -596,7 +597,7 @@ func TestLogoutAPI(t *testing.T) { defer cancel() dirName := t.TempDir() - viper.Reset() + server_utils.ResetTestState() config.InitConfig() viper.Set("ConfigDir", dirName) viper.Set("Server.UIPasswordFile", tempPasswdFile.Name()) @@ -677,10 +678,10 @@ func TestListOIDCEnabledServersHandler(t *testing.T) { router := gin.New() router.GET("/oauth", listOIDCEnabledServersHandler) t.Cleanup(func() { - viper.Reset() + server_utils.ResetTestState() }) t.Run("registry-included-by-default", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() expected := OIDCEnabledServerRes{ODICEnabledServers: []string{"registry"}} req, err := http.NewRequest("GET", "/oauth", nil) assert.NoError(t, err) @@ -701,7 +702,7 @@ func TestListOIDCEnabledServersHandler(t *testing.T) { }) t.Run("origin-included-if-flag-is-on", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("Origin.EnableOIDC", true) expected := OIDCEnabledServerRes{ODICEnabledServers: []string{"registry", "origin"}} req, err := http.NewRequest("GET", "/oauth", nil) @@ -723,7 +724,7 @@ func TestListOIDCEnabledServersHandler(t *testing.T) { }) t.Run("cache-included-if-flag-is-on", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("Cache.EnableOIDC", true) expected := OIDCEnabledServerRes{ODICEnabledServers: []string{"registry", "cache"}} req, err := http.NewRequest("GET", "/oauth", nil) @@ -745,7 +746,7 @@ func TestListOIDCEnabledServersHandler(t *testing.T) { }) t.Run("director-included-if-flag-is-on", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("Director.EnableOIDC", true) expected := OIDCEnabledServerRes{ODICEnabledServers: []string{"registry", "director"}} req, err := http.NewRequest("GET", "/oauth", nil) @@ -767,7 +768,7 @@ func TestListOIDCEnabledServersHandler(t *testing.T) { }) t.Run("origin-cache-both-included-if-flags-are-on", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("Origin.EnableOIDC", true) viper.Set("Cache.EnableOIDC", true) viper.Set("Director.EnableOIDC", true) diff --git a/web_ui/engine_test.go b/web_ui/engine_test.go index 58f8378fd..c828331cb 100644 --- a/web_ui/engine_test.go +++ b/web_ui/engine_test.go @@ -42,13 +42,14 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/param" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" ) // Setup a gin engine that will serve up a /ping endpoint on a Unix domain socket. func setupPingEngine(t *testing.T, ctx context.Context, egrp *errgroup.Group) (chan bool, context.CancelFunc, string) { dirname := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("Logging.Level", "Debug") viper.Set("ConfigDir", dirname) viper.Set("Server.WebPort", 8444) diff --git a/web_ui/prometheus_test.go b/web_ui/prometheus_test.go index afc1fc52e..22197aade 100644 --- a/web_ui/prometheus_test.go +++ b/web_ui/prometheus_test.go @@ -39,6 +39,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/param" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/test_utils" "github.com/pelicanplatform/pelican/token" "github.com/pelicanplatform/pelican/token_scopes" @@ -49,7 +50,7 @@ func TestPrometheusUnprotected(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() av1 := route.New().WithPrefix("/api/v1.0/prometheus") av1.Get("/query", func(w http.ResponseWriter, r *http.Request) { @@ -102,7 +103,7 @@ func TestPrometheusProtectionCookieAuth(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() av1 := route.New().WithPrefix("/api/v1.0/prometheus") @@ -161,7 +162,7 @@ func TestPrometheusProtectionOriginHeaderScope(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() viper.Set("Server.ExternalWebUrl", "https://test-origin.org:8444") viper.Set("Monitoring.PromQLAuthorization", true) diff --git a/web_ui/ui_test.go b/web_ui/ui_test.go index a0c507ff1..a57c8e1e2 100644 --- a/web_ui/ui_test.go +++ b/web_ui/ui_test.go @@ -41,6 +41,7 @@ import ( "github.com/pelicanplatform/pelican/config" "github.com/pelicanplatform/pelican/param" "github.com/pelicanplatform/pelican/server_structs" + "github.com/pelicanplatform/pelican/server_utils" "github.com/pelicanplatform/pelican/token" "github.com/pelicanplatform/pelican/token_scopes" ) @@ -208,12 +209,12 @@ func TestHandleWebUIAuth(t *testing.T) { }) t.Run("403-for-logged-in-non-admin-user", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() // We let the frontend to handle unauthorized user (if the password is initialzied) setupTestAuthDB(t) t.Cleanup(func() { cleanupAuthDB() - viper.Reset() + server_utils.ResetTestState() }) tmpDir := t.TempDir() diff --git a/web_ui/ui_unix_test.go b/web_ui/ui_unix_test.go index 09256f5a8..38f109ed4 100644 --- a/web_ui/ui_unix_test.go +++ b/web_ui/ui_unix_test.go @@ -31,10 +31,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/tg123/go-htpasswd" + + "github.com/pelicanplatform/pelican/server_utils" ) func TestDoReload(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() savedAuthDB := authDB.Load() authDB.Store(nil) defer authDB.Store(savedAuthDB) diff --git a/xrootd/authorization_test.go b/xrootd/authorization_test.go index 64356e0ac..f1b69a8cd 100644 --- a/xrootd/authorization_test.go +++ b/xrootd/authorization_test.go @@ -164,7 +164,7 @@ func TestOSDFAuthRetrieval(t *testing.T) { transport.TLSClientConfig = oldConfig }) - viper.Reset() + server_utils.ResetTestState() viper.Set("Federation.TopologyUrl", "https://topology.opensciencegrid.org/") viper.Set("Server.Hostname", "sc-origin.chtc.wisc.edu") @@ -172,7 +172,7 @@ func TestOSDFAuthRetrieval(t *testing.T) { _, err := getOSDFAuthFiles(originServer) require.NoError(t, err, "error") - viper.Reset() + server_utils.ResetTestState() } func TestOSDFAuthCreation(t *testing.T) { @@ -261,10 +261,9 @@ func TestOSDFAuthCreation(t *testing.T) { for _, testInput := range tests { t.Run(testInput.desc, func(t *testing.T) { dirName := t.TempDir() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() viper.Set("Xrootd.Authfile", filepath.Join(dirName, "authfile")) viper.Set("Federation.TopologyUrl", ts.URL) @@ -301,7 +300,7 @@ func TestOSDFAuthCreation(t *testing.T) { require.NoError(t, err, "Error reading generated authfile") require.Equal(t, testInput.authOut, string(genAuth)) - viper.Reset() + server_utils.ResetTestState() }) } } @@ -338,10 +337,10 @@ func TestEmitAuthfile(t *testing.T) { for _, testInput := range tests { t.Run(testInput.desc, func(t *testing.T) { dirName := t.TempDir() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() + viper.Set("Xrootd.Authfile", filepath.Join(dirName, "authfile")) viper.Set("Origin.RunLocation", dirName) viper.Set("Origin.FederationPrefix", "/") @@ -364,10 +363,10 @@ func TestEmitAuthfile(t *testing.T) { func TestEmitCfg(t *testing.T) { dirname := t.TempDir() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() + viper.Set("Origin.RunLocation", dirname) err := config.InitClient() assert.Nil(t, err) @@ -444,10 +443,10 @@ func TestDeduplicateBasePaths(t *testing.T) { func TestLoadScitokensConfig(t *testing.T) { dirname := t.TempDir() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() + viper.Set("Origin.RunLocation", dirname) err := config.InitClient() assert.Nil(t, err) @@ -479,10 +478,10 @@ func TestLoadScitokensConfig(t *testing.T) { // Test that merging the configuration works without throwing any errors func TestMergeConfig(t *testing.T) { dirname := t.TempDir() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() + viper.Set("Origin.RunLocation", dirname) viper.Set("Origin.Port", 8443) viper.Set("Origin.StoragePrefix", "/") @@ -528,10 +527,10 @@ func TestGenerateConfig(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() + viper.Set("Origin.SelfTest", false) issuer, err := GenerateMonitoringIssuer() require.NoError(t, err) @@ -553,7 +552,7 @@ func TestGenerateConfig(t *testing.T) { assert.Equal(t, "/pelican/monitoring", issuer.BasePaths[0]) assert.Equal(t, "xrootd", issuer.DefaultUser) - viper.Reset() + server_utils.ResetTestState() viper.Set("Origin.SelfTest", false) viper.Set("Origin.ScitokensDefaultUser", "user1") viper.Set("Origin.ScitokensMapSubject", true) @@ -575,12 +574,12 @@ func TestGenerateConfig(t *testing.T) { } func TestWriteOriginAuthFiles(t *testing.T) { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + originAuthTester := func(server server_structs.XRootDServer, authStart string, authResult string) func(t *testing.T) { return func(t *testing.T) { - defer viper.Reset() - defer server_utils.ResetOriginExports() + defer server_utils.ResetTestState() + viper.Set("Origin.StorageType", "posix") dirname := t.TempDir() viper.Set("Origin.RunLocation", dirname) @@ -626,7 +625,7 @@ func TestWriteCacheAuthFiles(t *testing.T) { return func(t *testing.T) { dirname := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("Cache.RunLocation", dirname) if server.GetServerType().IsEnabled(server_structs.OriginType) { viper.Set("Xrootd.ScitokensConfig", filepath.Join(dirname, "scitokens-origin-generated.cfg")) @@ -735,7 +734,7 @@ func TestWriteOriginScitokensConfig(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() + server_utils.ResetTestState() dirname := t.TempDir() os.Setenv("PELICAN_ORIGIN_RUNLOCATION", dirname) defer os.Unsetenv("PELICAN_ORIGIN_RUNLOCATION") diff --git a/xrootd/fed_test.go b/xrootd/fed_test.go index 82bae9795..f46528122 100644 --- a/xrootd/fed_test.go +++ b/xrootd/fed_test.go @@ -50,11 +50,10 @@ var ( ) func TestHttpOriginConfig(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() viper.Set("ConfigDir", t.TempDir()) server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + defer server_utils.ResetTestState() body := "Hello, World!" srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/xrootd/origin_test.go b/xrootd/origin_test.go index e71aad452..e2c6a696d 100644 --- a/xrootd/origin_test.go +++ b/xrootd/origin_test.go @@ -152,10 +152,9 @@ func TestOrigin(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() viper.Set("Origin.StoragePrefix", t.TempDir()) viper.Set("Origin.FederationPrefix", "/test") @@ -191,10 +190,8 @@ func TestMultiExportOrigin(t *testing.T) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() - defer viper.Reset() - server_utils.ResetOriginExports() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + defer server_utils.ResetTestState() viper.SetConfigType("yaml") // Use viper to read in the embedded config @@ -239,10 +236,10 @@ func runS3Test(t *testing.T, bucketName, urlStyle, objectName string) { defer func() { require.NoError(t, egrp.Wait()) }() defer cancel() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() + federationPrefix := "/test" regionName := "us-east-1" serviceUrl := "https://s3.amazonaws.com" diff --git a/xrootd/xrootd_config_test.go b/xrootd/xrootd_config_test.go index 59a1b01ec..204324112 100644 --- a/xrootd/xrootd_config_test.go +++ b/xrootd/xrootd_config_test.go @@ -52,8 +52,8 @@ type xrootdTest struct { } func (x *xrootdTest) setup() { - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + dirname, err := os.MkdirTemp("", "tmpDir") require.NoError(x.T, err) x.T.Cleanup(func() { @@ -83,10 +83,10 @@ func TestXrootDOriginConfig(t *testing.T) { t.Cleanup(func() { os.RemoveAll(dirname) }) - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() + viper.Set("Configdir", dirname) viper.Set("Origin.RunLocation", dirname) viper.Set("Xrootd.RunLocation", dirname) @@ -117,7 +117,7 @@ func TestXrootDOriginConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "cms.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOriginCmsIncorrectConfig", func(t *testing.T) { @@ -131,7 +131,7 @@ func TestXrootDOriginConfig(t *testing.T) { configPath, err := ConfigXrootd(ctx, true) require.Error(t, err) assert.NotNil(t, configPath) - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOriginScitokensCorrectConfig", func(t *testing.T) { @@ -154,7 +154,7 @@ func TestXrootDOriginConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "scitokens.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOriginScitokensIncorrectConfig", func(t *testing.T) { @@ -168,7 +168,7 @@ func TestXrootDOriginConfig(t *testing.T) { configPath, err := ConfigXrootd(ctx, true) require.Error(t, err) assert.NotNil(t, configPath) - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOriginXrdCorrectConfig", func(t *testing.T) { @@ -191,7 +191,7 @@ func TestXrootDOriginConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "xrd.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOriginXrdIncorrectConfig", func(t *testing.T) { @@ -205,7 +205,7 @@ func TestXrootDOriginConfig(t *testing.T) { configPath, err := ConfigXrootd(ctx, true) require.Error(t, err) assert.NotNil(t, configPath) - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOriginXrootdCorrectConfig", func(t *testing.T) { @@ -228,7 +228,7 @@ func TestXrootDOriginConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "xrootd.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOriginXrootdIncorrectConfig", func(t *testing.T) { @@ -242,7 +242,7 @@ func TestXrootDOriginConfig(t *testing.T) { configPath, err := ConfigXrootd(ctx, true) require.Error(t, err) assert.NotNil(t, configPath) - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOsdfWithXRDHOSTAndPort", func(t *testing.T) { @@ -259,7 +259,7 @@ func TestXrootDOriginConfig(t *testing.T) { assert.NotNil(t, configPath) assert.Equal(t, "my-xrootd.com", os.Getenv("XRDHOST")) - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestOsdfWithXRDHOSTAndNoPort", func(t *testing.T) { @@ -276,7 +276,7 @@ func TestXrootDOriginConfig(t *testing.T) { assert.NotNil(t, configPath) assert.Equal(t, "my-xrootd.com", os.Getenv("XRDHOST")) - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestPelicanWithXRDHOST", func(t *testing.T) { @@ -294,7 +294,7 @@ func TestXrootDOriginConfig(t *testing.T) { _, xrdhostIsSet := os.LookupEnv("XRDHOST") assert.False(t, xrdhostIsSet, "XRDHOST should only be set in OSDF mode") - viper.Reset() + server_utils.ResetTestState() }) } @@ -308,8 +308,8 @@ func TestXrootDCacheConfig(t *testing.T) { t.Cleanup(func() { os.RemoveAll(dirname) }) - viper.Reset() - server_utils.ResetOriginExports() + server_utils.ResetTestState() + viper.Set("Cache.RunLocation", dirname) viper.Set("ConfigDir", dirname) config.InitConfig() @@ -318,8 +318,8 @@ func TestXrootDCacheConfig(t *testing.T) { assert.NotNil(t, configPath) t.Run("TestCacheThrottlePluginEnabled", func(t *testing.T) { - defer viper.Reset() - defer server_utils.ResetOriginExports() + defer server_utils.ResetTestState() + xrootd := xrootdTest{T: t} xrootd.setup() @@ -343,8 +343,8 @@ func TestXrootDCacheConfig(t *testing.T) { }) t.Run("TestCacheThrottlePluginDisabled", func(t *testing.T) { - defer viper.Reset() - defer server_utils.ResetOriginExports() + defer server_utils.ResetTestState() + xrootd := xrootdTest{T: t} xrootd.setup() @@ -384,7 +384,7 @@ func TestXrootDCacheConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "ofs.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestCacheOfsIncorrectConfig", func(t *testing.T) { @@ -420,7 +420,7 @@ func TestXrootDCacheConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "pfc.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestCachePfcIncorrectConfig", func(t *testing.T) { @@ -434,7 +434,7 @@ func TestXrootDCacheConfig(t *testing.T) { configPath, err := ConfigXrootd(ctx, true) require.Error(t, err) assert.NotNil(t, configPath) - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestCachePssCorrectConfig", func(t *testing.T) { @@ -457,7 +457,7 @@ func TestXrootDCacheConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "pss.setopt DebugLevel 4") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestCachePssIncorrectConfig", func(t *testing.T) { @@ -493,7 +493,7 @@ func TestXrootDCacheConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "scitokens.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestCacheScitokensIncorrectConfig", func(t *testing.T) { @@ -529,7 +529,7 @@ func TestXrootDCacheConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "xrd.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestCacheXrdIncorrectConfig", func(t *testing.T) { @@ -565,7 +565,7 @@ func TestXrootDCacheConfig(t *testing.T) { content, err := io.ReadAll(file) assert.NoError(t, err) assert.Contains(t, string(content), "xrootd.trace debug") - viper.Reset() + server_utils.ResetTestState() }) t.Run("TestCacheXrootdIncorrectConfig", func(t *testing.T) { @@ -589,10 +589,10 @@ func TestUpdateAuth(t *testing.T) { runDirname := t.TempDir() configDirname := t.TempDir() - viper.Reset() - server_utils.ResetOriginExports() - defer viper.Reset() - defer server_utils.ResetOriginExports() + server_utils.ResetTestState() + + defer server_utils.ResetTestState() + viper.Set("Logging.Level", "Debug") viper.Set("Origin.RunLocation", runDirname) viper.Set("ConfigDir", configDirname) @@ -686,7 +686,7 @@ func TestCopyCertificates(t *testing.T) { runDirname := t.TempDir() configDirname := t.TempDir() - viper.Reset() + server_utils.ResetTestState() viper.Set("Logging.Level", "Debug") viper.Set("Origin.RunLocation", runDirname) viper.Set("ConfigDir", configDirname) @@ -770,9 +770,9 @@ func TestCopyCertificates(t *testing.T) { } func TestAuthIntervalUnmarshal(t *testing.T) { - defer viper.Reset() + defer server_utils.ResetTestState() t.Run("test-minutes-to-seconds", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() var xrdConfig XrootdConfig viper.Set("Xrootd.AuthRefreshInterval", "5m") err := viper.Unmarshal(&xrdConfig, viper.DecodeHook(combinedDecodeHookFunc())) @@ -781,7 +781,7 @@ func TestAuthIntervalUnmarshal(t *testing.T) { }) t.Run("test-hours-to-seconds", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() var xrdConfig XrootdConfig viper.Set("Xrootd.AuthRefreshInterval", "24h") err := viper.Unmarshal(&xrdConfig, viper.DecodeHook(combinedDecodeHookFunc())) @@ -790,7 +790,7 @@ func TestAuthIntervalUnmarshal(t *testing.T) { }) t.Run("test-seconds-to-seconds", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() var xrdConfig XrootdConfig viper.Set("Xrootd.AuthRefreshInterval", "100s") err := viper.Unmarshal(&xrdConfig, viper.DecodeHook(combinedDecodeHookFunc())) @@ -799,7 +799,7 @@ func TestAuthIntervalUnmarshal(t *testing.T) { }) t.Run("test-less-than-60s", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() var xrdConfig XrootdConfig viper.Set("Xrootd.AuthRefreshInterval", "10") err := viper.Unmarshal(&xrdConfig, viper.DecodeHook(combinedDecodeHookFunc())) @@ -809,7 +809,7 @@ func TestAuthIntervalUnmarshal(t *testing.T) { }) t.Run("test-no-suffix-to-seconds", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() var xrdConfig XrootdConfig viper.Set("Xrootd.AuthRefreshInterval", "99") err := viper.Unmarshal(&xrdConfig, viper.DecodeHook(combinedDecodeHookFunc())) @@ -818,7 +818,7 @@ func TestAuthIntervalUnmarshal(t *testing.T) { }) t.Run("test-less-than-second", func(t *testing.T) { - viper.Reset() + server_utils.ResetTestState() var xrdConfig XrootdConfig viper.Set("Xrootd.AuthRefreshInterval", "0.5s") err := viper.Unmarshal(&xrdConfig, viper.DecodeHook(combinedDecodeHookFunc()))