Skip to content

Commit

Permalink
fix bug with initconfig and updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
joereuss12 committed Oct 23, 2023
1 parent 2c4311b commit 3f62f2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func GetTransport() *http.Transport {
}

func InitConfig() {
viper.SetConfigType("yaml")
// 1) Set up defaults.yaml
err := viper.MergeConfig(strings.NewReader(defaultsYaml))
if err != nil {
Expand All @@ -308,8 +309,8 @@ func InitConfig() {
cobra.CheckErr(err)
}
}
if viper.GetString("config") != "" {
viper.SetConfigFile(viper.GetString("config"))
if configFile := viper.GetString("config"); configFile != "" {
viper.SetConfigFile(configFile)
} else {
home, err := os.UserHomeDir()
if err != nil {
Expand All @@ -323,7 +324,7 @@ func InitConfig() {
viper.SetConfigName("pelican")
}

viper.SetEnvPrefix(GetPreferredPrefix())
viper.SetEnvPrefix(prefix)
viper.AutomaticEnv()
// This line allows viper to use an env var like ORIGIN_VALUE to override the viper string "Origin.Value"
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
Expand Down
36 changes: 26 additions & 10 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,38 @@ func TestDialerTimeout(t *testing.T) {
viper.Set("Transport.Dialer.Timeout", time.Second*10)
}

func TestDefaultsYaml(t *testing.T) {
InitConfig() // should set up pelican.yaml and defaults.yaml
// create a temp config file to use
func TestInitConfig(t *testing.T) {
// Set prefix to OSDF to ensure that config is being set
testingPreferredPrefix = "OSDF"

InitConfig() // Should set up pelican.yaml, osdf.yaml and defaults.yaml

// Create a temp config file to use
tempCfgFile, err := os.CreateTemp("", "pelican-*.yaml")
viper.Set("config", tempCfgFile.Name())
if err != nil {
t.Fatalf("Failed to make temp file: %v", err)
}
// Check if debug is false
assert.False(t, param.Debug.GetBool())
assert.False(t, viper.GetBool("Debug"))
viper.Set("Debug", true) // should write to temp config file (should have higher priority like how pelican.yaml behaves)

// Check if server address is correct by defaults.yaml
assert.True(t, param.Server_Address.GetString() == "0.0.0.0")
// Check that Federation Discovery url is correct by osdf.yaml
assert.True(t, param.Federation_DiscoveryUrl.GetString() == "osg-htc.org")

viper.Set("Server.Address", "1.1.1.1") // should write to temp config file
if err := viper.WriteConfigAs(tempCfgFile.Name()); err != nil {
t.Fatalf("Failed to write to config file: %v", err)
}
// Check if debug was set and is now true
assert.True(t, param.Debug.GetBool())
assert.True(t, viper.GetBool("Debug"))
viper.Reset()
viper.Set("config", tempCfgFile.Name()) // Set the temp file as the new 'pelican.yaml'
InitConfig()

// Check if server address overrides the default
assert.True(t, param.Server_Address.GetString() == "1.1.1.1")
viper.Reset()

//Test if prefix is not set, should not be able to find osdfYaml configuration
testingPreferredPrefix = ""
InitConfig()
assert.True(t, param.Federation_DiscoveryUrl.GetString() == "")
}

0 comments on commit 3f62f2f

Please sign in to comment.