Skip to content

Commit

Permalink
Merge pull request #19409 from hashicorp/jbardin/terraform-tests
Browse files Browse the repository at this point in the history
 fixes for the remaining tests
  • Loading branch information
jbardin authored Nov 20, 2018
2 parents 884aa38 + f375691 commit 407fcc0
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 7 deletions.
2 changes: 1 addition & 1 deletion helper/plugin/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func TestNormalizeFlatmapContainers(t *testing.T) {
expect: map[string]string{"id": "78629a0f5f3f164f"},
},
{
attrs: map[string]string{"set.2.required": "bar", "set.2.list.#": "1", "set.2.list.0": "x", "set.1.list.#": "0"},
attrs: map[string]string{"set.2.required": "bar", "set.2.list.#": "1", "set.2.list.0": "x", "set.1.list.#": "0", "set.#": "2"},
expect: map[string]string{"set.2.list.#": "1", "set.2.list.0": "x", "set.2.required": "bar", "set.#": "1"},
},
} {
Expand Down
7 changes: 6 additions & 1 deletion states/state_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,16 @@ func (m *Module) testString() string {
}
}
attrKeys := make([]string, 0, len(attributes))
for ak, _ := range attributes {
for ak, val := range attributes {
if ak == "id" {
continue
}

// don't show empty containers in the output
if val == "0" && (strings.HasSuffix(ak, ".#") || strings.HasSuffix(ak, ".%")) {
continue
}

attrKeys = append(attrKeys, ak)
}

Expand Down
103 changes: 99 additions & 4 deletions terraform/context_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,56 @@ func TestContext2Plan_computedList(t *testing.T) {
},
},
}
p.DiffFn = testDiffFn
p.DiffFn = func(info *InstanceInfo, s *InstanceState, c *ResourceConfig) (*InstanceDiff, error) {
diff := &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{},
}

computedKeys := map[string]bool{}
for _, k := range c.ComputedKeys {
computedKeys[k] = true
}

compute, _ := c.Raw["compute"].(string)
if compute != "" {
diff.Attributes[compute] = &ResourceAttrDiff{
Old: "",
New: "",
NewComputed: true,
}
}

fooOld := s.Attributes["foo"]
fooNew, _ := c.Raw["foo"].(string)
if fooOld != fooNew {
diff.Attributes["foo"] = &ResourceAttrDiff{
Old: fooOld,
New: fooNew,
NewComputed: computedKeys["foo"],
}
}

numOld := s.Attributes["num"]
numNew, _ := c.Raw["num"].(string)
if numOld != numNew {
diff.Attributes["num"] = &ResourceAttrDiff{
Old: numOld,
New: numNew,
NewComputed: computedKeys["num"],
}
}

listOld := s.Attributes["list.#"]
if listOld == "" {
diff.Attributes["list.#"] = &ResourceAttrDiff{
Old: "",
New: "",
NewComputed: true,
}
}

return diff, nil
}

ctx := testContext2(t, &ContextOpts{
Config: m,
Expand Down Expand Up @@ -2110,7 +2159,8 @@ func TestContext2Plan_computedList(t *testing.T) {
switch i := ric.Addr.String(); i {
case "aws_instance.bar":
checkVals(t, objectVal(t, schema, map[string]cty.Value{
"foo": cty.UnknownVal(cty.String),
"list": cty.UnknownVal(cty.List(cty.String)),
"foo": cty.UnknownVal(cty.String),
}), ric.After)
case "aws_instance.foo":
checkVals(t, objectVal(t, schema, map[string]cty.Value{
Expand All @@ -2129,6 +2179,7 @@ func TestContext2Plan_computedMultiIndex(t *testing.T) {
m := testModule(t, "plan-computed-multi-index")
p := testProvider("aws")
p.DiffFn = testDiffFn

p.GetSchemaReturn = &ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
"aws_instance": {
Expand All @@ -2141,6 +2192,47 @@ func TestContext2Plan_computedMultiIndex(t *testing.T) {
},
}

p.DiffFn = func(info *InstanceInfo, s *InstanceState, c *ResourceConfig) (*InstanceDiff, error) {
diff := &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{},
}

compute, _ := c.Raw["compute"].(string)
if compute != "" {
diff.Attributes[compute] = &ResourceAttrDiff{
Old: "",
New: "",
NewComputed: true,
}
}

fooOld := s.Attributes["foo"]
fooNew, _ := c.Raw["foo"].(string)
fooComputed := false
for _, k := range c.ComputedKeys {
if k == "foo" {
fooComputed = true
}
}
if fooNew != "" {
diff.Attributes["foo"] = &ResourceAttrDiff{
Old: fooOld,
New: fooNew,
NewComputed: fooComputed,
}
}

ipOld := s.Attributes["ip"]
ipComputed := ipOld == ""
diff.Attributes["ip"] = &ResourceAttrDiff{
Old: ipOld,
New: "",
NewComputed: ipComputed,
}

return diff, nil
}

ctx := testContext2(t, &ContextOpts{
Config: m,
ProviderResolver: providers.ResolverFixed(
Expand Down Expand Up @@ -2174,14 +2266,17 @@ func TestContext2Plan_computedMultiIndex(t *testing.T) {
switch i := ric.Addr.String(); i {
case "aws_instance.foo[0]":
checkVals(t, objectVal(t, schema, map[string]cty.Value{
"ip": cty.UnknownVal(cty.List(cty.String)),
"ip": cty.UnknownVal(cty.List(cty.String)),
"foo": cty.ListValEmpty(cty.String),
}), ric.After)
case "aws_instance.foo[1]":
checkVals(t, objectVal(t, schema, map[string]cty.Value{
"ip": cty.UnknownVal(cty.List(cty.String)),
"ip": cty.UnknownVal(cty.List(cty.String)),
"foo": cty.ListValEmpty(cty.String),
}), ric.After)
case "aws_instance.bar[0]":
checkVals(t, objectVal(t, schema, map[string]cty.Value{
"ip": cty.UnknownVal(cty.List(cty.String)),
"foo": cty.UnknownVal(cty.List(cty.String)),
}), ric.After)
default:
Expand Down
2 changes: 1 addition & 1 deletion terraform/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func (d *InstanceDiff) applyCollectionDiff(attrName string, oldAttrs map[string]
// check the index first for special handling
for k, diff := range d.Attributes {
// check the index value, which can be set, and 0
if k == attrName+".#" || k == attrName+".%" {
if k == attrName+".#" || k == attrName+".%" || k == attrName {
if diff.NewRemoved {
return result, nil
}
Expand Down
2 changes: 2 additions & 0 deletions terraform/provider_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
}
priorState := NewInstanceStateShimmedFromValue(r.PriorState, 0)
cfg := NewResourceConfigShimmed(r.Config, schema)

legacyDiff, err := p.DiffFn(info, priorState, cfg)

var res providers.PlanResourceChangeResponse
Expand All @@ -294,6 +295,7 @@ func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
if err != nil {
res.Diagnostics = res.Diagnostics.Append(err)
}

res.PlannedState = newVal

var requiresNew []string
Expand Down

0 comments on commit 407fcc0

Please sign in to comment.