Skip to content

Commit 88c3554

Browse files
authored
Merge pull request hashicorp#9096 from hashicorp/f-resource-config
ResourceConfig.Equal, DeepCopy
2 parents 2b577ea + ea342b7 commit 88c3554

7 files changed

Lines changed: 122 additions & 25 deletions

File tree

config/interpolate_walk_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,15 @@ func TestInterpolationWalker_replace(t *testing.T) {
179179
return tc.Value, nil
180180
}
181181

182-
w := &interpolationWalker{F: fn, Replace: true}
183-
if err := reflectwalk.Walk(tc.Input, w); err != nil {
184-
t.Fatalf("err: %s", err)
185-
}
186-
187-
if !reflect.DeepEqual(tc.Input, tc.Output) {
188-
t.Fatalf("%d: bad:\n\nexpected:%#v\ngot:%#v", i, tc.Output, tc.Input)
189-
}
182+
t.Run(fmt.Sprintf("walk-%d", i), func(t *testing.T) {
183+
w := &interpolationWalker{F: fn, Replace: true}
184+
if err := reflectwalk.Walk(tc.Input, w); err != nil {
185+
t.Fatalf("err: %s", err)
186+
}
187+
188+
if !reflect.DeepEqual(tc.Input, tc.Output) {
189+
t.Fatalf("%d: bad:\n\nexpected:%#v\ngot:%#v", i, tc.Output, tc.Input)
190+
}
191+
})
190192
}
191193
}

terraform/resource.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/hashicorp/terraform/config"
10+
"github.com/mitchellh/copystructure"
1011
)
1112

1213
// ResourceProvisionerConfig is used to pair a provisioner
@@ -93,6 +94,45 @@ func NewResourceConfig(c *config.RawConfig) *ResourceConfig {
9394
return result
9495
}
9596

97+
// DeepCopy performs a deep copy of the configuration. This makes it safe
98+
// to modify any of the structures that are part of the resource config without
99+
// affecting the original configuration.
100+
func (c *ResourceConfig) DeepCopy() *ResourceConfig {
101+
// Copy, this will copy all the exported attributes
102+
copy, err := copystructure.Config{Lock: true}.Copy(c)
103+
if err != nil {
104+
panic(err)
105+
}
106+
107+
// Force the type
108+
result := copy.(*ResourceConfig)
109+
110+
// For the raw configuration, we can just use its own copy method
111+
result.raw = c.raw.Copy()
112+
113+
return result
114+
}
115+
116+
// Equal checks the equality of two resource configs.
117+
func (c *ResourceConfig) Equal(c2 *ResourceConfig) bool {
118+
// Two resource configs if their exported properties are equal.
119+
// We don't compare "raw" because it is never used again after
120+
// initialization and for all intents and purposes they are equal
121+
// if the exported properties are equal.
122+
check := [][2]interface{}{
123+
{c.ComputedKeys, c2.ComputedKeys},
124+
{c.Raw, c2.Raw},
125+
{c.Config, c2.Config},
126+
}
127+
for _, pair := range check {
128+
if !reflect.DeepEqual(pair[0], pair[1]) {
129+
return false
130+
}
131+
}
132+
133+
return true
134+
}
135+
96136
// CheckSet checks that the given list of configuration keys is
97137
// properly set. If not, errors are returned for each unset key.
98138
//

terraform/resource_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package terraform
22

33
import (
4+
"fmt"
45
"reflect"
56
"testing"
67

@@ -208,10 +209,33 @@ func TestResourceConfigGet(t *testing.T) {
208209
rc := NewResourceConfig(rawC)
209210
rc.interpolateForce()
210211

211-
v, _ := rc.Get(tc.Key)
212-
if !reflect.DeepEqual(v, tc.Value) {
213-
t.Fatalf("%d bad: %#v", i, v)
212+
// Test getting a key
213+
t.Run(fmt.Sprintf("get-%d", i), func(t *testing.T) {
214+
v, _ := rc.Get(tc.Key)
215+
if !reflect.DeepEqual(v, tc.Value) {
216+
t.Fatalf("%d bad: %#v", i, v)
217+
}
218+
})
219+
220+
// If we have vars, we don't test copying
221+
if len(tc.Vars) > 0 {
222+
continue
214223
}
224+
225+
// Test copying and equality
226+
t.Run(fmt.Sprintf("copy-and-equal-%d", i), func(t *testing.T) {
227+
copy := rc.DeepCopy()
228+
if !reflect.DeepEqual(copy, rc) {
229+
t.Fatalf("bad:\n\n%#v\n\n%#v", copy, rc)
230+
}
231+
232+
if !copy.Equal(rc) {
233+
t.Fatalf("copy != rc:\n\n%#v\n\n%#v", copy, rc)
234+
}
235+
if !rc.Equal(copy) {
236+
t.Fatalf("rc != copy:\n\n%#v\n\n%#v", copy, rc)
237+
}
238+
})
215239
}
216240
}
217241

terraform/state_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package terraform
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"reflect"
78
"strings"
89
"testing"
@@ -272,11 +273,13 @@ func TestStateDeepCopy(t *testing.T) {
272273
}
273274

274275
for i, tc := range cases {
275-
actual := tc.F(tc.One.DeepCopy())
276-
expected := tc.F(tc.Two)
277-
if !reflect.DeepEqual(actual, expected) {
278-
t.Fatalf("Bad: %d\n\n%s\n\n%s", i, actual, expected)
279-
}
276+
t.Run(fmt.Sprintf("copy-%d", i), func(t *testing.T) {
277+
actual := tc.F(tc.One.DeepCopy())
278+
expected := tc.F(tc.Two)
279+
if !reflect.DeepEqual(actual, expected) {
280+
t.Fatalf("Bad: %d\n\n%s\n\n%s", i, actual, expected)
281+
}
282+
})
280283
}
281284
}
282285

vendor/github.com/mitchellh/copystructure/copystructure.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/mitchellh/reflectwalk/reflectwalk.go

Lines changed: 19 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/vendor.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,10 +1472,10 @@
14721472
"revision": "8631ce90f28644f54aeedcb3e389a85174e067d1"
14731473
},
14741474
{
1475-
"checksumSHA1": "Vfkp+PcZ1wZ4+D6AsHTpKkdsQG0=",
1475+
"checksumSHA1": "EDAtec3XSbTjw6gWG+NNScows9M=",
14761476
"path": "github.com/mitchellh/copystructure",
1477-
"revision": "501dcbdc7c358c4d0bfa066018834bedca79fde3",
1478-
"revisionTime": "2016-09-16T19:51:24Z"
1477+
"revision": "49a4444999946bce1882f9db0eb3ba0a44ed1fbb",
1478+
"revisionTime": "2016-09-28T02:49:35Z"
14791479
},
14801480
{
14811481
"path": "github.com/mitchellh/go-homedir",
@@ -1507,8 +1507,10 @@
15071507
"revision": "6e6954073784f7ee67b28f2d22749d6479151ed7"
15081508
},
15091509
{
1510+
"checksumSHA1": "kXh6sdGViiRK0REpIWydJvpsyY0=",
15101511
"path": "github.com/mitchellh/reflectwalk",
1511-
"revision": "eecf4c70c626c7cfbb95c90195bc34d386c74ac6"
1512+
"revision": "0c9480f65513be815a88d6076a3d8d95d4274236",
1513+
"revisionTime": "2016-09-28T02:49:03Z"
15121514
},
15131515
{
15141516
"checksumSHA1": "/iig5lYSPCL3C8J7e4nTAevYNDE=",

0 commit comments

Comments
 (0)