Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable the Prometheus translator unit tests #1527

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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