diff --git a/.github/workflows/integrationTest.yml b/.github/workflows/integrationTest.yml index 55c01468f9..47de772049 100644 --- a/.github/workflows/integrationTest.yml +++ b/.github/workflows/integrationTest.yml @@ -86,6 +86,33 @@ jobs: if: steps.cached_binaries.outputs.cache-hit != 'true' run: aws s3 cp build/bin s3://${S3_INTEGRATION_BUCKET}/integration-test/binary/${{ github.sha }} --recursive + GenerateTestMatrix: + name: 'GenerateTestMatrix' + runs-on: ubuntu-latest + outputs: + ec2_linux_matrix: ${{ steps.set-matrix.outputs.ec2_linux_matrix }} + ec2_windows_matrix: ${{ steps.set-matrix.outputs.ec2_windows_matrix }} + steps: + - uses: actions/checkout@v2 + + #TODO upgrade to 1.17 after @ZhenyuTan-amz updates our go build version + - name: Set up Go 1.x + uses: actions/setup-go@v2 + with: + go-version: ~1.15.15 + + - name: Generate matrix + id: set-matrix + run: | + go run --tags=generator integration/generator/test_case_generator.go + echo "::set-output name=ec2_linux_matrix::$(echo $(cat integration/generator/resources/linux_complete_test_matrix.json))" + echo "::set-output name=ec2_windows_matrix::$(echo $(cat integration/generator/resources/windows_complete_test_matrix.json))" + + - name: Echo test plan matrix + run: | + echo ${{ steps.set-matrix.outputs.ec2_linux_matrix }} + echo ${{ steps.set-matrix.outputs.ec2_windows_matrix }} + MakeMSIZip: name: 'MakeMSIZip' runs-on: ubuntu-latest @@ -311,7 +338,7 @@ jobs: aws s3 cp terraform.tfstate s3://${S3_INTEGRATION_BUCKET}/integration-test/local-stack-terraform-state/${GITHUB_SHA}/terraform.tfstate EC2LinuxIntegrationTest: - needs: [MakeBinary, StartLocalStack] + needs: [MakeBinary, StartLocalStack, GenerateTestMatrix] name: 'EC2LinuxIntegrationTest' runs-on: ubuntu-latest defaults: @@ -320,53 +347,7 @@ jobs: strategy: fail-fast: false matrix: - arrays: [ - { os: "ubuntu", username: "ubuntu", - # ubuntu needs to have dpkg lock cleared before installing cw agent - installAgentCommand: "aptdcon --safe-upgrade && sudo dpkg -i -E ./amazon-cloudwatch-agent.deb", - ami: "cloudwatch-agent-integration-test-ubuntu*", caCertPath: "/etc/ssl/certs/ca-certificates.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.deb" }, - { os: "al2", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-al2*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "rhel8", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-rhel8-base*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "rhel8-1", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-rhel8-1*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "rhel8-2", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-rhel8-2*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "rhel8-3", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-rhel8-3*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "rhel8-4", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-rhel8-4*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "ol8-1", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-ol8-1*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "ol8-2", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-ol8-2*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "o8-3", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-ol8-3*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" }, - { os: "ol8-4", username: "ec2-user", - installAgentCommand: "rpm -U ./amazon-cloudwatch-agent.rpm", - ami: "cloudwatch-agent-integration-test-ol8-4*", caCertPath: "/etc/ssl/certs/ca-bundle.crt", - arc: "amd64", binaryName: "amazon-cloudwatch-agent.rpm" } - ] + arrays: ${{ fromJson(needs.GenerateTestMatrix.outputs.ec2_linux_matrix) }} steps: - uses: actions/checkout@v2 @@ -413,13 +394,14 @@ jobs: -var="key_name=${KEY_NAME}" -var="test_name=cw-integ-test-${{ matrix.arrays.os }}" -var="iam_instance_profile=${IAM_ROLE}" + -var="tag=${{ matrix.arrays.tag }}" - name: Terraform destroy if: ${{ always() && steps.ec2-linux-integration-test.outputs.cache-hit != 'true' }} run: terraform destroy --auto-approve EC2WinIntegrationTest: - needs: [BuildMSI] + needs: [BuildMSI, GenerateTestMatrix] name: 'EC2WinIntegrationTest' runs-on: ubuntu-latest defaults: @@ -428,9 +410,7 @@ jobs: strategy: fail-fast: false matrix: - arrays: [ - { os: "win-2022", ami: "cloudwatch-agent-integration-test-win-2022*"} - ] + arrays: ${{ fromJson(needs.GenerateTestMatrix.outputs.ec2_windows_matrix) }} steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 163bc41901..0c2fc0329d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ CWAGENT_VERSION .terraform.* terraform.* **/.terraform/* -coverage.txt \ No newline at end of file +coverage.txt +integration/generator/resources/*complete*.json \ No newline at end of file diff --git a/Makefile b/Makefile index 79e2dcf20e..76c016fda1 100644 --- a/Makefile +++ b/Makefile @@ -142,9 +142,6 @@ fmt-sh: install-tools test: CGO_ENABLED=0 go test -coverprofile coverage.txt -failfast ./awscsm/... ./cfg/... ./cmd/... ./handlers/... ./internal/... ./logger/... ./logs/... ./metric/... ./plugins/... ./profiler/... ./tool/... ./translator/... -integration-test: - go test ./integration/test/... -p 1 -v --tags=integration - integration-cleaner: go run ./integration/clean/clean_ami.go --tags=clean diff --git a/integration/generator/resources/linux_test_matrix.json b/integration/generator/resources/linux_test_matrix.json new file mode 100644 index 0000000000..0b83ae923f --- /dev/null +++ b/integration/generator/resources/linux_test_matrix.json @@ -0,0 +1,100 @@ +[ + { + "os": "ubuntu", + "username": "ubuntu", + "installAgentCommand": "sudo dpkg -i -E ./amazon-cloudwatch-agent.deb", + "ami": "cloudwatch-agent-integration-test-ubuntu*", + "caCertPath": "/etc/ssl/certs/ca-certificates.crt", + "arc": "amd64", "binaryName": "amazon-cloudwatch-agent.deb" + }, + { + "os": "al2", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-al2*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "rhel8", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-rhel8-base*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "rhel8-1", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-rhel8-1*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "rhel8-2", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-rhel8-2*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "rhel8-3", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-rhel8-3*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "rhel8-4", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-rhel8-4*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "ol8-1", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-ol8-1*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "ol8-2", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-ol8-2*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "ol8-3", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-ol8-3*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + }, + { + "os": "ol8-4", + "username": "ec2-user", + "installAgentCommand": "rpm -U ./amazon-cloudwatch-agent.rpm", + "ami": "cloudwatch-agent-integration-test-ol8-4*", + "caCertPath": "/etc/ssl/certs/ca-bundle.crt", + "arc": "amd64", + "binaryName": "amazon-cloudwatch-agent.rpm" + } +] \ No newline at end of file diff --git a/integration/generator/resources/mac_test_matrix.json b/integration/generator/resources/mac_test_matrix.json new file mode 100644 index 0000000000..81ed610905 --- /dev/null +++ b/integration/generator/resources/mac_test_matrix.json @@ -0,0 +1,5 @@ +[ + { + + } +] \ No newline at end of file diff --git a/integration/generator/resources/windows_test_matrix.json b/integration/generator/resources/windows_test_matrix.json new file mode 100644 index 0000000000..ae66556fee --- /dev/null +++ b/integration/generator/resources/windows_test_matrix.json @@ -0,0 +1,6 @@ +[ + { + "os": "win-2022", + "ami": "cloudwatch-agent-integration-test-win-2022*" + } +] \ No newline at end of file diff --git a/integration/generator/test_case_generator.go b/integration/generator/test_case_generator.go new file mode 100644 index 0000000000..1c9fd2ff59 --- /dev/null +++ b/integration/generator/test_case_generator.go @@ -0,0 +1,75 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT + +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" +) + +const ( + linux = "linux" + windows = "windows" + mac = "mac" + tag = "tag" +) + +//you can't have a const map in golang +var osToTagMap = map[string][]string{ + linux: {"integration"}, + windows: {}, + mac: {}, +} + +func main() { + for osType, tags := range osToTagMap { + testMatrix := genMatrix(osType, tags) + writeTestMatrixFile(osType, testMatrix) + } +} + +func genMatrix(targetOS string, tags []string) []map[string]string { + openTestMatrix, err := os.Open(fmt.Sprintf("integration/generator/resources/%v_test_matrix.json", targetOS)) + if err != nil { + panic(fmt.Sprintf("can't read file %v_test_matrix.json err %v", targetOS, err)) + } + byteValueTestMatrix, _ := ioutil.ReadAll(openTestMatrix) + _ = openTestMatrix.Close() + var testMatrix []map[string]string + err = json.Unmarshal([]byte(byteValueTestMatrix), &testMatrix) + if err != nil { + panic(fmt.Sprintf("can't unmarshall file %v_test_matrix.json err %v", targetOS, err)) + } + + var testMatrixComplete []map[string]string + for _, test := range testMatrix { + testLine := copyMap(test) + for _, testTag := range tags { + testLine[tag] = testTag + } + testMatrixComplete = append(testMatrixComplete, testLine) + } + return testMatrixComplete +} + +func writeTestMatrixFile(targetOS string, testMatrix []map[string]string) { + bytes, err := json.MarshalIndent(testMatrix, "", " ") + if err != nil { + panic(fmt.Sprintf("Can't marshal json for target os %v, err %v", targetOS, err)) + } + err = ioutil.WriteFile(fmt.Sprintf("integration/generator/resources/%v_complete_test_matrix.json", targetOS), bytes, os.ModePerm) + if err != nil { + panic(fmt.Sprintf("Can't write json to file for target os %v, err %v", targetOS, err)) + } +} + +func copyMap(mapToCopy map[string]string) map[string]string { + testLine := make(map[string]string) + for key, value := range mapToCopy { + testLine[key] = value + } + return testLine +} diff --git a/integration/terraform/ec2/linux/main.tf b/integration/terraform/ec2/linux/main.tf index ef713a6343..79b4ff6d18 100644 --- a/integration/terraform/ec2/linux/main.tf +++ b/integration/terraform/ec2/linux/main.tf @@ -23,7 +23,7 @@ resource "aws_instance" "integration-test" { "export LOCAL_STACK_HOST_NAME=${var.local_stack_host_name}", "echo run tests with the tag integration, one at a time, and verbose", "cd ~/amazon-cloudwatch-agent", - "make integration-test" + "go test ./integration/test/... -p 1 -v --tags=${var.tag}" ] connection { type = "ssh" diff --git a/integration/terraform/ec2/linux/variables.tf b/integration/terraform/ec2/linux/variables.tf index 6fccb179e3..aab8b2d424 100644 --- a/integration/terraform/ec2/linux/variables.tf +++ b/integration/terraform/ec2/linux/variables.tf @@ -82,4 +82,9 @@ variable "s3_bucket" { variable "test_name" { type = string default = "" +} + +variable "tag" { + type = string + default = "" } \ No newline at end of file