2021-06-21 16:53:16 +02:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hcltest"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeMovedBlock(t *testing.T) {
|
|
|
|
blockRange := hcl.Range{
|
|
|
|
Filename: "mock.tf",
|
|
|
|
Start: hcl.Pos{Line: 3, Column: 12, Byte: 27},
|
|
|
|
End: hcl.Pos{Line: 3, Column: 19, Byte: 34},
|
|
|
|
}
|
|
|
|
|
|
|
|
foo_expr := hcltest.MockExprTraversalSrc("test_instance.foo")
|
|
|
|
bar_expr := hcltest.MockExprTraversalSrc("test_instance.bar")
|
|
|
|
|
|
|
|
foo_index_expr := hcltest.MockExprTraversalSrc("test_instance.foo[1]")
|
|
|
|
bar_index_expr := hcltest.MockExprTraversalSrc("test_instance.bar[\"one\"]")
|
|
|
|
|
|
|
|
mod_foo_expr := hcltest.MockExprTraversalSrc("module.foo")
|
|
|
|
mod_bar_expr := hcltest.MockExprTraversalSrc("module.bar")
|
|
|
|
|
|
|
|
tests := map[string]struct {
|
|
|
|
input *hcl.Block
|
|
|
|
want *Moved
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
&hcl.Block{
|
|
|
|
Type: "moved",
|
|
|
|
Body: hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Attributes: hcl.Attributes{
|
|
|
|
"from": {
|
|
|
|
Name: "from",
|
|
|
|
Expr: foo_expr,
|
|
|
|
},
|
|
|
|
"to": {
|
|
|
|
Name: "to",
|
|
|
|
Expr: bar_expr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
DefRange: blockRange,
|
|
|
|
},
|
|
|
|
&Moved{
|
2021-06-30 18:22:02 +02:00
|
|
|
From: mustMoveEndpointFromExpr(foo_expr),
|
|
|
|
To: mustMoveEndpointFromExpr(bar_expr),
|
2021-06-21 16:53:16 +02:00
|
|
|
DeclRange: blockRange,
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
"indexed resources": {
|
|
|
|
&hcl.Block{
|
|
|
|
Type: "moved",
|
|
|
|
Body: hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Attributes: hcl.Attributes{
|
|
|
|
"from": {
|
|
|
|
Name: "from",
|
|
|
|
Expr: foo_index_expr,
|
|
|
|
},
|
|
|
|
"to": {
|
|
|
|
Name: "to",
|
|
|
|
Expr: bar_index_expr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
DefRange: blockRange,
|
|
|
|
},
|
|
|
|
&Moved{
|
2021-06-30 18:22:02 +02:00
|
|
|
From: mustMoveEndpointFromExpr(foo_index_expr),
|
|
|
|
To: mustMoveEndpointFromExpr(bar_index_expr),
|
2021-06-21 16:53:16 +02:00
|
|
|
DeclRange: blockRange,
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
"modules": {
|
|
|
|
&hcl.Block{
|
|
|
|
Type: "moved",
|
|
|
|
Body: hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Attributes: hcl.Attributes{
|
|
|
|
"from": {
|
|
|
|
Name: "from",
|
|
|
|
Expr: mod_foo_expr,
|
|
|
|
},
|
|
|
|
"to": {
|
|
|
|
Name: "to",
|
|
|
|
Expr: mod_bar_expr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
DefRange: blockRange,
|
|
|
|
},
|
|
|
|
&Moved{
|
2021-06-30 18:22:02 +02:00
|
|
|
From: mustMoveEndpointFromExpr(mod_foo_expr),
|
|
|
|
To: mustMoveEndpointFromExpr(mod_bar_expr),
|
2021-06-21 16:53:16 +02:00
|
|
|
DeclRange: blockRange,
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
"error: missing argument": {
|
|
|
|
&hcl.Block{
|
|
|
|
Type: "moved",
|
|
|
|
Body: hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Attributes: hcl.Attributes{
|
|
|
|
"from": {
|
|
|
|
Name: "from",
|
|
|
|
Expr: foo_expr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
DefRange: blockRange,
|
|
|
|
},
|
|
|
|
&Moved{
|
2021-06-30 18:22:02 +02:00
|
|
|
From: mustMoveEndpointFromExpr(foo_expr),
|
2021-06-21 16:53:16 +02:00
|
|
|
DeclRange: blockRange,
|
|
|
|
},
|
|
|
|
"Missing required argument",
|
|
|
|
},
|
|
|
|
"error: type mismatch": {
|
|
|
|
&hcl.Block{
|
|
|
|
Type: "moved",
|
|
|
|
Body: hcltest.MockBody(&hcl.BodyContent{
|
|
|
|
Attributes: hcl.Attributes{
|
|
|
|
"to": {
|
|
|
|
Name: "to",
|
|
|
|
Expr: foo_expr,
|
|
|
|
},
|
|
|
|
"from": {
|
|
|
|
Name: "from",
|
|
|
|
Expr: mod_foo_expr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
DefRange: blockRange,
|
|
|
|
},
|
|
|
|
&Moved{
|
2021-06-30 18:22:02 +02:00
|
|
|
To: mustMoveEndpointFromExpr(foo_expr),
|
|
|
|
From: mustMoveEndpointFromExpr(mod_foo_expr),
|
2021-06-21 16:53:16 +02:00
|
|
|
DeclRange: blockRange,
|
|
|
|
},
|
2021-06-30 18:22:02 +02:00
|
|
|
"Invalid \"moved\" addresses",
|
2021-06-21 16:53:16 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
got, diags := decodeMovedBlock(test.input)
|
|
|
|
|
|
|
|
if diags.HasErrors() {
|
|
|
|
if test.err == "" {
|
|
|
|
t.Fatalf("unexpected error: %s", diags.Errs())
|
|
|
|
}
|
|
|
|
if gotErr := diags[0].Summary; gotErr != test.err {
|
|
|
|
t.Errorf("wrong error, got %q, want %q", gotErr, test.err)
|
|
|
|
}
|
|
|
|
} else if test.err != "" {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:22:02 +02:00
|
|
|
if !cmp.Equal(got, test.want, cmp.AllowUnexported(addrs.MoveEndpoint{})) {
|
2021-06-21 16:53:16 +02:00
|
|
|
t.Fatalf("wrong result: %s", cmp.Diff(got, test.want))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 19:46:15 +02:00
|
|
|
func TestMovedBlocksInModule(t *testing.T) {
|
|
|
|
parser := NewParser(nil)
|
|
|
|
mod, diags := parser.LoadConfigDir("testdata/valid-modules/moved-blocks")
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Errorf("unexpected error: %s", diags.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var gotPairs [][2]string
|
|
|
|
for _, mc := range mod.Moved {
|
|
|
|
gotPairs = append(gotPairs, [2]string{mc.From.String(), mc.To.String()})
|
|
|
|
}
|
|
|
|
wantPairs := [][2]string{
|
|
|
|
{`test.foo`, `test.bar`},
|
|
|
|
{`test.foo`, `test.bar["bloop"]`},
|
|
|
|
{`module.a`, `module.b`},
|
|
|
|
{`module.a`, `module.a["foo"]`},
|
|
|
|
{`test.foo`, `module.a.test.foo`},
|
|
|
|
{`data.test.foo`, `data.test.bar`},
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(wantPairs, gotPairs); diff != "" {
|
|
|
|
t.Errorf("wrong addresses\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:22:02 +02:00
|
|
|
func mustMoveEndpointFromExpr(expr hcl.Expression) *addrs.MoveEndpoint {
|
2021-06-21 16:53:16 +02:00
|
|
|
traversal, hcldiags := hcl.AbsTraversalForExpr(expr)
|
|
|
|
if hcldiags.HasErrors() {
|
|
|
|
panic(hcldiags.Errs())
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:22:02 +02:00
|
|
|
ep, diags := addrs.ParseMoveEndpoint(traversal)
|
2021-06-21 16:53:16 +02:00
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Err())
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:22:02 +02:00
|
|
|
return ep
|
2021-06-21 16:53:16 +02:00
|
|
|
}
|