Skip to content

Commit

Permalink
Merge pull request PelicanPlatform#1619 from h2zh/reset
Browse files Browse the repository at this point in the history
New Reset function
  • Loading branch information
bbockelm authored Oct 9, 2024
2 parents b921c4f + 4c935df commit c9ea4a2
Show file tree
Hide file tree
Showing 55 changed files with 415 additions and 397 deletions.
2 changes: 1 addition & 1 deletion broker/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion broker/token_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 4 additions & 3 deletions cache/advertise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions cache/cache_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down
27 changes: 11 additions & 16 deletions client/fed_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()

})
}

Expand All @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
71 changes: 33 additions & 38 deletions client/fed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions client/handle_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit c9ea4a2

Please sign in to comment.