Skip to content

Commit cd8dcf7

Browse files
Jordan-Williams2Jordan-Williams2
authored andcommitted
tests: update test code
1 parent 5eb0682 commit cd8dcf7

File tree

10 files changed

+253
-44
lines changed

10 files changed

+253
-44
lines changed

examples/basic/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ terraform {
33
required_providers {
44
# renovate is set up to keep provider version at the latest for all DA solutions
55
ibm = {
6-
source = "IBM-Cloud/ibm"
6+
source = "ibm-cloud/ibm"
77
version = "1.71.3"
88
}
99
}

solutions/deploy/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ You need the following permissions to run this module:
101101

102102
| Name | Version |
103103
|------|---------|
104-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.2.0 |
104+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.3.0 |
105105
| <a name="requirement_external"></a> [external](#requirement\_external) | >= 2.3.4 |
106106
| <a name="requirement_helm"></a> [helm](#requirement\_helm) | >= 2.8.0, <3.0.0 |
107-
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.66.0, < 2.0.0 |
107+
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | 1.71.3 |
108108

109109
### Modules
110110

@@ -120,10 +120,10 @@ You need the following permissions to run this module:
120120

121121
| Name | Type |
122122
|------|------|
123-
| [ibm_container_addons.odf_cluster_addon](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/container_addons) | resource |
123+
| [ibm_container_addons.odf_cluster_addon](https://registry.terraform.io/providers/ibm-cloud/ibm/1.71.3/docs/resources/container_addons) | resource |
124124
| [external_external.schematics](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
125-
| [ibm_container_cluster_config.cluster_config](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/container_cluster_config) | data source |
126-
| [ibm_container_vpc_cluster.cluster_info](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/container_vpc_cluster) | data source |
125+
| [ibm_container_cluster_config.cluster_config](https://registry.terraform.io/providers/ibm-cloud/ibm/1.71.3/docs/data-sources/container_cluster_config) | data source |
126+
| [ibm_container_vpc_cluster.cluster_info](https://registry.terraform.io/providers/ibm-cloud/ibm/1.71.3/docs/data-sources/container_vpc_cluster) | data source |
127127

128128
### Inputs
129129

solutions/deploy/cpd-image-build/version.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
terraform {
2-
required_version = ">= 1.2.0"
2+
required_version = ">=1.3.0"
33
required_providers {
44
ibm = {
5-
source = "IBM-Cloud/ibm"
6-
version = ">= 1.68.1, < 2.0.0"
5+
source = "ibm-cloud/ibm"
6+
version = "1.71.3"
77
}
88
random = {
99
source = "hashicorp/random"

solutions/deploy/version.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
terraform {
2-
required_version = ">= 1.2.0"
2+
required_version = ">=1.3.0"
33
required_providers {
44
ibm = {
5-
source = "IBM-Cloud/ibm"
6-
version = ">= 1.66.0, < 2.0.0"
5+
source = "ibm-cloud/ibm"
6+
version = "1.71.3"
77
}
88
external = {
99
source = "hashicorp/external"

tests/pr_test.go

Lines changed: 176 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,200 @@
22
package test
33

44
import (
5+
"crypto/rand"
6+
"encoding/base64"
7+
"fmt"
8+
"log"
9+
"os"
10+
"strings"
511
"testing"
612

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"
717
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common"
820
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
921
)
1022

1123
// 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"
3330

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+
}
3641

42+
// A test to pass existing resources to the CloudPak DA
43+
func TestRunStandardSolution(t *testing.T) {
3744
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")
3860

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+
})
4073

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+
}
44102

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+
}
45114
}
46115

47-
// Upgrade test (using advanced example)
48-
func TestRunUpgradeExample(t *testing.T) {
116+
func TestRunStandardUpgradeSolution(t *testing.T) {
49117
t.Parallel()
50-
options := setupOptions(t, "cp4dup", defaultExampleTerraformDir)
51118

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()
54175
assert.Nil(t, err, "This should not have errored")
55176
assert.NotNil(t, output, "Expected some output")
56177
}
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
57201
}

tests/resources/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
##############################################################################
2+
# SLZ VPC
3+
##############################################################################
4+
5+
module "landing_zone" {
6+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-landing-zone//patterns//roks-quickstart?ref=v7.0.1"
7+
ibmcloud_api_key = var.ibmcloud_api_key
8+
region = var.region
9+
prefix = var.prefix
10+
resource_tags = var.resource_tags
11+
}

tests/resources/outputs.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
5+
output "prefix" {
6+
value = module.landing_zone.prefix
7+
description = "prefix"
8+
}
9+
10+
output "region" {
11+
value = var.region
12+
description = "Region where SLZ ROKS Cluster is deployed."
13+
}
14+
15+
output "workload_cluster_id" {
16+
value = module.landing_zone.workload_cluster_id
17+
description = "workload cluster ID."
18+
}

tests/resources/provider.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
region = var.region
4+
}

tests/resources/variables.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "ibmcloud_api_key" {
2+
type = string
3+
description = "The IBM Cloud API Key"
4+
sensitive = true
5+
}
6+
7+
variable "region" {
8+
type = string
9+
description = "Region to provision all resources created by this example"
10+
default = "us-south"
11+
}
12+
13+
variable "prefix" {
14+
type = string
15+
description = "Prefix to append to all resources created by this example"
16+
default = "slz-vpc"
17+
}
18+
19+
variable "resource_tags" {
20+
type = list(string)
21+
description = "Optional list of tags to be added to created resources"
22+
default = []
23+
}

tests/resources/version.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.0.0"
3+
required_providers {
4+
ibm = {
5+
source = "ibm-cloud/ibm"
6+
version = ">= 1.49.0, < 2.0.0"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)