From bc4aa537ebc77775b8eac51052533f7cd4efcf49 Mon Sep 17 00:00:00 2001 From: Brenna Hewer-Darroch <21015366+bcmdarroch@users.noreply.github.com> Date: Fri, 18 Jun 2021 13:03:46 -0700 Subject: [PATCH] refactor vault cluster test config to use variable vault cluster --- .../provider/resource_vault_cluster_test.go | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/internal/provider/resource_vault_cluster_test.go b/internal/provider/resource_vault_cluster_test.go index de3b59ce6..818a1a862 100644 --- a/internal/provider/resource_vault_cluster_test.go +++ b/internal/provider/resource_vault_cluster_test.go @@ -10,42 +10,39 @@ import ( "github.com/hashicorp/terraform-provider-hcp/internal/clients" ) -var testAccVaultClusterConfig = ` -resource "hcp_hvn" "test" { - hvn_id = "test-hvn" - cloud_provider = "aws" - region = "us-west-2" -} - +var vaultCluster = ` resource "hcp_vault_cluster" "test" { cluster_id = "test-vault-cluster" hvn_id = hcp_hvn.test.hvn_id tier = "dev" } +` -data "hcp_vault_cluster" "test" { - cluster_id = hcp_vault_cluster.test.cluster_id +// sets public_endpoint to true +var updatedVaultCluster = ` +resource "hcp_vault_cluster" "test" { + cluster_id = "test-vault-cluster" + hvn_id = hcp_hvn.test.hvn_id + tier = "dev" + public_endpoint = true } ` -var testAccUpdatedVaultClusterConfig = ` +func setTestAccVaultClusterConfig(vaultCluster string) string { + return fmt.Sprintf(` resource "hcp_hvn" "test" { hvn_id = "test-hvn" cloud_provider = "aws" region = "us-west-2" } -resource "hcp_vault_cluster" "test" { - cluster_id = "test-vault-cluster" - hvn_id = hcp_hvn.test.hvn_id - tier = "dev" - public_endpoint = true -} +%s data "hcp_vault_cluster" "test" { cluster_id = hcp_vault_cluster.test.cluster_id } -` +`, vaultCluster) +} // This includes tests against both the resource and the corresponding datasource // to shorten testing time. @@ -60,7 +57,7 @@ func TestAccVaultCluster(t *testing.T) { Steps: []resource.TestStep{ // Tests create { - Config: testConfig(testAccVaultClusterConfig), + Config: testConfig(setTestAccVaultClusterConfig(vaultCluster)), Check: resource.ComposeTestCheckFunc( testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), @@ -95,7 +92,7 @@ func TestAccVaultCluster(t *testing.T) { }, // This step is a subsequent terraform apply that verifies that no state is modified. { - Config: testConfig(testAccVaultClusterConfig), + Config: testConfig(setTestAccVaultClusterConfig(vaultCluster)), Check: resource.ComposeTestCheckFunc( testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-vault-cluster"), @@ -116,7 +113,7 @@ func TestAccVaultCluster(t *testing.T) { }, // Tests datasource { - Config: testConfig(testAccVaultClusterConfig), + Config: testConfig(setTestAccVaultClusterConfig(vaultCluster)), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair(resourceName, "cluster_id", dataSourceName, "cluster_id"), resource.TestCheckResourceAttrPair(resourceName, "hvn_id", dataSourceName, "hvn_id"), @@ -137,7 +134,7 @@ func TestAccVaultCluster(t *testing.T) { }, // This step verifies the successful update of updatable fields. { - Config: testConfig(testAccUpdatedVaultClusterConfig), + Config: testConfig(setTestAccVaultClusterConfig(updatedVaultCluster)), Check: resource.ComposeTestCheckFunc( testAccCheckVaultClusterExists(resourceName), resource.TestCheckResourceAttr(resourceName, "public_endpoint", "true"),