Skip to content

Commit

Permalink
Modern s3 terraform test
Browse files Browse the repository at this point in the history
  • Loading branch information
rahearn committed Jun 18, 2024
1 parent 4fffd83 commit 5ef63e1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Terraform Test

on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
name: Integration test
strategy:
fail-fast: false
max-parallel: 2
matrix:
module: ["s3"]
steps:
- uses: actions/checkout@v4

- name: terraform test ${{ matrix.module }}
uses: dflook/terraform-test@v1
with:
path: ${{ matrix.module }}
variables: |
cf_user = "${{ secrets.CF_USER }}"
cf_password = "${{ secrets.CF_PASSWORD }}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Local .terraform directories
**/.terraform/*
**/.terraform.lock.hcl

# .tfstate files
*.tfstate
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,15 @@ module "egress_space" {
]
}
```

## Testing

[Terraform tests](https://developer.hashicorp.com/terraform/language/tests) are in progress of being written. To run for any module with a `tests` directory:

1. cd to module root. Example: `cd s3`
1. Run `terraform init`
1. Run `terraform test -var cf_user="SERVICE_DEPLOYER_USERNAME" -var cf_password="SERVICE_DEPLOYER_PASSWORD"`

### tf-cg-test-support

This module is used to help setup the cloudfoundry provider for each test. It is not a useful module on its own
35 changes: 35 additions & 0 deletions s3/tests/creation.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
provider "cloudfoundry" {
api_url = "https://api.fr.cloud.gov"
user = run.setup.cf_user
password = run.setup.cf_password
}

variables {
cf_org_name = "sandbox-gsa"
cf_space_name = "ryan.ahearn"
s3_plan_name = "basic-sandbox"
name = "terraform-cloudgov-s3-test"
}

run "setup" {
module {
source = "../tf-cg-test-support"
}
}

run "test_creation" {
assert {
condition = can(regex("^\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}$", output.bucket_id))
error_message = "Bucket ID should be a GUID"
}

assert {
condition = cloudfoundry_service_instance.bucket.id == output.bucket_id
error_message = "Bucket ID output must match the service instance"
}

assert {
condition = cloudfoundry_service_instance.bucket.service_plan == data.cloudfoundry_service.s3.service_plans["basic-sandbox"]
error_message = "Service Plan should be 'basic-sandbox'"
}
}
13 changes: 13 additions & 0 deletions tf-cg-test-support/echo_cf_vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "cf_user" {}
variable "cf_password" {
sensitive = true
}

output "cf_user" {
value = var.cf_user
}

output "cf_password" {
value = var.cf_password
sensitive = true
}

0 comments on commit 5ef63e1

Please sign in to comment.