2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2016-10-07 14:00:50 -05:00
|
|
|
|
|
|
|
import (
|
2020-09-25 14:10:32 -05:00
|
|
|
"strings"
|
2016-10-07 14:00:50 -05:00
|
|
|
"testing"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
2020-09-25 14:10:32 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2022-08-03 11:45:13 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/checks"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs"
|
|
|
|
"github.com/opentofu/opentofu/internal/lang/marks"
|
|
|
|
"github.com/opentofu/opentofu/internal/states"
|
2016-10-07 14:00:50 -05:00
|
|
|
)
|
|
|
|
|
2020-09-25 14:10:32 -05:00
|
|
|
func TestNodeApplyableOutputExecute_knownValue(t *testing.T) {
|
2016-10-07 14:00:50 -05:00
|
|
|
ctx := new(MockEvalContext)
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
ctx.StateState = states.NewState().SyncWrapper()
|
2020-09-25 14:10:32 -05:00
|
|
|
ctx.RefreshStateState = states.NewState().SyncWrapper()
|
2022-06-15 20:00:20 -05:00
|
|
|
ctx.ChecksState = checks.NewState(nil)
|
2016-10-07 14:00:50 -05:00
|
|
|
|
2020-09-25 14:10:32 -05:00
|
|
|
config := &configs.Output{Name: "map-output"}
|
|
|
|
addr := addrs.OutputValue{Name: config.Name}.Absolute(addrs.RootModuleInstance)
|
|
|
|
node := &NodeApplyableOutput{Config: config, Addr: addr}
|
|
|
|
val := cty.MapVal(map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("b"),
|
|
|
|
})
|
|
|
|
ctx.EvaluateExprResult = val
|
|
|
|
|
|
|
|
err := node.Execute(ctx, walkApply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected execute error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outputVal := ctx.StateState.OutputValue(addr)
|
|
|
|
if got, want := outputVal.Value, val; !got.RawEquals(want) {
|
|
|
|
t.Errorf("wrong output value in state\n got: %#v\nwant: %#v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.RefreshStateCalled {
|
|
|
|
t.Fatal("should have called RefreshState, but didn't")
|
|
|
|
}
|
|
|
|
refreshOutputVal := ctx.RefreshStateState.OutputValue(addr)
|
|
|
|
if got, want := refreshOutputVal.Value, val; !got.RawEquals(want) {
|
|
|
|
t.Fatalf("wrong output value in refresh state\n got: %#v\nwant: %#v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeApplyableOutputExecute_noState(t *testing.T) {
|
|
|
|
ctx := new(MockEvalContext)
|
|
|
|
|
|
|
|
config := &configs.Output{Name: "map-output"}
|
|
|
|
addr := addrs.OutputValue{Name: config.Name}.Absolute(addrs.RootModuleInstance)
|
|
|
|
node := &NodeApplyableOutput{Config: config, Addr: addr}
|
|
|
|
val := cty.MapVal(map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("b"),
|
|
|
|
})
|
|
|
|
ctx.EvaluateExprResult = val
|
|
|
|
|
|
|
|
err := node.Execute(ctx, walkApply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected execute error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeApplyableOutputExecute_invalidDependsOn(t *testing.T) {
|
|
|
|
ctx := new(MockEvalContext)
|
|
|
|
ctx.StateState = states.NewState().SyncWrapper()
|
2022-06-15 20:00:20 -05:00
|
|
|
ctx.ChecksState = checks.NewState(nil)
|
2020-09-25 14:10:32 -05:00
|
|
|
|
|
|
|
config := &configs.Output{
|
|
|
|
Name: "map-output",
|
|
|
|
DependsOn: []hcl.Traversal{
|
|
|
|
{
|
|
|
|
hcl.TraverseRoot{Name: "test_instance"},
|
|
|
|
hcl.TraverseAttr{Name: "foo"},
|
|
|
|
hcl.TraverseAttr{Name: "bar"},
|
|
|
|
},
|
2016-10-07 14:00:50 -05:00
|
|
|
},
|
|
|
|
}
|
2020-09-25 14:10:32 -05:00
|
|
|
addr := addrs.OutputValue{Name: config.Name}.Absolute(addrs.RootModuleInstance)
|
|
|
|
node := &NodeApplyableOutput{Config: config, Addr: addr}
|
|
|
|
val := cty.MapVal(map[string]cty.Value{
|
|
|
|
"a": cty.StringVal("b"),
|
|
|
|
})
|
|
|
|
ctx.EvaluateExprResult = val
|
2016-10-07 14:00:50 -05:00
|
|
|
|
2020-10-28 12:47:04 -05:00
|
|
|
diags := node.Execute(ctx, walkApply)
|
|
|
|
if !diags.HasErrors() {
|
2020-09-25 14:10:32 -05:00
|
|
|
t.Fatal("expected execute error, but there was none")
|
|
|
|
}
|
2020-10-28 12:47:04 -05:00
|
|
|
if got, want := diags.Err().Error(), "Invalid depends_on reference"; !strings.Contains(got, want) {
|
2020-09-25 14:10:32 -05:00
|
|
|
t.Errorf("expected error to include %q, but was: %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeApplyableOutputExecute_sensitiveValueNotOutput(t *testing.T) {
|
|
|
|
ctx := new(MockEvalContext)
|
|
|
|
ctx.StateState = states.NewState().SyncWrapper()
|
2022-06-15 20:00:20 -05:00
|
|
|
ctx.ChecksState = checks.NewState(nil)
|
2020-09-25 14:10:32 -05:00
|
|
|
|
|
|
|
config := &configs.Output{Name: "map-output"}
|
|
|
|
addr := addrs.OutputValue{Name: config.Name}.Absolute(addrs.RootModuleInstance)
|
|
|
|
node := &NodeApplyableOutput{Config: config, Addr: addr}
|
|
|
|
val := cty.MapVal(map[string]cty.Value{
|
2021-06-24 16:53:43 -05:00
|
|
|
"a": cty.StringVal("b").Mark(marks.Sensitive),
|
2020-09-25 14:10:32 -05:00
|
|
|
})
|
|
|
|
ctx.EvaluateExprResult = val
|
|
|
|
|
2020-10-28 12:47:04 -05:00
|
|
|
diags := node.Execute(ctx, walkApply)
|
|
|
|
if !diags.HasErrors() {
|
2020-09-25 14:10:32 -05:00
|
|
|
t.Fatal("expected execute error, but there was none")
|
|
|
|
}
|
2020-10-28 12:47:04 -05:00
|
|
|
if got, want := diags.Err().Error(), "Output refers to sensitive values"; !strings.Contains(got, want) {
|
2020-09-25 14:10:32 -05:00
|
|
|
t.Errorf("expected error to include %q, but was: %s", want, got)
|
2016-10-07 14:00:50 -05:00
|
|
|
}
|
2020-09-25 14:10:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeApplyableOutputExecute_sensitiveValueAndOutput(t *testing.T) {
|
|
|
|
ctx := new(MockEvalContext)
|
|
|
|
ctx.StateState = states.NewState().SyncWrapper()
|
2022-06-15 20:00:20 -05:00
|
|
|
ctx.ChecksState = checks.NewState(nil)
|
2020-09-25 14:10:32 -05:00
|
|
|
|
|
|
|
config := &configs.Output{
|
|
|
|
Name: "map-output",
|
|
|
|
Sensitive: true,
|
|
|
|
}
|
|
|
|
addr := addrs.OutputValue{Name: config.Name}.Absolute(addrs.RootModuleInstance)
|
|
|
|
node := &NodeApplyableOutput{Config: config, Addr: addr}
|
|
|
|
val := cty.MapVal(map[string]cty.Value{
|
2021-06-24 16:53:43 -05:00
|
|
|
"a": cty.StringVal("b").Mark(marks.Sensitive),
|
2020-09-25 14:10:32 -05:00
|
|
|
})
|
|
|
|
ctx.EvaluateExprResult = val
|
2020-09-08 13:02:45 -05:00
|
|
|
|
2020-09-25 14:10:32 -05:00
|
|
|
err := node.Execute(ctx, walkApply)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected execute error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarked value should be stored in state
|
|
|
|
outputVal := ctx.StateState.OutputValue(addr)
|
|
|
|
want, _ := val.UnmarkDeep()
|
|
|
|
if got := outputVal.Value; !got.RawEquals(want) {
|
|
|
|
t.Errorf("wrong output value in state\n got: %#v\nwant: %#v", got, want)
|
|
|
|
}
|
2020-09-08 13:02:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeDestroyableOutputExecute(t *testing.T) {
|
|
|
|
outputAddr := addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
state := states.NewState()
|
|
|
|
state.Module(addrs.RootModuleInstance).SetOutputValue("foo", cty.StringVal("bar"), false)
|
|
|
|
state.OutputValue(outputAddr)
|
|
|
|
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
StateState: state.SyncWrapper(),
|
|
|
|
}
|
|
|
|
node := NodeDestroyableOutput{Addr: outputAddr}
|
|
|
|
|
2020-10-28 12:47:04 -05:00
|
|
|
diags := node.Execute(ctx, walkApply)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatalf("Unexpected error: %s", diags.Err())
|
2020-09-08 13:02:45 -05:00
|
|
|
}
|
|
|
|
if state.OutputValue(outputAddr) != nil {
|
|
|
|
t.Fatal("Unexpected outputs in state after removal")
|
|
|
|
}
|
2016-10-07 14:00:50 -05:00
|
|
|
}
|
2022-08-03 11:45:13 -05:00
|
|
|
|
|
|
|
func TestNodeDestroyableOutputExecute_notInState(t *testing.T) {
|
|
|
|
outputAddr := addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
state := states.NewState()
|
|
|
|
|
|
|
|
ctx := &MockEvalContext{
|
|
|
|
StateState: state.SyncWrapper(),
|
|
|
|
}
|
|
|
|
node := NodeDestroyableOutput{Addr: outputAddr}
|
|
|
|
|
|
|
|
diags := node.Execute(ctx, walkApply)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatalf("Unexpected error: %s", diags.Err())
|
|
|
|
}
|
|
|
|
if state.OutputValue(outputAddr) != nil {
|
|
|
|
t.Fatal("Unexpected outputs in state after removal")
|
|
|
|
}
|
|
|
|
}
|