Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,15 @@ func setDefaults() {
viper.SetDefault("android.enabled", true)
viper.SetDefault("android.max_retry", 0)

// API defaults
viper.SetDefault("api.push_uri", "/api/push")
viper.SetDefault("api.stat_go_uri", "/api/stat/go")
viper.SetDefault("api.stat_app_uri", "/api/stat/app")
viper.SetDefault("api.config_uri", "/api/config")
viper.SetDefault("api.sys_stat_uri", "/sys/stats")
viper.SetDefault("api.metric_uri", "/metrics")
viper.SetDefault("api.health_uri", "/healthz")

// gRPC defaults
viper.SetDefault("grpc.enabled", false)
viper.SetDefault("grpc.port", "9000")
Expand All @@ -355,6 +364,15 @@ func setDefaults() {

// Stat defaults
viper.SetDefault("stat.engine", "memory")

// Log defaults
viper.SetDefault("log.format", "string")
viper.SetDefault("log.access_log", "stdout")
viper.SetDefault("log.access_level", "debug")
viper.SetDefault("log.error_log", "stderr")
viper.SetDefault("log.error_level", "error")
viper.SetDefault("log.hide_token", true)
viper.SetDefault("log.hide_messages", false)
}

// LoadConf load config from file and read in environment variables that match
Expand Down
112 changes: 108 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ func TestLoadConfigFromEnv(t *testing.T) {
os.Setenv("GORUSH_API_HEALTH_URI", "/healthz")
os.Setenv("GORUSH_CORE_FEEDBACK_HOOK_URL", "http://example.com")
os.Setenv("GORUSH_CORE_FEEDBACK_HEADER", "x-api-key:1234567890 x-auth-key:0987654321")

t.Cleanup(func() {
os.Unsetenv("GORUSH_CORE_PORT")
os.Unsetenv("GORUSH_GRPC_ENABLED")
os.Unsetenv("GORUSH_CORE_MAX_NOTIFICATION")
os.Unsetenv("GORUSH_IOS_KEY_ID")
os.Unsetenv("GORUSH_IOS_TEAM_ID")
os.Unsetenv("GORUSH_API_HEALTH_URI")
os.Unsetenv("GORUSH_CORE_FEEDBACK_HOOK_URL")
os.Unsetenv("GORUSH_CORE_FEEDBACK_HEADER")
})

ConfGorush, err := LoadConf("testdata/config.yml")
if err != nil {
panic("failed to load config.yml from file")
Expand Down Expand Up @@ -325,6 +337,11 @@ func TestRedisDBConfigurationFromEnv(t *testing.T) {
os.Setenv("GORUSH_QUEUE_REDIS_DB", "7")
os.Setenv("GORUSH_STAT_REDIS_DB", "9")

t.Cleanup(func() {
os.Unsetenv("GORUSH_QUEUE_REDIS_DB")
os.Unsetenv("GORUSH_STAT_REDIS_DB")
})

conf, err := LoadConf()
if err != nil {
t.Fatalf("failed to load config: %v", err)
Expand All @@ -335,13 +352,15 @@ func TestRedisDBConfigurationFromEnv(t *testing.T) {

// Test stat.redis.db is properly loaded from env
assert.Equal(t, 9, conf.Stat.Redis.DB)

// Clean up
os.Unsetenv("GORUSH_QUEUE_REDIS_DB")
os.Unsetenv("GORUSH_STAT_REDIS_DB")
}

func TestLoadWrongDefaultYAMLConfig(t *testing.T) {
// Backup original defaultConf
originalDefaultConf := defaultConf
defer func() {
defaultConf = originalDefaultConf
}()

defaultConf = []byte(`a`)
_, err := LoadConf()
assert.Error(t, err)
Expand Down Expand Up @@ -700,3 +719,88 @@ func TestSecurityValidationIntegration(t *testing.T) {
}
})
}

func TestAPIDefaultsFromEnv(t *testing.T) {
// Test that API endpoint defaults can be overridden by environment variables
os.Setenv("GORUSH_API_PUSH_URI", "/custom/push")
os.Setenv("GORUSH_API_STAT_GO_URI", "/custom/stat/go")
os.Setenv("GORUSH_API_METRIC_URI", "/custom/metrics")

t.Cleanup(func() {
os.Unsetenv("GORUSH_API_PUSH_URI")
os.Unsetenv("GORUSH_API_STAT_GO_URI")
os.Unsetenv("GORUSH_API_METRIC_URI")
})

conf, err := LoadConf()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}

assert.Equal(t, "/custom/push", conf.API.PushURI)
assert.Equal(t, "/custom/stat/go", conf.API.StatGoURI)
assert.Equal(t, "/custom/metrics", conf.API.MetricURI)
}

func TestLogDefaultsFromEnv(t *testing.T) {
// Test that log level defaults can be overridden by environment variables
os.Setenv("GORUSH_LOG_ACCESS_LEVEL", "info")
os.Setenv("GORUSH_LOG_ERROR_LEVEL", "warn")
os.Setenv("GORUSH_LOG_FORMAT", "json")

t.Cleanup(func() {
os.Unsetenv("GORUSH_LOG_ACCESS_LEVEL")
os.Unsetenv("GORUSH_LOG_ERROR_LEVEL")
os.Unsetenv("GORUSH_LOG_FORMAT")
})

conf, err := LoadConf()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}

assert.Equal(t, "info", conf.Log.AccessLevel)
assert.Equal(t, "warn", conf.Log.ErrorLevel)
assert.Equal(t, "json", conf.Log.Format)
}

func TestLogLevelDefaultsWhenEmpty(t *testing.T) {
// Test that when no log level is specified, defaults are used
// This was the original bug - empty log levels caused initialization to fail
conf, err := LoadConf()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}

// Verify defaults are set
assert.NotEmpty(t, conf.Log.AccessLevel, "access level should have default value")
assert.NotEmpty(t, conf.Log.ErrorLevel, "error level should have default value")
assert.Equal(t, "debug", conf.Log.AccessLevel)
assert.Equal(t, "error", conf.Log.ErrorLevel)
}

