-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconfig_test.go
259 lines (233 loc) · 9.44 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package config
import (
"context"
"fmt"
"strings"
"testing"
"github.com/knadh/koanf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var parentDir = "../"
// TestNewConfig tests the NewConfig function.
func TestNewConfig(t *testing.T) {
config := NewConfig(
t.Context(), Config{GlobalConfigFile: GlobalConfigFilename, PluginConfigFile: PluginsConfigFilename})
assert.NotNil(t, config)
assert.Equal(t, GlobalConfigFilename, config.GlobalConfigFile)
assert.Equal(t, PluginsConfigFilename, config.PluginConfigFile)
assert.Equal(t, GlobalConfig{}, config.globalDefaults)
assert.Equal(t, PluginConfig{}, config.pluginDefaults)
assert.Equal(t, GlobalConfig{}, config.Global)
assert.Equal(t, PluginConfig{}, config.Plugin)
assert.Equal(t, koanf.New("."), config.GlobalKoanf)
assert.Equal(t, koanf.New("."), config.PluginKoanf)
}
// TestInitConfig tests the InitConfig function, which practically tests all
// the other functions.
func TestInitConfig(t *testing.T) {
ctx := t.Context()
config := NewConfig(ctx,
Config{
GlobalConfigFile: parentDir + "cmd/testdata/gatewayd.yaml",
PluginConfigFile: parentDir + PluginsConfigFilename,
},
)
err := config.InitConfig(ctx)
require.Nil(t, err)
assert.NotNil(t, config.Global)
assert.NotEqual(t, GlobalConfig{}, config.Global)
assert.Contains(t, config.Global.Servers, Default)
assert.Contains(t, config.Global.Servers, "test") // Test the multi-tenant configuration.
assert.NotNil(t, config.Plugin)
assert.NotEqual(t, PluginConfig{}, config.Plugin)
assert.Len(t, config.Plugin.Plugins, 1)
assert.NotNil(t, config.GlobalKoanf)
assert.NotEqual(t, config.GlobalKoanf, koanf.New("."))
assert.Equal(t, DefaultLogLevel, config.GlobalKoanf.String("loggers.default.level"))
assert.NotNil(t, config.PluginKoanf)
assert.NotEqual(t, config.PluginKoanf, koanf.New("."))
assert.NotNil(t, config.globalDefaults)
assert.NotEqual(t, GlobalConfig{}, config.globalDefaults)
assert.Contains(t, config.globalDefaults.Servers, Default)
assert.Contains(t, config.globalDefaults.Servers, "test")
assert.NotNil(t, config.pluginDefaults)
assert.NotEqual(t, PluginConfig{}, config.pluginDefaults)
assert.Empty(t, config.pluginDefaults.Plugins)
}
// TestInitConfigMultiTenant tests the InitConfig function with a multi-tenant configuration.
func TestInitConfigMultiTenant(t *testing.T) {
ctx := t.Context()
config := NewConfig(ctx,
Config{GlobalConfigFile: parentDir + GlobalConfigFilename, PluginConfigFile: parentDir + PluginsConfigFilename})
err := config.InitConfig(ctx)
require.Nil(t, err)
assert.NotNil(t, config.Global)
assert.NotEqual(t, GlobalConfig{}, config.Global)
assert.Contains(t, config.Global.Servers, Default)
assert.NotNil(t, config.Plugin)
assert.NotEqual(t, PluginConfig{}, config.Plugin)
assert.Len(t, config.Plugin.Plugins, 1)
assert.NotNil(t, config.GlobalKoanf)
assert.NotEqual(t, config.GlobalKoanf, koanf.New("."))
assert.Equal(t, DefaultLogLevel, config.GlobalKoanf.String("loggers.default.level"))
assert.NotNil(t, config.PluginKoanf)
assert.NotEqual(t, config.PluginKoanf, koanf.New("."))
assert.NotNil(t, config.globalDefaults)
assert.NotEqual(t, GlobalConfig{}, config.globalDefaults)
assert.Contains(t, config.globalDefaults.Servers, Default)
assert.NotNil(t, config.pluginDefaults)
assert.NotEqual(t, PluginConfig{}, config.pluginDefaults)
assert.Empty(t, config.pluginDefaults.Plugins)
}
// TestInitConfigMissingFile tests the InitConfig function with a missing file.
func TestInitConfigMissingKeys(t *testing.T) {
ctx := t.Context()
config := NewConfig(ctx,
Config{
GlobalConfigFile: "./testdata/missing_keys.yaml",
PluginConfigFile: parentDir + PluginsConfigFilename,
},
)
err := config.InitConfig(ctx)
assert.Error(t, err)
assert.Contains(t, err.Error(),
"validation failed, OriginalError: failed to validate global configuration")
}
// TestInitConfigMissingFile tests the InitConfig function with a missing file.
func TestInitConfigMissingFile(t *testing.T) {
ctx := t.Context()
config := NewConfig(ctx,
Config{
GlobalConfigFile: "./testdata/missing_file.yaml",
PluginConfigFile: parentDir + PluginsConfigFilename,
},
)
err := config.InitConfig(ctx)
assert.Error(t, err)
assert.Contains(
t,
err.Error(),
"error parsing config, OriginalError: failed to load global configuration: "+
"open testdata/missing_file.yaml: no such file or directory")
}
// TestMergeGlobalConfig tests the MergeGlobalConfig function.
func TestMergeGlobalConfig(t *testing.T) {
ctx := t.Context()
config := NewConfig(ctx,
Config{GlobalConfigFile: parentDir + GlobalConfigFilename, PluginConfigFile: parentDir + PluginsConfigFilename})
err := config.InitConfig(ctx)
require.Nil(t, err)
// The default log level is info.
assert.Equal(t, DefaultLogLevel, config.Global.Loggers[Default].Level)
// Merge a config that sets the log level to debug.
err = config.MergeGlobalConfig(ctx, map[string]any{
"loggers": map[string]any{
"default": map[string]any{
"level": "debug",
},
},
})
require.Nil(t, err)
assert.NotNil(t, config.Global)
assert.NotEqual(t, GlobalConfig{}, config.Global)
// The log level should now be debug.
assert.Equal(t, "debug", config.Global.Loggers[Default].Level)
}
// initializeConfig initializes the configuration with the given context.
// It returns a pointer to the Config struct. If configuration initialization fails,
// the test will fail with an error message.
func initializeConfig(ctx context.Context, t *testing.T) *Config {
t.Helper()
config := NewConfig(ctx, Config{
GlobalConfigFile: parentDir + GlobalConfigFilename,
PluginConfigFile: parentDir + PluginsConfigFilename,
})
err := config.InitConfig(ctx)
require.Nil(t, err)
return config
}
// serverLoadBalancerStrategyOverwrite sets the environment variable for server nested configuration
// and verifies that the configuration is correctly loaded with the expected value.
func ServerLoadBalancerStrategyOverwrite(t *testing.T) {
t.Helper()
ctx := t.Context()
// Convert to uppercase
upperDefaultGroup := strings.ToUpper(Default)
// Format environment variable name
envVarName := fmt.Sprintf("GATEWAYD_SERVERS_%s_LOADBALANCER_STRATEGY", upperDefaultGroup)
// Set the environment variable
t.Setenv(envVarName, "test")
config := initializeConfig(ctx, t)
assert.Equal(t, "test", config.Global.Servers[Default].LoadBalancer.Strategy)
}
// pluginDefaultPolicyOverwrite sets the environment variable for plugin configuration
// and verifies that the configuration is correctly loaded with the expected value.
func pluginDefaultPolicyOverwrite(t *testing.T) {
t.Helper()
ctx := t.Context()
// Set the environment variable
t.Setenv("GATEWAYD_DEFAULTPOLICY", "test")
config := initializeConfig(ctx, t)
assert.Equal(t, "test", config.Plugin.DefaultPolicy)
}
// clientNetworkOverwrite sets the environment variable for client network configuration
// and verifies that the configuration is correctly loaded with the expected value.
func clientNetworkOverwrite(t *testing.T) {
t.Helper()
ctx := t.Context()
// Convert to uppercase
upperDefaultGroup := strings.ToUpper(Default)
upperDefaultBlock := strings.ToUpper(DefaultConfigurationBlock)
// Format environment variable name
envVarName := fmt.Sprintf("GATEWAYD_CLIENTS_%s_%s_NETWORK", upperDefaultGroup, upperDefaultBlock)
// Set the environment variable
t.Setenv(envVarName, "udp")
config := initializeConfig(ctx, t)
assert.Equal(t, "udp", config.Global.Clients[Default][DefaultConfigurationBlock].Network)
}
// serverNetworkOverwrite sets the environment variable for server network configuration
// and verifies that the configuration is correctly loaded with the expected value.
func serverNetworkOverwrite(t *testing.T) {
t.Helper()
ctx := t.Context()
// Convert to uppercase
upperDefaultGroup := strings.ToUpper(Default)
// Format environment variable name
envVarName := fmt.Sprintf("GATEWAYD_SERVERS_%s_NETWORK", upperDefaultGroup)
// Set the environment variable
t.Setenv(envVarName, "udp")
config := initializeConfig(ctx, t)
assert.Equal(t, "udp", config.Global.Servers[Default].Network)
}
// TestLoadEnvVariables runs a suite of tests to verify that environment variables are correctly
// loaded into the configuration. Each test scenario sets a specific environment variable and
// checks if the configuration reflects the expected value.
func TestLoadEnvVariables(t *testing.T) {
scenarios := map[string]func(t *testing.T){
"serverLoadBalancerStrategyOverwrite": ServerLoadBalancerStrategyOverwrite,
"pluginLocalPathOverwrite": pluginDefaultPolicyOverwrite,
"ClientNetworkOverwrite": clientNetworkOverwrite,
"ServerNetworkOverwrite": serverNetworkOverwrite,
}
for scenario, fn := range scenarios {
t.Run(scenario, func(t *testing.T) {
fn(t)
})
}
}
// TestConvertKeysToLowercaseSuccess verifies that after calling ConvertKeysToLowercase,
// all keys in the config.Global.Clients map are converted to lowercase.
func TestConvertKeysToLowercaseSuccess(t *testing.T) {
ctx := t.Context()
config := NewConfig(ctx,
Config{GlobalConfigFile: parentDir + GlobalConfigFilename, PluginConfigFile: parentDir + PluginsConfigFilename})
err := config.ConvertKeysToLowercase(ctx)
require.Nil(t, err)
for configurationGroupName, configurationGroup := range config.Global.Clients {
assert.Equal(t, configurationGroupName, strings.ToLower(configurationGroupName))
for configuraionBlockName := range configurationGroup {
assert.Equal(t, configuraionBlockName, strings.ToLower(configuraionBlockName))
}
}
}