Skip to content

Commit 787ec30

Browse files
author
Emma Turetsky
committed
Addressed PR comments, reworked export handling in NewFedTest
1 parent 58891f3 commit 787ec30

9 files changed

Lines changed: 101 additions & 93 deletions

File tree

client/sharing_url_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
"github.com/pelicanplatform/pelican/client"
3737
"github.com/pelicanplatform/pelican/config"
38+
"github.com/pelicanplatform/pelican/param"
3839
"github.com/pelicanplatform/pelican/test_utils"
3940
)
4041

@@ -98,9 +99,9 @@ func TestSharingUrl(t *testing.T) {
9899
defer os.Unsetenv("PELICAN_SKIP_TERMINAL_CHECK")
99100

100101
test_utils.InitClient(t, map[string]any{
101-
"Logging.Level": "debug",
102-
"TLSSkipVerify": true,
103-
"Federation.DiscoveryUrl": server.URL,
102+
param.Logging_Level.GetName(): "debug",
103+
param.TLSSkipVerify.GetName(): true,
104+
param.Federation_DiscoveryUrl.GetName(): server.URL,
104105
})
105106

106107
// Call QueryDirector with the test server URL and a source path

cmd/plugin_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -561,16 +561,10 @@ func TestPluginDirectRead(t *testing.T) {
561561
defer server_utils.ResetTestState()
562562

563563
dirName := t.TempDir()
564-
565-
viper.Set("Logging.Level", "debug")
566-
viper.Set("Origin.StorageType", "posix")
567-
viper.Set("Origin.FederationPrefix", "/test")
568-
viper.Set("Origin.StoragePrefix", "/<SOMETHING THAT WILL BE OVERRIDDEN>")
569-
viper.Set("Origin.EnablePublicReads", true)
570-
viper.Set("Origin.EnableDirectReads", true)
571564
// We are purposely creating a test with a config of a single-space.
572565
// The empty string indicates using the default config, which we don't want.
573-
fed := fed_test_utils.NewFedTest(t, " ")
566+
567+
fed := fed_test_utils.NewFedTest(t, publicTestOrigin)
574568
host := param.Server_Hostname.GetString() + ":" + strconv.Itoa(param.Server_WebPort.GetInt())
575569

576570
log.Debugln("Will create origin file at", fed.Exports[0].StoragePrefix)
@@ -851,14 +845,9 @@ func TestPluginRecursiveDownload(t *testing.T) {
851845

852846
dirName := t.TempDir()
853847

854-
viper.Set("Logging.Level", "debug")
855-
viper.Set("Origin.StorageType", "posix")
856-
viper.Set("Origin.FederationPrefix", "/test")
857-
viper.Set("Origin.StoragePrefix", "/<THIS WILL BE OVERRIDDEN>")
858-
viper.Set("Origin.EnablePublicReads", true)
859848
// We are purposely creating a test with a config of a single-space.
860849
// The empty string indicates using the default config, which we don't want.
861-
fed := fed_test_utils.NewFedTest(t, " ")
850+
fed := fed_test_utils.NewFedTest(t, publicTestOrigin)
862851
host := param.Server_Hostname.GetString() + ":" + strconv.Itoa(param.Server_WebPort.GetInt())
863852

864853
// Drop the testFileContent into the origin directory
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Origin export configuration to test full multi-export capabilities
1+
# Origin export configuration for testing
22

33
Origin:
44
# Things that configure the origin itself
@@ -8,5 +8,4 @@ Origin:
88
Exports:
99
- StoragePrefix: /<SHOULD BE OVERRIDDEN>
1010
FederationPrefix: /test
11-
# Don't set Reads -- it should be toggled true by setting PublicReads
1211
Capabilities: ["PublicReads", "Writes", "DirectReads", "Listings"]

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ func SetServerDefaults(v *viper.Viper) error {
12511251
// is thrown
12521252
func InitServer(ctx context.Context, currentServers server_structs.ServerType) error {
12531253
InitConfigInternal()
1254+
logging.FlushLogs(true)
12541255
setEnabledServer(currentServers)
12551256

12561257
// Output warnings before the defaults are set. The SetServerDefaults function sets the default values
@@ -1814,6 +1815,7 @@ func ResetConfig() {
18141815
fedDiscoveryOnce = &sync.Once{}
18151816
globalFedInfo = pelican_url.FederationDiscovery{}
18161817
globalFedErr = nil
1818+
18171819
setServerOnce = sync.Once{}
18181820

18191821
ResetIssuerPrivateKeys()

director/discovery_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************
22
*
3-
* Copyright (C) 2024, Pelican Project, Morgridge Institute for Research
3+
* Copyright (C) 2025, Pelican Project, Morgridge Institute for Research
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License"); you
66
* may not use this file except in compliance with the License. You may
@@ -29,7 +29,6 @@ import (
2929
"github.com/stretchr/testify/assert"
3030
"github.com/stretchr/testify/require"
3131

32-
"github.com/pelicanplatform/pelican/config"
3332
"github.com/pelicanplatform/pelican/pelican_url"
3433
"github.com/pelicanplatform/pelican/server_structs"
3534
"github.com/pelicanplatform/pelican/server_utils"
@@ -215,7 +214,6 @@ func TestOidcDiscoveryHandler(t *testing.T) {
215214
test_utils.InitClient(t, map[string]any{
216215
"Federation.DirectorUrl": tc.dirUrl,
217216
})
218-
require.NoError(t, config.InitClient())
219217

220218
w := httptest.NewRecorder()
221219
req, _ := http.NewRequest("GET", "/test"+oidcDiscoveryPath, nil)

director/fed_token_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ func parseJWT(tokenString string) (scopes []string, issuer string, err error) {
175175
}
176176

177177
func TestCreateFedTok(t *testing.T) {
178-
server_utils.ResetTestState()
179-
defer server_utils.ResetTestState()
180178

181179
testCases := []struct {
182180
name string
@@ -219,6 +217,9 @@ func TestCreateFedTok(t *testing.T) {
219217

220218
for _, tc := range testCases {
221219
t.Run(tc.name, func(t *testing.T) {
220+
server_utils.ResetTestState()
221+
defer server_utils.ResetTestState()
222+
222223
w := httptest.NewRecorder()
223224
c, _ := gin.CreateTestContext(w)
224225

fed_test_utils/fed.go

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/stretchr/testify/assert"
3939
"github.com/stretchr/testify/require"
4040
"golang.org/x/sync/errgroup"
41+
"gopkg.in/yaml.v3"
4142

4243
"github.com/pelicanplatform/pelican/config"
4344
"github.com/pelicanplatform/pelican/director"
@@ -157,60 +158,86 @@ func NewFedTest(t *testing.T, originConfig string) (ft *FedTest) {
157158
require.NoError(t, err)
158159

159160
// Read in any config we may have set
161+
var originConf interface{}
160162
if originConfig != "" {
161163
viper.SetConfigType("yaml")
162164
err = viper.MergeConfig(strings.NewReader(originConfig))
163165
require.NoError(t, err, "error reading config")
166+
167+
err := yaml.Unmarshal([]byte(originConfig), &originConf)
168+
require.NoError(t, err, "error unmarshalling into interface")
169+
170+
confMap := originConf.(map[string]interface{})
171+
172+
originRaw, exists := confMap["Origin"]
173+
174+
if exists {
175+
originMap := originRaw.(map[string]interface{})
176+
177+
// Override the test directory from the config file with our temp directory
178+
if exportsRaw, ok := originMap["Exports"]; ok {
179+
for i, item := range exportsRaw.([]interface{}) {
180+
originDir, err := os.MkdirTemp("", fmt.Sprintf("Export%d", i))
181+
assert.NoError(t, err)
182+
t.Cleanup(func() {
183+
err := os.RemoveAll(originDir)
184+
require.NoError(t, err)
185+
})
186+
187+
exportMap := item.(map[string]interface{})
188+
exportMap["StoragePrefix"] = originDir
189+
190+
// Change the permissions of the temporary origin directory
191+
permissions = os.FileMode(0755)
192+
err = os.Chmod(originDir, permissions)
193+
require.NoError(t, err)
194+
195+
// Change ownership on the temporary origin directory so files can be uploaded
196+
uinfo, err := config.GetDaemonUserInfo()
197+
require.NoError(t, err)
198+
require.NoError(t, os.Chown(originDir, uinfo.Uid, uinfo.Gid))
199+
200+
// Start off with a Hello World file we can use for testing in each of our exports
201+
err = os.WriteFile(filepath.Join(originDir, "hello_world.txt"), []byte("Hello, World!"), os.FileMode(0644))
202+
require.NoError(t, err)
203+
}
204+
} else {
205+
originDir, err := os.MkdirTemp("", fmt.Sprintf("Export%s", "test"))
206+
assert.NoError(t, err)
207+
t.Cleanup(func() {
208+
err := os.RemoveAll(originDir)
209+
require.NoError(t, err)
210+
})
211+
212+
originMap["StoragePrefix"] = originDir
213+
214+
permissions = os.FileMode(0755)
215+
err = os.Chmod(originDir, permissions)
216+
require.NoError(t, err)
217+
218+
// Change ownership on the temporary origin directory so files can be uploaded
219+
uinfo, err := config.GetDaemonUserInfo()
220+
require.NoError(t, err)
221+
require.NoError(t, os.Chown(originDir, uinfo.Uid, uinfo.Gid))
222+
223+
// Start off with a Hello World file we can use for testing in the StoragePrefix
224+
err = os.WriteFile(filepath.Join(originDir, "hello_world.txt"), []byte("Hello, World!"), os.FileMode(0644))
225+
require.NoError(t, err)
226+
}
227+
}
164228
}
165-
// Now call GetOriginExports and check the struct
166-
exports, err := server_utils.GetOriginExports()
167-
require.NoError(t, err, "error getting origin exports")
168-
ft.Exports = exports
169-
170-
// Override the test directory from the config file with our temp directory
171-
for i := 0; i < len(ft.Exports); i++ {
172-
originDir, err := os.MkdirTemp("", fmt.Sprintf("Export%d", i))
173-
assert.NoError(t, err)
174-
t.Cleanup(func() {
175-
err := os.RemoveAll(originDir)
176-
require.NoError(t, err)
177-
})
178-
179-
// Set the storage prefix to the temporary origin directory
180-
ft.Exports[i].StoragePrefix = originDir
181-
// Our exports object becomes global -- we must reset in between each fed test
182-
t.Cleanup(func() {
183-
server_utils.ResetOriginExports()
184-
})
185-
186-
// Change the permissions of the temporary origin directory
187-
permissions = os.FileMode(0755)
188-
err = os.Chmod(originDir, permissions)
189-
require.NoError(t, err)
190229

191-
// Change ownership on the temporary origin directory so files can be uploaded
192-
uinfo, err := config.GetDaemonUserInfo()
193-
require.NoError(t, err)
194-
require.NoError(t, os.Chown(originDir, uinfo.Uid, uinfo.Gid))
230+
confDir := t.TempDir()
231+
outputPath := filepath.Join(confDir, "tempfile_*.yaml")
195232

196-
// Start off with a Hello World file we can use for testing in each of our exports
197-
err = os.WriteFile(filepath.Join(originDir, "hello_world.txt"), []byte("Hello, World!"), os.FileMode(0644))
198-
require.NoError(t, err)
199-
}
200-
originConf := strings.NewReader(originConfig)
233+
outputData, err := yaml.Marshal(&originConf)
234+
require.NoError(t, err, "error marshalling struct into yaml format")
201235

202-
tmpFile, err := os.CreateTemp("", "tempfile_*.yaml")
203-
if err != nil {
204-
t.Fatalf("Error creating temporary config file for fed test: %v", err)
205-
}
206-
defer tmpFile.Close()
236+
err = os.WriteFile(outputPath, outputData, 0644)
237+
require.NoError(t, err, "error writing out temporary config file for fed test")
207238

208-
_, err = io.Copy(tmpFile, originConf)
209-
if err != nil {
210-
t.Fatalf("Error writing to temporary config file for fed test: %v", err)
211-
}
239+
viper.Set("config", outputPath)
212240

213-
viper.Set("config", tmpFile.Name())
214241
servers, _, err := launchers.LaunchModules(ctx, modules)
215242
require.NoError(t, err)
216243

@@ -275,6 +302,9 @@ func NewFedTest(t *testing.T, originConfig string) (ft *FedTest) {
275302

276303
ft.Token = token
277304

305+
ft.Exports, err = server_utils.GetOriginExports()
306+
require.NoError(t, err)
307+
278308
// Explicitly run tmpPath cleanup AFTER cancel and egrp are done -- otherwise we end up
279309
// with a race condition where removing tmpPath might happen while the server is still
280310
// using it, resulting in "error: unlinkat <tmpPath>: directory not empty"

xrootd/authorization_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,8 @@ func TestEmitCfg(t *testing.T) {
375375

376376
defer server_utils.ResetTestState()
377377

378-
test_utils.InitClient(t, map[string]any{
379-
"Origin.RunLocation": dirname,
380-
})
378+
test_utils.InitClient(t, map[string]any{})
379+
viper.Set(param.Origin_RunLocation.GetName(), dirname)
381380

382381
configTester := func(cfg *ScitokensCfg, configResult string) func(t *testing.T) {
383382
return func(t *testing.T) {
@@ -455,9 +454,9 @@ func TestLoadScitokensConfig(t *testing.T) {
455454

456455
defer server_utils.ResetTestState()
457456

458-
test_utils.InitClient(t, map[string]any{
459-
"Origin.RunLocation": dirname,
460-
})
457+
test_utils.InitClient(t, map[string]any{})
458+
459+
viper.Set(param.Origin_RunLocation.GetName(), dirname)
461460

462461
configTester := func(configResult string) func(t *testing.T) {
463462
return func(t *testing.T) {
@@ -490,16 +489,16 @@ func TestMergeConfig(t *testing.T) {
490489

491490
defer server_utils.ResetTestState()
492491

493-
viper.Set("Origin.RunLocation", dirname)
494-
viper.Set("Origin.Port", 8443)
495-
viper.Set("Origin.StoragePrefix", "/")
496-
viper.Set("Origin.FederationPrefix", "/")
492+
viper.Set(param.Origin_RunLocation.GetName(), dirname)
493+
viper.Set(param.Origin_Port.GetName(), 8443)
494+
viper.Set(param.Origin_StoragePrefix.GetName(), "/")
495+
viper.Set(param.Origin_FederationPrefix.GetName(), "/")
497496
// We don't inherit any defaults at this level of code -- in order to recognize
498497
// that this is an authorized prefix, we must set either EnableReads && !EnablePublicReads
499498
// or EnableWrites
500-
viper.Set("Origin.EnableReads", true)
499+
viper.Set(param.Origin_EnableReads.GetName(), true)
501500
scitokensConfigFile := filepath.Join(dirname, "scitokens-input.cfg")
502-
viper.Set("Xrootd.ScitokensConfig", scitokensConfigFile)
501+
viper.Set(param.Xrootd_ScitokensConfig.GetName(), scitokensConfigFile)
503502

504503
configTester := func(configInput string, postProcess func(*testing.T, ScitokensCfg)) func(t *testing.T) {
505504
return func(t *testing.T) {

xrootd/xrootd_config_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,17 @@ import (
4848

4949
func setupXrootd(t *testing.T, ctx context.Context, server server_structs.ServerType) {
5050
tmpDir := t.TempDir()
51+
server_utils.ResetTestState()
5152

5253
viper.Set("ConfigDir", tmpDir)
5354
viper.Set("Xrootd.RunLocation", tmpDir)
5455
viper.Set("Cache.RunLocation", tmpDir)
5556
viper.Set("Origin.RunLocation", tmpDir)
56-
57-
server_utils.ResetTestState()
58-
59-
dirname, err := os.MkdirTemp("", "tmpDir")
60-
require.NoError(t, err)
61-
t.Cleanup(func() {
62-
os.RemoveAll(dirname)
63-
})
64-
viper.Set("ConfigDir", dirname)
65-
viper.Set("Xrootd.RunLocation", dirname)
66-
viper.Set("Cache.RunLocation", dirname)
67-
viper.Set("Origin.RunLocation", dirname)
6857
viper.Set("Origin.StoragePrefix", "/")
6958
viper.Set("Origin.FederationPrefix", "/")
7059
viper.Set("Server.IssuerUrl", "https://my-xrootd.com:8444")
7160

72-
err = config.InitServer(ctx, server)
61+
err := config.InitServer(ctx, server)
7362
require.NoError(t, err)
7463

7564
}

0 commit comments

Comments
 (0)