Skip to content

Commit

Permalink
[TT-1263] fix automation keeper benchmark (#13511)
Browse files Browse the repository at this point in the history
* remove now-unnecessary custom logic from testconfig; toml library version bump fixes the issue

* either read embedded configs or read them from FS, never both
  • Loading branch information
Tofel authored Jun 12, 2024
1 parent 3f56b3e commit c0983b4
Showing 1 changed file with 23 additions and 106 deletions.
129 changes: 23 additions & 106 deletions integration-tests/testconfig/testconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/google/uuid"
"github.com/pelletier/go-toml/v2"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"golang.org/x/text/cases"
"golang.org/x/text/language"

Expand Down Expand Up @@ -279,15 +278,6 @@ func GetConfig(configurationName string, product Product) (TestConfig, error) {
testConfig.ConfigurationName = configurationName
logger.Debug().Msgf("Will apply configuration named '%s' if it is found in any of the configs", configurationName)

var handleSpecialOverrides = func(logger zerolog.Logger, filename, configurationName string, target *TestConfig, content []byte, product Product) error {
switch product {
case Automation:
return handleAutomationConfigOverride(logger, filename, configurationName, target, content)
default:
return handleDefaultConfigOverride(logger, filename, configurationName, target, content)
}
}

// read embedded configs is build tag "embed" is set
// this makes our life much easier when using a binary
if areConfigsEmbedded {
Expand All @@ -302,34 +292,34 @@ func GetConfig(configurationName string, product Product) (TestConfig, error) {
return TestConfig{}, errors.Wrapf(err, "error reading embedded config")
}

err = handleSpecialOverrides(logger, fileName, configurationName, &testConfig, file, product)
err = ctf_config.BytesToAnyTomlStruct(logger, fileName, configurationName, &testConfig, file)
if err != nil {
return TestConfig{}, errors.Wrapf(err, "error unmarshalling embedded config")
}
}
}

logger.Info().Msg("Reading configs from file system")
for _, fileName := range fileNames {
logger.Debug().Msgf("Looking for config file %s", fileName)
filePath, err := osutil.FindFile(fileName, osutil.DEFAULT_STOP_FILE_NAME, 3)
} else {
logger.Info().Msg("Reading configs from file system")
for _, fileName := range fileNames {
logger.Debug().Msgf("Looking for config file %s", fileName)
filePath, err := osutil.FindFile(fileName, osutil.DEFAULT_STOP_FILE_NAME, 3)

if err != nil && errors.Is(err, os.ErrNotExist) {
logger.Debug().Msgf("Config file %s not found", fileName)
continue
} else if err != nil {
return TestConfig{}, errors.Wrapf(err, "error looking for file %s", filePath)
}
logger.Debug().Str("location", filePath).Msgf("Found config file %s", fileName)
if err != nil && errors.Is(err, os.ErrNotExist) {
logger.Debug().Msgf("Config file %s not found", fileName)
continue
} else if err != nil {
return TestConfig{}, errors.Wrapf(err, "error looking for file %s", filePath)
}
logger.Debug().Str("location", filePath).Msgf("Found config file %s", fileName)

content, err := readFile(filePath)
if err != nil {
return TestConfig{}, errors.Wrapf(err, "error reading file %s", filePath)
}
content, err := readFile(filePath)
if err != nil {
return TestConfig{}, errors.Wrapf(err, "error reading file %s", filePath)
}

err = handleSpecialOverrides(logger, fileName, configurationName, &testConfig, content, product)
if err != nil {
return TestConfig{}, errors.Wrapf(err, "error reading file %s", filePath)
err = ctf_config.BytesToAnyTomlStruct(logger, fileName, configurationName, &testConfig, content)
if err != nil {
return TestConfig{}, errors.Wrapf(err, "error reading file %s", filePath)
}
}
}

Expand All @@ -342,15 +332,15 @@ func GetConfig(configurationName string, product Product) (TestConfig, error) {
return TestConfig{}, err
}

err = handleSpecialOverrides(logger, Base64OverrideEnvVarName, configurationName, &testConfig, decoded, product)
err = ctf_config.BytesToAnyTomlStruct(logger, Base64OverrideEnvVarName, configurationName, &testConfig, decoded)
if err != nil {
return TestConfig{}, errors.Wrapf(err, "error unmarshaling base64 config")
}
} else {
logger.Debug().Msg("Base64 config override from environment variable not found")
}

// it neede some custom logic, so we do it separately
// it needs some custom logic, so we do it separately
err := testConfig.readNetworkConfiguration()
if err != nil {
return TestConfig{}, errors.Wrapf(err, "error reading network config")
Expand Down Expand Up @@ -570,76 +560,3 @@ func readFile(filePath string) ([]byte, error) {

return content, nil
}

func handleAutomationConfigOverride(logger zerolog.Logger, filename, configurationName string, target *TestConfig, content []byte) error {
logger.Debug().Msgf("Handling automation config override for %s", filename)
oldConfig := MustCopy(target)
newConfig := TestConfig{}

err := ctf_config.BytesToAnyTomlStruct(logger, filename, configurationName, &target, content)
if err != nil {
return errors.Wrapf(err, "error reading file %s", filename)
}

err = ctf_config.BytesToAnyTomlStruct(logger, filename, configurationName, &newConfig, content)
if err != nil {
return errors.Wrapf(err, "error reading file %s", filename)
}

// override instead of merging
if (newConfig.Automation != nil && len(newConfig.Automation.Load) > 0) && (oldConfig != nil && oldConfig.Automation != nil && len(oldConfig.Automation.Load) > 0) {
target.Automation.Load = newConfig.Automation.Load
}

return nil
}

func handleDefaultConfigOverride(logger zerolog.Logger, filename, configurationName string, target *TestConfig, content []byte) error {
logger.Debug().Msgf("Handling default config override for %s", filename)
oldConfig := MustCopy(target)
newConfig := TestConfig{}

err := ctf_config.BytesToAnyTomlStruct(logger, filename, configurationName, &target, content)
if err != nil {
return errors.Wrapf(err, "error reading file %s", filename)
}

err = ctf_config.BytesToAnyTomlStruct(logger, filename, configurationName, &newConfig, content)
if err != nil {
return errors.Wrapf(err, "error reading file %s", filename)
}

// temporary fix for Duration not being correctly copied
if oldConfig != nil && oldConfig.Seth != nil && oldConfig.Seth.Networks != nil {
for i, old_network := range oldConfig.Seth.Networks {
for _, target_network := range target.Seth.Networks {
if old_network.ChainID == target_network.ChainID {
oldConfig.Seth.Networks[i].TxnTimeout = target_network.TxnTimeout
}
}
}
}

// override instead of merging
if (newConfig.Seth != nil && len(newConfig.Seth.Networks) > 0) && (oldConfig != nil && oldConfig.Seth != nil && len(oldConfig.Seth.Networks) > 0) {
networksToUse := map[string]*seth.Network{}
for i, old_network := range oldConfig.Seth.Networks {
for _, new_network := range newConfig.Seth.Networks {
if old_network.ChainID == new_network.ChainID {
oldConfig.Seth.Networks[i] = new_network
break
}
if _, ok := networksToUse[new_network.ChainID]; !ok {
networksToUse[new_network.ChainID] = new_network
}
}
networksToUse[old_network.ChainID] = oldConfig.Seth.Networks[i]
}
target.Seth.Networks = []*seth.Network{}
for _, network := range networksToUse {
target.Seth.Networks = append(target.Seth.Networks, network)
}
}

return nil
}

0 comments on commit c0983b4

Please sign in to comment.