Skip to content

Commit

Permalink
Revert "Replace some unnecessary checks with import tests (GoogleClou…
Browse files Browse the repository at this point in the history
…dPlatform#2500)"

This reverts commit e12927a.
  • Loading branch information
JanMa authored Oct 25, 2019
1 parent bd71e1e commit 6288680
Show file tree
Hide file tree
Showing 8 changed files with 640 additions and 148 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
40 changes: 30 additions & 10 deletions third_party/terraform/tests/resource_cloudiot_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func TestAccCloudIoTRegistry_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccCloudIoTRegistry_basic(registryName),
Check: resource.ComposeTestCheckFunc(
testAccCloudIoTRegistryExists(
"google_cloudiot_registry.foobar"),
),
},
{
ResourceName: "google_cloudiot_registry.foobar",
Expand All @@ -68,6 +72,10 @@ func TestAccCloudIoTRegistry_extended(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccCloudIoTRegistry_extended(registryName),
Check: resource.ComposeTestCheckFunc(
testAccCloudIoTRegistryExists(
"google_cloudiot_registry.foobar"),
),
},
{
ResourceName: "google_cloudiot_registry.foobar",
Expand All @@ -90,20 +98,14 @@ func TestAccCloudIoTRegistry_update(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccCloudIoTRegistry_basic(registryName),
},
{
ResourceName: "google_cloudiot_registry.foobar",
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
testAccCloudIoTRegistryExists(
"google_cloudiot_registry.foobar"),
),
},
{
Config: testAccCloudIoTRegistry_extended(registryName),
},
{
ResourceName: "google_cloudiot_registry.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccCloudIoTRegistry_basic(registryName),
},
Expand Down Expand Up @@ -221,6 +223,24 @@ func testAccCheckCloudIoTRegistryDestroy(s *terraform.State) error {
return nil
}

func testAccCloudIoTRegistryExists(n string) 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)
_, err := config.clientCloudIoT.Projects.Locations.Registries.Get(rs.Primary.ID).Do()
if err != nil {
return fmt.Errorf("Registry does not exist")
}
return nil
}
}

func testAccCloudIoTRegistry_basic(registryName string) string {
return fmt.Sprintf(`
resource "google_cloudiot_registry" "foobar" {
Expand Down
119 changes: 108 additions & 11 deletions third_party/terraform/tests/resource_compute_autoscaler_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
func TestAccComputeAutoscaler_update(t *testing.T) {
t.Parallel()

var ascaler compute.Autoscaler

var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
Expand All @@ -27,21 +29,20 @@ func TestAccComputeAutoscaler_update(t *testing.T) {
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name),
},
resource.TestStep{
ResourceName: "google_compute_autoscaler.foobar",
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeAutoscalerExists(
"google_compute_autoscaler.foobar", &ascaler),
),
},
resource.TestStep{
Config: testAccComputeAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeAutoscalerExists(
"google_compute_autoscaler.foobar", &ascaler),
testAccCheckComputeAutoscalerUpdated(
"google_compute_autoscaler.foobar", 10),
),
},
resource.TestStep{
ResourceName: "google_compute_autoscaler.foobar",
ImportState: true,
ImportStateVerify: true,
},

},
})
}
Expand All @@ -61,6 +62,7 @@ func TestAccComputeAutoscaler_multicondition(t *testing.T) {
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeAutoscaler_multicondition(it_name, tp_name, igm_name, autoscaler_name),
Check: testAccCheckComputeAutoscalerMultifunction("google_compute_autoscaler.foobar"),
},
resource.TestStep{
ResourceName: "google_compute_autoscaler.foobar",
Expand All @@ -71,6 +73,101 @@ func TestAccComputeAutoscaler_multicondition(t *testing.T) {
})
}

func testAccCheckComputeAutoscalerExists(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, "/")
zone, name := idParts[0], idParts[1]
found, err := config.clientCompute.Autoscalers.Get(
config.Project, zone, name).Do()
if err != nil {
return err
}

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

*ascaler = *found

return nil
}
}

func testAccCheckComputeAutoscalerMultifunction(n string) 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, "/")
zone, name := idParts[0], idParts[1]
found, err := config.clientCompute.Autoscalers.Get(
config.Project, zone, name).Do()
if err != nil {
return err
}

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

if found.AutoscalingPolicy.CpuUtilization.UtilizationTarget == 0.5 && found.AutoscalingPolicy.LoadBalancingUtilization.UtilizationTarget == 0.5 {
return nil
}
return fmt.Errorf("Util target for CPU: %f, for LB: %f - should have been 0.5 for each.",
found.AutoscalingPolicy.CpuUtilization.UtilizationTarget,
found.AutoscalingPolicy.LoadBalancingUtilization.UtilizationTarget)

}
}

func testAccCheckComputeAutoscalerUpdated(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, "/")
zone, name := idParts[0], idParts[1]
ascaler, err := config.clientCompute.Autoscalers.Get(
config.Project, zone, name).Do()
if err != nil {
return err
}

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

return nil
}
}

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

0 comments on commit 6288680

Please sign in to comment.