mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-06 14:13:16 -06:00
36d0a50427
This is part of a general effort to move all of Terraform's non-library package surface under internal in order to reinforce that these are for internal use within Terraform only. If you were previously importing packages under this prefix into an external codebase, you could pin to an earlier release tag as an interim solution until you've make a plan to achieve the same functionality some other way.
149 lines
2.6 KiB
Go
149 lines
2.6 KiB
Go
package terraform
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestStripRemovedStateAttributes(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
state map[string]interface{}
|
|
expect map[string]interface{}
|
|
ty cty.Type
|
|
modified bool
|
|
}{
|
|
{
|
|
"removed string",
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
"b": "gone",
|
|
},
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
},
|
|
cty.Object(map[string]cty.Type{
|
|
"a": cty.String,
|
|
}),
|
|
true,
|
|
},
|
|
{
|
|
"removed null",
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
"b": nil,
|
|
},
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
},
|
|
cty.Object(map[string]cty.Type{
|
|
"a": cty.String,
|
|
}),
|
|
true,
|
|
},
|
|
{
|
|
"removed nested string",
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
"b": map[string]interface{}{
|
|
"a": "ok",
|
|
"b": "removed",
|
|
},
|
|
},
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
"b": map[string]interface{}{
|
|
"a": "ok",
|
|
},
|
|
},
|
|
cty.Object(map[string]cty.Type{
|
|
"a": cty.String,
|
|
"b": cty.Object(map[string]cty.Type{
|
|
"a": cty.String,
|
|
}),
|
|
}),
|
|
true,
|
|
},
|
|
{
|
|
"removed nested list",
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
"b": map[string]interface{}{
|
|
"a": "ok",
|
|
"b": []interface{}{"removed"},
|
|
},
|
|
},
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
"b": map[string]interface{}{
|
|
"a": "ok",
|
|
},
|
|
},
|
|
cty.Object(map[string]cty.Type{
|
|
"a": cty.String,
|
|
"b": cty.Object(map[string]cty.Type{
|
|
"a": cty.String,
|
|
}),
|
|
}),
|
|
true,
|
|
},
|
|
{
|
|
"removed keys in set of objs",
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
"b": map[string]interface{}{
|
|
"a": "ok",
|
|
"set": []interface{}{
|
|
map[string]interface{}{
|
|
"x": "ok",
|
|
"y": "removed",
|
|
},
|
|
map[string]interface{}{
|
|
"x": "ok",
|
|
"y": "removed",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
map[string]interface{}{
|
|
"a": "ok",
|
|
"b": map[string]interface{}{
|
|
"a": "ok",
|
|
"set": []interface{}{
|
|
map[string]interface{}{
|
|
"x": "ok",
|
|
},
|
|
map[string]interface{}{
|
|
"x": "ok",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
cty.Object(map[string]cty.Type{
|
|
"a": cty.String,
|
|
"b": cty.Object(map[string]cty.Type{
|
|
"a": cty.String,
|
|
"set": cty.Set(cty.Object(map[string]cty.Type{
|
|
"x": cty.String,
|
|
})),
|
|
}),
|
|
}),
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
modified := removeRemovedAttrs(tc.state, tc.ty)
|
|
if !reflect.DeepEqual(tc.state, tc.expect) {
|
|
t.Fatalf("expected: %#v\n got: %#v\n", tc.expect, tc.state)
|
|
}
|
|
if modified != tc.modified {
|
|
t.Fatal("incorrect return value")
|
|
}
|
|
})
|
|
}
|
|
}
|