Skip to content

Commit 75fd25c

Browse files
feat: add basic tests for local variables (#65)
## what - generate basic tests for logic performed in `locals { ... }` - test the generated `userdata.sh` script. --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ec4aa07 commit 75fd25c

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ backend.tf.json
4444
**/*.bak
4545
**/*.*swp
4646
**/.DS_Store
47+
48+
.cursor/
49+
50+
.claude/
51+
CLAUDE.md

tests/locals.tftest.hcl

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Common variables used across all test runs
2+
variables {
3+
vpc_id = "vpc-test123"
4+
subnet_ids = ["subnet-test123"]
5+
namespace = "test"
6+
name = "tailscale"
7+
}
8+
9+
# Mock tailscale provider because it expects a real authentication (so we provide it a fake tailscale_tailnet_key)
10+
mock_provider "tailscale" {
11+
mock_resource "tailscale_tailnet_key" {
12+
defaults = {
13+
key = "fake-tailscale-tailnet-key"
14+
}
15+
}
16+
override_resource {
17+
target = tailscale_tailnet_key.default
18+
values = {
19+
key = "fake-tailscale-tailnet-key"
20+
}
21+
}
22+
}
23+
24+
mock_provider "aws" {
25+
mock_data "aws_iam_policy_document" {
26+
defaults = {
27+
json = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"ec2.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}"
28+
}
29+
}
30+
mock_resource "aws_launch_template" {
31+
defaults = {
32+
id = "lt-mock123456789"
33+
}
34+
}
35+
}
36+
37+
run "test_primary_tag_provided" {
38+
command = plan
39+
40+
variables {
41+
primary_tag = "test-router"
42+
}
43+
44+
# Test that the primary_tag is set to the provided value
45+
assert {
46+
condition = local.primary_tag == "test-router"
47+
error_message = "Expected local.primary_tag to be 'test-router'"
48+
}
49+
50+
# Test that the prefixed_primary_tag is set to the provided value
51+
assert {
52+
condition = local.prefixed_primary_tag == "tag:test-router"
53+
error_message = "Expected local.prefixed_primary_tag to be 'tag:test-router'"
54+
}
55+
}
56+
57+
run "test_local_userdata_rendered_template" {
58+
command = apply # because we need access to the tailscale_tailnet_key.default.key value
59+
60+
variables {
61+
primary_tag = "test-router"
62+
additional_tags = ["test-tag1", "test-tag2"]
63+
}
64+
65+
# Ensure userdata script contains `tailscale up` and tailscale key
66+
assert {
67+
condition = (
68+
strcontains(local.userdata, "tailscale up") &&
69+
strcontains(local.userdata, "--authkey=fake-tailscale-tailnet-key") &&
70+
strcontains(local.userdata, "SystemMaxUse=200M")
71+
)
72+
error_message = "Expected userdata to contain tailscale up command, authkey, and journald config"
73+
}
74+
75+
# Ensure userdata script contains transformed additional tags
76+
assert {
77+
condition = strcontains(local.userdata, "tag:test-tag1") && strcontains(local.userdata, "tag:test-tag2")
78+
error_message = "Expected userdata to contain additional tags"
79+
}
80+
}
81+
82+
run "test_tailscaled_extra_flags" {
83+
command = apply # because we need access to the tailscale_tailnet_key.default.key value
84+
85+
variables {
86+
tailscaled_extra_flags = ["--state=mem:", "--verbose=1"]
87+
}
88+
89+
# Test that tailscaled_extra_flags are rendered in userdata
90+
assert {
91+
condition = strcontains(local.userdata, "--state=mem:") && strcontains(local.userdata, "--verbose=1")
92+
error_message = "Expected userdata to contain tailscaled extra flags"
93+
}
94+
}

0 commit comments

Comments
 (0)