Skip to content

Commit 685e77b

Browse files
committed
add plan context provider func test
1 parent fcb1d52 commit 685e77b

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package terraform
5+
6+
import (
7+
"bytes"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform/internal/addrs"
11+
"github.com/hashicorp/terraform/internal/plans"
12+
"github.com/hashicorp/terraform/internal/providers"
13+
"github.com/hashicorp/terraform/internal/states"
14+
"github.com/zclconf/go-cty/cty"
15+
)
16+
17+
func TestContext2Plan_providerFunctionBasic(t *testing.T) {
18+
m := testModuleInline(t, map[string]string{
19+
"main.tf": `
20+
terraform {
21+
required_providers {
22+
test = {
23+
source = "registry.terraform.io/hashicorp/test"
24+
}
25+
}
26+
}
27+
28+
locals {
29+
input = {
30+
key = "value"
31+
}
32+
33+
expected = {
34+
key = "value"
35+
}
36+
}
37+
38+
output "noop_equals" {
39+
// The false branch will fail to evaluate entirely if our condition doesn't
40+
// hold true. This is not a normal way to check a condition, but it's been
41+
// seen in the wild, so adding it here for variety.
42+
value = provider::test::noop(local.input) == local.expected ? "ok" : {}["fail"]
43+
}
44+
`,
45+
})
46+
47+
p := new(MockProvider)
48+
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
49+
Functions: map[string]providers.FunctionDecl{
50+
"noop": providers.FunctionDecl{
51+
Parameters: []providers.FunctionParam{
52+
{
53+
Name: "any",
54+
Type: cty.DynamicPseudoType,
55+
},
56+
},
57+
ReturnType: cty.DynamicPseudoType,
58+
},
59+
},
60+
}
61+
p.CallFunctionFn = func(req providers.CallFunctionRequest) (resp providers.CallFunctionResponse) {
62+
resp.Result = req.Arguments[0]
63+
return resp
64+
}
65+
66+
ctx := testContext2(t, &ContextOpts{
67+
Providers: map[addrs.Provider]providers.Factory{
68+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
69+
},
70+
})
71+
72+
plan, diags := ctx.Plan(m, states.NewState(), SimplePlanOpts(plans.NormalMode, testInputValuesUnset(m.Module.Variables)))
73+
assertNoErrors(t, diags)
74+
75+
// there is exactly one output, which is a dynamically typed string
76+
if !bytes.Equal([]byte("\x92\xc4\b\"string\"\xa2ok"), plan.Changes.Outputs[0].After) {
77+
t.Fatalf("got output dynamic value of %q", plan.Changes.Outputs[0].After)
78+
}
79+
}

0 commit comments

Comments
 (0)