Skip to content

Commit

Permalink
chore(ci): update sweeper registration for Metal resources (#593)
Browse files Browse the repository at this point in the history
When we migrated some resources to separate packages under
`internal/resources`, the sweepers for the resources that were moved
stopped running. This adds a new `internal/sweeper` package that sets up
the sweepers for resources under `internal/resources`.

In most cases, that change would be enough by itself, but the Metal
sweepers have some interdependencies, and those interdependencies only
work correctly if every relevant sweeper is registered within the same
TestMain function. To resolve that, thi moves the test sweepers
for Metal resources that are still in `equinix/` from the test file to
the main resource file and adds an `equinix.AddTestSweepers` shim that
allows the sweeper in `internal/sweep` to know about all Metal sweepers
regardless of location.
  • Loading branch information
ctreatma committed Feb 28, 2024
1 parent 12c5b6b commit 1e6d563
Show file tree
Hide file tree
Showing 23 changed files with 543 additions and 421 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/acctest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@ jobs:
METAL_AUTH_TOKEN: ${{ secrets.METAL_AUTH_TOKEN }}
SWEEP: "all" #Flag required to define the regions that the sweeper is to be ran in
SWEEP_ALLOW_FAILURES: "true" #Enable to allow Sweeper Tests to continue after failures
SWEEP_DIR: "./equinix"
run: |
# Added sweep-run to filter Metal test
go test ${SWEEP_DIR} -v -timeout 180m -sweep=${SWEEP} -sweep-allow-failures=${SWEEP_ALLOW_FAILURES} -sweep-run=$(grep -o 'AddTestSweepers("[^"]*"' equinix/resource_metal_* |cut -d '"' -f2 | paste -s -d, -)
go test $(go list ./... | grep 'internal/sweep\|equinix/equinix') -v -timeout 180m -sweep=${SWEEP} -sweep-allow-failures=${SWEEP_ALLOW_FAILURES} -sweep-run=$(grep -or 'AddTestSweepers("[^"]*"' | grep "_metal_" |cut -d '"' -f2 | paste -s -d, -)
- name: Upload coverage to Codecov
if: ${{ always() }}
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TEST ?=$$(go list ./... |grep -v 'vendor'|grep -v 'tests')
INSTALL_DIR =~/.terraform.d/plugins
BINARY =terraform-provider-equinix
SWEEP ?=all #Flag required to define the regions that the sweeper is to be ran in
SWEEP_DIR ?=./equinix
SWEEP_DIR ?=$$(go list ./... | grep 'internal/sweep\|equinix/equinix')
SWEEP_ARGS =-timeout 60m
ACCTEST_TIMEOUT ?= 180m
ACCTEST_PARALLELISM ?= 8
Expand Down
58 changes: 0 additions & 58 deletions equinix/resource_metal_device_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package equinix
import (
"context"
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"
Expand All @@ -13,7 +12,6 @@ import (
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"
"golang.org/x/exp/slices"

"github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/google/uuid"
Expand All @@ -30,62 +28,6 @@ var (
preferable_os = []string{"ubuntu_20_04"}
)

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

func testSweepDevices(region string) error {
log.Printf("[DEBUG] Sweeping devices")
ctx := context.Background()
config, err := sharedConfigForRegion(region)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping devices: %s", err)
}
metal := config.NewMetalClientForTesting()
ps, _, err := metal.ProjectsApi.FindProjects(ctx).Execute()
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting project list for sweepeing devices: %s", err)
}
pids := []string{}
for _, p := range ps.Projects {
if isSweepableTestResource(p.GetName()) {
pids = append(pids, p.GetId())
}
}
dids := []string{}
for _, pid := range pids {
ds, _, err := metal.DevicesApi.FindProjectDevices(ctx, pid).Execute()
if err != nil {
log.Printf("Error listing devices to sweep: %s", err)
continue
}
for _, d := range ds.Devices {
if isSweepableTestResource(d.GetHostname()) {
nonSweepableDeviceStates := []metalv1.DeviceState{
metalv1.DEVICESTATE_PROVISIONING,
metalv1.DEVICESTATE_DEPROVISIONING,
}
if slices.Contains(nonSweepableDeviceStates, d.GetState()) {
log.Printf("[WARNING] skipping sweep for device %s because it is still %s", d.GetId(), d.GetState())
} else {
dids = append(dids, d.GetId())
}
}
}
}

for _, did := range dids {
_, err := metal.DevicesApi.DeleteDevice(ctx, did).Execute()
if err != nil {
return fmt.Errorf("Error deleting device %s", err)
}
}
return nil
}

// Regexp vars for use with resource.ExpectError
var (
matchErrMustBeProvided = regexp.MustCompile(".* must be provided when .*")
Expand Down
36 changes: 0 additions & 36 deletions equinix/resource_metal_organization_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package equinix

import (
"fmt"
"log"
"testing"
"time"

Expand All @@ -14,41 +13,6 @@ import (
"github.com/packethost/packngo"
)

func init() {
resource.AddTestSweepers("equinix_metal_organization", &resource.Sweeper{
Name: "equinix_metal_organization",
Dependencies: []string{"equinix_metal_project"},
F: testSweepOrganizations,
})
}

func testSweepOrganizations(region string) error {
log.Printf("[DEBUG] Sweeping organizations")
config, err := sharedConfigForRegion(region)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping organizations: %s", err)
}
metal := config.NewMetalClient()
os, _, err := metal.Organizations.List(nil)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting org list for sweeping organizations: %s", err)
}
oids := []string{}
for _, o := range os {
if isSweepableTestResource(o.Name) {
oids = append(oids, o.ID)
}
}
for _, oid := range oids {
log.Printf("Removing organization %s", oid)
_, err := metal.Organizations.Delete(oid)
if err != nil {
return fmt.Errorf("Error deleting organization %s", err)
}
}
return nil
}

func TestAccMetalOrganization_create(t *testing.T) {
var org, org2 packngo.Organization
rInt := acctest.RandInt()
Expand Down
44 changes: 1 addition & 43 deletions equinix/resource_metal_user_api_key_acc_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,6 @@
package equinix

import (
"fmt"
"log"
"net/http"

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

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

// Will remove all User API keys with Description starting with "tfacc-"
func testSweepUserAPIKeys(region string) error {
log.Printf("[DEBUG] Sweeping user_api keys")
config, err := sharedConfigForRegion(region)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping user_api keys: %s", err)
}
metal := config.NewMetalClient()
userApiKeys, _, err := metal.APIKeys.UserList(nil)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting list for sweeping user_api keys: %s", err)
}
ids := []string{}
for _, k := range userApiKeys {
if isSweepableTestResource(k.Description) {
ids = append(ids, k.ID)
}
}
for _, id := range ids {
log.Printf("Removing user api key %s", id)
resp, err := metal.APIKeys.Delete(id)
if err != nil && resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("Error deleting user_api key %s", err)
}
}
return nil
}
// "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

