1+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ # SPDX-License-Identifier: MIT
3+
4+ name : PR Test
5+ env :
6+ TERRAFORM_AWS_ASSUME_ROLE : ${{ vars.TERRAFORM_AWS_ASSUME_ROLE }}
7+ TERRAFORM_AWS_ASSUME_ROLE_DURATION : 14400 # 4 hours
8+ CWA_GITHUB_TEST_REPO_NAME : " aws/amazon-cloudwatch-agent-test"
9+ CWA_GITHUB_TEST_REPO_URL : " https://github.com/aws/amazon-cloudwatch-agent-test.git"
10+ CWA_GITHUB_TEST_REPO_BRANCH : " main"
11+ KEY_NAME : ${{ secrets.KEY_NAME }}
12+ PRIVATE_KEY : ${{ secrets.AWS_PRIVATE_KEY }}
13+ S3_INTEGRATION_BUCKET : ${{ vars.S3_INTEGRATION_BUCKET }}
14+
15+ on :
16+ workflow_dispatch :
17+ pull_request :
18+ branches :
19+ - main*
20+ - feature*
21+ types :
22+ - opened
23+ - synchronize
24+ - reopened
25+ - ready_for_review
26+ - labeled
27+ - unlabeled
28+
29+ concurrency :
30+ group : ${{ github.workflow }}-${{ github.ref_name }}
31+ cancel-in-progress : true
32+
33+ jobs :
34+ CheckLabel :
35+ runs-on : ubuntu-latest
36+ outputs :
37+ should_run : ${{ steps.check.outputs.has_label }}
38+ steps :
39+ - id : check
40+ run : |
41+ if [[ "${{ contains(github.event.pull_request.labels.*.name, 'ready for testing') }}" == "true" ]]; then
42+ echo "has_label=true" >> $GITHUB_OUTPUT
43+ else
44+ echo "has_label=false" >> $GITHUB_OUTPUT
45+ fi
46+
47+ BuildAndUpload :
48+ needs : [ CheckLabel ]
49+ if : needs.CheckLabel.outputs.should_run == 'true'
50+ uses : ./.github/workflows/test-build.yml
51+ secrets : inherit
52+ permissions :
53+ id-token : write
54+ contents : read
55+ with :
56+ BucketKey : " integration-test/binary/${{ github.sha }}"
57+ PackageBucketKey : " integration-test/packaging/${{ github.sha }}"
58+ TerraformAWSAssumeRole : ${{ vars.TERRAFORM_AWS_ASSUME_ROLE }}
59+ Bucket : ${{ vars.S3_INTEGRATION_BUCKET }}
60+
61+ OutputEnvVariables :
62+ name : ' OutputEnvVariables'
63+ needs : [ CheckLabel ]
64+ if : needs.CheckLabel.outputs.should_run == 'true'
65+ runs-on : ubuntu-latest
66+ outputs :
67+ CWA_GITHUB_TEST_REPO_NAME : ${{ steps.set-outputs.outputs.CWA_GITHUB_TEST_REPO_NAME }}
68+ CWA_GITHUB_TEST_REPO_URL : ${{ steps.set-outputs.outputs.CWA_GITHUB_TEST_REPO_URL }}
69+ CWA_GITHUB_TEST_REPO_BRANCH : ${{ steps.set-outputs.outputs.CWA_GITHUB_TEST_REPO_BRANCH }}
70+ steps :
71+ - name : SetOutputs
72+ id : set-outputs
73+ run : |
74+ CWA_GITHUB_TEST_REPO_BRANCH=${{ env.CWA_GITHUB_TEST_REPO_BRANCH }}
75+
76+ echo "CWA_GITHUB_TEST_REPO_NAME=${{ env.CWA_GITHUB_TEST_REPO_NAME }}" >> "$GITHUB_OUTPUT"
77+ echo "CWA_GITHUB_TEST_REPO_URL=${{ env.CWA_GITHUB_TEST_REPO_URL }}" >> "$GITHUB_OUTPUT"
78+ echo "CWA_GITHUB_TEST_REPO_BRANCH=${CWA_GITHUB_TEST_REPO_BRANCH:-${{ env.CWA_GITHUB_TEST_REPO_BRANCH }}}" >> "$GITHUB_OUTPUT"
79+
80+ - name : Echo test variables
81+ run : |
82+ echo "build_id: ${{ github.sha }}"
83+ echo "CWA_GITHUB_TEST_REPO_NAME: ${{ steps.set-outputs.outputs.CWA_GITHUB_TEST_REPO_NAME }}"
84+ echo "CWA_GITHUB_TEST_REPO_URL: ${{ steps.set-outputs.outputs.CWA_GITHUB_TEST_REPO_URL }}"
85+ echo "CWA_GITHUB_TEST_REPO_BRANCH: ${{ steps.set-outputs.outputs.CWA_GITHUB_TEST_REPO_BRANCH }}"
86+
87+ - uses : actions/checkout@v3
88+ with :
89+ repository : ${{ steps.set-outputs.outputs.CWA_GITHUB_TEST_REPO_NAME }}
90+ ref : ${{ steps.set-outputs.outputs.CWA_GITHUB_TEST_REPO_BRANCH }}
91+
92+ - name : Set up Go 1.x
93+ uses : actions/setup-go@v4
94+ with :
95+ go-version : ~1.22.2
96+
97+ StartLocalStack :
98+ name : ' StartLocalStack'
99+ needs : [ CheckLabel, OutputEnvVariables ]
100+ if : needs.CheckLabel.outputs.should_run == 'true'
101+ uses : ./.github/workflows/start-localstack.yml
102+ secrets : inherit
103+ permissions :
104+ id-token : write
105+ contents : read
106+ with :
107+ region : us-west-2
108+ test_repo_name : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_NAME }}
109+ test_repo_branch : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_BRANCH }}
110+ terraform_assume_role : ${{ vars.TERRAFORM_AWS_ASSUME_ROLE }}
111+ test_repo_url : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_URL }}
112+ github_sha : ${{ github.sha }}
113+ s3_integration_bucket : ${{ vars.S3_INTEGRATION_BUCKET }}
114+
115+ GenerateTestMatrix :
116+ name : ' GenerateTestMatrix'
117+ needs : [ CheckLabel, OutputEnvVariables ]
118+ if : needs.CheckLabel.outputs.should_run == 'true'
119+ runs-on : ubuntu-latest
120+ outputs :
121+ ec2_linux_matrix : ${{ steps.set-matrix.outputs.ec2_linux_matrix }}
122+ ec2_selinux_matrix : ${{ steps.set-matrix.outputs.ec2_selinux_matrix }}
123+
124+ steps :
125+ - uses : actions/checkout@v3
126+ with :
127+ repository : ${{env.CWA_GITHUB_TEST_REPO_NAME}}
128+ ref : ${{env.CWA_GITHUB_TEST_REPO_BRANCH}}
129+
130+ - name : Set up Go 1.x
131+ uses : actions/setup-go@v4
132+ with :
133+ go-version : ~1.22.2
134+
135+ - name : Install jq
136+ run : sudo apt-get install -y jq
137+
138+ - name : Generate matrix
139+ id : set-matrix
140+ run : |
141+ go run --tags=generator generator/test_case_generator.go
142+ # Convert the output to valid JSON and filter out metric_value_benchmark tests
143+ jq -c '[.[] | select(.test_dir != "./test/metric_value_benchmark")]' generator/resources/ec2_linux_complete_test_matrix.json > filtered_matrix.json
144+ # Escape the JSON for GitHub Actions
145+ MATRIX=$(jq -c -r '@json' filtered_matrix.json)
146+ echo "ec2_linux_matrix=$MATRIX" >> $GITHUB_OUTPUT
147+ jq -c '[.[] | select(.test_dir != "./test/metric_value_benchmark")]' generator/resources/ec2_selinux_complete_test_matrix.json > filtered_matrix_selinux.json
148+ # Escape the JSON for GitHub Actions
149+ MATRIX=$(jq -c -r '@json' filtered_matrix_selinux.json)
150+ echo "ec2_selinux_matrix=$MATRIX" >> $GITHUB_OUTPUT
151+
152+
153+ - name : Echo test plan matrix
154+ run : |
155+ echo "ec2_linux_matrix: ${{ steps.set-matrix.outputs.ec2_linux_matrix }}"
156+ echo "ec2_selinux_matrix: ${{ steps.set-matrix.outputs.ec2_selinux_matrix }}"
157+
158+
159+ EC2LinuxIntegrationTest :
160+ name : ' EC2LinuxTests'
161+ needs : [ CheckLabel, GenerateTestMatrix, OutputEnvVariables, StartLocalStack, BuildAndUpload ]
162+ if : needs.CheckLabel.outputs.should_run == 'true'
163+ uses : ./.github/workflows/ec2-integration-test.yml
164+ with :
165+ build_id : ${{ github.sha }}
166+ test_dir : terraform/ec2/linux
167+ job_id : ec2-linux-integration-test
168+ test_props : ${{ needs.GenerateTestMatrix.outputs.ec2_linux_matrix }}
169+ test_repo_name : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_NAME }}
170+ test_repo_url : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_URL }}
171+ test_repo_branch : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_BRANCH }}
172+ localstack_host : ${{ needs.StartLocalStack.outputs.local_stack_host_name }}
173+ region : us-west-2
174+ terraform_assume_role : ${{ vars.TERRAFORM_AWS_ASSUME_ROLE }}
175+ s3_integration_bucket : ${{ vars.S3_INTEGRATION_BUCKET }}
176+ is_selinux_test : false
177+ secrets : inherit
178+
179+ EC2SELinuxIntegrationTest :
180+ name : ' EC2SELinuxTests'
181+ needs : [ CheckLabel, GenerateTestMatrix, OutputEnvVariables, StartLocalStack, BuildAndUpload ]
182+ if : needs.CheckLabel.outputs.should_run == 'true'
183+ uses : ./.github/workflows/ec2-integration-test.yml
184+ with :
185+ build_id : ${{ github.sha }}
186+ test_dir : terraform/ec2/linux
187+ job_id : ec2-linux-integration-test
188+ test_props : ${{ needs.GenerateTestMatrix.outputs.ec2_selinux_matrix }}
189+ test_repo_name : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_NAME }}
190+ test_repo_url : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_URL }}
191+ test_repo_branch : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_BRANCH }}
192+ localstack_host : ${{ needs.StartLocalStack.outputs.local_stack_host_name }}
193+ region : us-west-2
194+ terraform_assume_role : ${{ vars.TERRAFORM_AWS_ASSUME_ROLE }}
195+ s3_integration_bucket : ${{ vars.S3_INTEGRATION_BUCKET }}
196+ is_selinux_test : true
197+ secrets : inherit
198+
199+ StopLocalStack :
200+ name : ' StopLocalStack'
201+ needs : [ CheckLabel, StartLocalStack, EC2LinuxIntegrationTest, OutputEnvVariables ]
202+ if : ${{ always() && needs.StartLocalStack.result == 'success' && needs.CheckLabel.outputs.should_run == 'true'}}
203+ uses : ./.github/workflows/stop-localstack.yml
204+ secrets : inherit
205+ permissions :
206+ id-token : write
207+ contents : read
208+ with :
209+ region : us-west-2
210+ test_repo_name : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_NAME }}
211+ test_repo_branch : ${{ needs.OutputEnvVariables.outputs.CWA_GITHUB_TEST_REPO_BRANCH }}
212+ terraform_assume_role : ${{ vars.TERRAFORM_AWS_ASSUME_ROLE }}
213+ github_sha : ${{ github.sha }}
214+ s3_integration_bucket : ${{ vars.S3_INTEGRATION_BUCKET }}
215+
216+ verify-all :
217+ name : Verify All PR Test Jobs
218+ needs : [CheckLabel, BuildAndUpload, OutputEnvVariables, StartLocalStack,
219+ GenerateTestMatrix, EC2LinuxIntegrationTest, EC2SELinuxIntegrationTest,
220+ StopLocalStack]
221+ runs-on : ubuntu-latest
222+ if : always()
223+ steps :
224+ - name : Check Job Status
225+ run : |
226+ # Convert needs context to JSON and process with jq
227+ needs_json='${{ toJSON(needs) }}'
228+
229+ failed_jobs=()
230+ successful_jobs=()
231+
232+ # Loop through all jobs in needs context
233+ for job in $(echo "$needs_json" | jq -r 'keys[]'); do
234+ result=$(echo "$needs_json" | jq -r ".[\"$job\"].result")
235+
236+ if [[ "$result" == "failure" ]]; then
237+ failed_jobs+=("$job")
238+ elif [[ "$result" == "success" ]]; then
239+ successful_jobs+=("$job")
240+ fi
241+ done
242+
243+ echo "Successfully validated jobs:"
244+ printf '%s\n' "${successful_jobs[@]}"
245+
246+ if [ ${#failed_jobs[@]} -ne 0 ]; then
247+ echo -e "\nFailed jobs:"
248+ printf '%s\n' "${failed_jobs[@]}"
249+ exit 1
250+ fi
251+
252+ echo -e "\nAll required jobs completed without failures!"
0 commit comments