Skip to content

Commit

Permalink
Re-enable the Prometheus translator unit tests (#1527)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Feb 13, 2025
1 parent 8e12340 commit 2de795c
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions translator/tocwconfig/tocwconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/aws/amazon-cloudwatch-agent/cfg/commonconfig"
"github.com/aws/amazon-cloudwatch-agent/cfg/envconfig"
"github.com/aws/amazon-cloudwatch-agent/internal/retryer"
"github.com/aws/amazon-cloudwatch-agent/tool/testutil"
"github.com/aws/amazon-cloudwatch-agent/translator"
"github.com/aws/amazon-cloudwatch-agent/translator/cmdutil"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
Expand Down Expand Up @@ -426,52 +427,52 @@ func TestPrometheusConfigwithTargetAllocator(t *testing.T) {

}

//func TestOtelPrometheusConfig(t *testing.T) {
// resetContext(t)
// context.CurrentContext().SetRunInContainer(true)
// context.CurrentContext().SetMode(config.ModeEC2)
// testutil.SetPrometheusRemoteWriteTestingEnv(t)
// t.Setenv(config.HOST_NAME, "host_name_from_env")
// temp := t.TempDir()
// prometheusConfigFileName := filepath.Join(temp, "prometheus.yaml")
// ecsSdFileName := filepath.Join(temp, "ecs_sd_results.yaml")
// expectedEnvVars := map[string]string{}
// tokenReplacements := map[string]string{
// prometheusFileNameToken: strings.ReplaceAll(prometheusConfigFileName, "\\", "\\\\"),
// ecsSdFileNameToken: strings.ReplaceAll(ecsSdFileName, "\\", "\\\\"),
// }
// // Load prometheus config and replace ecs sd results file name token with temp file name
// testPrometheusConfig := strings.ReplaceAll(prometheusConfig, "{"+ecsSdFileNameToken+"}", ecsSdFileName)
// // Write the modified prometheus config to temp prometheus config file
// err := os.WriteFile(prometheusConfigFileName, []byte(testPrometheusConfig), os.ModePerm)
// require.NoError(t, err)
// // In the following checks, we first load the json and replace tokens with the temp files
// // Additionally, before comparing with actual, we again replace tokens with temp files in the expected toml & yaml
// checkTranslation(t, "prometheus_otel_config_linux", "linux", expectedEnvVars, "", tokenReplacements)
//}
//
//func TestCombinedPrometheusConfig(t *testing.T) {
// resetContext(t)
// context.CurrentContext().SetMode(config.ModeEC2)
// testutil.SetPrometheusRemoteWriteTestingEnv(t)
// t.Setenv(config.HOST_NAME, "host_name_from_env")
// temp := t.TempDir()
// prometheusConfigFileName := filepath.Join(temp, "prometheus.yaml")
// ecsSdFileName := filepath.Join(temp, "ecs_sd_results.yaml")
// expectedEnvVars := map[string]string{}
// tokenReplacements := map[string]string{
// prometheusFileNameToken: strings.ReplaceAll(prometheusConfigFileName, "\\", "\\\\"),
// ecsSdFileNameToken: strings.ReplaceAll(ecsSdFileName, "\\", "\\\\"),
// }
// // Load prometheus config and replace ecs sd results file name token with temp file name
// testPrometheusConfig := strings.ReplaceAll(prometheusConfig, "{"+ecsSdFileNameToken+"}", ecsSdFileName)
// // Write the modified prometheus config to temp prometheus config file
// err := os.WriteFile(prometheusConfigFileName, []byte(testPrometheusConfig), os.ModePerm)
// require.NoError(t, err)
// // In the following checks, we first load the json and replace tokens with the temp files
// // Additionally, before comparing with actual, we again replace tokens with temp files in the expected toml & yaml
// checkTranslation(t, "prometheus_combined_config_linux", "linux", expectedEnvVars, "", tokenReplacements)
//}
func TestOtelPrometheusConfig(t *testing.T) {
resetContext(t)
context.CurrentContext().SetRunInContainer(true)
context.CurrentContext().SetMode(config.ModeEC2)
testutil.SetPrometheusRemoteWriteTestingEnv(t)
t.Setenv(config.HOST_NAME, "host_name_from_env")
temp := t.TempDir()
prometheusConfigFileName := filepath.Join(temp, "prometheus.yaml")
ecsSdFileName := filepath.Join(temp, "ecs_sd_results.yaml")
expectedEnvVars := map[string]string{}
tokenReplacements := map[string]string{
prometheusFileNameToken: strings.ReplaceAll(prometheusConfigFileName, "\\", "\\\\"),
ecsSdFileNameToken: strings.ReplaceAll(ecsSdFileName, "\\", "\\\\"),
}
// Load prometheus config and replace ecs sd results file name token with temp file name
testPrometheusConfig := strings.ReplaceAll(prometheusConfig, "{"+ecsSdFileNameToken+"}", ecsSdFileName)
// Write the modified prometheus config to temp prometheus config file
err := os.WriteFile(prometheusConfigFileName, []byte(testPrometheusConfig), os.ModePerm)
require.NoError(t, err)
// In the following checks, we first load the json and replace tokens with the temp files
// Additionally, before comparing with actual, we again replace tokens with temp files in the expected toml & yaml
checkTranslation(t, "prometheus_otel_config_linux", "linux", expectedEnvVars, "", tokenReplacements)
}

func TestCombinedPrometheusConfig(t *testing.T) {
resetContext(t)
context.CurrentContext().SetMode(config.ModeEC2)
testutil.SetPrometheusRemoteWriteTestingEnv(t)
t.Setenv(config.HOST_NAME, "host_name_from_env")
temp := t.TempDir()
prometheusConfigFileName := filepath.Join(temp, "prometheus.yaml")
ecsSdFileName := filepath.Join(temp, "ecs_sd_results.yaml")
expectedEnvVars := map[string]string{}
tokenReplacements := map[string]string{
prometheusFileNameToken: strings.ReplaceAll(prometheusConfigFileName, "\\", "\\\\"),
ecsSdFileNameToken: strings.ReplaceAll(ecsSdFileName, "\\", "\\\\"),
}
// Load prometheus config and replace ecs sd results file name token with temp file name
testPrometheusConfig := strings.ReplaceAll(prometheusConfig, "{"+ecsSdFileNameToken+"}", ecsSdFileName)
// Write the modified prometheus config to temp prometheus config file
err := os.WriteFile(prometheusConfigFileName, []byte(testPrometheusConfig), os.ModePerm)
require.NoError(t, err)
// In the following checks, we first load the json and replace tokens with the temp files
// Additionally, before comparing with actual, we again replace tokens with temp files in the expected toml & yaml
checkTranslation(t, "prometheus_combined_config_linux", "linux", expectedEnvVars, "", tokenReplacements)
}

func TestBasicConfig(t *testing.T) {
testCases := map[string]testCase{
Expand Down

0 comments on commit 2de795c

Please sign in to comment.