From 81c91fcbdd6a3ec0bffde6e6c1e7791aaf302cef Mon Sep 17 00:00:00 2001 From: janvt Date: Wed, 25 Oct 2023 12:48:04 +0200 Subject: [PATCH 1/2] add terraform test --- .github/workflows/test.yaml | 57 +++++++++++++++++++++++++++++++ test/.gitignore | 0 tests/basic.tftest.hcl | 68 +++++++++++++++++++++++++++++++++++++ versions.tf | 2 +- 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yaml delete mode 100644 test/.gitignore create mode 100644 tests/basic.tftest.hcl diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..ff7d648 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,57 @@ +--- +############### +## Run tests ## +############### + +# +# Documentation: +# https://help.github.com/en/articles/workflow-syntax-for-github-actions +# + +name: Test +on: + pull_request: + push: + branches: [ main ] + +########################## +# Prevent duplicate jobs # +########################## +concurrency: + group: ${{ github.repository }} + cancel-in-progress: false + +permissions: + id-token: write + contents: read + +############### +# Run the job # +############### +jobs: + terraform-test: + name: Terraform Test + runs-on: ubuntu-latest + steps: + ############################ + # Checkout the source code # + ############################ + - name: Checkout + uses: actions/checkout@v3 + + ############################# + # Configure AWS credentials # + ############################# + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v2 + with: + role-to-assume: arn:aws:iam::${{ vars.AWS_TESTING_ACCOUNT_ID }}:role/${{ vars.AWS_TESTING_ROLE }} + aws-region: ${{ vars.AWS_TESTING_REGION }} + mask-aws-account-id: false + + ############# + # Run tests # + ############# + - name: Run Tests + timeout-minutes: 30 + run: terraform init && terraform test diff --git a/test/.gitignore b/test/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/tests/basic.tftest.hcl b/tests/basic.tftest.hcl new file mode 100644 index 0000000..d651e7e --- /dev/null +++ b/tests/basic.tftest.hcl @@ -0,0 +1,68 @@ +variables { + name = "test-budget" + recipients = ["tommy.tester@test.com"] + + budgets = [{ + name = "test-budget-1", + + budget_type = "COST" + limit_amount = 50 + limit_unit = "EUR" + + time_period_start = "2023-01-01_00:00" + time_period_end = "2087-06-15_00:00" + time_unit = "MONTHLY" + + notification = { + comparison_operator = "GREATER_THAN" + threshold = "69" + threshold_type = "PERCENTAGE" + notification_type = "FORECASTED" + } + }] +} + +run "basic_budget" { + command = plan + + assert { + condition = length(aws_budgets_budget.main) == 1 + error_message = "Expected one budget to be created." + } + + assert { + condition = contains(keys(aws_budgets_budget.main), "test-budget-1") + error_message = "Expected test-budget-1 to exist." + } + + assert { + condition = length(aws_budgets_budget.main["test-budget-1"].notification) == 1 + error_message = "Expected test-budget-1 to have one notification." + } + + assert { + condition = alltrue([ + for n in aws_budgets_budget.main["test-budget-1"].notification : can(length(n.subscriber_email_addresses) == 1) + ]) + + error_message = "Expected test-budget-1 notification to have one recipient." + } + + assert { + condition = aws_budgets_budget.main["test-budget-1"].limit_amount == "50" + error_message = "Expected test-budget-1 limit amount to be 50." + } + + assert { + condition = aws_budgets_budget.main["test-budget-1"].limit_unit == "EUR" + error_message = "Expected test-budget-1 limit unit to be EUR." + } + + assert { + condition = alltrue([ + for n in aws_budgets_budget.main["test-budget-1"].notification : can(n.threshold == 69) + ]) + + error_message = "Expected test-budget-1 threshold to be 69%." + } +} diff --git a/versions.tf b/versions.tf index f3a9560..02a6189 100644 --- a/versions.tf +++ b/versions.tf @@ -1,5 +1,5 @@ terraform { - required_version = ">= 1.3" + required_version = ">= 1.6" required_providers { aws = { From 9dd521345e855b359f0c21b41ba7b02abe8460d0 Mon Sep 17 00:00:00 2001 From: janvt Date: Wed, 25 Oct 2023 12:50:06 +0200 Subject: [PATCH 2/2] fmt --- tests/basic.tftest.hcl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/basic.tftest.hcl b/tests/basic.tftest.hcl index d651e7e..998ab39 100644 --- a/tests/basic.tftest.hcl +++ b/tests/basic.tftest.hcl @@ -49,12 +49,12 @@ run "basic_budget" { } assert { - condition = aws_budgets_budget.main["test-budget-1"].limit_amount == "50" + condition = aws_budgets_budget.main["test-budget-1"].limit_amount == "50" error_message = "Expected test-budget-1 limit amount to be 50." } assert { - condition = aws_budgets_budget.main["test-budget-1"].limit_unit == "EUR" + condition = aws_budgets_budget.main["test-budget-1"].limit_unit == "EUR" error_message = "Expected test-budget-1 limit unit to be EUR." }