-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpr_test.go
More file actions
450 lines (389 loc) · 17.2 KB
/
pr_test.go
File metadata and controls
450 lines (389 loc) · 17.2 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package test
import (
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/gruntwork-io/terratest/modules/files"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/cloudinfo"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testaddons"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testschematic"
)
/*
Global variables
*/
const basicExampleTerraformDir = "examples/basic"
const vpcFlowLogsExampleTerraformDir = "examples/vpc-flow-logs"
const landingZoneExampleTerraformDir = "examples/landing_zone"
const hubAndSpokeDelegatedExampleTerraformDir = "examples/hub-spoke-delegated-resolver"
const existingVPCExampleTerraformDir = "examples/existing_vpc"
const specificZoneExampleTerraformDir = "examples/specific-zone-only"
const vpcWithDnsExampleTerraformDir = "examples/vpc-with-dns"
const multipleSGExampleTerraformDir = "examples/multiple-sg-protocols"
const fullyConfigFlavorDir = "solutions/fully-configurable"
const quickStartFlavorDir = "solutions/quickstart"
const resourceGroup = "geretain-test-resources"
const yamlLocation = "../common-dev-assets/common-go-assets/common-permanent-resources.yaml"
const terraformVersion = "terraform_v1.12.2" // This should match the version in the ibm_catalog.json
var permanentResources map[string]interface{}
// To verify DNS records creation
var dnsRecordsMap = map[string][]map[string]interface{}{
"slz.com": {
{"name": "testA", "type": "A", "rdata": "1.2.3.4", "ttl": 3600},
{"name": "testAAAA", "type": "AAAA", "rdata": "2001:0db8:0012:0001:3c5e:7354:0000:5db5"},
{"name": "testCNAME", "type": "CNAME", "rdata": "test.com"},
{"name": "testTXT", "type": "TXT", "rdata": "textinformation", "ttl": 900},
{"name": "testMX", "type": "MX", "rdata": "mailserver.test.com", "preference": 10},
{"name": "testSRV", "type": "SRV", "rdata": "tester.com", "priority": 100, "weight": 100, "port": 8000, "service": "_sip", "protocol": "udp"},
}}
// To verify DNS zone creation
var dnsZoneMap = []map[string]interface{}{
{"name": "slz.com"},
}
var IgnoreUpdates = []string{
"module.slz_vpc.terraform_data.deprecation_warning",
"module.vpc.terraform_data.deprecation_warning[0]",
}
var IgnoreDestroys = []string{
"module.slz_vpc.terraform_data.deprecation_warning",
"module.vpc.terraform_data.deprecation_warning[0]",
}
func TestMain(m *testing.M) {
// Read the YAML file contents
var err error
permanentResources, err = common.LoadMapFromYaml(yamlLocation)
if err != nil {
log.Fatal(err)
}
os.Exit(m.Run())
}
func setupOptions(t *testing.T, prefix string, terraformDir string) *testhelper.TestOptions {
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
Testing: t,
TerraformDir: terraformDir,
Prefix: prefix,
ResourceGroup: resourceGroup,
TerraformVars: map[string]interface{}{
"access_tags": permanentResources["accessTags"],
},
IgnoreUpdates: testhelper.Exemptions{ // Ignore for consistency check
List: IgnoreUpdates,
},
IgnoreDestroys: testhelper.Exemptions{ // Ignore for consistency check
List: IgnoreDestroys,
},
})
return options
}
func TestRunVPCFlowLogsExample(t *testing.T) {
t.Parallel()
options := setupOptions(t, "slz-vpc", vpcFlowLogsExampleTerraformDir)
output, err := options.RunTestConsistency()
assert.Nil(t, err, "This should not have errored")
assert.NotNil(t, output, "Expected some output")
expectedOutputs := []string{"vpc_data"}
missingOutputs, outputErr := testhelper.ValidateTerraformOutputs(options.LastTestTerraformOutputs, expectedOutputs...)
assert.Empty(t, outputErr, fmt.Sprintf("Missing expected outputs: %s", missingOutputs))
}
func TestRunLandingZoneExample(t *testing.T) {
t.Parallel()
options := setupOptions(t, "slz", landingZoneExampleTerraformDir)
output, err := options.RunTestConsistency()
assert.Nil(t, err, "This should not have errored")
assert.NotNil(t, output, "Expected some output")
}
// Test the fully-configurable DA with defaults (no flow logs)
func TestFullyConfigurable(t *testing.T) {
t.Parallel()
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
Testing: t,
Region: "eu-de",
Prefix: "vpc-da",
TarIncludePatterns: []string{
"*.tf",
"dynamic_values/*.tf",
"dynamic_values/config_modules/*/*.tf",
fullyConfigFlavorDir + "/*.tf",
},
TemplateFolder: fullyConfigFlavorDir,
Tags: []string{"vpc-da-test"},
DeleteWorkspaceOnFail: false,
WaitJobCompleteMinutes: 120,
TerraformVersion: terraformVersion,
})
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
{Name: "existing_resource_group_name", Value: resourceGroup, DataType: "string"},
{Name: "region", Value: options.Region, DataType: "string"},
{Name: "resource_tags", Value: options.Tags, DataType: "list(string)"},
{Name: "access_tags", Value: permanentResources["accessTags"], DataType: "list(string)"},
{Name: "prefix", Value: options.Prefix, DataType: "string"},
}
err := options.RunSchematicTest()
assert.Nil(t, err, "This should not have errored")
}
func setupQuickstartSchematicsOptions(t *testing.T, prefix string) *testschematic.TestSchematicOptions {
t.Helper()
t.Parallel()
// Verify ibmcloud_api_key variable is set
checkVariable := "TF_VAR_ibmcloud_api_key"
val, present := os.LookupEnv(checkVariable)
require.True(t, present, checkVariable+" environment variable not set")
require.NotEqual(t, "", val, checkVariable+" environment variable is empty")
// Programmatically determine region to use based on availability
region, _ := testhelper.GetBestVpcRegion(
val,
"../common-dev-assets/common-go-assets/cloudinfo-region-vpc-gen2-prefs.yaml",
"eu-de",
)
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
Testing: t,
Region: region,
Prefix: prefix,
TarIncludePatterns: []string{
"*.tf",
"dynamic_values/*.tf",
"dynamic_values/config_modules/*/*.tf",
quickStartFlavorDir + "/*.tf",
},
TemplateFolder: quickStartFlavorDir,
Tags: []string{"vpc-qs-test"},
DeleteWorkspaceOnFail: false,
WaitJobCompleteMinutes: 120,
TerraformVersion: terraformVersion,
})
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
{
Name: "ibmcloud_api_key",
Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"],
DataType: "string",
Secure: true,
},
{Name: "existing_resource_group_name", Value: resourceGroup, DataType: "string"},
{Name: "region", Value: options.Region, DataType: "string"},
{Name: "resource_tags", Value: options.Tags, DataType: "list(string)"},
{Name: "access_tags", Value: permanentResources["accessTags"], DataType: "list(string)"},
{Name: "prefix", Value: options.Prefix, DataType: "string"},
}
return options
}
func TestQuickstartDefaultConfigSchematics(t *testing.T) {
options := setupQuickstartSchematicsOptions(t, "vpc-qs")
err := options.RunSchematicTest()
assert.Nil(t, err, "This should not have errored")
}
func TestQuickstartDefaultConfigUpgradeSchematics(t *testing.T) {
options := setupQuickstartSchematicsOptions(t, "vpc-qs-upg")
err := options.RunSchematicUpgradeTest()
if !options.UpgradeTestSkipped {
assert.Nil(t, err, "This should not have errored")
}
}
func validateEnvVariable(t *testing.T, varName string) string {
val, present := os.LookupEnv(varName)
require.True(t, present, "%s environment variable not set", varName)
require.NotEqual(t, "", val, "%s environment variable is empty", varName)
return val
}
func setupTerraform(t *testing.T, prefix, realTerraformDir string) *terraform.Options {
tempTerraformDir, err := files.CopyTerraformFolderToTemp(realTerraformDir, prefix)
require.NoError(t, err, "Failed to create temporary Terraform folder")
apiKey := validateEnvVariable(t, "TF_VAR_ibmcloud_api_key") // pragma: allowlist secret
region, _ := testhelper.GetBestVpcRegion(apiKey, "../common-dev-assets/common-go-assets/cloudinfo-region-vpc-gen2-prefs.yaml", "eu-de")
existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: tempTerraformDir,
Vars: map[string]interface{}{
"prefix": prefix,
"region": region,
"create_vpc": false,
"create_db": true,
},
// Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
// This is the same as setting the -upgrade=true flag with terraform.
Upgrade: true,
})
terraform.WorkspaceSelectOrNew(t, existingTerraformOptions, prefix)
_, err = terraform.InitAndApplyE(t, existingTerraformOptions)
require.NoError(t, err, "Init and Apply of temp existing resource failed")
return existingTerraformOptions
}
func cleanupTerraform(t *testing.T, options *terraform.Options, prefix string) {
if t.Failed() && strings.ToLower(os.Getenv("DO_NOT_DESTROY_ON_FAILURE")) == "true" {
fmt.Println("Terratest failed. Debug the test and delete resources manually.")
return
}
logger.Log(t, "START: Destroy (existing resources)")
terraform.Destroy(t, options)
terraform.WorkspaceDelete(t, options, prefix)
logger.Log(t, "END: Destroy (existing resources)")
}
func TestFullyConfigurableWithFlowLogs(t *testing.T) {
t.Parallel()
// Provision resources first
// Verify ibmcloud_api_key variable is set
checkVariable := "TF_VAR_ibmcloud_api_key"
val, present := os.LookupEnv(checkVariable)
require.True(t, present, checkVariable+" environment variable not set")
require.NotEqual(t, "", val, checkVariable+" environment variable is empty")
prefix := fmt.Sprintf("vpc-f-%s", strings.ToLower(random.UniqueId()))
existingTerraformOptions := setupTerraform(t, prefix, "./existing-resources")
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
Testing: t,
Region: "eu-de", // Hardcoding region to avoid jp-osa, as jp-osa does not support COS association with HPCS.
Prefix: prefix,
TarIncludePatterns: []string{
"*.tf",
"dynamic_values/*.tf",
"dynamic_values/config_modules/*/*.tf",
fullyConfigFlavorDir + "/*.tf",
},
TemplateFolder: fullyConfigFlavorDir,
Tags: []string{"vpc-da-test"},
DeleteWorkspaceOnFail: false,
WaitJobCompleteMinutes: 120,
TerraformVersion: terraformVersion,
IgnoreUpdates: testhelper.Exemptions{ // Ignore for consistency check
List: IgnoreUpdates,
},
IgnoreDestroys: testhelper.Exemptions{ // Ignore for consistency check
List: IgnoreDestroys,
},
})
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
{Name: "existing_resource_group_name", Value: resourceGroup, DataType: "string"},
{Name: "region", Value: options.Region, DataType: "string"},
{Name: "resource_tags", Value: options.Tags, DataType: "list(string)"},
{Name: "access_tags", Value: permanentResources["accessTags"], DataType: "list(string)"},
{Name: "prefix", Value: options.Prefix, DataType: "string"},
{Name: "enable_vpc_flow_logs", Value: "true", DataType: "bool"},
{Name: "existing_cos_instance_crn", Value: permanentResources["general_test_storage_cos_instance_crn"], DataType: "string"},
{Name: "kms_encryption_enabled_bucket", Value: "true", DataType: "bool"},
{Name: "existing_kms_instance_crn", Value: permanentResources["hpcs_south_crn"], DataType: "string"},
{Name: "vpe_gateway_cloud_services", Value: []map[string]string{{"service_name": "kms"}, {"service_name": "cloud-object-storage"}}, DataType: "list(object{})"},
{Name: "vpe_gateway_cloud_service_by_crn", Value: []map[string]string{{"crn": terraform.Output(t, existingTerraformOptions, "postgresql_db_crn"), "vpe_name": "pg"}}, DataType: "list(object{})"},
{Name: "vpn_gateways", Value: []map[string]string{{"name": options.Prefix + "-vpn", "subnet_name": "subnet-c"}}, DataType: "list(object{})"},
}
require.NoError(t, options.RunSchematicTest(), "This should not have errored")
cleanupTerraform(t, existingTerraformOptions, prefix)
}
// Test the upgrade of fully-configurable DA with defaults
func TestRunUpgradeFullyConfigurable(t *testing.T) {
t.Parallel()
// Verify ibmcloud_api_key variable is set
checkVariable := "TF_VAR_ibmcloud_api_key"
val, present := os.LookupEnv(checkVariable)
require.True(t, present, checkVariable+" environment variable not set")
require.NotEqual(t, "", val, checkVariable+" environment variable is empty")
prefix := fmt.Sprintf("vpc-u-%s", strings.ToLower(random.UniqueId()))
existingTerraformOptions := setupTerraform(t, prefix, "./existing-resources")
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
Testing: t,
Region: "eu-de", // Hardcoding region to avoid jp-osa, as jp-osa does not support COS association with HPCS.
Prefix: prefix,
TarIncludePatterns: []string{
"*.tf",
"dynamic_values/*.tf",
"dynamic_values/config_modules/*/*.tf",
fullyConfigFlavorDir + "/*.tf",
},
TemplateFolder: fullyConfigFlavorDir,
Tags: []string{"vpc-da-test"},
DeleteWorkspaceOnFail: false,
WaitJobCompleteMinutes: 120,
CheckApplyResultForUpgrade: true,
TerraformVersion: terraformVersion,
IgnoreUpdates: testhelper.Exemptions{ // Ignore for consistency check
List: IgnoreUpdates,
},
IgnoreDestroys: testhelper.Exemptions{ // Ignore for consistency check
List: IgnoreDestroys,
},
})
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
{Name: "existing_resource_group_name", Value: resourceGroup, DataType: "string"},
{Name: "region", Value: options.Region, DataType: "string"},
{Name: "resource_tags", Value: options.Tags, DataType: "list(string)"},
{Name: "access_tags", Value: permanentResources["accessTags"], DataType: "list(string)"},
{Name: "prefix", Value: options.Prefix, DataType: "string"},
{Name: "enable_vpc_flow_logs", Value: "true", DataType: "bool"},
{Name: "existing_cos_instance_crn", Value: permanentResources["general_test_storage_cos_instance_crn"], DataType: "string"},
{Name: "kms_encryption_enabled_bucket", Value: "true", DataType: "bool"},
{Name: "existing_kms_instance_crn", Value: permanentResources["hpcs_south_crn"], DataType: "string"},
{Name: "vpe_gateway_cloud_services", Value: []map[string]string{{"service_name": "kms"}, {"service_name": "cloud-object-storage"}}, DataType: "list(object{})"},
{Name: "vpe_gateway_cloud_service_by_crn", Value: []map[string]string{{"crn": terraform.Output(t, existingTerraformOptions, "postgresql_db_crn"), "vpe_name": "pg"}}, DataType: "list(object{})"},
{Name: "vpn_gateways", Value: []map[string]string{{"name": options.Prefix + "-vpn", "subnet_name": "subnet-c"}}, DataType: "list(object{})"},
}
err := options.RunSchematicUpgradeTest()
if !options.UpgradeTestSkipped {
assert.Nil(t, err, "This should not have errored")
}
cleanupTerraform(t, existingTerraformOptions, prefix)
}
func TestRunHubAndSpokeDelegatedExample(t *testing.T) {
t.Parallel()
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
Testing: t,
TerraformDir: hubAndSpokeDelegatedExampleTerraformDir,
Prefix: "has-slz",
ResourceGroup: resourceGroup,
Region: "us-south",
PostApplyHook: func(options *testhelper.TestOptions) error {
terraformOptions := options.TerraformOptions
terraformOptions.Vars["update_delegated_resolver"] = true
_, err := terraform.ApplyE(options.Testing, terraformOptions)
return err
},
})
output, err := options.RunTestConsistency()
assert.Nil(t, err, "This should not have errored")
assert.NotNil(t, output, "Expected some output")
}
func TestVpcAddonDefaultConfiguration(t *testing.T) {
t.Parallel()
options := testaddons.TestAddonsOptionsDefault(&testaddons.TestAddonOptions{
Testing: t,
Prefix: "vpc-ad",
ResourceGroup: resourceGroup,
QuietMode: false, // Suppress logs except on failure
})
options.AddonConfig = cloudinfo.NewAddonConfigTerraform(
options.Prefix,
"deploy-arch-ibm-slz-vpc",
"fully-configurable",
map[string]interface{}{
"region": "us-south",
},
)
// Disable target / route creation to prevent hitting quota in account
options.AddonConfig.Dependencies = []cloudinfo.AddonConfig{
{
OfferingName: "deploy-arch-ibm-cloud-monitoring",
OfferingFlavor: "fully-configurable",
Inputs: map[string]interface{}{
"enable_metrics_routing_to_cloud_monitoring": false,
},
Enabled: core.BoolPtr(true),
},
{
OfferingName: "deploy-arch-ibm-activity-tracker",
OfferingFlavor: "fully-configurable",
Inputs: map[string]interface{}{
"enable_activity_tracker_event_routing_to_cloud_logs": false,
},
Enabled: core.BoolPtr(true),
},
}
err := options.RunAddonTest()
require.NoError(t, err)
}