2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-08-27 09:51:49 -05:00
|
|
|
package opentf
|
2017-07-01 13:08:15 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
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
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
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
|
|
|
"github.com/zclconf/go-cty/cty"
|
2018-05-04 21:24:06 -05:00
|
|
|
|
2023-08-17 07:45:11 -05:00
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/addrs"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/configs"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/configs/hcl2shim"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/states"
|
2017-07-01 13:08:15 -05:00
|
|
|
)
|
|
|
|
|
2020-09-09 14:59:29 -05:00
|
|
|
func TestNodeLocalExecute(t *testing.T) {
|
2017-07-01 13:08:15 -05:00
|
|
|
tests := []struct {
|
|
|
|
Value string
|
|
|
|
Want interface{}
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"hello!",
|
|
|
|
"hello!",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
},
|
2018-12-19 14:38:18 -06:00
|
|
|
{
|
|
|
|
"Hello, ${local.foo}",
|
|
|
|
nil,
|
|
|
|
true, // self-referencing
|
|
|
|
},
|
2017-07-01 13:08:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Value, func(t *testing.T) {
|
2018-05-04 21:24:06 -05:00
|
|
|
expr, diags := hclsyntax.ParseTemplate([]byte(test.Value), "", hcl.Pos{Line: 1, Column: 1})
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Error())
|
2017-07-01 13:08:15 -05:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:59:29 -05:00
|
|
|
n := &NodeLocal{
|
|
|
|
Addr: addrs.LocalValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
Config: &configs.Local{
|
|
|
|
Expr: expr,
|
|
|
|
},
|
2017-07-01 13:08:15 -05:00
|
|
|
}
|
|
|
|
ctx := &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
|
|
|
StateState: states.NewState().SyncWrapper(),
|
2017-07-01 13:08:15 -05:00
|
|
|
|
2018-05-04 21:24:06 -05:00
|
|
|
EvaluateExprResult: hcl2shim.HCL2ValueFromConfigValue(test.Want),
|
2017-07-01 13:08:15 -05:00
|
|
|
}
|
|
|
|
|
2020-09-09 14:59:29 -05:00
|
|
|
err := n.Execute(ctx, walkApply)
|
2017-07-01 13:08:15 -05:00
|
|
|
if (err != nil) != test.Err {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %s", err)
|
|
|
|
} else {
|
|
|
|
t.Errorf("successful Eval; want error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
ms := ctx.StateState.Module(addrs.RootModuleInstance)
|
|
|
|
gotLocals := ms.LocalValues
|
2018-12-19 14:38:18 -06:00
|
|
|
wantLocals := map[string]cty.Value{}
|
|
|
|
if test.Want != nil {
|
|
|
|
wantLocals["foo"] = hcl2shim.HCL2ValueFromConfigValue(test.Want)
|
2017-07-01 13:08:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(gotLocals, wantLocals) {
|
|
|
|
t.Errorf(
|
|
|
|
"wrong locals after Eval\ngot: %swant: %s",
|
|
|
|
spew.Sdump(gotLocals), spew.Sdump(wantLocals),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|