Skip to content

Commit

Permalink
Generate JSON File With Linux Test AMI X Tag (aws#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethAmazon authored Apr 15, 2022
1 parent abc8148 commit 6f87fad
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 57 deletions.
84 changes: 32 additions & 52 deletions .github/workflows/integrationTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -310,7 +337,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:
Expand All @@ -319,53 +346,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

Expand Down Expand Up @@ -412,13 +393,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:
Expand All @@ -427,9 +409,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

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ CWAGENT_VERSION
.terraform.*
terraform.*
**/.terraform/*
coverage.txt
coverage.txt
integration/generator/resources/*complete*.json
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
100 changes: 100 additions & 0 deletions integration/generator/resources/linux_test_matrix.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
5 changes: 5 additions & 0 deletions integration/generator/resources/mac_test_matrix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{

}
]
6 changes: 6 additions & 0 deletions integration/generator/resources/windows_test_matrix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"os": "win-2022",
"ami": "cloudwatch-agent-integration-test-win-2022*"
}
]
75 changes: 75 additions & 0 deletions integration/generator/test_case_generator.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion integration/terraform/ec2/linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions integration/terraform/ec2/linux/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ variable "s3_bucket" {
variable "test_name" {
type = string
default = ""
}

variable "tag" {
type = string
default = ""
}

0 comments on commit 6f87fad

Please sign in to comment.