301 lines
5.7 KiB
Go
301 lines
5.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestConfig_Validate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
config *Config
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Valid config",
|
|
config: &Config{
|
|
Server: ServerConfig{
|
|
Port: ":8080",
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "info",
|
|
Format: "console",
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Empty port",
|
|
config: &Config{
|
|
Server: ServerConfig{
|
|
Port: "",
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "info",
|
|
Format: "console",
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Invalid log level",
|
|
config: &Config{
|
|
Server: ServerConfig{
|
|
Port: ":8080",
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "invalid",
|
|
Format: "console",
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "Invalid log format",
|
|
config: &Config{
|
|
Server: ServerConfig{
|
|
Port: ":8080",
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "info",
|
|
Format: "invalid",
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "TLS enabled without cert",
|
|
config: &Config{
|
|
Server: ServerConfig{
|
|
Port: ":8080",
|
|
TLSEnabled: true,
|
|
TLSCertFile: "",
|
|
TLSKeyFile: "",
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "info",
|
|
Format: "console",
|
|
},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "TLS enabled with cert and key",
|
|
config: &Config{
|
|
Server: ServerConfig{
|
|
Port: ":8080",
|
|
TLSEnabled: true,
|
|
TLSCertFile: "/path/to/cert.pem",
|
|
TLSKeyFile: "/path/to/key.pem",
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "info",
|
|
Format: "console",
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.config.Validate()
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetEnv(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
defaultValue string
|
|
envValue string
|
|
want string
|
|
}{
|
|
{
|
|
name: "Environment variable set",
|
|
key: "TEST_VAR",
|
|
defaultValue: "default",
|
|
envValue: "custom",
|
|
want: "custom",
|
|
},
|
|
{
|
|
name: "Environment variable not set",
|
|
key: "UNSET_VAR",
|
|
defaultValue: "default",
|
|
envValue: "",
|
|
want: "default",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.envValue != "" {
|
|
os.Setenv(tt.key, tt.envValue)
|
|
defer os.Unsetenv(tt.key)
|
|
}
|
|
|
|
got := getEnv(tt.key, tt.defaultValue)
|
|
if got != tt.want {
|
|
t.Errorf("getEnv() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetEnvBool(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
defaultValue bool
|
|
envValue string
|
|
want bool
|
|
}{
|
|
{
|
|
name: "true value",
|
|
key: "TEST_BOOL",
|
|
defaultValue: false,
|
|
envValue: "true",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "false value",
|
|
key: "TEST_BOOL",
|
|
defaultValue: true,
|
|
envValue: "false",
|
|
want: false,
|
|
},
|
|
{
|
|
name: "1 value",
|
|
key: "TEST_BOOL",
|
|
defaultValue: false,
|
|
envValue: "1",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "invalid value uses default",
|
|
key: "TEST_BOOL",
|
|
defaultValue: true,
|
|
envValue: "invalid",
|
|
want: true,
|
|
},
|
|
{
|
|
name: "not set uses default",
|
|
key: "UNSET_BOOL",
|
|
defaultValue: true,
|
|
envValue: "",
|
|
want: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.envValue != "" {
|
|
os.Setenv(tt.key, tt.envValue)
|
|
defer os.Unsetenv(tt.key)
|
|
}
|
|
|
|
got := getEnvBool(tt.key, tt.defaultValue)
|
|
if got != tt.want {
|
|
t.Errorf("getEnvBool() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetEnvInt(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
defaultValue int
|
|
envValue string
|
|
want int
|
|
}{
|
|
{
|
|
name: "valid integer",
|
|
key: "TEST_INT",
|
|
defaultValue: 10,
|
|
envValue: "42",
|
|
want: 42,
|
|
},
|
|
{
|
|
name: "invalid integer uses default",
|
|
key: "TEST_INT",
|
|
defaultValue: 10,
|
|
envValue: "invalid",
|
|
want: 10,
|
|
},
|
|
{
|
|
name: "not set uses default",
|
|
key: "UNSET_INT",
|
|
defaultValue: 100,
|
|
envValue: "",
|
|
want: 100,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.envValue != "" {
|
|
os.Setenv(tt.key, tt.envValue)
|
|
defer os.Unsetenv(tt.key)
|
|
}
|
|
|
|
got := getEnvInt(tt.key, tt.defaultValue)
|
|
if got != tt.want {
|
|
t.Errorf("getEnvInt() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetEnvDuration(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
defaultValue time.Duration
|
|
envValue string
|
|
want time.Duration
|
|
}{
|
|
{
|
|
name: "valid duration",
|
|
key: "TEST_DURATION",
|
|
defaultValue: 10 * time.Second,
|
|
envValue: "30s",
|
|
want: 30 * time.Second,
|
|
},
|
|
{
|
|
name: "invalid duration uses default",
|
|
key: "TEST_DURATION",
|
|
defaultValue: 10 * time.Second,
|
|
envValue: "invalid",
|
|
want: 10 * time.Second,
|
|
},
|
|
{
|
|
name: "not set uses default",
|
|
key: "UNSET_DURATION",
|
|
defaultValue: 1 * time.Minute,
|
|
envValue: "",
|
|
want: 1 * time.Minute,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if tt.envValue != "" {
|
|
os.Setenv(tt.key, tt.envValue)
|
|
defer os.Unsetenv(tt.key)
|
|
}
|
|
|
|
got := getEnvDuration(tt.key, tt.defaultValue)
|
|
if got != tt.want {
|
|
t.Errorf("getEnvDuration() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|