helper/schema: set diff tests

This commit is contained in:
Mitchell Hashimoto 2014-08-20 21:02:42 -07:00
parent 475528adc3
commit 9ab5577beb
5 changed files with 141 additions and 9 deletions

View File

@ -153,13 +153,14 @@ func (d *ResourceData) getChange(
key string, key string,
oldLevel getSource, oldLevel getSource,
newLevel getSource) (interface{}, interface{}) { newLevel getSource) (interface{}, interface{}) {
var parts []string var parts, parts2 []string
if key != "" { if key != "" {
parts = strings.Split(key, ".") parts = strings.Split(key, ".")
parts2 = strings.Split(key, ".")
} }
o := d.getObject("", parts, d.schema, oldLevel) o := d.getObject("", parts, d.schema, oldLevel)
n := d.getObject("", parts, d.schema, newLevel) n := d.getObject("", parts2, d.schema, newLevel)
return o, n return o, n
} }
@ -191,14 +192,23 @@ func (d *ResourceData) getSet(
parts []string, parts []string,
schema *Schema, schema *Schema,
source getSource) interface{} { source getSource) interface{} {
s := &Set{F: schema.Set}
raw := d.getList(k, nil, schema, source) raw := d.getList(k, nil, schema, source)
if raw == nil { if raw == nil {
return nil if len(parts) > 0 {
return d.getList(k, parts, schema, source)
}
return s
} }
list := raw.([]interface{}) list := raw.([]interface{})
if len(list) == 0 { if len(list) == 0 {
return nil if len(parts) > 0 {
return d.getList(k, parts, schema, source)
}
return s
} }
// This is a reverse map of hash code => index in config used to // This is a reverse map of hash code => index in config used to
@ -219,7 +229,6 @@ func (d *ResourceData) getSet(
} }
// Build the set from all the items using the given hash code // Build the set from all the items using the given hash code
s := &Set{F: schema.Set}
for i, v := range list { for i, v := range list {
code := s.add(v) code := s.add(v)
if indexMap != nil { if indexMap != nil {
@ -413,11 +422,13 @@ func (d *ResourceData) getPrimitive(
if err := mapstructure.WeakDecode(v, &result); err != nil { if err := mapstructure.WeakDecode(v, &result); err != nil {
panic(err) panic(err)
} }
} else {
result = ""
}
resultSet = true resultSet = true
} else {
result = ""
resultSet = false
}
} }
if d.diff != nil && source >= getSourceDiff { if d.diff != nil && source >= getSourceDiff {

View File

@ -238,6 +238,12 @@ func (m schemaMap) diffList(
diff *terraform.ResourceDiff, diff *terraform.ResourceDiff,
d *ResourceData) error { d *ResourceData) error {
o, n, _ := d.diffChange(k) o, n, _ := d.diffChange(k)
if s, ok := o.(*Set); ok {
o = s.List()
}
if s, ok := n.(*Set); ok {
n = s.List()
}
os := o.([]interface{}) os := o.([]interface{})
vs := n.([]interface{}) vs := n.([]interface{})
@ -355,7 +361,7 @@ func (m schemaMap) diffSet(
schema *Schema, schema *Schema,
diff *terraform.ResourceDiff, diff *terraform.ResourceDiff,
d *ResourceData) error { d *ResourceData) error {
return nil return m.diffList(k, schema, diff, d)
} }
func (m schemaMap) diffString( func (m schemaMap) diffString(
@ -380,9 +386,15 @@ func (m schemaMap) diffString(
} }
} }
removed := false
if o != nil && n == nil {
removed = true
}
diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{ diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
Old: os, Old: os,
New: ns, New: ns,
NewRemoved: removed,
}) })
return nil return nil

View File

@ -365,6 +365,88 @@ func TestSchemaMap_Diff(t *testing.T) {
Err: false, Err: false,
}, },
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Required: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
State: &terraform.ResourceState{
Attributes: map[string]string{
"ports.#": "2",
"ports.0": "2",
"ports.1": "1",
},
},
Config: map[string]interface{}{
"ports": []interface{}{5, 2, 1},
},
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ports.#": &terraform.ResourceAttrDiff{
Old: "2",
New: "3",
},
"ports.2": &terraform.ResourceAttrDiff{
Old: "",
New: "5",
},
},
},
Err: false,
},
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Required: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
State: &terraform.ResourceState{
Attributes: map[string]string{
"ports.#": "2",
"ports.0": "2",
"ports.1": "1",
},
},
Config: map[string]interface{}{},
Diff: &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"ports.#": &terraform.ResourceAttrDiff{
Old: "2",
New: "0",
},
"ports.0": &terraform.ResourceAttrDiff{
Old: "1",
NewRemoved: true,
},
"ports.1": &terraform.ResourceAttrDiff{
Old: "2",
NewRemoved: true,
},
},
},
Err: false,
},
/* /*
* List of structure decode * List of structure decode
*/ */

View File

@ -19,6 +19,17 @@ func (s *Set) Add(item interface{}) {
s.add(item) s.add(item)
} }
// Contains checks if the set has the given item.
func (s *Set) Contains(item interface{}) bool {
_, ok := s.m[s.F(item)]
return ok
}
// Len returns the amount of items in the set.
func (s *Set) Len() int {
return len(s.m)
}
// List returns the elements of this set in slice format. // List returns the elements of this set in slice format.
// //
// The order of the returned elements is deterministic. Given the same // The order of the returned elements is deterministic. Given the same
@ -93,6 +104,10 @@ func (s *Set) add(item interface{}) int {
return code return code
} }
func (s *Set) index(item interface{}) int {
return sort.SearchInts(s.listCode(), s.F(item))
}
func (s *Set) listCode() []int { func (s *Set) listCode() []int {
// Sort the hash codes so the order of the list is deterministic // Sort the hash codes so the order of the list is deterministic
keys := make([]int, 0, len(s.m)) keys := make([]int, 0, len(s.m))

View File

@ -18,6 +18,18 @@ func TestSetAdd(t *testing.T) {
} }
} }
func TestSetContains(t *testing.T) {
s := &Set{F: testSetInt}
s.Add(5)
if s.Contains(2) {
t.Fatal("should not contain")
}
if !s.Contains(5) {
t.Fatal("should contain")
}
}
func TestSetDifference(t *testing.T) { func TestSetDifference(t *testing.T) {
s1 := &Set{F: testSetInt} s1 := &Set{F: testSetInt}
s2:= &Set{F: testSetInt} s2:= &Set{F: testSetInt}