// Commented out because it lists existing user API keys in debug log

Expand Down
50 changes: 0 additions & 50 deletions equinix/resource_metal_virtual_circuit_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package equinix

import (
"fmt"
"log"
"os"
"testing"

Expand All @@ -11,61 +10,12 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/packethost/packngo"
)

const (
metalDedicatedConnIDEnvVar = "TF_ACC_METAL_DEDICATED_CONNECTION_ID"
)

func init() {
resource.AddTestSweepers("equinix_metal_virtual_circuit", &resource.Sweeper{
Name: "equinix_metal_virtual_circuit",
Dependencies: []string{},
F: testSweepVirtualCircuits,
})
}

func testSweepVirtualCircuits(region string) error {
log.Printf("[DEBUG] Sweeping VirtualCircuits")
config, err := sharedConfigForRegion(region)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping VirtualCircuits: %s", err)
}
metal := config.NewMetalClient()
orgList, _, err := metal.Organizations.List(nil)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting organization list for sweeping VirtualCircuits: %s", err)
}
vcs := map[string]*packngo.VirtualCircuit{}
for _, org := range orgList {
conns, _, err := metal.Connections.OrganizationList(org.ID, &packngo.GetOptions{Includes: []string{"ports"}})
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting connections list for sweeping VirtualCircuits: %s", err)
}
for _, conn := range conns {
if conn.Type != packngo.ConnectionShared {
for _, port := range conn.Ports {
for _, vc := range port.VirtualCircuits {
if isSweepableTestResource(vc.Name) {
vcs[vc.ID] = &vc
}
}
}
}
}
}
for _, vc := range vcs {
log.Printf("[INFO][SWEEPER_LOG] Deleting VirtualCircuit: %s", vc.Name)
_, err := metal.VirtualCircuits.Delete(vc.ID)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error deleting VirtualCircuit: %s", err)
}
}

return nil
}

func testAccMetalVirtualCircuitCheckDestroyed(s *terraform.State) error {
client := testAccProvider.Meta().(*config.Config).Metal

Expand Down
50 changes: 0 additions & 50 deletions equinix/resource_metal_vlan_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package equinix

import (
"fmt"
"log"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/config"
Expand All @@ -13,55 +12,6 @@ import (
"github.com/packethost/packngo"
)

func init() {
resource.AddTestSweepers("equinix_metal_vlan", &resource.Sweeper{
Name: "equinix_metal_vlan",
Dependencies: []string{"equinix_metal_virtual_circuit", "equinix_metal_vrf", "equinix_metal_device"},
F: testSweepVlans,
})
}

func testSweepVlans(region string) error {
log.Printf("[DEBUG] Sweeping vlans")
config, err := sharedConfigForRegion(region)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping vlans: %s", err)
}
metal := config.NewMetalClient()
ps, _, err := metal.Projects.List(nil)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting project list for sweeping vlans: %s", err)
}
pids := []string{}
for _, p := range ps {
if isSweepableTestResource(p.Name) {
pids = append(pids, p.ID)
}
}
dids := []string{}
for _, pid := range pids {
ds, _, err := metal.ProjectVirtualNetworks.List(pid, nil)
if err != nil {
log.Printf("Error listing vlans to sweep: %s", err)
continue
}
for _, d := range ds.VirtualNetworks {
if isSweepableTestResource(d.Description) {
dids = append(dids, d.ID)
}
}
}

for _, did := range dids {
log.Printf("Removing vlan %s", did)
_, err := metal.ProjectVirtualNetworks.Delete(did)
if err != nil {
return fmt.Errorf("Error deleting vlan %s", err)
}
}
return nil
}

func testAccCheckMetalVlanConfig_metro(projSuffix, metro, desc string) string {
return fmt.Sprintf(`
resource "equinix_metal_project" "foobar" {
Expand Down
Loading

0 comments on commit 1e6d563

Please sign in to comment.