From 50efb3932fe7166c2b4cc5099cac43349c9e36e3 Mon Sep 17 00:00:00 2001 From: Jeffrey Chien Date: Mon, 3 Feb 2025 16:07:56 -0500 Subject: [PATCH] Re-enable the Prometheus translator tests --- translator/tocwconfig/tocwconfig_test.go | 93 ++++++++++++------------ 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/translator/tocwconfig/tocwconfig_test.go b/translator/tocwconfig/tocwconfig_test.go index 7b45433150..dc69be4ed4 100644 --- a/translator/tocwconfig/tocwconfig_test.go +++ b/translator/tocwconfig/tocwconfig_test.go @@ -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" @@ -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{