Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/go test multiplatform workflow #1

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 59 additions & 48 deletions .github/workflows/go-test-multiplatform.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This GitHub action runs Packer go tests across
# multiple runners.
# only test results from ubuntu-latest environment will be posted to test obj storage bucket after GHA execution
#

name: "Go Test Multi-Platform"
Expand All @@ -28,25 +27,6 @@ jobs:
run: |
echo "Found Go $(cat .go-version)"
echo "go-version=$(cat .go-version)" >> $GITHUB_OUTPUT
get-test-dependencies:
runs-on: ubuntu-latest
steps:
- name: Install system deps
run: sudo apt-get install -y build-essential

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install Python deps
run: pip3 install requests wheel boto3

- name: Set release version env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

- name: Install go-junit-report
run: go install github.com/jstemmer/go-junit-report/v2@latest

darwin-go-tests:
needs:
Expand All @@ -60,9 +40,7 @@ jobs:
go-version: ${{ needs.get-go-version.outputs.go-version }}
- run: |
echo "Testing with Go ${{ needs.get-go-version.outputs.go-version }}"
filename=$(ls | grep -E '^[0-9]{12}_packer_test_report\.json')

go test -race -count 1 ./... -timeout=3m -v
go test -race -count 1 ./... -timeout=3m

windows-go-tests:
needs:
Expand All @@ -76,57 +54,90 @@ jobs:
go-version: ${{ needs.get-go-version.outputs.go-version }}
- run: |
echo "Testing with Go ${{ needs.get-go-version.outputs.go-version }}"
filename=$(ls | grep -E '^[0-9]{12}_packer_test_report\.json')

go test -race -count 1 ./... -timeout=3m -v
go test -race -count 1 ./... -timeout=3m

linux-go-tests:
needs:
- get-go-version
- get-test-dependencies
runs-on: ubuntu-latest
env:
EXIT_STATUS: 0
name: Linux Go tests
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: ${{ needs.get-go-version.outputs.go-version }}
- run: |
echo "Testing with Go ${{ needs.get-go-version.outputs.go-version }}"
filename=$(ls | grep -E '^[0-9]{12}_packer_test_report\.json')

go test -race -count 1 ./... -timeout=3m -v -json | tee "$filename"
- name: Install go-junit-report
run: go install github.com/jstemmer/go-junit-report/v2@latest

- name: Convert JSON Report to XML
- name: Run integration tests
run: |
filename=$(ls | grep -E '^[0-9]{12}_packer_test_report\.json')
timestamp=$(date +'%Y%m%d%H%M')
report_filename="${timestamp}_packer_test_report.xml"
echo "Testing with Go ${{ needs.get-go-version.outputs.go-version }}"
if ! go test -race -count 1 ./... -timeout=3m | go-junit-report -set-exit-code > "$report_filename"; then
echo "EXIT_STATUS=1" >> $GITHUB_ENV
fi
cat "$report_filename"

if [ -f "$filename" ]; then
go_junit_report_dir=$(go env GOPATH)/bin
export PATH="$PATH:$go_junit_report_dir"
xml_filename=$(echo "$filename" | sed 's/\.json$/.xml/')
go-junit-report < "$filename" > "$xml_filename"
echo "Conversion from JSON to XML completed successfully."
- name: Upload test report
uses: actions/upload-artifact@v4
with:
name: test-reports
path: ${{ github.workspace }}/${timestamp}_packer_test_report.xml

- name: Test Execution Status Handler
run: |
if [[ "$EXIT_STATUS" != 0 ]]; then
echo "Test execution contains failure(s), check Run Integration tests step above"
exit $EXIT_STATUS
else
echo "JSON test report file not found."
exit 1
echo "Tests passed!"
fi
env:
GO111MODULE: on

process-upload-report:
needs: linux-go-tests
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

# Download the artifact generated by the 'linux-go-tests' job
- name: Download test report
uses: actions/download-artifact@v4
with:
name: test-reports

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install Python deps
run: pip3 install requests wheel boto3

- name: Set release version env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

# Add additional information to XML report
- name: Add additional information to XML report
run: |
filename=$(ls | grep -E '^[0-9]{12}_packer_test_report\.xml$')
filename=$(ls test-reports | grep -E '^[0-9]{12}_packer_test_report\.xml$')
python scripts/add_to_xml_test_report.py \
--branch_name "${{ env.RELEASE_VERSION }}" \
--gha_run_id "$GITHUB_RUN_ID" \
--gha_run_number "$GITHUB_RUN_NUMBER" \
--xmlfile "${filename}"
--xmlfile "test-reports/${filename}"

# Upload test results to the bucket
- name: Upload test results to bucket
run: |
report_filename=$(ls test-reports | grep -E '^[0-9]{12}_packer_test_report\.xml$')
python3 scripts/test_report_upload_script.py "test-reports/${report_filename}"
env:
LINODE_CLI_OBJ_ACCESS_KEY: ${{ secrets.LINODE_CLI_OBJ_ACCESS_KEY }}
LINODE_CLI_OBJ_SECRET_KEY: ${{ secrets.LINODE_CLI_OBJ_SECRET_KEY }}
run: |
report_filename=$(ls | grep -E '^[0-9]{12}_packer_test_report\.xml$')
python3 scripts/test_report_upload_script.py "${report_filename}"

40 changes: 40 additions & 0 deletions scripts/test_report_upload_script.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import boto3
import sys
import os
import xml.etree.ElementTree as ET
from botocore.exceptions import NoCredentialsError

ACCESS_KEY = os.environ.get('LINODE_CLI_OBJ_ACCESS_KEY')
Expand All @@ -14,6 +15,44 @@
"region_name": "us-southeast-1",
}

def change_xml_report_to_tod_acceptable_version(file_name):
# Load the original XML file
tree = ET.parse(file_name)
root = tree.getroot()

testsuites_element = root

# total
total_tests = int(testsuites_element.get('tests')) if testsuites_element.get('tests') is not None else 0
total_failures = int(testsuites_element.get('failures')) if testsuites_element.get('failures') is not None else 0
total_errors = int(testsuites_element.get('errors')) if testsuites_element.get('errors') is not None else 0
total_skipped = int(testsuites_element.get('skipped')) if testsuites_element.get('skipped') is not None else 0

# Create a new <testsuites> element with aggregated values
new_testsuites = ET.Element("testsuites")
new_testsuites.set("tests", str(total_tests))
new_testsuites.set("failures", str(total_failures))
new_testsuites.set("errors", str(total_errors))
new_testsuites.set("skipped", str(total_skipped))

# Create a new <testsuite> element under <testsuites>
new_testsuite = ET.SubElement(new_testsuites, "testsuite", attrib=testsuites_element.attrib)

for testcase in root.findall('.//testcase'):
new_testcase = ET.SubElement(new_testsuite, "testcase", attrib=testcase.attrib)
for child in testcase:
new_testcase.append(child)

# Save the new XML to a file
try:
new_tree = ET.ElementTree(new_testsuites)
new_tree.write(file_name, encoding="UTF-8", xml_declaration=True)
print("XML content successfully over-written to " + file_name)

except Exception as e:
print("Error writing XML content:", str(e))



def upload_to_linode_object_storage(file_name):
try:
Expand All @@ -38,4 +77,5 @@ def upload_to_linode_object_storage(file_name):
print('Error: The provided file name is empty or invalid.')
sys.exit(1)

change_xml_report_to_tod_acceptable_version(file_name)
upload_to_linode_object_storage(file_name)
Loading