diff --git a/.github/parse_logs.py b/.github/parse_logs.py new file mode 100644 index 00000000..f373e5d7 --- /dev/null +++ b/.github/parse_logs.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# (C) Copyright 2024 Hewlett Packard Enterprise Development LP + +import os +import sys + + +def main(args): + n = len(args) + if n != 2: + print("Pass the log directory or txt file path") + return 1 + log_path = args[1] + file_content = '' + if log_path.endswith(".txt"): + with open(log_path) as f: + file_content += (f.read()) + else: + for x in os.listdir(log_path): + if x.endswith(".txt"): + with open(os.path.join(log_path, x)) as f: + file_content += (f.read()) + test_count = file_content.count('RUN') - file_content.count('SKIP:') + pass_count = file_content.count('PASS:') + fail_count = file_content.count('FAIL:') + print( + f"\nTestcases Ran: {test_count}; \n" + f"Testcases Passed: {pass_count}; \n" + f"Testcases Failed: {fail_count}; \n") + return 0 + +if __name__ == "__main__": + exit(main(sys.argv)) diff --git a/.github/workflows/acc.yml b/.github/workflows/acc.yml index f7edd6a6..b861ae4f 100644 --- a/.github/workflows/acc.yml +++ b/.github/workflows/acc.yml @@ -7,46 +7,33 @@ on: # Allows you to run this workflow manually from the Actions tab workflow_dispatch: inputs: - logLevel: - description: 'Log level' + test_case: + description: 'Enter testcases sperated by space. Leave empty for all' + required: false + default: '' + type: string + test_description: + description: 'Enter description for the test' required: true - default: 'warning' - tags: - description: 'Test scenario tags' + default: 'Check all Terraform Testcases' + type: string + release: types: [published] -env: - HPEGL_IAM_SERVICE_URL: ${{ secrets.HPEGL_IAM_SERVICE_URL }} - HPEGL_TENANT_ID: ${{ secrets.HPEGL_TENANT_ID }} - HPEGL_USER_SECRET: ${{ secrets.HPEGL_USER_SECRET }} - HPEGL_USER_ID: ${{ secrets.HPEGL_USER_ID }} - HPEGL_VMAAS_API_URL: ${{ secrets.HPEGL_VMAAS_API_URL }} - HPEGL_VMAAS_LOCATION: ${{ secrets.HPEGL_VMAAS_LOCATION }} - HPEGL_VMAAS_SPACE_NAME: ${{ secrets.HPEGL_VMAAS_SPACE_NAME}} - TF_ACC: ${{ secrets.TF_ACC }} -jobs: - acc: - runs-on: ubuntu-20.04 - strategy: - matrix: - go: [ '1.17' ] - name: Acceptance Tests - steps: - - name: Checkout workspace - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: 1.17 - - name: Install dependencies - run: | - sudo apt-get install -y wget jq - wget https://releases.hashicorp.com/terraform/1.0.0/terraform_1.0.0_linux_amd64.zip - sudo unzip -fo terraform_1.0.0_linux_amd64.zip -d /usr/local/bin - - name: Install necessary tools - run: make tools - - - name: Run Acceptance test - run: | - make acceptance \ No newline at end of file +jobs: + acc-test: + uses: ./.github/workflows/reusable-dev-acc.yml + with: + test_case: ${{ inputs.test_case }} + test_description: ${{ inputs.test_description }} + test_case_folder: 'acc-testcases' + secrets: + DEV_HPEGL_IAM_SERVICE_URL: ${{ secrets.HPEGL_IAM_SERVICE_URL }} + DEV_HPEGL_TENANT_ID: ${{ secrets.HPEGL_TENANT_ID }} + DEV_HPEGL_USER_SECRET: ${{ secrets.HPEGL_USER_SECRET }} + DEV_HPEGL_USER_ID: ${{ secrets.HPEGL_USER_ID }} + DEV_HPEGL_VMAAS_API_URL: ${{ secrets.HPEGL_VMAAS_API_URL }} + DEV_HPEGL_VMAAS_LOCATION: ${{ secrets.HPEGL_VMAAS_LOCATION }} + DEV_HPEGL_VMAAS_SPACE_NAME: ${{ secrets.HPEGL_VMAAS_SPACE_NAME}} + TF_ACC: ${{ secrets.TF_ACC }} diff --git a/.github/workflows/cicd-dev-acc.yml b/.github/workflows/cicd-dev-acc.yml index 962477da..769fa775 100644 --- a/.github/workflows/cicd-dev-acc.yml +++ b/.github/workflows/cicd-dev-acc.yml @@ -1,26 +1,73 @@ -name: Dev Acceptance Tests for CI CD +name: IaC Tests for CI CD Gating Job +# This workflow runs all the acc-dev-testcases on: workflow_dispatch: jobs: test-provider: - uses: ./.github/workflows/dev-acc.yml + uses: ./.github/workflows/reusable-dev-acc.yml with: test_case: TestProvider + test_description: Check for valid terraform provider secrets: inherit test-datasouces: - needs: - - test-provider - if: "${{ always() && needs.test-provider.result != 'failed' }}" - uses: ./.github/workflows/dev-acc.yml + needs: [test-provider] + if: "always() && ${{ needs.test-provider.result == 'success' }}" + uses: ./.github/workflows/reusable-dev-acc.yml with: test_case: TestAccDataSource + test_description: GET call usecase validations secrets: inherit test-vmaas-instance: - uses: ./.github/workflows/dev-acc.yml + uses: ./.github/workflows/reusable-dev-acc.yml + needs: [test-datasouces] + if: "always()" with: - test_case: TestVmaasInstance + test_case: TestVmaasInstance TestAccResourceInstance + test_description: Instance usecase validations secrets: inherit + + test-vmaas-lb: + uses: ./.github/workflows/reusable-dev-acc.yml + needs: [test-vmaas-instance] + if: "always()" + with: + test_case: TestVmaasLB TestAccResourceLB TestVmaasLoadBalancerPlan TestAccResourceLoadBalancerCreate + test_description: Loadbalancer usecase validations + secrets: inherit + + test-vmaas-network: + uses: ./.github/workflows/reusable-dev-acc.yml + needs: [test-vmaas-lb] + if: "always()" + with: + test_case: TestVmaasNetworkPlan TestAccResourceNetworkCreate TestAccResourceRouter TestVmaasRouter TestAccResourceTier TestVmaasRouteBGPNeighborPlan + test_description: NSX Network usecase validations + secrets: inherit + + process-logs: + runs-on: ubuntu-20.04 + needs: + - test-vmaas-network + if: "always()" + steps: + - name: Checkout workspace + uses: actions/checkout@v4 + - name: Download logs + id: logs + uses: actions/download-artifact@v4 + with: + path: tmp/artifacts + merge-multiple: true + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + - name: Print Result and Publish + run: | + LOG_RESULT=$(python .github/parse_logs.py '${{ steps.logs.outputs.download-path }}') + echo $LOG_RESULT + curl -X POST -H 'Content-type: application/json' --data "{'text':'CICD Terraform IaC Test results $LOG_RESULT and report link - https://github.com/HewlettPackard/hpegl-vmaas-terraform-resources/actions/runs/${{ github.run_id }}'}" '${{ secrets.TEAMS_URL_CICD }}' diff --git a/.github/workflows/cicd-prod-acc.yml b/.github/workflows/cicd-prod-acc.yml new file mode 100644 index 00000000..6a1cd9ec --- /dev/null +++ b/.github/workflows/cicd-prod-acc.yml @@ -0,0 +1,44 @@ +name: IaC Tests for CI CD Solution Job + +on: + workflow_dispatch: + +jobs: + acc-test: + uses: ./.github/workflows/reusable-dev-acc.yml + with: + test_description: IaC Terraform Testcase + test_case_folder: 'acc-testcases' + secrets: + DEV_HPEGL_IAM_SERVICE_URL: ${{ secrets.HPEGL_IAM_SERVICE_URL }} + DEV_HPEGL_TENANT_ID: ${{ secrets.HPEGL_TENANT_ID }} + DEV_HPEGL_USER_SECRET: ${{ secrets.HPEGL_USER_SECRET }} + DEV_HPEGL_USER_ID: ${{ secrets.HPEGL_USER_ID }} + DEV_HPEGL_VMAAS_API_URL: ${{ secrets.HPEGL_VMAAS_API_URL }} + DEV_HPEGL_VMAAS_LOCATION: ${{ secrets.HPEGL_VMAAS_LOCATION }} + DEV_HPEGL_VMAAS_SPACE_NAME: ${{ secrets.HPEGL_VMAAS_SPACE_NAME}} + TF_ACC: ${{ secrets.TF_ACC }} + + process-logs: + runs-on: ubuntu-20.04 + needs: + - acc-test + if: "always()" + steps: + - name: Checkout workspace + uses: actions/checkout@v4 + - name: Download logs + id: logs + uses: actions/download-artifact@v4 + with: + path: tmp/artifacts + merge-multiple: true + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + - name: Print Result and Publish + run: | + LOG_RESULT=$(python .github/parse_logs.py '${{ steps.logs.outputs.download-path }}') + echo $LOG_RESULT + curl -X POST -H 'Content-type: application/json' --data "{'text':'CICD Terraform IaC Test results $LOG_RESULT and report link - https://github.com/HewlettPackard/hpegl-vmaas-terraform-resources/actions/runs/${{ github.run_id }}'}" '${{ secrets.SLACK_URL_CICD }}' \ No newline at end of file diff --git a/.github/workflows/dev-acc.yml b/.github/workflows/dev-acc.yml index 8316ea06..2a695e35 100644 --- a/.github/workflows/dev-acc.yml +++ b/.github/workflows/dev-acc.yml @@ -1,4 +1,6 @@ -name: Dev Acceptance Tests +name: Dev Acceptance Testing +# This workflow is intended to run a particular set of testcases +# If want to execute all test, consider running cicd-dev-acc.yml on: # Runs every 2 days once at 3AM @@ -8,56 +10,28 @@ on: workflow_dispatch: inputs: test_case: - description: 'Enter testcase' - required: false - default: '' + description: 'Enter testcases sperated by space' + required: true + default: 'TestProvider' + type: string + test_description: + description: 'Enter description for the test' + required: true + default: 'Check Terraform Dev Testcase' type: string - # release: - # types: [published] -env: - HPEGL_IAM_SERVICE_URL: ${{ secrets.DEV_HPEGL_IAM_SERVICE_URL }} - HPEGL_TENANT_ID: ${{ secrets.DEV_HPEGL_TENANT_ID }} - HPEGL_USER_SECRET: ${{ secrets.DEV_HPEGL_USER_SECRET }} - HPEGL_USER_ID: ${{ secrets.DEV_HPEGL_USER_ID }} - HPEGL_VMAAS_API_URL: ${{ secrets.DEV_HPEGL_VMAAS_API_URL }} - HPEGL_VMAAS_LOCATION: ${{ secrets.DEV_HPEGL_VMAAS_LOCATION }} - HPEGL_VMAAS_SPACE_NAME: ${{ secrets.DEV_HPEGL_VMAAS_SPACE_NAME}} - TF_ACC: ${{ secrets.TF_ACC }} - LOG_FILE: "${{ github.event.inputs.test_case }}Logs.txt" jobs: - acc: - runs-on: ubuntu-20.04 - strategy: - matrix: - go: [ '1.17' ] - name: Dev Acceptance Tests - steps: - - name: Checkout workspace - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: 1.17 - - name: Install dependencies - run: | - sudo apt-get install -y wget jq - wget https://releases.hashicorp.com/terraform/1.0.0/terraform_1.0.0_linux_amd64.zip - sudo unzip -fo terraform_1.0.0_linux_amd64.zip -d /usr/local/bin - - - name: Install necessary tools - run: make tools - - - name: Run Acceptance test - run: | - echo "Run Date: $(date +'%Y-%m-%d Time: %H:%M:%S %z' )" >> $LOG_FILE - export TF_ACC_TEST_PATH="$(pwd)/acc-dev-testcases" && make acceptance case='${{ github.event.inputs.test_case }}' >> $LOG_FILE - - - name: Print testcases output - run: cat $LOG_FILE - - - name: Push the report to github artifacts - uses: actions/upload-artifact@v3 - with: - name: "Terraform Test report: ${{ github.event.inputs.test_case }}" - path: ${{ env.LOG_FILE }} + acc-test: + uses: ./.github/workflows/reusable-dev-acc.yml + with: + test_case: ${{ inputs.test_case }} + test_description: ${{ inputs.test_description }} + secrets: + DEV_HPEGL_IAM_SERVICE_URL: ${{ secrets.DEV_HPEGL_IAM_SERVICE_URL }} + DEV_HPEGL_TENANT_ID: ${{ secrets.DEV_HPEGL_TENANT_ID }} + DEV_HPEGL_USER_SECRET: ${{ secrets.DEV_HPEGL_USER_SECRET }} + DEV_HPEGL_USER_ID: ${{ secrets.DEV_HPEGL_USER_ID }} + DEV_HPEGL_VMAAS_API_URL: ${{ secrets.DEV_HPEGL_VMAAS_API_URL }} + DEV_HPEGL_VMAAS_LOCATION: ${{ secrets.DEV_HPEGL_VMAAS_LOCATION }} + DEV_HPEGL_VMAAS_SPACE_NAME: ${{ secrets.DEV_HPEGL_VMAAS_SPACE_NAME}} + TF_ACC: ${{ secrets.TF_ACC }} diff --git a/.github/workflows/reusable-dev-acc.yml b/.github/workflows/reusable-dev-acc.yml new file mode 100644 index 00000000..0976c472 --- /dev/null +++ b/.github/workflows/reusable-dev-acc.yml @@ -0,0 +1,109 @@ +name: Reusable Worflow for running Acceptance Tests. By default handles dev testing. + +on: + workflow_call: + inputs: + test_case: + description: 'Enter testcases sperated by space' + required: false + default: '' + type: string + test_description: + description: 'Enter description for the test' + required: true + default: '' + type: string + test_case_folder: + description: 'Enter folder for test suite' + required: false + default: 'acc-dev-testcases' + type: string + secrets: + DEV_HPEGL_IAM_SERVICE_URL: + required: true + DEV_HPEGL_TENANT_ID: + required: true + DEV_HPEGL_USER_SECRET: + required: true + DEV_HPEGL_USER_ID: + required: true + DEV_HPEGL_VMAAS_API_URL: + required: true + DEV_HPEGL_VMAAS_LOCATION: + required: true + DEV_HPEGL_VMAAS_SPACE_NAME: + required: true + TF_ACC: + required: true + +env: + HPEGL_IAM_SERVICE_URL: ${{ secrets.DEV_HPEGL_IAM_SERVICE_URL }} + HPEGL_TENANT_ID: ${{ secrets.DEV_HPEGL_TENANT_ID }} + HPEGL_USER_SECRET: ${{ secrets.DEV_HPEGL_USER_SECRET }} + HPEGL_USER_ID: ${{ secrets.DEV_HPEGL_USER_ID }} + HPEGL_VMAAS_API_URL: ${{ secrets.DEV_HPEGL_VMAAS_API_URL }} + HPEGL_VMAAS_LOCATION: ${{ secrets.DEV_HPEGL_VMAAS_LOCATION }} + HPEGL_VMAAS_SPACE_NAME: ${{ secrets.DEV_HPEGL_VMAAS_SPACE_NAME}} + TF_ACC: ${{ secrets.TF_ACC }} + LOG_FILE: "Terraform Log - ${{ inputs.test_description }}.txt" +jobs: + acc: + runs-on: ubuntu-20.04 + strategy: + matrix: + go: [ '1.17' ] + name: Acceptance Test Job + steps: + - name: Checkout workspace + uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: 1.17 + - name: Install dependencies + run: | + sudo apt-get install -y wget jq + wget https://releases.hashicorp.com/terraform/1.0.0/terraform_1.0.0_linux_amd64.zip + sudo unzip -fo terraform_1.0.0_linux_amd64.zip -d /usr/local/bin + + - name: Install necessary tools + run: make tools + + - name: Run Acceptance test + run: | + echo "Run Date: $(date +'%Y-%m-%d Time: %H:%M:%S %z' )" >> "$LOG_FILE" + JOB_FAILED=false + + if [[ -z "${{ inputs.test_case }}" ]]; then + export TF_ACC_TEST_PATH="$(pwd)/${{ inputs.test_case_folder }}" && make acceptance >> "$LOG_FILE" + + else + for t in ${{ inputs.test_case }}; do + if ! (export TF_ACC_TEST_PATH="$(pwd)/${{ inputs.test_case_folder }}" && make acceptance case="$t" >> "$LOG_FILE") + then + JOB_FAILED=true + continue + fi + done + if $JOB_FAILED; then exit 1; fi + fi + + + - name: Print testcases output + if: always() + run: cat "$LOG_FILE" + + - name: Print testcases count + if: always() + run: python .github/parse_logs.py "$LOG_FILE" + + - name: Push the report to github artifacts + if: always() + uses: actions/upload-artifact@v4 + with: + name: "IaC Test Report - ${{ inputs.test_description }}" + path: "${{ env.LOG_FILE }}" diff --git a/Makefile b/Makefile index 66bc022f..9c728d73 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,7 @@ coverage: vendor ACC_TEST_FILE_LOCATION=github.com/HewlettPackard/hpegl-vmaas-terraform-resources/internal/acceptance_test acceptance: @if [ "${case}" != "" ]; then \ - TF_ACC=true go test -run $(case) -v -timeout=2000s -cover $(ACC_TEST_FILE_LOCATION); \ + TF_ACC=true go test -run $(case) -v -timeout=20000s -cover $(ACC_TEST_FILE_LOCATION); \ else \ TF_ACC=true go test -v -timeout=50000s -cover $(ACC_TEST_FILE_LOCATION) -parallel 1;\ fi