@@ -130,7 +130,7 @@ func TestGetConfig(t *testing.T) {
130
130
require .NoError (t , err )
131
131
132
132
// 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 )
134
134
135
135
config , err := GetConfig ("12345" )
136
136
require .NoError (t , err )
@@ -183,7 +183,7 @@ func TestGetConfig(t *testing.T) {
183
183
require .NoError (t , err )
184
184
185
185
// 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 )
187
187
188
188
updatedTag := "updated-tag"
189
189
updatedLogLevel := "fatal"
@@ -226,7 +226,7 @@ func TestGetConfig(t *testing.T) {
226
226
require .NoError (t , err )
227
227
228
228
// 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 )
230
230
231
231
// Copy config file with updated values to current directory
232
232
updatedTempConfDeleteFunc , err := sysutils .CopyFile (fmt .Sprintf ("%s/%s" , testCfgDir , updateCfgFile ), updatedTempCfgFile )
@@ -292,13 +292,13 @@ func TestGetConfig(t *testing.T) {
292
292
require .NoError (t , err )
293
293
294
294
// 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 )
296
296
297
297
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 )
302
302
303
303
config , err := GetConfig ("5678" )
304
304
require .NoError (t , err )
@@ -331,13 +331,13 @@ func TestGetConfig(t *testing.T) {
331
331
require .NoError (t , err )
332
332
333
333
// 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 )
335
335
336
336
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 )
341
341
342
342
config , err := GetConfig ("5678" )
343
343
require .NoError (t , err )
@@ -370,7 +370,7 @@ extensions:
370
370
require .NoError (t , err )
371
371
372
372
// 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 )
374
374
375
375
config , err := GetConfig ("5678" )
376
376
require .NoError (t , err )
@@ -394,7 +394,7 @@ func TestUpdateAgentConfig(t *testing.T) {
394
394
}()
395
395
require .NoError (t , err )
396
396
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 )
398
398
399
399
// Get the current config so we can correctly set a few testcase variables
400
400
curConf , err := GetConfig ("12345" )
@@ -499,8 +499,11 @@ func TestDeprecatedEnvPrefixMigration(t *testing.T) {
499
499
}()
500
500
require .NoError (t , err )
501
501
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 ()
504
507
505
508
config , err := GetConfig ("1234" )
506
509
require .NoError (t , err )
@@ -509,19 +512,26 @@ func TestDeprecatedEnvPrefixMigration(t *testing.T) {
509
512
assert .Equal (t , want , got )
510
513
}
511
514
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
+ }
515
523
require .NoError (t , err )
516
524
}
517
525
518
- func cleanEnv (t * testing.T , confFileName , dynamicConfFileAbsPath string ) {
526
+ func cleanEnv (t * testing.T , confFileName , dynamicConfFileAbsPath string , register bool ) {
519
527
os .Clearenv ()
520
528
ROOT_COMMAND .ResetFlags ()
521
529
ROOT_COMMAND .ResetCommands ()
522
530
Viper = viper .NewWithOptions (viper .KeyDelimiter (agent_config .KeyDelimiter ))
523
531
SetDefaults ()
524
- RegisterFlags ()
532
+ if register {
533
+ RegisterFlags ()
534
+ }
525
535
526
536
cfg , err := RegisterConfigFile (dynamicConfFileAbsPath , confFileName , searchPaths ... )
527
537
require .NoError (t , err )
0 commit comments