-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig_test.go
105 lines (83 loc) · 3.02 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package config
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func clearEnv() {
os.Unsetenv("APP_NAME")
os.Unsetenv("HTTP_PORT")
os.Unsetenv("LOG_LEVEL")
os.Unsetenv("DB_POOL_MAX")
os.Unsetenv("DB_URL")
}
func TestNewConfig_Defaults(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying environment variables
clearEnv() // Clear environment variables to ensure defaults are tested
cfg, err := NewConfig()
cfg.App.EncryptionKey = "test" // Added to pass the test
assert.NoError(t, err)
assert.NotNil(t, cfg)
// Verify default values
assert.Equal(t, "console", cfg.App.Name)
assert.Equal(t, "open-amt-cloud-toolkit/console", cfg.App.Repo)
assert.Equal(t, "DEVELOPMENT", cfg.App.Version)
assert.Equal(t, "test", cfg.App.EncryptionKey)
assert.Equal(t, "8181", cfg.HTTP.Port)
assert.Equal(t, []string{"*"}, cfg.HTTP.AllowedOrigins)
assert.Equal(t, []string{"*"}, cfg.HTTP.AllowedHeaders)
assert.Equal(t, "info", cfg.Log.Level)
assert.Equal(t, 2, cfg.DB.PoolMax)
}
func TestNewConfig_EnvVars(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying environment variables
// Set environment variables
os.Setenv("APP_NAME", "testApp")
os.Setenv("HTTP_PORT", "9090")
os.Setenv("LOG_LEVEL", "debug")
os.Setenv("DB_POOL_MAX", "10")
os.Setenv("DB_URL", "postgres://user:password@localhost:5432/testdb")
defer clearEnv() // Ensure environment variables are cleared after test
cfg, err := NewConfig()
assert.NoError(t, err)
assert.NotNil(t, cfg)
// Verify environment variable values
assert.Equal(t, "testApp", cfg.App.Name)
assert.Equal(t, "9090", cfg.HTTP.Port)
assert.Equal(t, "debug", cfg.Log.Level)
assert.Equal(t, 10, cfg.DB.PoolMax)
assert.Equal(t, "postgres://user:password@localhost:5432/testdb", cfg.DB.URL)
}
func TestNewConfig_FileAndEnvVars(t *testing.T) { //nolint:paralleltest // cannot have simultaneous tests modifying environment variables
clearEnv() // Clear environment variables before setting new ones
// Create a temporary config file
configYAML := `
app:
name: fileApp
http:
port: "8080"
logger:
log_level: warn
postgres:
pool_max: 5
url: postgres://fileuser:filepassword@localhost:5432/filedb
`
configFilePath := "./test_config.yml"
err := os.WriteFile(configFilePath, []byte(configYAML), 0o600)
assert.NoError(t, err)
defer os.Remove(configFilePath)
// Set environment variables
os.Setenv("APP_NAME", "envApp")
os.Setenv("HTTP_PORT", "9090")
os.Setenv("LOG_LEVEL", "debug")
os.Setenv("DB_POOL_MAX", "10")
os.Setenv("DB_URL", "postgres://envuser:envpassword@localhost:5432/envdb")
defer clearEnv() // Ensure environment variables are cleared after test
cfg, err := NewConfig()
assert.NoError(t, err)
assert.NotNil(t, cfg)
// Verify environment variable values override file values
assert.Equal(t, "envApp", cfg.App.Name)
assert.Equal(t, "9090", cfg.HTTP.Port)
assert.Equal(t, "debug", cfg.Log.Level)
assert.Equal(t, 10, cfg.DB.PoolMax)
assert.Equal(t, "postgres://envuser:envpassword@localhost:5432/envdb", cfg.DB.URL)
}