Skip to content

Commit

Permalink
Merging to release-1.8: getting pump type from config file when loadi…
Browse files Browse the repository at this point in the history
…ng env vars (#747)

getting pump type from config file when loading env vars (#747)
  • Loading branch information
buger authored Nov 24, 2023
1 parent dabee97 commit 9532149
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 8 deletions.
17 changes: 9 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ func (cfg *TykPumpConfiguration) LoadPumpsByEnv() error {
//first we look for all the pumps names in the env vars from the os
for _, env := range os.Environ() {
if strings.HasPrefix(env, PUMPS_ENV_PREFIX) {

// We trim everything after PUMPS_ENV_PREFIX. For example, if we have TYK_PUMP_PUMPS_CSV_TYPE we would have CSV_TYPE here
envWoPrefix := strings.TrimPrefix(env, PUMPS_ENV_PREFIX+"_")

Expand All @@ -297,7 +296,6 @@ func (cfg *TykPumpConfiguration) LoadPumpsByEnv() error {

//The name of the pump is always going to be the first keyword after the PUMPS_ENV_PREFIX
pmpName := strings.ToUpper(envSplit[0])

osPumpsEnvNames[pmpName] = true
}
}
Expand All @@ -312,18 +310,21 @@ func (cfg *TykPumpConfiguration) LoadPumpsByEnv() error {
pmp = jsonPump
}
//We look if the pmpName is one of our available pumps. If it's not, we look if the env with the TYPE filed exists.
var pmpType string
if _, ok := pumps.AvailablePumps[strings.ToLower(pmpName)]; !ok {
pmpType, found := os.LookupEnv(PUMPS_ENV_PREFIX + "_" + pmpName + "_TYPE")
var found bool
pmpType, found = os.LookupEnv(PUMPS_ENV_PREFIX + "_" + pmpName + "_TYPE")
if !found {
log.Error(fmt.Sprintf("TYPE Env var for pump %s not found", pmpName))
continue
if pmp.Type == "" {
log.Error(fmt.Sprintf("TYPE Env var for pump %s not found", pmpName))
continue
}
pmpType = pmp.Type
}
pmp.Type = pmpType
} else {
pmp.Type = pmpName
pmpType = pmpName
}

pmpType := pmp.Type
//We fetch the env vars for that pump.
overrideErr := envconfig.Process(PUMPS_ENV_PREFIX+"_"+pmpName, &pmp)
if overrideErr != nil {
Expand Down
113 changes: 113 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,116 @@ func TestIgnoreConfig(t *testing.T) {
assert.Equal(t, 5, initialConfig.PurgeDelay, "Nonexistent config file should not affect the configuration")
})
}

func TestTykPumpConfiguration_LoadPumpsByEnv(t *testing.T) {
tcs := []struct {
cfg *TykPumpConfiguration
wanted map[string]PumpConfig
setup func()
teardown func()
name string
}{
{
name: "no initial pumps",
cfg: &TykPumpConfiguration{},
setup: func() {
os.Setenv(ENV_PREVIX+"_PUMPS_ENVTEST_TYPE", "mongo-pump-aggregate")
os.Setenv(ENV_PREVIX+"_PUMPS_ENVTEST_META_MONGOURL", "mongodb://localhost:27017")
},
teardown: func() {
os.Unsetenv(ENV_PREVIX + "_PUMPS_ENVTEST_TYPE")
os.Unsetenv(ENV_PREVIX + "_PUMPS_ENVTEST_META_MONGOURL")
},
wanted: map[string]PumpConfig{
"ENVTEST": {
Type: "mongo-pump-aggregate",
Meta: map[string]interface{}{
"meta_env_prefix": ENV_PREVIX + "_PUMPS_ENVTEST_META",
},
},
},
},
{
name: "with initial pumps",
cfg: &TykPumpConfiguration{
Pumps: map[string]PumpConfig{
"INITIAL": {
Type: "csv",
Meta: map[string]interface{}{
"csv_dir": "/tmp",
},
},
},
},
setup: func() {
os.Setenv(ENV_PREVIX+"_PUMPS_ENVTEST_TYPE", "mongo-pump-aggregate")
os.Setenv(ENV_PREVIX+"_PUMPS_ENVTEST_META_MONGOURL", "mongodb://localhost:27017")
},
teardown: func() {
os.Unsetenv(ENV_PREVIX + "_PUMPS_ENVTEST_TYPE")
os.Unsetenv(ENV_PREVIX + "_PUMPS_ENVTEST_META_MONGOURL")
},
wanted: map[string]PumpConfig{
"INITIAL": {
Type: "csv",
Meta: map[string]interface{}{
"csv_dir": "/tmp",
},
},
"ENVTEST": {
Type: "mongo-pump-aggregate",
Meta: map[string]interface{}{
"meta_env_prefix": ENV_PREVIX + "_PUMPS_ENVTEST_META",
},
},
},
},
{
name: "type env var not found and type in cfg is empty",
cfg: &TykPumpConfiguration{},
setup: func() {
os.Setenv(ENV_PREVIX+"_PUMPS_ENVTEST_META_MONGOURL", "mongodb://localhost:27017")
},
teardown: func() {
os.Unsetenv(ENV_PREVIX + "_PUMPS_ENVTEST_META_MONGOURL")
},
wanted: map[string]PumpConfig{},
},
{
name: "type env var not found but type in cfg is set",
cfg: &TykPumpConfiguration{
Pumps: map[string]PumpConfig{
"ENVTEST": {
Type: "mongo",
},
},
},
setup: func() {
// Deliberately not setting the TYPE env var for ENVTEST
os.Setenv(ENV_PREVIX+"_PUMPS_ENVTEST_META_MONGOURL", "mongodb://localhost:27017")
},
teardown: func() {
os.Unsetenv(ENV_PREVIX + "_PUMPS_ENVTEST_META_MONGOURL")
},
wanted: map[string]PumpConfig{
"ENVTEST": {
Type: "mongo", // Expecting the predefined type to be retained
Meta: map[string]interface{}{
"meta_env_prefix": ENV_PREVIX + "_PUMPS_ENVTEST_META",
},
},
},
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
defer tc.teardown()

err := tc.cfg.LoadPumpsByEnv()
assert.NoError(t, err)
assert.Equal(t, tc.wanted, tc.cfg.Pumps)
})
}
}

0 comments on commit 9532149

Please sign in to comment.