forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
316 lines (303 loc) · 11.6 KB
/
examples_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
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package test
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"testing"
"time"
"github.com/pulumi/pulumi/pkg/testing/integration"
"github.com/stretchr/testify/assert"
)
func TestExamples(t *testing.T) {
awsRegion := os.Getenv("AWS_REGION")
if awsRegion == "" {
awsRegion = "us-west-1"
fmt.Println("Defaulting AWS_REGION to 'us-west-1'. You can override using the AWS_REGION environment variable")
}
azureEnviron := os.Getenv("ARM_ENVIRONMENT")
if azureEnviron == "" {
azureEnviron = "public"
fmt.Println("Defaulting ARM_ENVIRONMENT to 'public'. You can override using the ARM_ENVIRONMENT variable")
}
cwd, err := os.Getwd()
if !assert.NoError(t, err, "expected a valid working directory: %v", err) {
return
}
base := integration.ProgramTestOptions{
Tracing: "https://tracing.pulumi-engineering.com/collector/api/v1/spans",
}
examples := []integration.ProgramTestOptions{
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "aws-js-s3-folder"),
SkipBuild: true,
Config: map[string]string{
"aws:region": awsRegion,
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPResult(t, "http:https://"+stack.Outputs["websiteUrl"].(string), func(body string) bool {
return assert.Contains(t, body, "Hello, Pulumi!")
})
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "aws-js-s3-folder-component"),
SkipBuild: true,
Config: map[string]string{
"aws:region": awsRegion,
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPResult(t, stack.Outputs["websiteUrl"].(string), func(body string) bool {
return assert.Contains(t, body, "Hello, Pulumi!")
})
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "aws-js-webserver"),
SkipBuild: true,
Config: map[string]string{
"aws:region": awsRegion,
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPHelloWorld(t, stack.Outputs["publicHostName"])
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "aws-js-webserver-component"),
SkipBuild: true,
Config: map[string]string{
"aws:region": awsRegion,
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPHelloWorld(t, stack.Outputs["webUrl"])
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "azure-js-webserver"),
SkipBuild: true,
Config: map[string]string{
"azure:environment": azureEnviron,
"username": "testuser",
"password": "testTEST1234+-*/",
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPHelloWorld(t, stack.Outputs["publicIP"])
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "azure-ts-functions"),
SkipBuild: true,
Config: map[string]string{
"azure:environment": azureEnviron,
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPResult(t, stack.Outputs["endpoint"], func(body string) bool {
return assert.Contains(t, body, "Greetings from Azure Functions!")
})
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "azure-ts-appservice"),
SkipBuild: true,
Config: map[string]string{
"azure:environment": azureEnviron,
"sqlPassword": "2@Password@2",
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPResult(t, stack.Outputs["endpoint"], func(body string) bool {
return assert.Contains(t, body, "Greetings from Azure App Service!")
})
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "aws-ts-ruby-on-rails"),
SkipBuild: true,
Config: map[string]string{
"aws:region": awsRegion,
"dbUser": "testUser",
"dbPassword": "2@Password@2",
"dbRootPassword": "2@Password@2",
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
// Due to setup time on the vm this output does not show up for several minutes so
// increase wait time a bit
maxWait := 10 * time.Minute
assertHTTPResultWithRetry(t, stack.Outputs["websiteURL"], maxWait, func(body string) bool {
return assert.Contains(t, body, "New Note")
})
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "cloud-js-api"),
SkipBuild: true,
Config: map[string]string{
"aws:region": awsRegion,
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPResult(t, stack.Outputs["endpoint"].(string)+"/hello", func(body string) bool {
return assert.Contains(t, body, "{\"route\":\"hello\",\"count\":1}")
})
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "cloud-js-containers"),
SkipBuild: true,
Config: map[string]string{
// use us-west-2 to assure fargate
"aws:region": "us-west-2",
"cloud-aws:useFargate": "true",
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPResult(t, stack.Outputs["hostname"], func(body string) bool {
return assert.Contains(t, body, "<p>Hello, containers!</p>")
})
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "cloud-js-httpserver"),
SkipBuild: true,
Config: map[string]string{
"cloud:provider": "aws",
"aws:region": awsRegion,
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPResult(t, stack.Outputs["endpoint"].(string)+"/hello", func(body string) bool {
return assert.Contains(t, body, "{\"route\":\"/hello\",\"count\":1}")
})
},
}),
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "cloud-ts-url-shortener-cache-http"),
SkipBuild: true,
Config: map[string]string{
// use us-west-2 to assure fargate
"aws:region": "us-west-2",
"url-shortener-cache:redisPassword": "s3cr7Password",
"cloud:provider": "aws",
"cloud-aws:useFargate": "true",
},
// TODO: This test is not returning a valid payload see issue: https://github.com/pulumi/examples/issues/155
// ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
// assertHTTPResult(t, stack.Outputs["endpointUrl"], func(body string) bool {
// return assert.Contains(t, body, "<title>Short URL Manager</title>")
// })
// },
}),
// TODO: This test fails due to a bug in the Terraform Azure provider in which the
// service principal is not available when attempting to create the K8s cluster.
// See the azure-ts-aks-example readme for more detail.
// base.With(integration.ProgramTestOptions{
// Dir: path.Join(cwd, "..", "..", "azure-ts-aks-mean"),
// SkipBuild: true,
// Config: map[string]string{
// "azure:environment": azureEnviron,
// "password": "testTEST1234+_^$",
// "sshPublicKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDeREOgHTUgPT00PTr7iQF9JwZQ4QF1VeaLk2nHKRvWYOCiky6hDtzhmLM0k0Ib9Y7cwFbhObR+8yZpCgfSX3Hc3w2I1n6lXFpMfzr+wdbpx97N4fc1EHGUr9qT3UM1COqN6e/BEosQcMVaXSCpjqL1jeNaRDAnAS2Y3q1MFeXAvj9rwq8EHTqqAc1hW9Lq4SjSiA98STil5dGw6DWRhNtf6zs4UBy8UipKsmuXtclR0gKnoEP83ahMJOpCIjuknPZhb+HsiNjFWf+Os9U6kaS5vGrbXC8nggrVE57ow88pLCBL+3mBk1vBg6bJuLBCp2WTqRzDMhSDQ3AcWqkucGqf dremy@remthinkpad",
// },
// ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
// assertHTTPResult(t, stack.Outputs["endpoint"], func(body string) bool {
// return assert.Contains(t, body, "<title>Node/Angular Todo App</title>>")
// })
// },
// }),
// TODO[pulumi/pulumi#1606] This test is failing in CI, disabling until this issue is resolved.
// base.With(integration.ProgramTestOptions{
// Dir: path.Join(cwd, "..", "..", "aws-py-webserver"),
// Verbose: true,
// DebugLogLevel: 8,
// DebugUpdates: true,
// SkipBuild: true,
// Config: map[string]string{
// "aws:region": awsRegion,
// },
// ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
// assertHTTPHelloWorld(t, stack.Outputs["public_dns"])
// },
// }),
}
longExamples := []integration.ProgramTestOptions{
base.With(integration.ProgramTestOptions{
Dir: path.Join(cwd, "..", "..", "azure-ts-aks-helm"),
SkipBuild: true,
Config: map[string]string{
"azure:environment": azureEnviron,
"password": "testTEST1234+_^$",
"sshPublicKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDeREOgHTUgPT00PTr7iQF9JwZQ4QF1VeaLk2nHKRvWYOCiky6hDtzhmLM0k0Ib9Y7cwFbhObR+8yZpCgfSX3Hc3w2I1n6lXFpMfzr+wdbpx97N4fc1EHGUr9qT3UM1COqN6e/BEosQcMVaXSCpjqL1jeNaRDAnAS2Y3q1MFeXAvj9rwq8EHTqqAc1hW9Lq4SjSiA98STil5dGw6DWRhNtf6zs4UBy8UipKsmuXtclR0gKnoEP83ahMJOpCIjuknPZhb+HsiNjFWf+Os9U6kaS5vGrbXC8nggrVE57ow88pLCBL+3mBk1vBg6bJuLBCp2WTqRzDMhSDQ3AcWqkucGqf dremy@remthinkpad",
},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
assertHTTPResult(t, stack.Outputs["serviceIP"], func(body string) bool {
return assert.Contains(t, body, "It works!")
})
},
}),
}
// Only include the long examples on non-Short test runs
if !testing.Short() {
fmt.Println("Running full test suite including longer tests. To run only shorter tests run with the -short option.")
examples = append(examples, longExamples...)
} else {
fmt.Println("Running short test suite. To run full test suite run without -short option")
}
for _, ex := range examples {
example := ex
t.Run(example.Dir, func(t *testing.T) {
integration.ProgramTest(t, &example)
})
}
}
func assertHTTPResult(t *testing.T, output interface{}, check func(string) bool) bool {
return assertHTTPResultWithRetry(t, output, 3*time.Minute, check)
}
func assertHTTPResultWithRetry(t *testing.T, output interface{}, maxWait time.Duration, check func(string) bool) bool {
hostname, ok := output.(string)
if !assert.True(t, ok, fmt.Sprintf("expected `%s` output", output)) {
return false
}
if !(strings.HasPrefix(hostname, "http:https://") || strings.HasPrefix(hostname, "https://")) {
hostname = fmt.Sprintf("http:https://%s", hostname)
}
var err error
var resp *http.Response
startTime := time.Now()
count, sleep := 0, 0
for true {
now := time.Now()
resp, err = http.Get(hostname)
if err == nil {
break
}
if now.Sub(startTime) >= maxWait {
fmt.Printf("Timeout after %v. Unable to http.get %v successfully.", maxWait, hostname)
break
}
count++
// delay 10s, 20s, then 30s and stay at 30s
if sleep > 30 {
sleep = 30
} else {
sleep += 10
}
time.Sleep(time.Duration(sleep) * time.Second)
fmt.Printf("Http Error: %v\n", err)
fmt.Printf(" Retry: %v, elapsed wait: %v, max wait %v\n", count, now.Sub(startTime), maxWait)
}
if !assert.NoError(t, err) {
return false
}
// Read the body
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if !assert.NoError(t, err) {
return false
}
// Verify it matches expectations
return check(string(body))
}
func assertHTTPHelloWorld(t *testing.T, output interface{}) bool {
return assertHTTPResult(t, output, func(s string) bool {
return assert.Equal(t, "Hello, World!\n", s)
})
}