Skip to content

Commit cab9d74

Browse files
More detailed test for env variables migration (#709)
* More detailed test for env variables migration
1 parent 63cfc54 commit cab9d74

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/core/config/config_test.go

+32-22
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestGetConfig(t *testing.T) {
130130
require.NoError(t, err)
131131

132132
// Initialize environment with the empty configs
133-
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
133+
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile), true)
134134

135135
config, err := GetConfig("12345")
136136
require.NoError(t, err)
@@ -183,7 +183,7 @@ func TestGetConfig(t *testing.T) {
183183
require.NoError(t, err)
184184

185185
// Initialize environment with the empty configs
186-
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
186+
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile), true)
187187

188188
updatedTag := "updated-tag"
189189
updatedLogLevel := "fatal"
@@ -226,7 +226,7 @@ func TestGetConfig(t *testing.T) {
226226
require.NoError(t, err)
227227

228228
// Initialize environment with the empty configs
229-
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
229+
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile), true)
230230

231231
// Copy config file with updated values to current directory
232232
updatedTempConfDeleteFunc, err := sysutils.CopyFile(fmt.Sprintf("%s/%s", testCfgDir, updateCfgFile), updatedTempCfgFile)
@@ -292,13 +292,13 @@ func TestGetConfig(t *testing.T) {
292292
require.NoError(t, err)
293293

294294
// Initialize environment with specified configs
295-
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
295+
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile), true)
296296

297297
envTags := "env tags"
298-
setEnvVariable(t, ServerHost, updatedServerHost)
299-
setEnvVariable(t, LogLevel, updatedLogLevel)
300-
setEnvVariable(t, LogPath, updatedLogPath)
301-
setEnvVariable(t, TagsKey, envTags)
298+
setEnvVariable(t, EnvPrefix, ServerHost, updatedServerHost)
299+
setEnvVariable(t, EnvPrefix, LogLevel, updatedLogLevel)
300+
setEnvVariable(t, EnvPrefix, LogPath, updatedLogPath)
301+
setEnvVariable(t, EnvPrefix, TagsKey, envTags)
302302

303303
config, err := GetConfig("5678")
304304
require.NoError(t, err)
@@ -331,13 +331,13 @@ func TestGetConfig(t *testing.T) {
331331
require.NoError(t, err)
332332

333333
// Initialize environment with the empty configs
334-
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
334+
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile), true)
335335

336336
envTags := "env tags"
337-
setEnvVariable(t, ServerHost, updatedServerHost)
338-
setEnvVariable(t, LogLevel, updatedLogLevel)
339-
setEnvVariable(t, LogPath, updatedLogPath)
340-
setEnvVariable(t, TagsKey, envTags)
337+
setEnvVariable(t, EnvPrefix, ServerHost, updatedServerHost)
338+
setEnvVariable(t, EnvPrefix, LogLevel, updatedLogLevel)
339+
setEnvVariable(t, EnvPrefix, LogPath, updatedLogPath)
340+
setEnvVariable(t, EnvPrefix, TagsKey, envTags)
341341

342342
config, err := GetConfig("5678")
343343
require.NoError(t, err)
@@ -370,7 +370,7 @@ extensions:
370370
require.NoError(t, err)
371371

372372
// Initialize environment with specified configs
373-
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
373+
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile), true)
374374

375375
config, err := GetConfig("5678")
376376
require.NoError(t, err)
@@ -394,7 +394,7 @@ func TestUpdateAgentConfig(t *testing.T) {
394394
}()
395395
require.NoError(t, err)
396396

397-
cleanEnv(t, "empty_config.conf", fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
397+
cleanEnv(t, "empty_config.conf", fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile), true)
398398

399399
// Get the current config so we can correctly set a few testcase variables
400400
curConf, err := GetConfig("12345")
@@ -499,8 +499,11 @@ func TestDeprecatedEnvPrefixMigration(t *testing.T) {
499499
}()
500500
require.NoError(t, err)
501501

502-
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
503-
setEnvVariable(t, "tls_skip_verify", "true")
502+
cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile), false)
503+
setEnvVariable(t, LegacyEnvPrefix, "tls_skip_verify", "true")
504+
setEnvVariable(t, EnvPrefix, "tls_skip_verify", "")
505+
506+
RegisterFlags()
504507

505508
config, err := GetConfig("1234")
506509
require.NoError(t, err)
@@ -509,19 +512,26 @@ func TestDeprecatedEnvPrefixMigration(t *testing.T) {
509512
assert.Equal(t, want, got)
510513
}
511514

512-
func setEnvVariable(t *testing.T, name string, value string) {
513-
key := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + name)
514-
err := os.Setenv(key, value)
515+
func setEnvVariable(t *testing.T, prefix, name, value string) {
516+
var err error
517+
key := strings.ToUpper(prefix + agent_config.KeyDelimiter + name)
518+
if value == "" {
519+
err = os.Unsetenv(key)
520+
} else {
521+
t.Setenv(key, value)
522+
}
515523
require.NoError(t, err)
516524
}
517525

518-
func cleanEnv(t *testing.T, confFileName, dynamicConfFileAbsPath string) {
526+
func cleanEnv(t *testing.T, confFileName, dynamicConfFileAbsPath string, register bool) {
519527
os.Clearenv()
520528
ROOT_COMMAND.ResetFlags()
521529
ROOT_COMMAND.ResetCommands()
522530
Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter))
523531
SetDefaults()
524-
RegisterFlags()
532+
if register {
533+
RegisterFlags()
534+
}
525535

526536
cfg, err := RegisterConfigFile(dynamicConfFileAbsPath, confFileName, searchPaths...)
527537
require.NoError(t, err)

0 commit comments

Comments
 (0)