|
2 | 2 | package test |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "crypto/rand" |
| 6 | + "encoding/base64" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "strings" |
5 | 11 | "testing" |
6 | 12 |
|
| 13 | + "github.com/gruntwork-io/terratest/modules/files" |
| 14 | + "github.com/gruntwork-io/terratest/modules/logger" |
| 15 | + "github.com/gruntwork-io/terratest/modules/random" |
| 16 | + "github.com/gruntwork-io/terratest/modules/terraform" |
7 | 17 | "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common" |
8 | 20 | "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper" |
9 | 21 | ) |
10 | 22 |
|
11 | 23 | // Ensure every example directory has a corresponding test |
12 | | -const defaultExampleTerraformDir = "examples/basic" |
13 | | - |
14 | | -func setupOptions(t *testing.T, prefix string, dir string) *testhelper.TestOptions { |
15 | | - options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{ |
16 | | - Testing: t, |
17 | | - TerraformDir: dir, |
18 | | - Prefix: prefix, |
19 | | - IgnoreAdds: testhelper.Exemptions{ // Ignore for consistency check |
20 | | - List: []string{}, |
21 | | - }, |
22 | | - IgnoreUpdates: testhelper.Exemptions{ // Ignore for consistency check |
23 | | - List: []string{ |
24 | | - "module.cloudpak_data.module.cloud_pak_deployer.helm_release.cloud_pak_deployer_helm_release", |
25 | | - }, |
26 | | - }, |
27 | | - IgnoreDestroys: testhelper.Exemptions{ // Ignore for consistency check |
28 | | - List: []string{}, |
29 | | - }, |
30 | | - }) |
31 | | - return options |
32 | | -} |
| 24 | +const instanceFlavorDir = "solutions/deploy" |
| 25 | + |
| 26 | +var permanentResources map[string]interface{} |
| 27 | + |
| 28 | +// Define a struct with fields that match the structure of the YAML data |
| 29 | +const yamlLocation = "../common-dev-assets/common-go-assets/common-permanent-resources.yaml" |
33 | 30 |
|
34 | | -// Consistency test for the basic example |
35 | | -func TestRunBasicExample(t *testing.T) { |
| 31 | +func TestMain(m *testing.M) { |
| 32 | + // Read the YAML file contents |
| 33 | + var err error |
| 34 | + permanentResources, err = common.LoadMapFromYaml(yamlLocation) |
| 35 | + if err != nil { |
| 36 | + log.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + os.Exit(m.Run()) |
| 40 | +} |
36 | 41 |
|
| 42 | +// A test to pass existing resources to the CloudPak DA |
| 43 | +func TestRunStandardSolution(t *testing.T) { |
37 | 44 | t.Parallel() |
| 45 | + // ------------------------------------------------------------------------------------ |
| 46 | + // Provision ROK's first |
| 47 | + // ------------------------------------------------------------------------------------ |
| 48 | + |
| 49 | + prefix := fmt.Sprintf("cp-ex-%s", strings.ToLower(random.UniqueId())) |
| 50 | + realTerraformDir := "./resources" |
| 51 | + tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId()))) |
| 52 | + tags := common.GetTagsFromTravis() |
| 53 | + region := "us-south" |
| 54 | + |
| 55 | + // Verify ibmcloud_api_key variable is set |
| 56 | + checkVariable := "TF_VAR_ibmcloud_api_key" |
| 57 | + val, present := os.LookupEnv(checkVariable) |
| 58 | + require.True(t, present, checkVariable+" environment variable not set") |
| 59 | + require.NotEqual(t, "", val, checkVariable+" environment variable is empty") |
38 | 60 |
|
39 | | - options := setupOptions(t, "cp4d", defaultExampleTerraformDir) |
| 61 | + logger.Log(t, "Tempdir: ", tempTerraformDir) |
| 62 | + existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ |
| 63 | + TerraformDir: tempTerraformDir, |
| 64 | + Vars: map[string]interface{}{ |
| 65 | + "prefix": prefix, |
| 66 | + "region": region, |
| 67 | + "resource_tags": tags, |
| 68 | + }, |
| 69 | + // Set Upgrade to true to ensure latest version of providers and modules are used by terratest. |
| 70 | + // This is the same as setting the -upgrade=true flag with terraform. |
| 71 | + Upgrade: true, |
| 72 | + }) |
40 | 73 |
|
41 | | - output, err := options.RunTestConsistency() |
42 | | - assert.Nil(t, err, "This should not have errored") |
43 | | - assert.NotNil(t, output, "Expected some output") |
| 74 | + terraform.WorkspaceSelectOrNew(t, existingTerraformOptions, prefix) |
| 75 | + _, existErr := terraform.InitAndApplyE(t, existingTerraformOptions) |
| 76 | + if existErr != nil { |
| 77 | + assert.True(t, existErr == nil, "Init and Apply of temp existing resource failed") |
| 78 | + } else { |
| 79 | + // ------------------------------------------------------------------------------------ |
| 80 | + // Deploy Cloudpak DA passing using existing ROKS instance |
| 81 | + // ------------------------------------------------------------------------------------ |
| 82 | + options := testhelper.TestOptionsDefault(&testhelper.TestOptions{ |
| 83 | + Testing: t, |
| 84 | + TerraformDir: instanceFlavorDir, |
| 85 | + // Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur |
| 86 | + ImplicitRequired: false, |
| 87 | + TerraformVars: map[string]interface{}{ |
| 88 | + "prefix": prefix, |
| 89 | + "region": region, |
| 90 | + "cluster_name": terraform.Output(t, existingTerraformOptions, "workload_cluster_id"), |
| 91 | + "cloud_pak_deployer_image": "quay.io/cloud-pak-deployer/cloud-pak-deployer", |
| 92 | + "cpd_admin_password": GetRandomAdminPassword(t), |
| 93 | + "cpd_entitlement_key": "entitlementKey", |
| 94 | + "install_odf_cluster_addon": false, |
| 95 | + }, |
| 96 | + }) |
| 97 | + |
| 98 | + output, err := options.RunTestConsistency() |
| 99 | + assert.Nil(t, err, "This should not have errored") |
| 100 | + assert.NotNil(t, output, "Expected some output") |
| 101 | + } |
44 | 102 |
|
| 103 | + // Check if "DO_NOT_DESTROY_ON_FAILURE" is set |
| 104 | + envVal, _ := os.LookupEnv("DO_NOT_DESTROY_ON_FAILURE") |
| 105 | + // Destroy the temporary existing resources if required |
| 106 | + if t.Failed() && strings.ToLower(envVal) == "true" { |
| 107 | + fmt.Println("Terratest failed. Debug the test and delete resources manually.") |
| 108 | + } else { |
| 109 | + logger.Log(t, "START: Destroy (existing resources)") |
| 110 | + terraform.Destroy(t, existingTerraformOptions) |
| 111 | + terraform.WorkspaceDelete(t, existingTerraformOptions, prefix) |
| 112 | + logger.Log(t, "END: Destroy (existing resources)") |
| 113 | + } |
45 | 114 | } |
46 | 115 |
|
47 | | -// Upgrade test (using advanced example) |
48 | | -func TestRunUpgradeExample(t *testing.T) { |
| 116 | +func TestRunStandardUpgradeSolution(t *testing.T) { |
49 | 117 | t.Parallel() |
50 | | - options := setupOptions(t, "cp4dup", defaultExampleTerraformDir) |
51 | 118 |
|
52 | | - output, err := options.RunTestUpgrade() |
53 | | - if !options.UpgradeTestSkipped { |
| 119 | + prefix := fmt.Sprintf("rag-da-upgr-%s", strings.ToLower(random.UniqueId())) |
| 120 | + realTerraformDir := "./resources" |
| 121 | + tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId()))) |
| 122 | + tags := common.GetTagsFromTravis() |
| 123 | + region := "us-south" |
| 124 | + |
| 125 | + // Verify ibmcloud_api_key variable is set |
| 126 | + checkVariable := "TF_VAR_ibmcloud_api_key" |
| 127 | + val, present := os.LookupEnv(checkVariable) |
| 128 | + require.True(t, present, checkVariable+" environment variable not set") |
| 129 | + require.NotEqual(t, "", val, checkVariable+" environment variable is empty") |
| 130 | + |
| 131 | + logger.Log(t, "Tempdir: ", tempTerraformDir) |
| 132 | + existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ |
| 133 | + TerraformDir: tempTerraformDir, |
| 134 | + Vars: map[string]interface{}{ |
| 135 | + "prefix": prefix, |
| 136 | + "region": region, |
| 137 | + "resource_tags": tags, |
| 138 | + }, |
| 139 | + // Set Upgrade to true to ensure latest version of providers and modules are used by terratest. |
| 140 | + // This is the same as setting the -upgrade=true flag with terraform. |
| 141 | + Upgrade: true, |
| 142 | + }) |
| 143 | + |
| 144 | + terraform.WorkspaceSelectOrNew(t, existingTerraformOptions, prefix) |
| 145 | + _, existErr := terraform.InitAndApplyE(t, existingTerraformOptions) |
| 146 | + if existErr != nil { |
| 147 | + assert.True(t, existErr == nil, "Init and Apply of temp existing resource failed") |
| 148 | + } else { |
| 149 | + // ------------------------------------------------------------------------------------ |
| 150 | + // Deploy Cloudpak DA passing using existing ROKS instance |
| 151 | + // ------------------------------------------------------------------------------------ |
| 152 | + options := testhelper.TestOptionsDefault(&testhelper.TestOptions{ |
| 153 | + Testing: t, |
| 154 | + TerraformDir: instanceFlavorDir, |
| 155 | + // Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur |
| 156 | + ImplicitRequired: false, |
| 157 | + TerraformVars: map[string]interface{}{ |
| 158 | + "prefix": prefix, |
| 159 | + "region": region, |
| 160 | + "cluster_name": terraform.Output(t, existingTerraformOptions, "workload_cluster_id"), |
| 161 | + "cloud_pak_deployer_image": "quay.io/cloud-pak-deployer/cloud-pak-deployer", |
| 162 | + "cpd_admin_password": GetRandomAdminPassword(t), |
| 163 | + "cpd_entitlement_key": "entitlementKey", |
| 164 | + "install_odf_cluster_addon": false, |
| 165 | + }, |
| 166 | + }) |
| 167 | + |
| 168 | + options.IgnoreUpdates = testhelper.Exemptions{ |
| 169 | + List: []string{ |
| 170 | + "module.cloudpak_data.module.cloud_pak_deployer.helm_release.cloud_pak_deployer_helm_release", |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + output, err := options.RunTestUpgrade() |
54 | 175 | assert.Nil(t, err, "This should not have errored") |
55 | 176 | assert.NotNil(t, output, "Expected some output") |
56 | 177 | } |
| 178 | + |
| 179 | + // Check if "DO_NOT_DESTROY_ON_FAILURE" is set |
| 180 | + envVal, _ := os.LookupEnv("DO_NOT_DESTROY_ON_FAILURE") |
| 181 | + // Destroy the temporary existing resources if required |
| 182 | + if t.Failed() && strings.ToLower(envVal) == "true" { |
| 183 | + fmt.Println("Terratest failed. Debug the test and delete resources manually.") |
| 184 | + } else { |
| 185 | + logger.Log(t, "START: Destroy (existing resources)") |
| 186 | + terraform.Destroy(t, existingTerraformOptions) |
| 187 | + terraform.WorkspaceDelete(t, existingTerraformOptions, prefix) |
| 188 | + logger.Log(t, "END: Destroy (existing resources)") |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +func GetRandomAdminPassword(t *testing.T) string { |
| 193 | + // Generate a 15 char long random string for the admin_pass |
| 194 | + randomBytes := make([]byte, 13) |
| 195 | + _, randErr := rand.Read(randomBytes) |
| 196 | + require.Nil(t, randErr) // do not proceed if we can't gen a random password |
| 197 | + |
| 198 | + randomPass := "A1" + base64.URLEncoding.EncodeToString(randomBytes)[:13] |
| 199 | + |
| 200 | + return randomPass |
57 | 201 | } |
0 commit comments