generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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