Skip to content

Commit

Permalink
Add EMF UDP TCP Logs CA Bundle Tests (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethAmazon authored Aug 11, 2023
1 parent 11f3c4e commit a847e06
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 83 deletions.
44 changes: 27 additions & 17 deletions test/ca_bundle/ca_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ 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/"
originalPem = "original.pem"
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() {
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/ca_bundle/resources/emf.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/ca_bundle/resources/http/emf/config.json
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions test/ca_bundle/resources/https/emf/config.json
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

16 changes: 11 additions & 5 deletions util/common/agent_util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/aws/amazon-cloudwatch-agent-test/environment"
)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a847e06

Please sign in to comment.