Skip to content

Commit

Permalink
Merge branch 'aws:main' into main-addIntegTests
Browse files Browse the repository at this point in the history
  • Loading branch information
KlwntSingh authored Mar 3, 2024
2 parents bcc13b3 + 2e403f6 commit 73555b8
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 111 deletions.
2 changes: 1 addition & 1 deletion generator/resources/ec2_linux_test_matrix.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
{
"os": "al2023",
"username": "ec2-user",
"instanceType":"m7g.medium",
"instanceType":"m6g.medium",
"installAgentCommand": "go run ./install/install_agent.go rpm",
"ami": "cloudwatch-agent-integration-test-aarch64-al2023*",
"caCertPath": "/etc/ssl/certs/ca-bundle.crt",
Expand Down
43 changes: 40 additions & 3 deletions generator/test_case_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"

"github.com/mitchellh/mapstructure"
"golang.org/x/exp/slices"
)

type matrixRow struct {
Expand Down Expand Up @@ -108,6 +109,10 @@ var testTypeToTestConfig = map[string][]testConfig{
terraformDir: "terraform/ec2/creds",
targets: map[string]map[string]struct{}{"os": {"al2": {}}},
},
{
testDir: "./test/app_signals",
targets: map[string]map[string]struct{}{"os": {"al2": {}}, "arc": {"amd64": {}}},
},
},
/*
You can only place 1 mac instance on a dedicate host a single time.
Expand Down Expand Up @@ -215,6 +220,29 @@ var testTypeToTestConfig = map[string][]testConfig{
},
}

type partition struct {
configName string
tests []string
ami []string
}

var partitionTests = map[string]partition{
"commercial": {
configName: "",
tests: []string{},
ami: []string{},
},
"itar": {
configName: "_itar",
tests: []string{testTypeKeyEc2Linux},
ami: []string{"cloudwatch-agent-integration-test-aarch64-al2023*"},
},
"china": {configName: "_china",
tests: []string{testTypeKeyEc2Linux},
ami: []string{"cloudwatch-agent-integration-test-aarch64-al2023*"},
},
}

func copyAllEC2LinuxTestForOnpremTesting() {
/* Some tests need to be fixed in order to run in both environment, so for now for PoC, run one that works.
testTypeToTestConfig["ec2_linux_onprem"] = testTypeToTestConfig[testTypeKeyEc2Linux]
Expand All @@ -231,12 +259,17 @@ func main() {
copyAllEC2LinuxTestForOnpremTesting()

for testType, testConfigs := range testTypeToTestConfig {
testMatrix := genMatrix(testType, testConfigs)
writeTestMatrixFile(testType, testMatrix)
for _, partition := range partitionTests {
if len(partition.tests) != 0 && !slices.Contains(partition.tests, testType) {
continue
}
testMatrix := genMatrix(testType, testConfigs, partition.ami)
writeTestMatrixFile(testType+partition.configName, testMatrix)
}
}
}

func genMatrix(testType string, testConfigs []testConfig) []matrixRow {
func genMatrix(testType string, testConfigs []testConfig, ami []string) []matrixRow {
openTestMatrix, err := os.Open(fmt.Sprintf("generator/resources/%v_test_matrix.json", testType))

if err != nil {
Expand Down Expand Up @@ -267,6 +300,10 @@ func genMatrix(testType string, testConfigs []testConfig) []matrixRow {
log.Panicf("can't decode map test %v to metric line struct with error %v", testConfig, err)
}

if len(ami) != 0 && !slices.Contains(ami, row.Ami) {
continue
}

if testConfig.targets == nil || shouldAddTest(&row, testConfig.targets) {
testMatrixComplete = append(testMatrixComplete, row)
}
Expand Down
4 changes: 2 additions & 2 deletions localstack/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
# @TODO use latest when this is fixed https://github.com/localstack/localstack/issues/5502
# Use 0.12.20 since this is last version that worked for now
image: localstack/localstack:0.12.20
# Use 0.13.0 since this is last version that worked for now
image: localstack/localstack:0.13.0
network_mode: bridge
ports:
- "127.0.0.1:53:53"
Expand Down
4 changes: 3 additions & 1 deletion localstack/ls_tmp/snakeoil.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
DNS.2 = localhost.localstack.cloud
DNS.3 = *.us-west-2.compute.amazonaws.com
DNS.3 = *.us-west-2.compute.amazonaws.com
DNS.4 = *.us-gov-east-1.compute.amazonaws.com
DNS.5 = *.cn-north-1.compute.amazonaws.com
6 changes: 3 additions & 3 deletions terraform/ec2/localstack/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ resource "aws_instance" "integration-test" {
inline = [
"cloud-init status --wait",
"clone the agent and start the localstack",
"git clone ${var.github_test_repo}",
"git clone --branch ${var.github_test_repo_branch} ${var.github_test_repo}",
"cd amazon-cloudwatch-agent-test",
"git reset --hard ${var.cwa_test_github_sha}",
"echo set up ssl pem for localstack, then start localstack",
Expand All @@ -67,7 +67,7 @@ resource "aws_instance" "integration-test" {
]
connection {
type = "ssh"
user = "ubuntu"
user = "ec2-user"
private_key = local.private_key_content
host = self.public_dns
}
Expand All @@ -83,6 +83,6 @@ data "aws_ami" "latest" {

filter {
name = "name"
values = ["cloudwatch-agent-integration-test-ubuntu*"]
values = ["cloudwatch-agent-integration-test-aarch64-al2023*"]
}
}
6 changes: 5 additions & 1 deletion terraform/ec2/localstack/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

variable "ec2_instance_type" {
type = string
default = "t3a.medium"
default = "m6g.medium"
}

variable "ssh_key_name" {
Expand Down Expand Up @@ -39,4 +39,8 @@ variable "github_test_repo" {
variable "s3_bucket" {
type = string
default = ""
}

variable "github_test_repo_branch" {
default = "main"
}
1 change: 1 addition & 0 deletions terraform/ec2/mac/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ resource "null_resource" "integration_test" {
"sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:${module.validator.instance_agent_config}",
"./validator --validator-config=${module.validator.instance_validator_config} --preparation-mode=false",
"cd ~/amazon-cloudwatch-agent-test",
"echo run sanity test && sudo go test ./test/sanity -p 1 -v",
"sudo go test ./test/run_as_user -p 1 -timeout 1h -computeType=EC2 -bucket=${var.s3_bucket} -cwaCommitSha=${var.cwa_github_sha} -instanceId=${aws_instance.cwagent.id} -v",
]
}
Expand Down
4 changes: 4 additions & 0 deletions terraform/ec2/win/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ resource "null_resource" "integration_test_run_validator" {
"powershell.exe -Command \"Start-Sleep -s 60\"",
"powershell.exe -Command \"Invoke-WebRequest -Uri http://localhost:9404 -UseBasicParsing\"",
"set AWS_REGION=${var.region}",
"git clone --branch ${var.github_test_repo_branch} ${var.github_test_repo}",
"cd amazon-cloudwatch-agent-test",
"go test ./test/sanity -p 1 -v",
"cd ..",
"validator.exe --validator-config=${module.validator.instance_validator_config} --preparation-mode=true",
var.use_ssm ? "powershell \"& 'C:\\Program Files\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1' -a fetch-config -m ec2 -s -c ssm:${local.ssm_parameter_name}\"" : "powershell \"& 'C:\\Program Files\\Amazon\\AmazonCloudWatchAgent\\amazon-cloudwatch-agent-ctl.ps1' -a fetch-config -m ec2 -s -c file:${module.validator.instance_agent_config}\"",
"validator.exe --validator-config=${module.validator.instance_validator_config} --preparation-mode=false"
Expand Down
2 changes: 1 addition & 1 deletion terraform/eks/daemon/app_signals/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ resource "kubernetes_daemonset" "service" {
# Template Files
##########################################
locals {
cwagent_config = "../../../../${var.test_dir}/resources/config.json"
cwagent_config = "../../../../${var.test_dir}/agent_configs/config.json"
server_consumer = "../../../../${var.test_dir}/resources/metrics/server_consumer.json"
client_producer = "../../../../${var.test_dir}/resources/metrics/client_producer.json"
traces = "../../../../${var.test_dir}/resources/traces/traces.json"
Expand Down
File renamed without changes.
31 changes: 28 additions & 3 deletions test/app_signals/app_signals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func init() {

var (
eksTestRunners []*test_runner.EKSTestRunner
ec2TestRunners []*test_runner.TestRunner
)

func getEksTestRunners(env *environment.MetaData) []*test_runner.EKSTestRunner {
Expand All @@ -53,22 +54,41 @@ func getEksTestRunners(env *environment.MetaData) []*test_runner.EKSTestRunner {

eksTestRunners = []*test_runner.EKSTestRunner{
{
Runner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsServerConsumerTestName, "HostedIn.EKS.Cluster"},
Runner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsServerConsumerTestName, "HostedIn.EKS.Cluster", env.ComputeType},
Env: *env,
},
{
Runner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsClientProducerTestName, "HostedIn.EKS.Cluster"},
Runner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsClientProducerTestName, "HostedIn.EKS.Cluster", env.ComputeType},
Env: *env,
},
{
Runner: &AppSignalsTracesRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsTracesTestName, env.EKSClusterName},
Runner: &AppSignalsTracesRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsTracesTestName, env.EKSClusterName, env.ComputeType},
Env: *env,
},
}
}
return eksTestRunners
}

func getEc2TestRunners(env *environment.MetaData) []*test_runner.TestRunner {
if ec2TestRunners == nil {
factory := dimension.GetDimensionFactory(*env)

ec2TestRunners = []*test_runner.TestRunner{
{
TestRunner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsServerConsumerTestName, "HostedIn.Environment", env.ComputeType},
},
{
TestRunner: &AppSignalsMetricsRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsClientProducerTestName, "HostedIn.Environment", env.ComputeType},
},
{
TestRunner: &AppSignalsTracesRunner{test_runner.BaseTestRunner{DimensionFactory: factory}, AppSignalsTracesTestName, "Generic", env.ComputeType},
},
}
}
return ec2TestRunners
}

func (suite *AppSignalsTestSuite) TestAllInSuite() {
env := environment.GetEnvironmentMetaData()
switch env.ComputeType {
Expand All @@ -77,6 +97,11 @@ func (suite *AppSignalsTestSuite) TestAllInSuite() {
for _, testRunner := range getEksTestRunners(env) {
testRunner.Run(suite, env)
}
case computetype.EC2:
log.Println("Environment compute type is EC2")
for _, testRunner := range getEc2TestRunners(env) {
suite.AddToSuiteResult(testRunner.Run())
}
default:
return
}
Expand Down
53 changes: 48 additions & 5 deletions test/app_signals/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ package app_signals
import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"

"github.com/aws/amazon-cloudwatch-agent-test/environment/computetype"
"github.com/aws/amazon-cloudwatch-agent-test/test/metric"
"github.com/aws/amazon-cloudwatch-agent-test/test/metric/dimension"
"github.com/aws/amazon-cloudwatch-agent-test/test/status"
"github.com/aws/amazon-cloudwatch-agent-test/test/test_runner"
"github.com/aws/amazon-cloudwatch-agent-test/util/common"
)

const testRetryCount = 6
Expand All @@ -21,12 +25,13 @@ type AppSignalsMetricsRunner struct {
test_runner.BaseTestRunner
testName string
dimensionKey string
computeType computetype.ComputeType
}

func (t *AppSignalsMetricsRunner) Validate() status.TestGroupResult {
metricsToFetch := t.GetMeasuredMetrics()
testResults := make([]status.TestResult, len(metricsToFetch))
instructions := GetInstructionsFromTestName(t.testName)
instructions := GetInstructionsFromTestName(t.testName, t.computeType)

for i, metricName := range metricsToFetch {
var testResult status.TestResult
Expand Down Expand Up @@ -59,18 +64,56 @@ func (t *AppSignalsMetricsRunner) GetMeasuredMetrics() []string {
}

func (e *AppSignalsMetricsRunner) GetAgentConfigFileName() string {
return ""
return "config.json"
}

func (e *AppSignalsMetricsRunner) SetupAfterAgentRun() error {
// sends metrics data only for EC2
if e.computeType == computetype.EC2 {
common.RunCommand("pwd")
cmd := `while true; export START_TIME=$(date +%s%N); do
cat ./resources/metrics/server_consumer.json | sed -e "s/START_TIME/$START_TIME/" > server_consumer.json;
curl -H 'Content-Type: application/json' -d @server_consumer.json -i http://127.0.0.1:4316/v1/metrics --verbose;
cat ./resources/metrics/client_producer.json | sed -e "s/START_TIME/$START_TIME/" > client_producer.json;
curl -H 'Content-Type: application/json' -d @client_producer.json -i http://127.0.0.1:4316/v1/metrics --verbose;
sleep 5; done`
return common.RunAsyncCommand(cmd)
}

return nil
}

func GetInstructionsFromTestName(testName string) []dimension.Instruction {
func GetInstructionsFromTestName(testName string, computeType computetype.ComputeType) []dimension.Instruction {
var instructions []dimension.Instruction
switch testName {
case AppSignalsClientProducerTestName:
return metric.ClientProducerInstructions
instructions = metric.ClientProducerInstructions
case AppSignalsServerConsumerTestName:
return metric.ServerConsumerInstructions
instructions = metric.ServerConsumerInstructions
default:
return nil
}

if computeType == computetype.EKS {
instructions = append(instructions, []dimension.Instruction{
{
Key: "HostedIn.EKS.Cluster",
Value: dimension.UnknownDimensionValue(),
},
{
Key: "HostedIn.K8s.Namespace",
Value: dimension.ExpectedDimensionValue{Value: aws.String("default")},
},
}...)
} else {
//EC2
instructions = append(instructions, dimension.Instruction{
Key: "HostedIn.Environment",
Value: dimension.ExpectedDimensionValue{Value: aws.String("Generic")},
})
}

return instructions
}

var _ test_runner.ITestRunner = (*AppSignalsMetricsRunner)(nil)
Loading

0 comments on commit 73555b8

Please sign in to comment.