-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpr_test.go
More file actions
239 lines (208 loc) · 8.41 KB
/
Copy pathpr_test.go
File metadata and controls
239 lines (208 loc) · 8.41 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
// Tests in this file are run in the PR pipeline and the continuous testing pipeline
package test
import (
"fmt"
"log"
"math/rand"
"os"
"strings"
"testing"
"github.com/IBM/go-sdk-core/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/assert"
"github.com/stretchr/testify/require"
"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"
)
// Use existing resource group
const resourceGroup = "geretain-test-resources"
const basicExampleDir = "examples/basic"
const existingExampleDir = "examples/existing-instance"
const fullyConfigurableSolutionTerraformDir = "solutions/fully-configurable"
const terraformVersion = "terraform_v1.12.2" // This should match the version in the ibm_catalog.json
// Define a struct with fields that match the structure of the YAML data
const yamlLocation = "../common-dev-assets/common-go-assets/common-permanent-resources.yaml"
var permanentResources map[string]interface{}
var sharedInfoSvc *cloudinfo.CloudInfoService
var validRegions = []string{
"au-syd",
"eu-gb",
"us-south",
"eu-de",
"ca-tor",
"jp-tok",
}
func TestMain(m *testing.M) {
sharedInfoSvc, _ = cloudinfo.NewCloudInfoServiceFromEnv("TF_VAR_ibmcloud_api_key", cloudinfo.CloudInfoServiceOptions{})
// Read the YAML file content
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, exampleDir string) *testhelper.TestOptions {
options := testhelper.TestOptionsDefault(&testhelper.TestOptions{
Testing: t,
TerraformDir: exampleDir,
Prefix: prefix,
ResourceGroup: resourceGroup,
})
options.TerraformVars = map[string]interface{}{
"access_tags": permanentResources["accessTags"],
"region": validRegions[rand.Intn(len(validRegions))],
"prefix": options.Prefix,
"resource_group": resourceGroup,
"resource_tags": options.Tags,
}
return options
}
func TestRunBasicExample(t *testing.T) {
options := setupOptions(t, "wxgo-basic", basicExampleDir)
output, err := options.RunTestConsistency()
assert.Nil(t, err, "This should not have errored")
assert.NotNil(t, output, "Expected some output")
}
func TestRunExistingResourcesExample(t *testing.T) {
// Provision watsonx.governance instance
prefix := fmt.Sprintf("ex-gov-%s", strings.ToLower(random.UniqueId()))
realTerraformDir := ".."
tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId())))
tags := common.GetTagsFromTravis()
// 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")
logger.Log(t, "Tempdir: ", tempTerraformDir)
existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: tempTerraformDir + "/tests/existing-resources",
Vars: map[string]interface{}{
"prefix": prefix,
"resource_tags": tags,
"access_tags": permanentResources["accessTags"],
"region": validRegions[rand.Intn(len(validRegions))],
},
// 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)
_, existErr := terraform.InitAndApplyE(t, existingTerraformOptions)
if existErr != nil {
assert.True(t, existErr == nil, "Init and Apply of temp existing resource failed")
} else {
outputs, err := terraform.OutputAllE(t, existingTerraformOptions)
require.NoError(t, err, "Failed to retrieve Terraform outputs")
expectedOutputs := []string{"account_id", "id", "crn", "guid", "name", "plan_id", "dashboard_url"}
_, tfOutputsErr := testhelper.ValidateTerraformOutputs(outputs, expectedOutputs...)
if assert.Nil(t, tfOutputsErr, tfOutputsErr) {
options := testhelper.TestOptionsDefault(&testhelper.TestOptions{
Testing: t,
TerraformDir: existingExampleDir,
// Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur
ImplicitRequired: false,
TerraformVars: map[string]interface{}{
"existing_watsonx_governance_instance_crn": terraform.Output(t, existingTerraformOptions, "crn"),
},
})
output, err := options.RunTestConsistency()
assert.Nil(t, err, "This should not have errored")
assert.NotNil(t, output, "Expected some output")
}
}
// Check if "DO_NOT_DESTROY_ON_FAILURE" is set
envVal, _ := os.LookupEnv("DO_NOT_DESTROY_ON_FAILURE")
// Destroy the temporary existing resources if required
if t.Failed() && strings.ToLower(envVal) == "true" {
fmt.Println("Terratest failed. Debug the test and delete resources manually.")
} else {
logger.Log(t, "START: Destroy (existing resources)")
terraform.Destroy(t, existingTerraformOptions)
terraform.WorkspaceDelete(t, existingTerraformOptions, prefix)
logger.Log(t, "END: Destroy (existing resources)")
}
}
func setupFullyConfigurableOptions(t *testing.T, prefix string) *testschematic.TestSchematicOptions {
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
Testing: t,
TemplateFolder: fullyConfigurableSolutionTerraformDir,
Region: validRegions[rand.Intn(len(validRegions))],
Prefix: prefix,
TarIncludePatterns: []string{
"*.tf",
fullyConfigurableSolutionTerraformDir + "/*.tf",
},
ResourceGroup: resourceGroup,
TerraformVersion: terraformVersion,
})
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
{Name: "prefix", Value: options.Prefix, DataType: "string"},
{Name: "region", Value: options.Region, DataType: "string"},
{Name: "existing_resource_group_name", Value: resourceGroup, DataType: "string"},
{Name: "provider_visibility", Value: "private", DataType: "string"},
{Name: "service_plan", Value: "essentials", DataType: "string"},
}
return options
}
func TestRunFullyConfigurableSolutionSchematics(t *testing.T) {
t.Parallel()
options := setupFullyConfigurableOptions(t, "wxgo-da")
err := options.RunSchematicTest()
assert.Nil(t, err, "This should not have errored")
}
func TestRunFullyConfigurableUpgradeSolutionSchematics(t *testing.T) {
t.Parallel()
options := setupFullyConfigurableOptions(t, "wxgo-upg")
options.CheckApplyResultForUpgrade = true
err := options.RunSchematicUpgradeTest()
if !options.UpgradeTestSkipped {
assert.Nil(t, err, "This should not have errored")
}
}
func TestDefaultConfiguration(t *testing.T) {
options := testaddons.TestAddonsOptionsDefault(&testaddons.TestAddonOptions{
Testing: t,
Prefix: "wxgvdeft",
ResourceGroup: resourceGroup,
QuietMode: true, // Suppress logs except on failure
})
options.AddonConfig = cloudinfo.NewAddonConfigTerraform(
options.Prefix,
"deploy-arch-ibm-watsonx-governance",
"fully-configurable",
map[string]interface{}{
"existing_resource_group_name": resourceGroup,
"region": validRegions[rand.Intn(len(validRegions))],
},
)
// 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)
}