Skip to content

feat: Migrate to Terratest #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open

feat: Migrate to Terratest #109

wants to merge 33 commits into from

Conversation

mitch-hamm
Copy link
Contributor

@mitch-hamm mitch-hamm commented Mar 19, 2025

Motivation

Our current test suite validates resources are created and become in a ready state but we don't have a way to nicely validate TF outputs and expected values.
By using Terratest we can validate both at the same time, keeping the test validation around resource ready state as well as validating output values so we can have comprehensive end to end testing.
This will also allow some parallelization in tests to help speed the test suite up

Modifications

  • Install Terratest
  • Create new test directory
  • Add config to use a locally built provider for the test run
  • Terraform configs now stored in main.tf files rather than string blocks
  • Use Terratest to apply and destroy
  • Validating resource and data block outputs have expected values

New tests folder

Screenshot 2025-03-19 at 11 04 31 AM

Terraform dev overrides

Provider config inside test suite Terraform files:

terraform {
  required_providers {
    streamnative = {
      source = "terraform.local/local/streamnative"
    }
  }
}

Terraformrc file to use locally built binary:

provider_installation {
  dev_overrides {
      "terraform.local/local/streamnative" = "/Users/<Username>/go/bin" #Or your GOBIN if it's defined as a different path
  }
}

Terraform config for testing:

resource "streamnative_pulsar_instance" "test-instance" {
  organization = "sndev"
  name = "terraform-pulsar-instance-test"
  availability_mode = "zonal"
  pool_name = "shared-gcp"
  pool_namespace = "streamnative"
  type = "standard"
}

data "streamnative_pulsar_instance" "test-instance" {
  depends_on = [streamnative_pulsar_instance.test-instance]
  name = streamnative_pulsar_instance.test-instance.name
  organization = streamnative_pulsar_instance.test-instance.organization
}

output "resource_availability_mode" {
  value = streamnative_pulsar_instance.test-instance.availability_mode
}

output "resource_id" {
  value = streamnative_pulsar_instance.test-instance.id
}

Terratest example test:

var org = "sndev"
var ready = "True"
var availabilityMode = "zonal"
var poolName = "shared-gcp"
var poolNamespace = "streamnative"

func TestPulsarInstance(t *testing.T) {
	t.Parallel()
	// Construct the terraform options with default retryable errors to handle the most common
	// retryable errors in terraform testing.
	terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
		// Set the path to the Terraform code that will be tested.
		TerraformDir: "tf-tests/pulsar_instance",
	})

	skipDestroy := os.Getenv("SKIP_DESTROY")

	if skipDestroy != "TRUE" {
		// Clean up resources with "terraform destroy" at the end of the test.
		defer terraform.Destroy(t, terraformOptions)
	}

	// Run "terraform apply". Fail the test if there are any errors.
	terraform.Apply(t, terraformOptions)

	// Run `terraform output` to get the values of output variables and check they have the expected values.
	resourceAvailabilityMode := terraform.Output(t, terraformOptions, "resource_availability_mode")
	resourceOrganization := terraform.Output(t, terraformOptions, "resource_organization")
	resourcePoolName := terraform.Output(t, terraformOptions, "resource_pool_name")
	resourcePoolNamespace := terraform.Output(t, terraformOptions, "resource_pool_namespace")
	resourceReady := terraform.Output(t, terraformOptions, "resource_ready")

	dataAvailabilityMode := terraform.Output(t, terraformOptions, "data_availability_mode")
	dataOrganization := terraform.Output(t, terraformOptions, "data_organization")
	dataPoolName := terraform.Output(t, terraformOptions, "data_pool_name")
	dataPoolNamespace := terraform.Output(t, terraformOptions, "data_pool_namespace")
	dataReady := terraform.Output(t, terraformOptions, "data_ready")

	assert.Equal(t, availabilityMode, resourceAvailabilityMode)
	assert.Equal(t, org, resourceOrganization)
	assert.Equal(t, poolName, resourcePoolName)
	assert.Equal(t, poolNamespace, resourcePoolNamespace)
	assert.Equal(t, ready, resourceReady)

	assert.Equal(t, availabilityMode, dataAvailabilityMode)
	assert.Equal(t, org, dataOrganization)
	assert.Equal(t, poolName, dataPoolName)
	assert.Equal(t, poolNamespace, dataPoolNamespace)
	assert.Equal(t, ready, dataReady)
}

@mitch-hamm mitch-hamm requested a review from a team as a code owner March 19, 2025 00:35
@mitch-hamm mitch-hamm changed the title WIP: Run terratest feat: Run terratest Mar 21, 2025
@mitch-hamm mitch-hamm changed the title feat: Run terratest feat: Migrate to Terratest Mar 21, 2025
@mitch-hamm mitch-hamm requested a review from ciiiii April 7, 2025 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant