diff --git a/test/ca_bundle/ca_bundle_test.go b/test/ca_bundle/ca_bundle_test.go index 2a041d932..9efb1d860 100644 --- a/test/ca_bundle/ca_bundle_test.go +++ b/test/ca_bundle/ca_bundle_test.go @@ -29,8 +29,6 @@ const ( commonConfigTOML = "/common-config.toml" targetString = "x509: certificate signed by unknown authority" - // Let the agent run for 30 seconds. This will give agent enough time to call server - agentRuntime = 30 * time.Second localstackS3Key = "integration-test/ls_tmp/%s" keyDelimiter = "/" localstackConfigPath = "../../localstack/ls_tmp/" @@ -38,11 +36,15 @@ const ( combinePem = "combine.pem" snakeOilPem = "snakeoil.pem" tmpDirectory = "/tmp/" + runEMF = "sudo bash resources/emf.sh" + logfile = "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log" ) type input struct { - findTarget bool - dataInput string + findTarget bool + commonConfigInput string + agentConfigInput string + testType string } func init() { @@ -58,28 +60,36 @@ func TestBundle(t *testing.T) { parameters := []input{ //Use the system pem ca bundle + local stack pem file ssl should connect thus target string not found - {dataInput: "resources/integration/ssl/with/combine/bundle", findTarget: false}, + {commonConfigInput: "resources/with/combine/", agentConfigInput: "resources/https/", findTarget: false, testType: "metric"}, + {commonConfigInput: "resources/with/combine/", agentConfigInput: "resources/https/", findTarget: false, testType: "emf"}, //Do not look for ca bundle with http connection should connect thus target string not found - {dataInput: "resources/integration/ssl/without/bundle/http", findTarget: false}, + {commonConfigInput: "resources/without/", agentConfigInput: "resources/http/", findTarget: false, testType: "metric"}, + {commonConfigInput: "resources/without/", agentConfigInput: "resources/http/", findTarget: false, testType: "emf"}, //Use the system pem ca bundle ssl should not connect thus target string found - {dataInput: "resources/integration/ssl/with/original/bundle", findTarget: true}, + {commonConfigInput: "resources/with/original/", agentConfigInput: "resources/https/", findTarget: true, testType: "metric"}, + {commonConfigInput: "resources/with/original/", agentConfigInput: "resources/https/", findTarget: true, testType: "emf"}, //Do not look for ca bundle should not connect thus target string found - {dataInput: "resources/integration/ssl/without/bundle", findTarget: true}, + {commonConfigInput: "resources/without/", agentConfigInput: "resources/https/", findTarget: true, testType: "metric"}, + {commonConfigInput: "resources/without/", agentConfigInput: "resources/https/", findTarget: true, testType: "emf"}, } for _, parameter := range parameters { //before test run - log.Printf("resource file location %s find target %t", parameter.dataInput, parameter.findTarget) - t.Run(fmt.Sprintf("resource file location %s find target %t", parameter.dataInput, parameter.findTarget), func(t *testing.T) { - common.ReplaceLocalStackHostName(parameter.dataInput + configJSON) - t.Logf("config file after localstack host replace %s", string(readFile(parameter.dataInput+configJSON))) - common.CopyFile(parameter.dataInput+configJSON, configOutputPath) - common.CopyFile(parameter.dataInput+commonConfigTOML, commonConfigOutputPath) + configFile := parameter.agentConfigInput + parameter.testType + configJSON + commonConfigFile := parameter.commonConfigInput + commonConfigTOML + log.Printf("common config file location %s agent config file %s find target %t", commonConfigFile, configFile, parameter.findTarget) + t.Run(fmt.Sprintf("common config file location %s agent config file %s find target %t", commonConfigFile, configFile, parameter.findTarget), func(t *testing.T) { + common.RecreateAgentLogfile(logfile) + common.ReplaceLocalStackHostName(configFile) + t.Logf("config file after localstack host replace %s", string(readFile(configFile))) + common.CopyFile(configFile, configOutputPath) + common.CopyFile(commonConfigFile, commonConfigOutputPath) common.StartAgent(configOutputPath, true, false) - time.Sleep(agentRuntime) - log.Printf("Agent has been running for : %s", agentRuntime.String()) + // this command will take 5 seconds time 12 = 1 minute + common.RunCommand(runEMF) + log.Printf("Agent has been running for : %s", time.Minute) common.StopAgent() - output := common.ReadAgentOutput(agentRuntime) + output := common.ReadAgentLogfile(logfile) containsTarget := outputLogContainsTarget(output) if (parameter.findTarget && !containsTarget) || (!parameter.findTarget && containsTarget) { t.Errorf("Find target is %t contains target is %t", parameter.findTarget, containsTarget) diff --git a/test/ca_bundle/resources/emf.sh b/test/ca_bundle/resources/emf.sh new file mode 100755 index 000000000..aff268b06 --- /dev/null +++ b/test/ca_bundle/resources/emf.sh @@ -0,0 +1,6 @@ +for times in {1..6} + do + sleep 5 + CURRENT_TIME=$(date +%s%N | cut -b1-13) + echo '{"_aws":{"Timestamp":'"${CURRENT_TIME}"',"LogGroupName":"MetricValueBenchmarkTest","CloudWatchMetrics":[{"Namespace":"MetricValueBenchmarkTest","Dimensions":[["Type","InstanceId"]],"Metrics":[{"Name":"EMFCounter","Unit":"Count"}]}]},"Type":"Counter","EMFCounter":5}' \ > /dev/udp/0.0.0.0/25888 + done \ No newline at end of file diff --git a/test/ca_bundle/resources/http/emf/config.json b/test/ca_bundle/resources/http/emf/config.json new file mode 100644 index 000000000..1c39a0eca --- /dev/null +++ b/test/ca_bundle/resources/http/emf/config.json @@ -0,0 +1,13 @@ +{ + "agent": { + "run_as_user": "root", + "debug": true + }, + "logs": { + "endpoint_override": "http://localhost.localstack.cloud:4566", + "metrics_collected": { + "emf": { } + }, + "force_flush_interval": 5 + } +} \ No newline at end of file diff --git a/test/ca_bundle/resources/integration/ssl/without/bundle/http/config.json b/test/ca_bundle/resources/http/metric/config.json similarity index 92% rename from test/ca_bundle/resources/integration/ssl/without/bundle/http/config.json rename to test/ca_bundle/resources/http/metric/config.json index a86156cd7..606a9a7f7 100644 --- a/test/ca_bundle/resources/integration/ssl/without/bundle/http/config.json +++ b/test/ca_bundle/resources/http/metric/config.json @@ -2,8 +2,7 @@ "agent": { "metrics_collection_interval": 10, "run_as_user": "root", - "debug": true, - "logfile": "" + "debug": true }, "metrics": { "endpoint_override": "http://localhost.localstack.cloud:4566", diff --git a/test/ca_bundle/resources/https/emf/config.json b/test/ca_bundle/resources/https/emf/config.json new file mode 100644 index 000000000..d49a63403 --- /dev/null +++ b/test/ca_bundle/resources/https/emf/config.json @@ -0,0 +1,13 @@ +{ + "agent": { + "run_as_user": "root", + "debug": true + }, + "logs": { + "endpoint_override": "https://localhost.localstack.cloud:4566", + "metrics_collected": { + "emf": { } + }, + "force_flush_interval": 5 + } +} \ No newline at end of file diff --git a/test/ca_bundle/resources/integration/ssl/with/combine/bundle/config.json b/test/ca_bundle/resources/https/metric/config.json similarity index 92% rename from test/ca_bundle/resources/integration/ssl/with/combine/bundle/config.json rename to test/ca_bundle/resources/https/metric/config.json index e40748824..8b16014fc 100644 --- a/test/ca_bundle/resources/integration/ssl/with/combine/bundle/config.json +++ b/test/ca_bundle/resources/https/metric/config.json @@ -2,8 +2,7 @@ "agent": { "metrics_collection_interval": 10, "run_as_user": "root", - "debug": true, - "logfile": "" + "debug": true }, "metrics": { "endpoint_override": "https://localhost.localstack.cloud:4566", diff --git a/test/ca_bundle/resources/integration/ssl/with/original/bundle/config.json b/test/ca_bundle/resources/integration/ssl/with/original/bundle/config.json deleted file mode 100644 index e40748824..000000000 --- a/test/ca_bundle/resources/integration/ssl/with/original/bundle/config.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "agent": { - "metrics_collection_interval": 10, - "run_as_user": "root", - "debug": true, - "logfile": "" - }, - "metrics": { - "endpoint_override": "https://localhost.localstack.cloud:4566", - "metrics_collected": { - "disk": { - "measurement": [ - "used_percent" - ], - "resources": [ - "*" - ] - }, - "mem": { - "measurement": [ - "mem_used_percent" - ] - } - }, - "force_flush_interval": 10 - } -} \ No newline at end of file diff --git a/test/ca_bundle/resources/integration/ssl/without/bundle/config.json b/test/ca_bundle/resources/integration/ssl/without/bundle/config.json deleted file mode 100644 index e40748824..000000000 --- a/test/ca_bundle/resources/integration/ssl/without/bundle/config.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "agent": { - "metrics_collection_interval": 10, - "run_as_user": "root", - "debug": true, - "logfile": "" - }, - "metrics": { - "endpoint_override": "https://localhost.localstack.cloud:4566", - "metrics_collected": { - "disk": { - "measurement": [ - "used_percent" - ], - "resources": [ - "*" - ] - }, - "mem": { - "measurement": [ - "mem_used_percent" - ] - } - }, - "force_flush_interval": 10 - } -} \ No newline at end of file diff --git a/test/ca_bundle/resources/integration/ssl/without/bundle/http/common-config.toml b/test/ca_bundle/resources/integration/ssl/without/bundle/http/common-config.toml deleted file mode 100644 index 2c2bf52d5..000000000 --- a/test/ca_bundle/resources/integration/ssl/without/bundle/http/common-config.toml +++ /dev/null @@ -1,3 +0,0 @@ - -# [ssl] -# ca_bundle_path = "/tmp/combine.pem" \ No newline at end of file diff --git a/test/ca_bundle/resources/integration/ssl/with/combine/bundle/common-config.toml b/test/ca_bundle/resources/with/combine/common-config.toml similarity index 100% rename from test/ca_bundle/resources/integration/ssl/with/combine/bundle/common-config.toml rename to test/ca_bundle/resources/with/combine/common-config.toml diff --git a/test/ca_bundle/resources/integration/ssl/with/original/bundle/common-config.toml b/test/ca_bundle/resources/with/original/common-config.toml similarity index 100% rename from test/ca_bundle/resources/integration/ssl/with/original/bundle/common-config.toml rename to test/ca_bundle/resources/with/original/common-config.toml diff --git a/test/ca_bundle/resources/integration/ssl/without/bundle/common-config.toml b/test/ca_bundle/resources/without/common-config.toml similarity index 100% rename from test/ca_bundle/resources/integration/ssl/without/bundle/common-config.toml rename to test/ca_bundle/resources/without/common-config.toml diff --git a/util/common/agent_util_unix.go b/util/common/agent_util_unix.go index 03c9a7287..f0c6db3bd 100644 --- a/util/common/agent_util_unix.go +++ b/util/common/agent_util_unix.go @@ -9,10 +9,10 @@ import ( "bytes" "fmt" "log" + "os" "os/exec" "path/filepath" "strings" - "time" "github.com/aws/amazon-cloudwatch-agent-test/environment" ) @@ -166,16 +166,22 @@ func StopAgent() { log.Printf("Agent is stopped") } -func ReadAgentOutput(d time.Duration) string { +func ReadAgentLogfile(logfile string) string { + out, err := os.ReadFile(logfile) + if err != nil { + log.Fatal(fmt.Sprint(err) + string(out)) + } + return string(out) +} + +func RecreateAgentLogfile(logfile string) { out, err := exec.Command("bash", "-c", - fmt.Sprintf("sudo journalctl -u amazon-cloudwatch-agent.service --since \"%s ago\" --no-pager -q", d.String())). + fmt.Sprintf("sudo rm %s", logfile)). Output() if err != nil { log.Fatal(fmt.Sprint(err) + string(out)) } - - return string(out) } func RunShellScript(path string, args ...string) (string, error) {