Skip to content

Commit

Permalink
Convert tests to import style (hashicorp#4808)
Browse files Browse the repository at this point in the history
Convert tests to import style
  • Loading branch information
c2thorn authored Nov 4, 2019
2 parents 1dde498 + b14abf8 commit 98ac61f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 252 deletions.
65 changes: 20 additions & 45 deletions google/resource_compute_network_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ func TestAccComputeNetworkEndpoint_networkEndpointsBasic(t *testing.T) {
{
// Create one endpoint
Config: testAccComputeNetworkEndpoint_networkEndpointsBasic(context),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNetworkEndpointWithPortExists("google_compute_network_endpoint.default", "90"),
),
},
{
ResourceName: "google_compute_network_endpoint.default",
Expand All @@ -42,18 +39,32 @@ func TestAccComputeNetworkEndpoint_networkEndpointsBasic(t *testing.T) {
// Force-recreate old endpoint
Config: testAccComputeNetworkEndpoint_networkEndpointsModified(context),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNetworkEndpointWithPortExists("google_compute_network_endpoint.default", "100"),
testAccCheckComputeNetworkEndpointWithPortsDestroyed(negId, "90"),
),
},
{
ResourceName: "google_compute_network_endpoint.default",
ImportState: true,
ImportStateVerify: true,
},
{
// Add two new endpoints
Config: testAccComputeNetworkEndpoint_networkEndpointsAdditional(context),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeNetworkEndpointWithPortExists("google_compute_network_endpoint.default", "100"),
testAccCheckComputeNetworkEndpointWithPortExists("google_compute_network_endpoint.add1", "101"),
testAccCheckComputeNetworkEndpointWithPortExists("google_compute_network_endpoint.add2", "102"),
),
},
{
ResourceName: "google_compute_network_endpoint.default",
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "google_compute_network_endpoint.add1",
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "google_compute_network_endpoint.add2",
ImportState: true,
ImportStateVerify: true,
},
{
// delete all endpoints
Expand Down Expand Up @@ -168,42 +179,6 @@ data "google_compute_image" "my_image" {
`, context)
}

// testAccCheckComputeNetworkEndpointExists makes sure the resource with given
// (Terraform) name exists, and returns identifying information about the
// existing endpoint
func testAccCheckComputeNetworkEndpointWithPortExists(name, port string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("resource %q not in path %q", name, s.RootModule().Path)
}

if rs.Type != "google_compute_network_endpoint" {
return fmt.Errorf("resource %q has unexpected type %q", name, rs.Type)
}

if rs.Primary.Attributes["port"] != port {
return fmt.Errorf("unexpected port %s for resource %s, expected %s", rs.Primary.Attributes["port"], name, port)
}

config := testAccProvider.Meta().(*Config)

negResourceId, err := replaceVarsForTest(config, rs, "projects/{{project}}/zones/{{zone}}/networkEndpointGroups/{{network_endpoint_group}}")
if err != nil {
return fmt.Errorf("creating URL for getting network endpoint %q failed: %v", name, err)
}

foundPorts, err := testAccComputeNetworkEndpointsListEndpointPorts(negResourceId)
if err != nil {
return fmt.Errorf("unable to confirm endpoints with port %s exists: %v", port, err)
}
if _, ok := foundPorts[port]; !ok {
return fmt.Errorf("did not find endpoint with port %s", port)
}
return nil
}
}

// testAccCheckComputeNetworkEndpointDestroyed makes sure the endpoint with
// given Terraform resource name and previous information (obtained from Exists)
// was destroyed properly.
Expand Down
83 changes: 10 additions & 73 deletions google/resource_compute_region_autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ package google

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"google.golang.org/api/compute/v1"
)

func TestAccComputeRegionAutoscaler_update(t *testing.T) {
var ascaler compute.Autoscaler

var it_name = fmt.Sprintf("region-autoscaler-test-%s", acctest.RandString(10))
var tp_name = fmt.Sprintf("region-autoscaler-test-%s", acctest.RandString(10))
var igm_name = fmt.Sprintf("region-autoscaler-test-%s", acctest.RandString(10))
Expand All @@ -26,82 +21,24 @@ func TestAccComputeRegionAutoscaler_update(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccComputeRegionAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionAutoscalerExists(
"google_compute_region_autoscaler.foobar", &ascaler),
),
},
{
ResourceName: "google_compute_region_autoscaler.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRegionAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRegionAutoscalerExists(
"google_compute_region_autoscaler.foobar", &ascaler),
testAccCheckComputeRegionAutoscalerUpdated(
"google_compute_region_autoscaler.foobar", 10),
),
},
{
ResourceName: "google_compute_region_autoscaler.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckComputeRegionAutoscalerExists(n string, ascaler *compute.Autoscaler) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

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

config := testAccProvider.Meta().(*Config)

idParts := strings.Split(rs.Primary.ID, "/")
region, name := idParts[0], idParts[1]
found, err := config.clientCompute.RegionAutoscalers.Get(config.Project, region, name).Do()
if err != nil {
return err
}

if found.Name != name {
return fmt.Errorf("Autoscaler not found")
}

*ascaler = *found

return nil
}
}

func testAccCheckComputeRegionAutoscalerUpdated(n string, max int64) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

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

config := testAccProvider.Meta().(*Config)

idParts := strings.Split(rs.Primary.ID, "/")
region, name := idParts[0], idParts[1]
ascaler, err := config.clientCompute.RegionAutoscalers.Get(config.Project, region, name).Do()
if err != nil {
return err
}

if ascaler.AutoscalingPolicy.MaxNumReplicas != max {
return fmt.Errorf("maximum replicas incorrect")
}

return nil
}
}

func testAccComputeRegionAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down
Loading

0 comments on commit 98ac61f

Please sign in to comment.