-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathexamples_go_test.go
355 lines (319 loc) · 9.25 KB
/
examples_go_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
//go:build go || all
// +build go all
package examples
import (
"encoding/json"
"fmt"
"math/rand"
"path/filepath"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
appconfigsdk "github.com/aws/aws-sdk-go/service/appconfig"
s3sdk "github.com/aws/aws-sdk-go/service/s3"
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAccWebserverGo(t *testing.T) {
test := integration.ProgramTestOptions{
Dir: filepath.Join(getCwd(t), "webserver-go"),
Dependencies: []string{
"github.com/pulumi/pulumi-aws/sdk/v6",
},
Config: map[string]string{"aws:region": getEnvRegion(t)},
}
integration.ProgramTest(t, &test)
}
func TestTagsCombinationsGo(t *testing.T) {
type testCase struct {
name string
s1 tagsState
s2 tagsState
}
testCases := []testCase{
{
"maintain a simple tag",
tagsState{ResourceTags: map[string]string{"x": "s"}},
tagsState{ResourceTags: map[string]string{"x": "s"}},
},
{
"add a simple tag",
tagsState{},
tagsState{ResourceTags: map[string]string{"x": "s"}},
},
{
"add an empty tag",
tagsState{},
tagsState{ResourceTags: map[string]string{"x": ""}},
},
{
"remove an empty default tag",
tagsState{DefaultTags: map[string]string{"x": "", "y": ""}},
tagsState{DefaultTags: map[string]string{"x": ""}},
},
{
"replace tags with empty",
tagsState{
DefaultTags: map[string]string{"x": "s"},
ResourceTags: nil,
},
tagsState{
DefaultTags: map[string]string{"x": "", "y": "s"},
ResourceTags: map[string]string{"x": "", "y": ""},
},
},
{
"maintain tags",
tagsState{
DefaultTags: map[string]string{"x": "s", "y": "s"},
ResourceTags: map[string]string{"x": "s", "y": "s"},
},
tagsState{
DefaultTags: map[string]string{"x": "s", "y": "s"},
ResourceTags: map[string]string{"x": "s", "y": "s"},
},
},
{
"regress 1",
tagsState{DefaultTags: map[string]string{"x": "s"}, ResourceTags: map[string]string{"x": "", "y": ""}},
tagsState{DefaultTags: map[string]string{"x": "s"}, ResourceTags: map[string]string{"x": "", "y": ""}},
},
{
"regress 2",
tagsState{DefaultTags: map[string]string{}, ResourceTags: map[string]string{"x": "s", "y": ""}},
tagsState{DefaultTags: map[string]string{"x": ""}, ResourceTags: map[string]string{}},
},
{
"regress 3",
tagsState{DefaultTags: map[string]string{"x": "", "y": "s"}, ResourceTags: map[string]string{"x": "s", "y": "s"}},
tagsState{DefaultTags: map[string]string{}, ResourceTags: map[string]string{"x": "", "y": "s"}},
},
}
for i, tc := range testCases {
tc := tc
i := i
t.Run(tc.name, func(t *testing.T) {
tc.s1.validateTransitionTo(t, i, tc.s2)
})
}
}
func TestRandomTagsCombinationsGo(t *testing.T) {
tagValues := []string{"", "s"} // empty values are conflated with unknowns in TF internals, must test
tagsValues := []map[string]string{
nil,
{},
}
for _, tag := range tagValues {
m := map[string]string{"x": tag}
tagsValues = append(tagsValues, m)
}
for _, tag1 := range tagValues {
for _, tag2 := range tagValues {
m := map[string]string{
"x": tag1,
"y": tag2,
}
tagsValues = append(tagsValues, m)
}
}
states := []tagsState{}
for _, tags1 := range tagsValues {
for _, tags2 := range tagsValues {
states = append(states, tagsState{
DefaultTags: tags1,
ResourceTags: tags2,
})
}
}
t.Logf("total state space: %v states", len(states))
t.Logf("random-sampling 100 state transitions")
for i := 0; i < 100; i++ {
i := i
t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
i := rand.Intn(len(states))
j := rand.Intn(len(states))
state1, state2 := states[i], states[j]
state1.validateTransitionTo(t, i, state2)
})
}
}
type tagsState struct {
DefaultTags map[string]string `json:"defaultTags"`
ResourceTags map[string]string `json:"resourceTags"`
}
func (st tagsState) serialize(t *testing.T) string {
bytes, err := json.Marshal(st)
require.NoError(t, err)
return string(bytes)
}
func (st tagsState) validateTransitionTo(t *testing.T, testIdent int, st2 tagsState) {
t.Logf("state1 = %v", st.serialize(t))
t.Logf("state2 = %v", st2.serialize(t))
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: "tags-combinations-go",
ExtraRuntimeValidation: st.validateStateResult(1),
EditDirs: []integration.EditDir{{
Dir: filepath.Join("tags-combinations-go", "step1"),
Additive: true,
ExtraRuntimeValidation: st2.validateStateResult(2),
}},
Config: map[string]string{
"aws:region": getEnvRegion(t),
"state1": st.serialize(t),
"state2": st2.serialize(t),
"testIdent": fmt.Sprintf("test%d", testIdent),
},
Quick: true,
})
}
func (st tagsState) expectedTags() map[string]string {
r := map[string]string{}
for k, v := range st.DefaultTags {
r[k] = v
}
for k, v := range st.ResourceTags {
r[k] = v
}
return r
}
func normTags(tags map[string]string) map[string]string {
if tags == nil {
return map[string]string{}
}
return tags
}
type tagsFetcher = func() (map[string]string, error)
func (st tagsState) assertTagsEqualWithRetry(
t *testing.T,
getActualTags tagsFetcher,
msg string,
) {
expectTags := st.expectedTags()
var actualTags map[string]string
for attempt := 0; attempt < 10; attempt++ {
var err error = nil
actualTags, err = getActualTags()
if err == nil && assert.ObjectsAreEqual(normTags(expectTags), normTags(actualTags)) {
break
}
if err != nil {
t.Logf("Ignoring error: %v", err)
} else {
t.Logf("Ignoring incorrect tags: %#v -- expecting #%v", actualTags, expectTags)
}
t.Logf("Failed to fetch tags, will attempt again in 5s")
time.Sleep(5 * time.Second)
t.Logf("Trying to fetch tags again, attempt %d", attempt+1)
}
require.Equalf(t, normTags(expectTags), normTags(actualTags), msg)
}
func (st tagsState) validateStateResult(phase int) func(
t *testing.T,
stack integration.RuntimeValidationStackInfo,
) {
return func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
for k, v := range stack.Outputs {
switch k {
case "bucket-name", "legacy-bucket-name", "appconfig-app-arn", "appconfig-env-arn", "get-appconfig-env":
continue
}
actualTagsJSON := v.(string)
var actualTags map[string]string
err := json.Unmarshal([]byte(actualTagsJSON), &actualTags)
require.NoError(t, err)
t.Logf("phase: %d", phase)
t.Logf("state: %v", st.serialize(t))
require.Equalf(t, normTags(st.expectedTags()), normTags(actualTags), "key=%s", k)
t.Logf("key=%s tags are as expected: %v", k, actualTagsJSON)
if k == "bucket" {
// TODO: uncomment when https://github.com/pulumi/pulumi-aws/issues/4258 is fixed
// getTags := stack.Outputs["get-bucket"].(string)
// assert.Equal(t, v.(string), getTags)
bucketName := stack.Outputs["bucket-name"].(string)
st.assertTagsEqualWithRetry(t,
fetchBucketTags(bucketName),
"bad bucket tags")
}
if k == "legacy-bucket" {
// TODO: uncomment when https://github.com/pulumi/pulumi-aws/issues/4258 is fixed
// getTags := stack.Outputs["get-legacy-bucket"].(string)
// assert.Equal(t, v.(string), getTags)
bucketName := stack.Outputs["legacy-bucket-name"].(string)
st.assertTagsEqualWithRetry(t,
fetchBucketTags(bucketName),
"bad legacy bucket tags")
}
if k == "appconfig-app" {
arn := stack.Outputs["appconfig-app-arn"].(string)
st.assertTagsEqualWithRetry(t,
fetchAppConfigTags(arn),
"bad appconfig app tags")
}
if k == "appconfig-env" {
getTags := stack.Outputs["get-appconfig-env"].(string)
isEqual(t, v.(string), getTags)
arn := stack.Outputs["appconfig-env-arn"].(string)
st.assertTagsEqualWithRetry(t,
fetchAppConfigTags(arn),
"bad appconfig app tags")
}
}
}
}
func isEqual(t *testing.T, a, b string) {
if a == "null" {
a = "{}"
}
if b == "null" {
b = "{}"
}
assert.Equal(t, a, b)
}
func fetchBucketTags(awsBucket string) tagsFetcher {
return func() (map[string]string, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
retries := 5
client := s3sdk.New(sess, &aws.Config{MaxRetries: &retries})
input := &s3sdk.GetBucketTaggingInput{
Bucket: &awsBucket,
}
result, err := client.GetBucketTagging(input)
if err != nil && strings.Contains(err.Error(), "NoSuchTagSet") {
return map[string]string{}, nil
}
if err != nil {
return nil, err
}
tags := make(map[string]string)
for _, tag := range result.TagSet {
tags[*tag.Key] = *tag.Value
}
return tags, nil
}
}
func fetchAppConfigTags(arn string) tagsFetcher {
return func() (map[string]string, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
retries := 5
client := appconfigsdk.New(sess, &aws.Config{MaxRetries: &retries})
out, err := client.ListTagsForResource(&appconfigsdk.ListTagsForResourceInput{
ResourceArn: &arn,
})
if err != nil {
return nil, err
}
res := map[string]string{}
for k, v := range out.Tags {
res[k] = *v
}
return res, nil
}
}