Skip to content

Commit

Permalink
Fix config tests to inherit logging levels and strip color codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhiemstrawisc committed Feb 10, 2025
1 parent 4e37d4e commit d78f658
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions cmd/config_printer/config_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"os"
"regexp"
"strings"
"testing"

Expand All @@ -33,22 +34,30 @@ import (
"github.com/pelicanplatform/pelican/config"
)

// Config commands use ansi encoding for color output. This function strips
// those characters for easier comparison of expected and actual output.
func stripAnsiCodes(input string) string {
ansiEscape := regexp.MustCompile(`\x1b\[[0-9;]*m`)
return ansiEscape.ReplaceAllString(input, "")
}

// Mock configuration setup
func setupMockConfig(t *testing.T) error {
// Setting Non-default values, mimics what we'd get from config file
viper.Set("Logging.Level", "trace")
viper.Set("Logging.Cache.Http", "info")
viper.Set("Logging.Cache.Xrootd", "info")
viper.Set("Logging.Origin.Http", "info")

// Set default config
config.SetBaseDefaultsInConfig(viper.GetViper())
viper.Set("ConfigDir", t.TempDir())
config.InitConfig()
if err := config.SetServerDefaults(viper.GetViper()); err != nil {
return err
}
if err := config.SetClientDefaults(viper.GetViper()); err != nil {
return err
}
// Setting Non-default values
viper.Set("Logging.Cache.Http", "info")
viper.Set("Logging.Cache.Xrootd", "info")
viper.Set("Logging.Level", "trace")
viper.Set("Logging.Origin.Http", "info")

return nil
}
Expand Down Expand Up @@ -119,7 +128,7 @@ func TestConfigGet(t *testing.T) {
<-done
os.Stdout = oldStdout

got := strings.TrimSpace(strings.ToLower(buf.String()))
got := strings.TrimSpace(strings.ToLower(stripAnsiCodes(buf.String())))

for _, expected := range expectedLines {
expectedLower := strings.ToLower(expected)
Expand Down Expand Up @@ -185,7 +194,7 @@ func TestConfigSummary(t *testing.T) {
<-done
os.Stdout = oldStdout

got := strings.TrimSpace(strings.ToLower(buf.String()))
got := strings.TrimSpace(strings.ToLower(stripAnsiCodes(buf.String())))

expectedLines := []string{`logging:`, ` cache:`, ` origin:`, ` level: trace`, ` http: info`}
notExpectedLines := []string{`debug: true`}
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ func SetServerDefaults(v *viper.Viper) error {
}

// If Pelican is at its default error level, do our custom mapping
if log.GetLevel() == log.ErrorLevel {
if defaultLevel == log.ErrorLevel.String() {
v.SetDefault(param.Logging_Origin_Scitokens.GetName(), "fatal")
v.SetDefault(param.Logging_Origin_Xrootd.GetName(), "info")
v.SetDefault(param.Logging_Cache_Scitokens.GetName(), "fatal")
Expand Down

0 comments on commit d78f658

Please # to comment.