Skip to content

Commit

Permalink
add sweepers to igm/rigm tests, prefix igm/rigms with tf-test (#3855) (
Browse files Browse the repository at this point in the history
…#7008)

* change test name prefixes to tf-test-

* add sweepers to igm and rigm tests

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 12, 2020
1 parent 2526a59 commit 4e9b7d8
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .changelog/3855.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
2 changes: 1 addition & 1 deletion google/data_source_google_compute_instance_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestAccDataSourceGoogleComputeInstanceGroup_fromIGM(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckDataSourceGoogleComputeInstanceGroup_fromIGM(fmt.Sprintf("test-igm-%d", randInt(t)), fmt.Sprintf("test-igm-%d", randInt(t))),
Config: testAccCheckDataSourceGoogleComputeInstanceGroup_fromIGM(fmt.Sprintf("tf-test-igm-%d", randInt(t)), fmt.Sprintf("tf-test-igm-%d", randInt(t))),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_compute_instance_group.test", "instances.#", "10"),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ func TestAccDataSourceRegionInstanceGroup(t *testing.T) {
// Randomness in instance template
skipIfVcr(t)
t.Parallel()
name := "acctest-" + randString(t, 6)
name := "tf-test-" + randString(t, 6)
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceRegionInstanceGroup_basic(fmt.Sprintf("test-rigm--%d", randInt(t)), name),
Config: testAccDataSourceRegionInstanceGroup_basic(fmt.Sprintf("tf-test-rigm--%d", randInt(t)), name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_compute_region_instance_group.data_source", "name", name),
resource.TestCheckResourceAttr("data.google_compute_region_instance_group.data_source", "project", getTestProjectFromEnv()),
Expand Down
16 changes: 8 additions & 8 deletions google/resource_compute_autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
func TestAccComputeAutoscaler_update(t *testing.T) {
t.Parallel()

var it_name = fmt.Sprintf("autoscaler-test-%s", randString(t, 10))
var tp_name = fmt.Sprintf("autoscaler-test-%s", randString(t, 10))
var igm_name = fmt.Sprintf("autoscaler-test-%s", randString(t, 10))
var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", randString(t, 10))
var it_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var tp_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var igm_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var autoscaler_name = fmt.Sprintf("tf-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -43,10 +43,10 @@ func TestAccComputeAutoscaler_update(t *testing.T) {
func TestAccComputeAutoscaler_multicondition(t *testing.T) {
t.Parallel()

var it_name = fmt.Sprintf("autoscaler-test-%s", randString(t, 10))
var tp_name = fmt.Sprintf("autoscaler-test-%s", randString(t, 10))
var igm_name = fmt.Sprintf("autoscaler-test-%s", randString(t, 10))
var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", randString(t, 10))
var it_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var tp_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var igm_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var autoscaler_name = fmt.Sprintf("tf-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
2 changes: 1 addition & 1 deletion google/resource_compute_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ data "google_compute_image" "debian_image" {
}
resource "google_compute_instance_group_manager" "foobar" {
name = "igm-internal"
name = "tf-test-igm-internal"
version {
instance_template = google_compute_instance_template.foobar.self_link
name = "primary"
Expand Down
59 changes: 59 additions & 0 deletions google/resource_compute_instance_group_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
package google

import (
"context"
"fmt"
"log"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func init() {
resource.AddTestSweepers("ComputeInstanceGroupManager", &resource.Sweeper{
Name: "ComputeInstanceGroupManager",
F: testSweepComputeInstanceGroupManager,
})
}

// At the time of writing, the CI only passes us-central1 as the region.
// Since we can read all instances across zones, we don't really use this param.
func testSweepComputeInstanceGroupManager(region string) error {
resourceName := "ComputeInstanceGroupManager"
log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName)

config, err := sharedConfigForRegion(region)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err)
return err
}

err = config.LoadAndValidate(context.Background())
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err)
return err
}

found, err := config.clientCompute.InstanceGroupManagers.AggregatedList(config.Project).Do()
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] Error in response from request: %s", err)
return nil
}

// Keep count of items that aren't sweepable for logging.
nonPrefixCount := 0
for zone, itemList := range found.Items {
for _, igm := range itemList.InstanceGroupManagers {
if !isSweepableTestResource(igm.Name) {
nonPrefixCount++
continue
}

// Don't wait on operations as we may have a lot to delete
_, err := config.clientCompute.InstanceGroupManagers.Delete(config.Project, GetResourceNameFromSelfLink(zone), igm.Name).Do()
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] Error deleting %s resource %s : %s", resourceName, igm.Name, err)
} else {
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, igm.Name)
}
}
}

if nonPrefixCount > 0 {
log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount)
}

return nil
}

func TestAccInstanceGroupManager_basic(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 3 additions & 3 deletions google/resource_compute_region_autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

func TestAccComputeRegionAutoscaler_update(t *testing.T) {
var it_name = fmt.Sprintf("region-autoscaler-test-%s", randString(t, 10))
var tp_name = fmt.Sprintf("region-autoscaler-test-%s", randString(t, 10))
var igm_name = fmt.Sprintf("region-autoscaler-test-%s", randString(t, 10))
var it_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var tp_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var igm_name = fmt.Sprintf("tf-test-%s", randString(t, 10))
var autoscaler_name = fmt.Sprintf("region-autoscaler-test-%s", randString(t, 10))

vcrTest(t, resource.TestCase{
Expand Down
57 changes: 57 additions & 0 deletions google/resource_compute_region_instance_group_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,71 @@
package google

import (
"context"
"fmt"
"log"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func init() {
resource.AddTestSweepers("ComputeRegionInstanceGroupManager", &resource.Sweeper{
Name: "ComputeRegionInstanceGroupManager",
F: testSweepComputeRegionInstanceGroupManager,
})
}

// At the time of writing, the CI only passes us-central1 as the region.
// Since we can read all instances across zones, we don't really use this param.
func testSweepComputeRegionInstanceGroupManager(region string) error {
resourceName := "ComputeRegionInstanceGroupManager"
log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName)

config, err := sharedConfigForRegion(region)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err)
return err
}

err = config.LoadAndValidate(context.Background())
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err)
return err
}

found, err := config.clientCompute.RegionInstanceGroupManagers.List(config.Project, region).Do()
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] Error in response from request: %s", err)
return nil
}

// Keep count of items that aren't sweepable for logging.
nonPrefixCount := 0
for _, rigm := range found.Items {
if !isSweepableTestResource(rigm.Name) {
nonPrefixCount++
continue
}

// Don't wait on operations as we may have a lot to delete
_, err := config.clientCompute.RegionInstanceGroupManagers.Delete(config.Project, region, rigm.Name).Do()
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] Error deleting %s resource %s : %s", resourceName, rigm.Name, err)
} else {
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, rigm.Name)
}
}

if nonPrefixCount > 0 {
log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount)
}

return nil
}

func TestAccRegionInstanceGroupManager_basic(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 4e9b7d8

Please sign in to comment.