From 016d4dc523dbd1197526da4a8afb74607d684404 Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Fri, 8 Aug 2025 13:31:35 +0500 Subject: [PATCH] chore(test): migrate to terraform test and add initial .tftest for zed Replace Bun-based test runner with Terraform native testing. Adds script to discover and run tests across modules and updates docs/scripts to use terraform test. --- MAINTAINER.md | 4 +-- package.json | 2 +- registry/coder/modules/zed/zed.tftest.hcl | 40 +++++++++++++++++++++++ scripts/terraform_test_all.sh | 26 +++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 registry/coder/modules/zed/zed.tftest.hcl create mode 100755 scripts/terraform_test_all.sh diff --git a/MAINTAINER.md b/MAINTAINER.md index 22326574..9959e59c 100644 --- a/MAINTAINER.md +++ b/MAINTAINER.md @@ -18,9 +18,9 @@ sudo apt install golang-go Check that PRs have: -- [ ] All required files (`main.tf`, `main.test.ts`, `README.md`) +- [ ] All required files (`main.tf`, `README.md`, at least one `.tftest.hcl`) - [ ] Proper frontmatter in README -- [ ] Working tests (`bun test`) +- [ ] Working tests (`terraform test`) - [ ] Formatted code (`bun run fmt`) - [ ] Avatar image for new namespaces (`avatar.png` or `avatar.svg` in `.images/`) diff --git a/package.json b/package.json index 7ca9f2ec..c2f9ff69 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "fmt": "bun x prettier --write **/*.sh **/*.ts **/*.md *.md && terraform fmt -recursive -diff", "fmt:ci": "bun x prettier --check **/*.sh **/*.ts **/*.md *.md && terraform fmt -check -recursive -diff", "terraform-validate": "./scripts/terraform_validate.sh", - "test": "bun test", + "test": "./scripts/terraform_test_all.sh", "update-version": "./update-version.sh" }, "devDependencies": { diff --git a/registry/coder/modules/zed/zed.tftest.hcl b/registry/coder/modules/zed/zed.tftest.hcl new file mode 100644 index 00000000..508b6550 --- /dev/null +++ b/registry/coder/modules/zed/zed.tftest.hcl @@ -0,0 +1,40 @@ +run "default_output" { + command = apply + + variables { + agent_id = "foo" + } + + assert { + condition = output.zed_url == "zed://ssh/default.coder" + error_message = "zed_url did not match expected default URL" + } +} + +run "adds_folder" { + command = apply + + variables { + agent_id = "foo" + folder = "/foo/bar" + } + + assert { + condition = output.zed_url == "zed://ssh/default.coder/foo/bar" + error_message = "zed_url did not include provided folder path" + } +} + +run "adds_agent_name" { + command = apply + + variables { + agent_id = "foo" + agent_name = "myagent" + } + + assert { + condition = output.zed_url == "zed://ssh/myagent.default.default.coder" + error_message = "zed_url did not include agent_name in hostname" + } +} diff --git a/scripts/terraform_test_all.sh b/scripts/terraform_test_all.sh new file mode 100755 index 00000000..3ba9b5b9 --- /dev/null +++ b/scripts/terraform_test_all.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Find all directories that contain any .tftest.hcl files and run terraform test in each + +run_dir() { + local dir="$1" + echo "==> Running terraform test in $dir" + (cd "$dir" && terraform init -upgrade -input=false -no-color >/dev/null && terraform test -no-color -verbose) +} + +mapfile -t test_dirs < <(find . -type f -name "*.tftest.hcl" -print0 | xargs -0 -I{} dirname {} | sort -u) + +if [[ ${#test_dirs[@]} -eq 0 ]]; then + echo "No .tftest.hcl tests found." + exit 0 +fi + +status=0 +for d in "${test_dirs[@]}"; do + if ! run_dir "$d"; then + status=1 + fi +done + +exit $status