forked from mtusi2026/mtusi2026lab3
242 lines
4.6 KiB
Go
242 lines
4.6 KiB
Go
package config
|
|
|
|
import "testing"
|
|
|
|
// TestConfig_New checks individual environment overrides and invalid settings.
|
|
func TestConfig_New(t *testing.T) {
|
|
t.Run("Positive: APP_PORT override", func(t *testing.T) {
|
|
// Arrange.
|
|
t.Setenv("APP_PORT", "18080")
|
|
|
|
// Act.
|
|
cfg, err := New()
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.HTTP.Port != 18080 {
|
|
t.Fatalf("APP_PORT=%v, want=%v", cfg.HTTP.Port, 18080)
|
|
}
|
|
})
|
|
t.Run("Positive: REQUEST_TIMEOUT_SECONDS override", func(t *testing.T) {
|
|
// Arrange.
|
|
t.Setenv("REQUEST_TIMEOUT_SECONDS", "4")
|
|
|
|
// Act.
|
|
cfg, err := New()
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.HTTP.TimeoutSeconds != 4 {
|
|
t.Fatalf("REQUEST_TIMEOUT_SECONDS=%v, want=%v", cfg.HTTP.TimeoutSeconds, 4)
|
|
}
|
|
})
|
|
t.Run("Positive: MAX_MESSAGES override", func(t *testing.T) {
|
|
// Arrange.
|
|
t.Setenv("MAX_MESSAGES", "25")
|
|
|
|
// Act.
|
|
cfg, err := New()
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Memory.MaxMessages != 25 {
|
|
t.Fatalf("MAX_MESSAGES=%v, want=%v", cfg.Memory.MaxMessages, 25)
|
|
}
|
|
})
|
|
t.Run("Positive: REQUESTS_PER_SECOND override", func(t *testing.T) {
|
|
// Arrange.
|
|
t.Setenv("REQUESTS_PER_SECOND", "12")
|
|
|
|
// Act.
|
|
cfg, err := New()
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.HTTP.Admission.RequestsPerSecond != 12 {
|
|
t.Fatalf("REQUESTS_PER_SECOND=%v, want=%v", cfg.HTTP.Admission.RequestsPerSecond, 12)
|
|
}
|
|
})
|
|
t.Run("Positive: LOG_LEVEL override", func(t *testing.T) {
|
|
// Arrange.
|
|
t.Setenv("LOG_LEVEL", "debug")
|
|
|
|
// Act.
|
|
cfg, err := New()
|
|
|
|
// Assert.
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Logger.Level != "debug" {
|
|
t.Fatalf("LOG_LEVEL=%v, want=%v", cfg.Logger.Level, "debug")
|
|
}
|
|
})
|
|
cases := []struct {
|
|
name string
|
|
key string
|
|
value string
|
|
}{
|
|
{"port exceeds TCP range", "APP_PORT", "65536"},
|
|
{"zero message capacity", "MAX_MESSAGES", "0"},
|
|
{"nonnumeric body limit", "MAX_BODY_BYTES", "bad"},
|
|
{"shutdown deadline exceeds bound", "SHUTDOWN_TIMEOUT_SECONDS", "10"},
|
|
{"hostname instead of bind IP", "APP_HOST", "hostname"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run("Negative: "+tc.name, func(t *testing.T) {
|
|
// Arrange.
|
|
t.Setenv(tc.key, tc.value)
|
|
|
|
// Act.
|
|
cfg, err := New()
|
|
|
|
// Assert.
|
|
if err == nil || cfg != nil {
|
|
t.Fatalf("invalid %s=%q returned config=%+v, error=%v", tc.key, tc.value, cfg, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestConfig_Validate rejects one missing section or invalid component per scenario.
|
|
func TestConfig_Validate(t *testing.T) {
|
|
t.Run("Negative: nil config", func(t *testing.T) {
|
|
// Arrange.
|
|
var cfg *Config
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatalf("accepted %s", "nil config")
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: missing HTTP", func(t *testing.T) {
|
|
// Arrange.
|
|
cfg := Default()
|
|
|
|
cfg.HTTP = nil
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatalf("accepted %s", "missing HTTP")
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: missing Memory", func(t *testing.T) {
|
|
// Arrange.
|
|
cfg := Default()
|
|
|
|
cfg.Memory = nil
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatalf("accepted %s", "missing Memory")
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: missing Logger", func(t *testing.T) {
|
|
// Arrange.
|
|
cfg := Default()
|
|
|
|
cfg.Logger = nil
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatalf("accepted %s", "missing Logger")
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: unknown log level", func(t *testing.T) {
|
|
// Arrange.
|
|
cfg := Default()
|
|
|
|
cfg.Logger.Level = "unknown"
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatalf("accepted %s", "unknown log level")
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: zero storage capacity", func(t *testing.T) {
|
|
// Arrange.
|
|
cfg := Default()
|
|
|
|
cfg.Memory.MaxUsers = 0
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatalf("accepted %s", "zero storage capacity")
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: zero concurrency", func(t *testing.T) {
|
|
// Arrange.
|
|
cfg := Default()
|
|
|
|
cfg.HTTP.Admission.MaxInFlight = 0
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatalf("accepted %s", "zero concurrency")
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestLogger_Validate rejects an absent logging section without panicking.
|
|
func TestLogger_Validate(t *testing.T) {
|
|
// Arrange.
|
|
var cfg *Logger
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatal("nil logger accepted")
|
|
}
|
|
}
|
|
|
|
// TestMemory_Validate rejects absent storage settings without panicking.
|
|
func TestMemory_Validate(t *testing.T) {
|
|
// Arrange.
|
|
var cfg *Memory
|
|
|
|
// Act.
|
|
err := cfg.Validate()
|
|
|
|
// Assert.
|
|
if err == nil {
|
|
t.Fatal("nil memory configuration accepted")
|
|
}
|
|
}
|