Skip to content
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

HCPE-1032 - Add basic consul acceptance test #78

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions internal/provider/resource_consul_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package provider

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-hcp/internal/clients"
)

var (
testAccConsulClusterConfig = fmt.Sprintf(`
resource "hcp_hvn" "test" {
hvn_id = "test-hvn"
cloud_provider = "aws"
region = "us-west-2"
}

resource "hcp_consul_cluster" "test" {
cluster_id = "test-consul-cluster"
hvn_id = hcp_hvn.test.hvn_id
tier = "development"
}`)
)

func TestAccConsulCluster(t *testing.T) {
resourceName := "hcp_consul_cluster.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckConsulClusterDestroy,
Steps: []resource.TestStep{
{
Config: testConfig(testAccConsulClusterConfig),
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulClusterExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-consul-cluster"),
resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"),
resource.TestCheckResourceAttr(resourceName, "tier", "development"),
resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"),
resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"),
resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"),
resource.TestCheckResourceAttr(resourceName, "datacenter", "test-consul-cluster"),
resource.TestCheckResourceAttr(resourceName, "scale", "1"),
resource.TestCheckResourceAttr(resourceName, "consul_snapshot_interval", "24h"),
resource.TestCheckResourceAttr(resourceName, "consul_snapshot_retention", "30d"),
resource.TestCheckResourceAttr(resourceName, "connect_enabled", "true"),
resource.TestCheckResourceAttrSet(resourceName, "organization_id"),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "consul_config_file"),
resource.TestCheckResourceAttrSet(resourceName, "consul_ca_file"),
resource.TestCheckResourceAttrSet(resourceName, "consul_version"),
resource.TestCheckNoResourceAttr(resourceName, "consul_public_endpoint_url"),
resource.TestCheckResourceAttrSet(resourceName, "consul_private_endpoint_url"),
resource.TestCheckResourceAttrSet(resourceName, "self_link"),
resource.TestCheckNoResourceAttr(resourceName, "primary_link"),
resource.TestCheckResourceAttrSet(resourceName, "consul_root_token_accessor_id"),
resource.TestCheckResourceAttrSet(resourceName, "consul_root_token_secret_id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("not found: %s", resourceName)
}

return rs.Primary.Attributes["cluster_id"], nil
},
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"consul_root_token_accessor_id", "consul_root_token_secret_id"},
},
{
Config: testConfig(testAccConsulClusterConfig),
Check: resource.ComposeTestCheckFunc(
testAccCheckConsulClusterExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "cluster_id", "test-consul-cluster"),
resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"),
resource.TestCheckResourceAttr(resourceName, "tier", "development"),
resource.TestCheckResourceAttr(resourceName, "cloud_provider", "aws"),
resource.TestCheckResourceAttr(resourceName, "region", "us-west-2"),
resource.TestCheckResourceAttr(resourceName, "public_endpoint", "false"),
resource.TestCheckResourceAttr(resourceName, "datacenter", "test-consul-cluster"),
resource.TestCheckResourceAttr(resourceName, "scale", "1"),
resource.TestCheckResourceAttr(resourceName, "consul_snapshot_interval", "24h"),
resource.TestCheckResourceAttr(resourceName, "consul_snapshot_retention", "30d"),
resource.TestCheckResourceAttr(resourceName, "connect_enabled", "true"),
resource.TestCheckResourceAttrSet(resourceName, "organization_id"),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "consul_config_file"),
resource.TestCheckResourceAttrSet(resourceName, "consul_ca_file"),
resource.TestCheckResourceAttrSet(resourceName, "consul_version"),
resource.TestCheckNoResourceAttr(resourceName, "consul_public_endpoint_url"),
resource.TestCheckResourceAttrSet(resourceName, "consul_private_endpoint_url"),
resource.TestCheckResourceAttrSet(resourceName, "self_link"),
resource.TestCheckNoResourceAttr(resourceName, "primary_link"),
resource.TestCheckResourceAttrSet(resourceName, "consul_root_token_accessor_id"),
resource.TestCheckResourceAttrSet(resourceName, "consul_root_token_secret_id"),
),
},
},
})
}

func testAccCheckConsulClusterExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("not found: %s", name)
}

id := rs.Primary.ID
if id == "" {
return fmt.Errorf("no ID is set")
}

client := testAccProvider.Meta().(*clients.Client)

link, err := buildLinkFromURL(id, ConsulClusterResourceType, client.Config.OrganizationID)
if err != nil {
return fmt.Errorf("unable to build link for %q: %v", id, err)
}

clusterID := link.ID
loc := link.Location

if _, err := clients.GetConsulClusterByID(context.Background(), client, loc, clusterID); err != nil {
return fmt.Errorf("unable to read Consul cluster %q: %v", id, err)
}

return nil
}
}

func testAccCheckConsulClusterDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*clients.Client)

for _, rs := range s.RootModule().Resources {
switch rs.Type {
case "hcp_consul_cluster":
id := rs.Primary.ID

link, err := buildLinkFromURL(id, ConsulClusterResourceType, client.Config.OrganizationID)
if err != nil {
return fmt.Errorf("unable to build link for %q: %v", id, err)
}

clusterID := link.ID
loc := link.Location

_, err = clients.GetConsulClusterByID(context.Background(), client, loc, clusterID)
if err == nil || !clients.IsResponseCodeNotFound(err) {
return fmt.Errorf("didn't get a 404 when reading destroyed Consul cluster %q: %v", id, err)
}

default:
continue
}
}
return nil
}
6 changes: 3 additions & 3 deletions internal/provider/resource_hvn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var (
hvn = fmt.Sprintf(`
testAccHvnConfig = fmt.Sprintf(`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a convention we need to follow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I only changed this so that the name was more clear and less likely to be reused by other code in the package

resource "hcp_hvn" "test" {
hvn_id = "test-hvn"
cloud_provider = "aws"
Expand All @@ -28,7 +28,7 @@ func TestAccHvn(t *testing.T) {
CheckDestroy: testAccCheckHvnDestroy,
Steps: []resource.TestStep{
{
Config: testConfig(hvn),
Config: testConfig(testAccHvnConfig),
Check: resource.ComposeTestCheckFunc(
testAccCheckHvnExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"),
Expand All @@ -55,7 +55,7 @@ func TestAccHvn(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testConfig(hvn),
Config: testConfig(testAccHvnConfig),
Check: resource.ComposeTestCheckFunc(
testAccCheckHvnExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "hvn_id", "test-hvn"),
Expand Down