func TestAllAPIEndpointsHaveDefaults(t *testing.T) {
// Test that all API endpoints have proper defaults
conf, err := LoadConf()
if err != nil {
t.Fatalf("failed to load config: %v", err)
}

// Verify all API endpoints have non-empty defaults
assert.NotEmpty(t, conf.API.PushURI, "push_uri should have default")
assert.NotEmpty(t, conf.API.StatGoURI, "stat_go_uri should have default")
assert.NotEmpty(t, conf.API.StatAppURI, "stat_app_uri should have default")
assert.NotEmpty(t, conf.API.ConfigURI, "config_uri should have default")
assert.NotEmpty(t, conf.API.SysStatURI, "sys_stat_uri should have default")
assert.NotEmpty(t, conf.API.MetricURI, "metric_uri should have default")
assert.NotEmpty(t, conf.API.HealthURI, "health_uri should have default")

// Verify correct default values
assert.Equal(t, "/api/push", conf.API.PushURI)
assert.Equal(t, "/api/stat/go", conf.API.StatGoURI)
assert.Equal(t, "/api/stat/app", conf.API.StatAppURI)
assert.Equal(t, "/api/config", conf.API.ConfigURI)
assert.Equal(t, "/sys/stats", conf.API.SysStatURI)
assert.Equal(t, "/metrics", conf.API.MetricURI)
assert.Equal(t, "/healthz", conf.API.HealthURI)
}
20 changes: 11 additions & 9 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ func TestPlatformStringAndValidity(t *testing.T) {
p Platform
expected string
}{
{PlatformIOS, "ios"},
{PlatformAndroid, "android"},
{PlatformHuawei, "huawei"},
{PlatformIOS, PlatformIOS.String()},
{PlatformAndroid, PlatformAndroid.String()},
{PlatformHuawei, PlatformHuawei.String()},
}

for _, c := range cases {
Expand All @@ -34,7 +34,7 @@ func TestParsePlatform(t *testing.T) {
if err != nil || p != PlatformIOS {
t.Fatalf("ParsePlatform ios failed: p=%v err=%v", p, err)
}
p, err = ParsePlatform("android")
p, err = ParsePlatform(PlatformAndroid.String())
if err != nil || p != PlatformAndroid {
t.Fatalf("ParsePlatform android failed: p=%v err=%v", p, err)
}
Expand All @@ -49,22 +49,25 @@ func TestParsePlatform(t *testing.T) {

func TestPlatformTextMarshaling(t *testing.T) {
b, err := PlatformIOS.MarshalText()
if err != nil || string(b) != "ios" {
if err != nil || string(b) != PlatformIOS.String() {
t.Fatalf("MarshalText: got %q err=%v", string(b), err)
}
var p Platform
if err := p.UnmarshalText([]byte("android")); err != nil || p != PlatformAndroid {
androidText := []byte(PlatformAndroid.String())
if err := p.UnmarshalText(androidText); err != nil || p != PlatformAndroid {
t.Fatalf("UnmarshalText: p=%v err=%v", p, err)
}
}

func TestPlatformJSONMarshaling(t *testing.T) {
b, err := json.Marshal(PlatformHuawei)
if err != nil || string(b) != "\"huawei\"" {
expected := `"` + PlatformHuawei.String() + `"`
if err != nil || string(b) != expected {
t.Fatalf("MarshalJSON: got %s err=%v", string(b), err)
}
var p Platform
if err := json.Unmarshal([]byte("\"ios\""), &p); err != nil || p != PlatformIOS {
iosJSON := []byte(`"` + PlatformIOS.String() + `"`)
if err := json.Unmarshal(iosJSON, &p); err != nil || p != PlatformIOS {
t.Fatalf("UnmarshalJSON string: p=%v err=%v", p, err)
}
// legacy numeric values
Expand All @@ -84,4 +87,3 @@ func TestLogBlock(t *testing.T) {
t.Fatalf("expected arbitrary value to be invalid")
}
}

Loading