Skip to content

Commit 0b35407

Browse files
authored
Allow functions in test mock blocks (#37883)
1 parent 35e81db commit 0b35407

File tree

21 files changed

+273
-71
lines changed

21 files changed

+273
-71
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: ENHANCEMENTS
2+
body: "Terraform Test: Allow functions within mock blocks"
3+
time: 2026-01-13T13:04:49.034917+01:00
4+
custom:
5+
Issue: "34672"

internal/command/test_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ func TestTest_Runs(t *testing.T) {
297297
},
298298
"mocking-invalid": {
299299
expectedErr: []string{
300-
"Invalid outputs attribute",
301300
"The override_during attribute must be a value of plan or apply.",
302301
},
303302
initCode: 1,
@@ -418,6 +417,21 @@ func TestTest_Runs(t *testing.T) {
418417
"no-tests": {
419418
code: 0,
420419
},
420+
"simple_pass_function": {
421+
expectedOut: []string{"2 passed, 0 failed."},
422+
code: 0,
423+
expectedResourceCount: 0,
424+
},
425+
"mocking-invalid-outputs": {
426+
expectedErr: []string{
427+
"Invalid outputs attribute",
428+
},
429+
code: 1,
430+
},
431+
"mock-sources-inline": {
432+
expectedOut: []string{"2 passed, 0 failed."},
433+
code: 0,
434+
},
421435
}
422436
for name, tc := range tcs {
423437
t.Run(name, func(t *testing.T) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
}
6+
}
7+
}
8+
9+
provider "test" {
10+
alias = "secondary"
11+
}
12+
13+
resource "test_resource" "foo" {
14+
value = "foo"
15+
}
16+
17+
resource "test_resource" "bar" {
18+
provider = test.secondary
19+
value = "bar"
20+
}
21+
22+
output "foo" {
23+
value = test_resource.foo.id
24+
}
25+
output "bar" {
26+
value = test_resource.bar.id
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
mock_provider "test" {
3+
source = "./testing/base"
4+
5+
mock_resource "test_resource" {
6+
defaults = {
7+
id = "local-mock-id" // should override the file-based mock
8+
}
9+
}
10+
}
11+
12+
mock_provider "test" {
13+
source = "./testing/base"
14+
alias = "secondary"
15+
}
16+
17+
run "test_foo" {
18+
assert {
19+
condition = output.foo == "local-mock-id"
20+
error_message = "invalid value"
21+
}
22+
}
23+
24+
25+
run "test_bar" {
26+
assert {
27+
condition = output.bar == "file-mock-id"
28+
error_message = "invalid value"
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# If read, this file should cause issues. But, it should be ignored.
2+
3+
mock_resource "test_resource" {}
4+
5+
mock_data "test_resource" {}
6+
7+
override_resource {
8+
target = test_resource.foo
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mock_resource "test_resource" {
2+
defaults = {
3+
id = "file-mock-id"
4+
}
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
}
6+
}
7+
}
8+
9+
variable "instances" {
10+
type = number
11+
}
12+
13+
resource "test_resource" "primary" {
14+
count = var.instances
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
override_resource {
2+
target = test_resource.primary
3+
values = "should be an object" // invalid
4+
}
5+
6+
variables {
7+
instances = 2
8+
}
9+
10+
run "test" {
11+
# We won't even execute this, as the configuration isn't valid.
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "test_resource" "foo" {
2+
value = "foo"
3+
}
4+
5+
resource "test_resource" "bar" {
6+
value = "bar"
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
mock_provider "test" {
2+
mock_resource "test_resource" {
3+
defaults = {
4+
id = format("f-%s", "foo")
5+
}
6+
}
7+
}
8+
9+
override_resource {
10+
target = test_resource.bar
11+
values = {
12+
id = format("%s-%s", uuid(), "bar")
13+
}
14+
}
15+
16+
run "validate_test_resource_foo" {
17+
assert {
18+
condition = test_resource.foo.id == "f-foo"
19+
error_message = "invalid value"
20+
}
21+
}
22+
23+
run "validate_test_resource_bar" {
24+
assert {
25+
condition = length(test_resource.bar.id) > 10
26+
error_message = "invalid value"
27+
}
28+
}

0 commit comments

Comments
 (0)