Skip to content

Commit 6b0f23e

Browse files
rfayclaude
andauthored
ci: add Terraform mock unit tests for all templates for #71 (Phase 2) (#102)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 900a46a commit 6b0f23e

5 files changed

Lines changed: 241 additions & 0 deletions

File tree

.github/workflows/validate.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jobs:
2424
- name: Validate
2525
run: terraform validate
2626
working-directory: ${{ matrix.template }}
27+
- name: Test
28+
run: terraform test
29+
working-directory: ${{ matrix.template }}
2730

2831
fmt:
2932
name: Format Check

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ validate: ## Validate all Terraform templates (requires terraform in PATH)
114114
fmt-check: ## Check Terraform formatting across all templates
115115
terraform fmt -check -recursive
116116

117+
.PHONY: test-templates
118+
test-templates: ## Run Terraform mock unit tests for all templates (requires terraform in PATH)
119+
@for t in $(TEMPLATES); do \
120+
echo "--- Testing $$t ---"; \
121+
(cd $$t && terraform test) || exit 1; \
122+
done
123+
@echo "All template tests passed."
124+
117125
.PHONY: clean
118126
clean: ## Remove local image
119127
@echo "Removing local images..."
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
mock_provider "coder" {
2+
mock_data "coder_workspace" {
3+
defaults = {
4+
start_count = 1
5+
id = "mock-workspace-id"
6+
name = "test-workspace"
7+
}
8+
}
9+
mock_data "coder_workspace_owner" {
10+
defaults = {
11+
name = "testuser"
12+
}
13+
}
14+
# vscode_extensions.value is jsondecode()d in locals; must be valid JSON
15+
mock_data "coder_parameter" {
16+
defaults = {
17+
value = "[]"
18+
}
19+
}
20+
}
21+
22+
mock_provider "docker" {}
23+
24+
# cache_path has no default so must be supplied in every run block.
25+
# Any path works here — the mock docker provider does not validate host paths.
26+
27+
run "plan_succeeds_with_defaults" {
28+
command = plan
29+
variables {
30+
cache_path = "/tmp/mock-cache"
31+
}
32+
}
33+
34+
run "container_created_when_started" {
35+
command = plan
36+
variables {
37+
cache_path = "/tmp/mock-cache"
38+
}
39+
assert {
40+
condition = length(docker_container.workspace) == 1
41+
error_message = "docker_container.workspace must be created when start_count=1"
42+
}
43+
}
44+
45+
run "cpu_below_minimum" {
46+
command = plan
47+
variables {
48+
cache_path = "/tmp/mock-cache"
49+
cpu = 0
50+
}
51+
expect_failures = [var.cpu]
52+
}
53+
54+
run "cpu_above_maximum" {
55+
command = plan
56+
variables {
57+
cache_path = "/tmp/mock-cache"
58+
cpu = 33
59+
}
60+
expect_failures = [var.cpu]
61+
}
62+
63+
run "memory_below_minimum" {
64+
command = plan
65+
variables {
66+
cache_path = "/tmp/mock-cache"
67+
memory = 1
68+
}
69+
expect_failures = [var.memory]
70+
}
71+
72+
run "memory_above_maximum" {
73+
command = plan
74+
variables {
75+
cache_path = "/tmp/mock-cache"
76+
memory = 129
77+
}
78+
expect_failures = [var.memory]
79+
}

freeform/tests/validate.tftest.hcl

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
mock_provider "coder" {
2+
mock_data "coder_workspace" {
3+
defaults = {
4+
start_count = 1
5+
id = "mock-workspace-id"
6+
name = "test-workspace"
7+
}
8+
}
9+
mock_data "coder_workspace_owner" {
10+
defaults = {
11+
name = "testuser"
12+
}
13+
}
14+
# vscode_extensions.value is jsondecode()d in locals; must be valid JSON
15+
mock_data "coder_parameter" {
16+
defaults = {
17+
value = "[]"
18+
}
19+
}
20+
}
21+
22+
mock_provider "docker" {}
23+
24+
run "plan_succeeds_with_defaults" {
25+
command = plan
26+
}
27+
28+
run "container_created_when_started" {
29+
command = plan
30+
assert {
31+
condition = length(docker_container.workspace) == 1
32+
error_message = "docker_container.workspace must be created when start_count=1"
33+
}
34+
}
35+
36+
run "adminer_off_by_default" {
37+
command = plan
38+
assert {
39+
condition = length(coder_app.adminer) == 0
40+
error_message = "coder_app.adminer must not be created when enable_adminer=false"
41+
}
42+
}
43+
44+
run "adminer_enabled" {
45+
command = plan
46+
variables {
47+
enable_adminer = true
48+
}
49+
assert {
50+
condition = length(coder_app.adminer) == 1
51+
error_message = "coder_app.adminer must be created when enable_adminer=true"
52+
}
53+
}
54+
55+
run "cpu_below_minimum" {
56+
command = plan
57+
variables {
58+
cpu = 0
59+
}
60+
expect_failures = [var.cpu]
61+
}
62+
63+
run "cpu_above_maximum" {
64+
command = plan
65+
variables {
66+
cpu = 33
67+
}
68+
expect_failures = [var.cpu]
69+
}
70+
71+
run "memory_below_minimum" {
72+
command = plan
73+
variables {
74+
memory = 1
75+
}
76+
expect_failures = [var.memory]
77+
}
78+
79+
run "memory_above_maximum" {
80+
command = plan
81+
variables {
82+
memory = 129
83+
}
84+
expect_failures = [var.memory]
85+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
mock_provider "coder" {
2+
mock_data "coder_workspace" {
3+
defaults = {
4+
start_count = 1
5+
id = "mock-workspace-id"
6+
name = "test-workspace"
7+
}
8+
}
9+
mock_data "coder_workspace_owner" {
10+
defaults = {
11+
name = "testuser"
12+
}
13+
}
14+
# vscode_extensions.value is jsondecode()d in locals; must be valid JSON
15+
mock_data "coder_parameter" {
16+
defaults = {
17+
value = "[]"
18+
}
19+
}
20+
}
21+
22+
mock_provider "docker" {}
23+
24+
run "plan_succeeds_with_defaults" {
25+
command = plan
26+
}
27+
28+
run "container_created_when_started" {
29+
command = plan
30+
assert {
31+
condition = length(docker_container.workspace) == 1
32+
error_message = "docker_container.workspace must be created when start_count=1"
33+
}
34+
}
35+
36+
run "cpu_below_minimum" {
37+
command = plan
38+
variables {
39+
cpu = 0
40+
}
41+
expect_failures = [var.cpu]
42+
}
43+
44+
run "cpu_above_maximum" {
45+
command = plan
46+
variables {
47+
cpu = 33
48+
}
49+
expect_failures = [var.cpu]
50+
}
51+
52+
run "memory_below_minimum" {
53+
command = plan
54+
variables {
55+
memory = 1
56+
}
57+
expect_failures = [var.memory]
58+
}
59+
60+
run "memory_above_maximum" {
61+
command = plan
62+
variables {
63+
memory = 129
64+
}
65+
expect_failures = [var.memory]
66+
}

0 commit comments

Comments
 (0)