Skip to content

Commit f403f04

Browse files
committed
always set RawExpr, test file-based mocks
1 parent 9cfdcea commit f403f04

File tree

11 files changed

+103
-46
lines changed

11 files changed

+103
-46
lines changed

internal/command/test_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func TestTest_Runs(t *testing.T) {
5252
initCode int
5353
skip bool
5454
description string
55-
selectFiles []string
5655
}{
5756
"simple_pass": {
5857
expectedOut: []string{"1 passed, 0 failed."},
@@ -429,6 +428,10 @@ func TestTest_Runs(t *testing.T) {
429428
},
430429
code: 1,
431430
},
431+
"mock-sources-inline": {
432+
expectedOut: []string{"2 passed, 0 failed."},
433+
code: 0,
434+
},
432435
}
433436
for name, tc := range tcs {
434437
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+
}

internal/configs/config_build_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"testing"
1515

1616
"github.com/davecgh/go-spew/spew"
17-
"github.com/zclconf/go-cty/cty"
1817

1918
version "github.com/hashicorp/go-version"
2019
"github.com/hashicorp/hcl/v2"
@@ -394,19 +393,6 @@ func TestBuildConfig_WithMockDataSourcesInline(t *testing.T) {
394393
if cfg == nil {
395394
t.Fatal("got nil config; want non-nil")
396395
}
397-
398-
provider := cfg.Module.Tests["main.tftest.hcl"].Providers["aws"]
399-
400-
// This time we want to check that the mock data defined inline took
401-
// precedence over the mock data defined in the data files.
402-
defaults := provider.MockData.MockResources["aws_s3_bucket"].Defaults
403-
expected := cty.ObjectVal(map[string]cty.Value{
404-
"arn": cty.StringVal("aws:s3:::bucket"),
405-
})
406-
407-
if !defaults.RawEquals(expected) {
408-
t.Errorf("expected: %s\nactual: %s", expected.GoString(), defaults.GoString())
409-
}
410396
}
411397

412398
func TestBuildConfig_WithNestedTestModules(t *testing.T) {

internal/configs/mock_provider.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ type MockResource struct {
181181
Type string
182182

183183
Defaults cty.Value
184-
RawValue hcl.Expression
184+
RawExpr hcl.Expression
185185

186186
// UseForPlan is true if the values should be computed during the planning
187187
// phase.
@@ -332,11 +332,11 @@ func decodeMockResourceBlock(block *hcl.Block, useForPlanDefault bool) (*MockRes
332332

333333
if defaults, exists := content.Attributes["defaults"]; exists {
334334
resource.DefaultsRange = defaults.Range
335-
resource.RawValue = defaults.Expr
335+
resource.RawExpr = defaults.Expr
336336
} else {
337337
// It's fine if we don't have any defaults, just means we'll generate
338338
// values for everything ourselves.
339-
resource.Defaults = cty.NilVal
339+
resource.RawExpr = hcl.StaticExpr(cty.EmptyObjectVal, hcl.Range{})
340340
}
341341

342342
useForPlan, useForPlanDiags := useForPlan(content, useForPlanDefault)
@@ -480,6 +480,11 @@ func decodeOverrideBlock(block *hcl.Block, attributeName string, blockName strin
480480
if attribute, exists := content.Attributes[attributeName]; exists {
481481
override.ValuesRange = attribute.Range
482482
override.RawExpr = attribute.Expr
483+
} else {
484+
// It's fine if we don't have any values, just means we'll generate
485+
// values for everything ourselves. We set this to an empty object so
486+
// it's equivalent to `values = {}` which makes later processing easier.
487+
override.RawExpr = hcl.StaticExpr(cty.EmptyObjectVal, hcl.Range{})
483488
}
484489

485490
useForPlan, useForPlanDiags := useForPlan(content, useForPlanDefault)

internal/configs/parser_config_dir_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,6 @@ func TestParserLoadTestFiles_Invalid(t *testing.T) {
280280
"invalid_data_override_target.tftest.hcl:8,3-24: Invalid override target; You can only target data sources from override_data blocks, not module.child.",
281281
"invalid_data_override_target.tftest.hcl:3,3-31: Invalid override target; You can only target data sources from override_data blocks, not aws_instance.target.",
282282
},
283-
"invalid_mock_data_sources": {
284-
"invalid_mock_data_sources.tftest.hcl:7,13-16: Variables not allowed; Variables may not be used here.",
285-
},
286-
"invalid_mock_resources": {
287-
"invalid_mock_resources.tftest.hcl:7,13-16: Variables not allowed; Variables may not be used here.",
288-
},
289283
"invalid_module_override": {
290284
"invalid_module_override.tftest.hcl:5,1-16: Missing target attribute; override_module blocks must specify a target address.",
291285
"invalid_module_override.tftest.hcl:11,3-9: Unsupported argument; An argument named \"values\" is not expected here.",

internal/moduletest/graph/node_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ func (n *NodeProviderConfigure) Execute(ctx *EvalContext) {
8282
// mock data, so we will evaluate the data here.
8383
if mock, ok := n.Provider.(*providers.Mock); ok {
8484
for _, res := range mock.Data.MockResources {
85-
values, hclDiags := res.RawValue.Value(hclContext)
85+
values, hclDiags := res.RawExpr.Value(hclContext)
8686
n.File.Diagnostics.Append(hclDiags)
8787
res.Defaults = values
8888
}
8989
for _, res := range mock.Data.MockDataSources {
90-
values, hclDiags := res.RawValue.Value(hclContext)
90+
values, hclDiags := res.RawExpr.Value(hclContext)
9191
n.File.Diagnostics.Append(hclDiags)
9292
res.Defaults = values
9393
}

internal/moduletest/graph/transform_providers.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,12 @@ func (t *TestProvidersTransformer) Transform(g *terraform.Graph) error {
4444
if err != nil {
4545
return fmt.Errorf("could not create provider instance: %w", err)
4646
}
47-
var mock *providers.Mock
4847

4948
if config.Mock {
50-
mock = &providers.Mock{
49+
impl = &providers.Mock{
5150
Provider: impl,
5251
Data: config.MockData,
5352
}
54-
impl = mock
5553
}
5654

5755
addr := addrs.RootProviderConfig{

0 commit comments

Comments
 (0)