terraform/helper/schema/field_writer_map_test.go

539 lines
9.5 KiB
Go
Raw Normal View History

package schema
import (
"reflect"
"testing"
)
func TestMapFieldWriter_impl(t *testing.T) {
var _ FieldWriter = new(MapFieldWriter)
}
func TestMapFieldWriter(t *testing.T) {
schema := map[string]*Schema{
"bool": &Schema{Type: TypeBool},
"int": &Schema{Type: TypeInt},
"string": &Schema{Type: TypeString},
"list": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeString},
},
"listInt": &Schema{
Type: TypeList,
Elem: &Schema{Type: TypeInt},
},
2015-02-18 23:44:46 +01:00
"listResource": &Schema{
Type: TypeList,
Optional: true,
Computed: true,
Elem: &Resource{
Schema: map[string]*Schema{
"value": &Schema{
Type: TypeInt,
Optional: true,
},
},
},
},
"map": &Schema{Type: TypeMap},
"set": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
"setDeep": &Schema{
Type: TypeSet,
Elem: &Resource{
Schema: map[string]*Schema{
"index": &Schema{Type: TypeInt},
"value": &Schema{Type: TypeString},
},
},
Set: func(a interface{}) int {
return a.(map[string]interface{})["index"].(int)
},
},
}
cases := map[string]struct {
Addr []string
Value interface{}
Err bool
Out map[string]string
}{
"noexist": {
[]string{"noexist"},
42,
true,
map[string]string{},
},
"bool": {
[]string{"bool"},
false,
false,
map[string]string{
"bool": "false",
},
},
"int": {
[]string{"int"},
42,
false,
map[string]string{
"int": "42",
},
},
"string": {
[]string{"string"},
"42",
false,
map[string]string{
"string": "42",
},
},
2015-09-16 22:35:10 +02:00
"string nil": {
[]string{"string"},
nil,
false,
map[string]string{
"string": "",
},
},
2015-02-18 23:44:46 +01:00
"list of resources": {
[]string{"listResource"},
[]interface{}{
map[string]interface{}{
"value": 80,
},
},
false,
map[string]string{
"listResource.#": "1",
"listResource.0.value": "80",
},
},
"list of resources empty": {
[]string{"listResource"},
[]interface{}{},
false,
map[string]string{
"listResource.#": "0",
},
},
"list of resources nil": {
[]string{"listResource"},
nil,
false,
map[string]string{
"listResource.#": "0",
},
},
"list of strings": {
[]string{"list"},
[]interface{}{"foo", "bar"},
false,
map[string]string{
"list.#": "2",
"list.0": "foo",
"list.1": "bar",
},
},
"list element": {
[]string{"list", "0"},
"string",
true,
map[string]string{},
},
"map": {
[]string{"map"},
map[string]interface{}{"foo": "bar"},
false,
map[string]string{
core: Use .% instead of .# for maps in state The flatmapped representation of state prior to this commit encoded maps and lists (and therefore by extension, sets) with a key corresponding to the number of elements, or the unknown variable indicator under a .# key and then individual items. For example, the list ["a", "b", "c"] would have been encoded as: listname.# = 3 listname.0 = "a" listname.1 = "b" listname.2 = "c" And the map {"key1": "value1", "key2", "value2"} would have been encoded as: mapname.# = 2 mapname.key1 = "value1" mapname.key2 = "value2" Sets use the hash code as the key - for example a set with a (fictional) hashcode calculation may look like: setname.# = 2 setname.12312512 = "value1" setname.56345233 = "value2" Prior to the work done to extend the type system, this was sufficient since the internal representation of these was effectively the same. However, following the separation of maps and lists into distinct first-class types, this encoding presents a problem: given a state file, it is impossible to tell the encoding of an empty list and an empty map apart. This presents problems for the type checker during interpolation, as many interpolation functions will operate on only one of these two structures. This commit therefore changes the representation in state of maps to use a "%" as the key for the number of elements. Consequently the map above will now be encoded as: mapname.% = 2 mapname.key1 = "value1" mapname.key2 = "value2" This has the effect of an empty list (or set) now being encoded as: listname.# = 0 And an empty map now being encoded as: mapname.% = 0 Therefore we can eliminate some nasty guessing logic from the resource variable supplier for interpolation, at the cost of having to migrate state up front (to follow in a subsequent commit). In order to reduce the number of potential situations in which resources would be "forced new", we continue to accept "#" as the count key when reading maps via helper/schema. There is no situation under which we can allow "#" as an actual map key in any case, as it would not be distinguishable from a list or set in state.
2016-06-05 10:34:43 +02:00
"map.%": "1",
"map.foo": "bar",
},
},
"map delete": {
[]string{"map"},
nil,
false,
map[string]string{
"map": "",
},
},
"map element": {
[]string{"map", "foo"},
"bar",
true,
map[string]string{},
},
"set": {
[]string{"set"},
[]interface{}{1, 2, 5},
false,
map[string]string{
"set.#": "3",
"set.1": "1",
"set.2": "2",
"set.5": "5",
},
},
2015-01-16 17:37:25 +01:00
"set nil": {
[]string{"set"},
nil,
false,
map[string]string{
"set.#": "0",
},
},
"set resource": {
[]string{"setDeep"},
[]interface{}{
map[string]interface{}{
"index": 10,
"value": "foo",
},
map[string]interface{}{
"index": 50,
"value": "bar",
},
},
false,
map[string]string{
"setDeep.#": "2",
"setDeep.10.index": "10",
"setDeep.10.value": "foo",
"setDeep.50.index": "50",
"setDeep.50.value": "bar",
},
},
"set element": {
[]string{"set", "5"},
5,
true,
map[string]string{},
},
2015-01-10 20:49:37 +01:00
"full object": {
nil,
map[string]interface{}{
"string": "foo",
"list": []interface{}{"foo", "bar"},
},
false,
map[string]string{
"string": "foo",
"list.#": "2",
"list.0": "foo",
"list.1": "bar",
},
},
}
for name, tc := range cases {
w := &MapFieldWriter{Schema: schema}
err := w.WriteField(tc.Addr, tc.Value)
2015-10-08 14:48:04 +02:00
if err != nil != tc.Err {
t.Fatalf("%s: err: %s", name, err)
}
actual := w.Map()
if !reflect.DeepEqual(actual, tc.Out) {
t.Fatalf("%s: bad: %#v", name, actual)
}
}
}
helper/schema: Clear existing map/set/list contents before overwriting There are situations where one may need to write to a set, list, or map more than once per single TF operation (apply/refresh/etc). In these cases, further writes using Set (example: d.Set("some_set", newSet)) currently create unstable results in the set writer (the name of the writer layer that holds the data set by these calls) because old keys are not being cleared out first. This bug is most visible when using sets. Example: First write to set writes elements that have been hashed at 10 and 20, and the second write writes elements that have been hashed at 30 and 40. While the set length has been correctly set at 2, since a set is basically a map (as is the entire map writer) and map results are non-deterministic, reads to this set will now deliver unstable results in a random but predictable fashion as the map results are delivered to the caller non-deterministic - sometimes you may correctly get 30 and 40, but sometimes you may get 10 and 20, or even 10 and 30, etc. This problem propagates to state which is even more damaging as unstable results are set to state where they become part of the permanent data set going forward. The problem also applies to lists and maps. This is probably more of an issue with maps as a map can contain any key/value combination and hence there is no predictable pattern where keys would be overwritten with default or zero values. This is contrary to complex lists, which has this problem as well, but since lists are deterministic and the length of a list properly gets updated during the overwrite, the problem is masked by the fact that a read will only read to the boundary of the list, skipping any bad data that may still be available due past the list boundary. This update clears the child contents of any set, list, or map before beginning a new write to address this issue. Tests are included for all three data types.
2017-11-05 20:14:51 +01:00
func TestMapFieldWriterCleanSet(t *testing.T) {
schema := map[string]*Schema{
"setDeep": &Schema{
Type: TypeSet,
Elem: &Resource{
Schema: map[string]*Schema{
"index": &Schema{Type: TypeInt},
"value": &Schema{Type: TypeString},
},
},
Set: func(a interface{}) int {
return a.(map[string]interface{})["index"].(int)
},
},
}
values := []struct {
Addr []string
Value interface{}
Out map[string]string
}{
{
[]string{"setDeep"},
[]interface{}{
map[string]interface{}{
"index": 10,
"value": "foo",
},
map[string]interface{}{
"index": 50,
"value": "bar",
},
},
map[string]string{
"setDeep.#": "2",
"setDeep.10.index": "10",
"setDeep.10.value": "foo",
"setDeep.50.index": "50",
"setDeep.50.value": "bar",
},
},
{
[]string{"setDeep"},
[]interface{}{
map[string]interface{}{
"index": 20,
"value": "baz",
},
map[string]interface{}{
"index": 60,
"value": "qux",
},
},
map[string]string{
"setDeep.#": "2",
"setDeep.20.index": "20",
"setDeep.20.value": "baz",
"setDeep.60.index": "60",
"setDeep.60.value": "qux",
},
},
{
[]string{"setDeep"},
[]interface{}{
map[string]interface{}{
"index": 30,
"value": "one",
},
map[string]interface{}{
"index": 70,
"value": "two",
},
},
map[string]string{
"setDeep.#": "2",
"setDeep.30.index": "30",
"setDeep.30.value": "one",
"setDeep.70.index": "70",
"setDeep.70.value": "two",
},
},
}
w := &MapFieldWriter{Schema: schema}
for n, tc := range values {
err := w.WriteField(tc.Addr, tc.Value)
if err != nil {
t.Fatalf("%d: err: %s", n, err)
}
actual := w.Map()
if !reflect.DeepEqual(actual, tc.Out) {
t.Fatalf("%d: bad: %#v", n, actual)
}
}
}
func TestMapFieldWriterCleanList(t *testing.T) {
schema := map[string]*Schema{
"listDeep": &Schema{
Type: TypeList,
Elem: &Resource{
Schema: map[string]*Schema{
"thing1": &Schema{Type: TypeString},
"thing2": &Schema{Type: TypeString},
},
},
},
}
values := []struct {
Addr []string
Value interface{}
Out map[string]string
}{
{
// Base list
[]string{"listDeep"},
[]interface{}{
map[string]interface{}{
"thing1": "a",
"thing2": "b",
},
map[string]interface{}{
"thing1": "c",
"thing2": "d",
},
map[string]interface{}{
"thing1": "e",
"thing2": "f",
},
map[string]interface{}{
"thing1": "g",
"thing2": "h",
},
},
map[string]string{
"listDeep.#": "4",
"listDeep.0.thing1": "a",
"listDeep.0.thing2": "b",
"listDeep.1.thing1": "c",
"listDeep.1.thing2": "d",
"listDeep.2.thing1": "e",
"listDeep.2.thing2": "f",
"listDeep.3.thing1": "g",
"listDeep.3.thing2": "h",
},
},
{
// Remove an element
[]string{"listDeep"},
[]interface{}{
map[string]interface{}{
"thing1": "a",
"thing2": "b",
},
map[string]interface{}{
"thing1": "c",
"thing2": "d",
},
map[string]interface{}{
"thing1": "e",
"thing2": "f",
},
},
map[string]string{
"listDeep.#": "3",
"listDeep.0.thing1": "a",
"listDeep.0.thing2": "b",
"listDeep.1.thing1": "c",
"listDeep.1.thing2": "d",
"listDeep.2.thing1": "e",
"listDeep.2.thing2": "f",
},
},
{
// Rewrite with missing keys. This should normally not be necessary, as
// hopefully the writers are writing zero values as necessary, but for
// brevity we want to make sure that what exists in the writer is exactly
// what the last write looked like coming from the provider.
[]string{"listDeep"},
[]interface{}{
map[string]interface{}{
"thing1": "a",
},
map[string]interface{}{
"thing1": "c",
},
map[string]interface{}{
"thing1": "e",
},
},
map[string]string{
"listDeep.#": "3",
"listDeep.0.thing1": "a",
"listDeep.1.thing1": "c",
"listDeep.2.thing1": "e",
},
},
}
w := &MapFieldWriter{Schema: schema}
for n, tc := range values {
err := w.WriteField(tc.Addr, tc.Value)
if err != nil {
t.Fatalf("%d: err: %s", n, err)
}
actual := w.Map()
if !reflect.DeepEqual(actual, tc.Out) {
t.Fatalf("%d: bad: %#v", n, actual)
}
}
}
func TestMapFieldWriterCleanMap(t *testing.T) {
schema := map[string]*Schema{
"map": &Schema{
Type: TypeMap,
},
}
values := []struct {
Value interface{}
Out map[string]string
}{
{
// Base map
map[string]interface{}{
"thing1": "a",
"thing2": "b",
"thing3": "c",
"thing4": "d",
},
map[string]string{
"map.%": "4",
"map.thing1": "a",
"map.thing2": "b",
"map.thing3": "c",
"map.thing4": "d",
},
},
{
// Base map
map[string]interface{}{
"thing1": "a",
"thing2": "b",
"thing4": "d",
},
map[string]string{
"map.%": "3",
"map.thing1": "a",
"map.thing2": "b",
"map.thing4": "d",
},
},
}
w := &MapFieldWriter{Schema: schema}
for n, tc := range values {
err := w.WriteField([]string{"map"}, tc.Value)
if err != nil {
t.Fatalf("%d: err: %s", n, err)
}
actual := w.Map()
if !reflect.DeepEqual(actual, tc.Out) {
t.Fatalf("%d: bad: %#v", n, actual)
}
}
}