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

ci: add opentofu test coverage #92

Merged
merged 7 commits into from
Jan 6, 2025
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
54 changes: 43 additions & 11 deletions .github/workflows/test-extensive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ on:
permissions:
contents: read

# Ensures only 1 action runs per PR and previous is canceled on new trigger
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
build:
name: build
Expand All @@ -36,25 +41,20 @@ jobs:
with:
version: latest

test:
name: integration
test-terraform:
name: integration-terraform
needs: build
runs-on: ubuntu-latest
if: '!github.event.pull_request.head.repo.fork'
timeout-minutes: 15
strategy:
fail-fast: false
max-parallel: 2
matrix:
terraform:
# TODO: only test one for now since we are creating and destroying actual
# resources. Decide which versions to support when we are closer to a supported
# release.
# - '1.0.*'
# - '1.1.*'
# - '1.2.*'
# - '1.3.*'
# - '1.4.*'
# - '1.5.*'
- '1.6.*'
- '1.9.*'
- latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
Expand All @@ -66,6 +66,38 @@ jobs:
with:
terraform_version: ${{ matrix.terraform }}
terraform_wrapper: false
- run: make testacc
env:
PLANETSCALE_SERVICE_TOKEN_NAME: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_NAME }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
timeout-minutes: 10

test-tofu:
name: integration-tofu
needs: build
runs-on: ubuntu-latest
if: '!github.event.pull_request.head.repo.fork'
timeout-minutes: 15
strategy:
max-parallel: 2
fail-fast: false
matrix:
tofu:
- '1.6.*'
- '1.7.*'
- '1.8.*'
- latest
steps:
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
with:
persist-credentials: false
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version-file: 'go.mod'
- uses: opentofu/setup-opentofu@v1
with:
tofu_version: ${{ matrix.tofu }}
tofu_wrapper: false
- run: make testacc
env:
PLANETSCALE_SERVICE_TOKEN_NAME: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_NAME }}
Expand Down
31 changes: 7 additions & 24 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
paths-ignore:
- 'README.md'
push:
branches:
- main
paths-ignore:
- 'README.md'

Expand Down Expand Up @@ -41,43 +43,24 @@ jobs:
go-version-file: 'go.mod'
- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
with:
terraform_version: 1.6.1
terraform_version: 1.9.*
terraform_wrapper: false
- run: go generate -x ./...
- name: git diff
run: |
git diff --compact-summary --exit-code || \
(echo; echo "Unexpected difference in directories after code generation. Run 'go generate ./...' command and commit."; exit 1)

test:
name: unit
unit-tests:
name: unit-tests
needs: build
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
terraform:
# TODO: only test one for now since we are creating and destroying actual
# resources. Decide which versions to support when we are closer to a supported
# release.
# - '1.0.*'
# - '1.1.*'
# - '1.2.*'
# - '1.3.*'
# - '1.4.*'
# - '1.5.*'
- '1.6.*'
timeout-minutes: 10
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
with:
go-version-file: 'go.mod'
- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
with:
terraform_version: ${{ matrix.terraform }}
terraform_wrapper: false
- run: go test -cover ./...
timeout-minutes: 10
- run: go test -cover ./...
10 changes: 9 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
default: testacc

.PHONY: build
build:
CGO_ENABLED=0 go build -v -trimpath .

.PHONY: lint
lint:
golangci-lint run -v ./...
Expand All @@ -13,4 +17,8 @@ testacc:
generate:
bash ./script/update_openapi_spec
bash ./script/generate
go generate ./...
go generate ./...

.PHONY: sweep
sweep:
bash ./script/sweep
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Run specific test: `make testacc TESTARGS='-run ^TestAccBranchResource$'`

Debug logs: `TF_PS_PROVIDER_DEBUG=1 TF_LOG=debug make testacc` (or `TF_LOG=trace`)

From time to time it may be necessary to manually cleanup databases created by the acceptance tests. Running `make sweep` will delete all db's created by the acceptance tests older than 24 hours. Alternatively you may run `AGE_SECS=900 make sweep` to supply a shorter age threshold.

## License

MPL v2.0
9 changes: 5 additions & 4 deletions internal/provider/branch_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ func TestAccBranchResource(t *testing.T) {
},
// ImportState testing
{
ResourceName: "planetscale_branch.test",
ImportStateId: fmt.Sprintf("%s,%s,%s", testAccOrg, dbName, branchName),
ImportState: true,
ImportStateVerify: true,
ResourceName: "planetscale_branch.test",
ImportStateId: fmt.Sprintf("%s,%s,%s", testAccOrg, dbName, branchName),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"updated_at", "schema_last_updated_at"},
},
// Update and Read testing
// TODO: Implement an update test.
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/database_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestAccDatabaseResource(t *testing.T) {
ImportStateId: fmt.Sprintf("%s,%s", testAccOrg, dbName),
ImportState: true,
ImportStateVerify: true,
// TODO: API does not return cluster_size which causes a diff on import. When fixed, remove this:
ImportStateVerifyIgnore: []string{"cluster_size"},
// TODO: API does not return cluster_size which causes a diff on import. When fixed, remove it.
ImportStateVerifyIgnore: []string{"cluster_size", "updated_at"},
},
// Update and Read testing
{
Expand Down
17 changes: 17 additions & 0 deletions script/sweep
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# delete db's older than 24 hours in the test org whose name starts with "testacc"

set -eou pipefail

ORG="${ORG:-planetscale-terraform-testing}"
AGE_SECS="${AGE_SECS:-86400}"

pscale database list --org "$ORG" --format json |
jq --arg age "$AGE_SECS" \
-r '.[] | select(
(.name | startswith("testacc")) and
(.created_at | sub("\\.\\d+"; "") | fromdateiso8601 < (now - ($age | tonumber))))
.name' |
while read -r dbname; do
pscale database delete "$dbname" --org "$ORG" --force
done
Loading