mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Add basic plan test coverage
This also unearthed that the marking must happen earlier in the eval_diff in order to produce a valid plan (so that the planned marked value matches the marked config value)
This commit is contained in:
parent
e4e16ccbd3
commit
4034cf9f75
@ -503,7 +503,6 @@ Note that the -target option is not suitable for routine use, and is provided on
|
|||||||
func (c *Context) Plan() (*plans.Plan, tfdiags.Diagnostics) {
|
func (c *Context) Plan() (*plans.Plan, tfdiags.Diagnostics) {
|
||||||
defer c.acquireRun("plan")()
|
defer c.acquireRun("plan")()
|
||||||
c.changes = plans.NewChanges()
|
c.changes = plans.NewChanges()
|
||||||
|
|
||||||
var diags tfdiags.Diagnostics
|
var diags tfdiags.Diagnostics
|
||||||
|
|
||||||
if len(c.targets) > 0 {
|
if len(c.targets) > 0 {
|
||||||
@ -575,6 +574,7 @@ The -target option is not for routine use, and is provided only for exceptional
|
|||||||
diags = diags.Append(walker.NonFatalDiagnostics)
|
diags = diags.Append(walker.NonFatalDiagnostics)
|
||||||
diags = diags.Append(walkDiags)
|
diags = diags.Append(walkDiags)
|
||||||
if walkDiags.HasErrors() {
|
if walkDiags.HasErrors() {
|
||||||
|
fmt.Println("walkerr")
|
||||||
return nil, diags
|
return nil, diags
|
||||||
}
|
}
|
||||||
p.Changes = c.changes
|
p.Changes = c.changes
|
||||||
|
@ -5626,6 +5626,61 @@ resource "aws_instance" "foo" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext2Plan_variableSensitivity(t *testing.T) {
|
||||||
|
m := testModule(t, "plan-variable-sensitivity")
|
||||||
|
|
||||||
|
p := testProvider("aws")
|
||||||
|
p.ValidateResourceTypeConfigFn = func(req providers.ValidateResourceTypeConfigRequest) (resp providers.ValidateResourceTypeConfigResponse) {
|
||||||
|
foo := req.Config.GetAttr("foo").AsString()
|
||||||
|
if foo == "bar" {
|
||||||
|
resp.Diagnostics = resp.Diagnostics.Append(errors.New("foo cannot be bar"))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
||||||
|
resp.PlannedState = req.ProposedNewState
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := testContext2(t, &ContextOpts{
|
||||||
|
Config: m,
|
||||||
|
Providers: map[addrs.Provider]providers.Factory{
|
||||||
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
plan, diags := ctx.Plan()
|
||||||
|
if diags.HasErrors() {
|
||||||
|
t.Fatalf("unexpected errors: %s", diags.Err())
|
||||||
|
}
|
||||||
|
schema := p.GetSchemaReturn.ResourceTypes["aws_instance"]
|
||||||
|
ty := schema.ImpliedType()
|
||||||
|
|
||||||
|
if len(plan.Changes.Resources) != 1 {
|
||||||
|
t.Fatal("expected 1 changes, got", len(plan.Changes.Resources))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, res := range plan.Changes.Resources {
|
||||||
|
if res.Action != plans.Create {
|
||||||
|
t.Fatalf("expected resource creation, got %s", res.Action)
|
||||||
|
}
|
||||||
|
ric, err := res.Decode(ty)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch i := ric.Addr.String(); i {
|
||||||
|
case "aws_instance.foo":
|
||||||
|
checkVals(t, objectVal(t, schema, map[string]cty.Value{
|
||||||
|
"foo": cty.StringVal("foo"),
|
||||||
|
}), ric.After)
|
||||||
|
default:
|
||||||
|
t.Fatal("unknown instance:", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func checkVals(t *testing.T, expected, got cty.Value) {
|
func checkVals(t *testing.T, expected, got cty.Value) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if !cmp.Equal(expected, got, valueComparer, typeComparer, equateEmpty) {
|
if !cmp.Equal(expected, got, valueComparer, typeComparer, equateEmpty) {
|
||||||
|
@ -256,6 +256,11 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
|
|||||||
panic(fmt.Sprintf("PlanResourceChange of %s produced nil value", absAddr.String()))
|
panic(fmt.Sprintf("PlanResourceChange of %s produced nil value", absAddr.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the marks back to the planned new value
|
||||||
|
if configVal.ContainsMarked() {
|
||||||
|
plannedNewVal = plannedNewVal.MarkWithPaths(unmarkedPaths)
|
||||||
|
}
|
||||||
|
|
||||||
// We allow the planned new value to disagree with configuration _values_
|
// We allow the planned new value to disagree with configuration _values_
|
||||||
// here, since that allows the provider to do special logic like a
|
// here, since that allows the provider to do special logic like a
|
||||||
// DiffSuppressFunc, but we still require that the provider produces
|
// DiffSuppressFunc, but we still require that the provider produces
|
||||||
@ -473,11 +478,6 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the marks back to the planned new value
|
|
||||||
if configVal.ContainsMarked() {
|
|
||||||
plannedNewVal = plannedNewVal.MarkWithPaths(unmarkedPaths)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call post-refresh hook
|
// Call post-refresh hook
|
||||||
if !n.Stub {
|
if !n.Stub {
|
||||||
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
err := ctx.Hook(func(h Hook) (HookAction, error) {
|
||||||
|
12
terraform/testdata/plan-variable-sensitivity/main.tf
vendored
Normal file
12
terraform/testdata/plan-variable-sensitivity/main.tf
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
terraform {
|
||||||
|
experiments = [sensitive_variables]
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "sensitive_var" {
|
||||||
|
default = "foo"
|
||||||
|
sensitive = true
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "foo" {
|
||||||
|
foo = var.sensitive_var
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user