The testaddons
package provides a framework for testing IBM Cloud add-ons. It allows you to run tests against different add-on configurations and validate their behavior.
The general process for testing an add-on that the framework handles for the user is as follows:
- Checks for local changes to be committed or pushed. This can be disabled with the
TestAddonOptions.SkipLocalChangeCheck
flag totrue
- Automatically determines the repository URL and branch from the current Git context (no need to specify repository URL or branch in your test configuration)
- Creates a temporary catalog in the IBM Cloud account
- Imports the offering/addon from the current branch to the temporary catalog
- Creates a test project in IBM Cloud
- Deploys the addon configuration to the project
- Deploys dependent configurations that are marked as
on by default
unless explicitly configured otherwise through theEnabled
flag - Updates the configurations with proper API key authentication using the
TF_VAR_ibmcloud_api_key
environment variable - Updates input configurations based on values provided in the test options
- Runs a deploy operation and waits for completion
- Runs an undeploy operation and waits for completion
- Cleans up by deleting the project and catalog
During this process, the framework will log detailed information about each step, including the repository URL and branch, catalog and project IDs, and the dependencies that are deployed.
This example shows how to run a basic test using the testaddons
package with a Terraform add-on. It sets up the test options, including the prefix and resource group, and then runs the addon test.
package test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/cloudinfo"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testaddons"
)
func setupAddonOptions(t *testing.T, prefix string) *testaddons.TestAddonOptions {
options := testaddons.TestAddonsOptionsDefault(&testaddons.TestAddonOptions{
Testing: t,
Prefix: prefix,
ResourceGroup: "my-project-rg",
})
return options
}
func TestRunTerraformAddon(t *testing.T) {
t.Parallel()
options := setupAddonOptions(t, "test-terraform-addon")
// Using the specialized Terraform helper function
options.AddonConfig = cloudinfo.NewAddonConfigTerraform(
options.Prefix, // prefix for unique resource naming
"test-addon", // offering name
"test-flavor", // offering flavor
map[string]interface{}{ // inputs
"prefix": options.Prefix,
},
)
err := options.RunAddonTest()
assert.Nil(t, err, "This should not have errored")
}
This example demonstrates how to configure a test with a Stack add-on.
func TestRunStackAddon(t *testing.T) {
t.Parallel()
options := setupAddonOptions(t, "test-stack-addon")
// Using the specialized Stack helper function
options.AddonConfig = cloudinfo.NewAddonConfigStack(
options.Prefix, // prefix for unique resource naming
"test-addon", // offering name
"test-flavor", // offering flavor
map[string]interface{}{ // inputs
"prefix": options.Prefix,
"region": "us-south",
},
)
err := options.RunAddonTest()
assert.Nil(t, err, "This should not have errored")
}
This example shows how to correctly configure dependencies of an addon by directly setting the Dependencies
array:
func TestRunAddonWithCustomDependencyConfig(t *testing.T) {
t.Parallel()
options := setupAddonOptions(t, "custom-dependency-config")
// Create the base addon config
options.AddonConfig = cloudinfo.NewAddonConfigStack(
"example-app",
"standard",
map[string]interface{}{
"prefix": options.Prefix,
"region": "us-south",
},
)
// Set dependencies is by directly assigning to the Dependencies array
options.AddonConfig.Dependencies = []cloudinfo.AddonConfig{
{
// First dependency
OfferingName: "database",
OfferingFlavor: "postgresql",
Inputs: map[string]interface{}{
"prefix": options.Prefix,
"plan": "standard",
},
Enabled: true, // explicitly enable this dependency
},
{
// Second dependency
OfferingName: "monitoring",
OfferingFlavor: "basic",
Enabled: false, // explicitly disable this dependency
}
}
err := options.RunAddonTest()
assert.Nil(t, err, "This should not have errored")
}
The TestAddonOptions
structure provides several configuration options that you can use to customize your addon tests:
Testing
- Required testing.T objectPrefix
- A unique prefix for all resources created during testingResourceGroup
- The resource group where resources will be createdRequiredEnvironmentVars
- Environment variables required for testing (TF_VAR_ibmcloud_api_key is required by default)
ProjectName
- The name of the test project (defaults to "addon" + prefix)ProjectDescription
- Description of the test projectProjectLocation
- The location for the projectProjectDestroyOnDelete
- Whether to destroy resources when deleting the projectProjectMonitoringEnabled
- Whether project monitoring is enabledProjectAutoDeploy
- Whether to automatically deploy configurationsProjectEnvironments
- Define custom environments
CatalogUseExisting
- Whether to use an existing catalogCatalogName
- The name of the catalog to create/use
DeployTimeoutMinutes
- Max time to wait for deployment (defaults to 6 hours)SkipTestTearDown
- Skip cleanup after testsSkipUndeploy
- Skip the undeploy stepSkipProjectDelete
- Don't delete the project after testingSkipLocalChangeCheck
- Skip checking for uncommitted local changesLocalChangesIgnorePattern
- Regex patterns for files to ignore in local change check
The framework provides several hook points where you can inject custom code:
PreDeployHook
- Executed before deploymentPostDeployHook
- Executed after successful deploymentPreUndeployHook
- Executed before undeploymentPostUndeployHook
- Executed after successful undeployment
Example of using hooks:
options.PreDeployHook = func(options *testaddons.TestAddonOptions) error {
// Do some extra pre configuration before deploying
return nil
}
options.PostDeployHook = func(options *testaddons.TestAddonOptions) error {
// Validate deployed resources or perform tests against them
return nil
}