Skip to content

Commit

Permalink
test: Fabric Connections test sweeper (#720)
Browse files Browse the repository at this point in the history
* Add test sweeper for fabric connections
* Modify internal/sweep package with constants, list var, and methods to
support test sweeping for fabric
* Add config for fabric test client
  • Loading branch information
thogarty authored Jul 18, 2024
1 parent bebfae0 commit 2633c91
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 18 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/fabric_acctest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ jobs:
SWEEP_DIR: "./equinix"
run: |
# Added sweep-run to filter Fabric PNFV test
go test ${SWEEP_DIR} -v -timeout 180m -sweep=${SWEEP} -sweep-allow-failures=${SWEEP_ALLOW_FAILURES} -sweep-run=$(grep -o 'AddTestSweepers("[^"]*PNFV"' equinix/resource_fabric_* | 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 "_fabric_" | cut -d '"' -f2 | paste -s -d, -)
- name: Upload coverage to Codecov
if: ${{ always() }}
Expand Down Expand Up @@ -194,10 +194,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 Fabric PFCR test
go test ${SWEEP_DIR} -v -timeout 180m -sweep=${SWEEP} -sweep-allow-failures=${SWEEP_ALLOW_FAILURES} -sweep-run=$(grep -o 'AddTestSweepers("[^"]*PFCR"' equinix/resource_fabric_* | 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 "_fabric_" | cut -d '"' -f2 | paste -s -d, -)
- name: Upload coverage to Codecov
if: ${{ always() }}
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ func (c *Config) NewFabricClientForSDK(d *schema.ResourceData) *fabricv4.APIClie
return client
}

// Shim for Fabric tests.
// Deprecated: when the acceptance package starts to contain API clients for testing/cleanup this will move with them
func (c *Config) NewFabricClientForTesting() *fabricv4.APIClient {
client := c.newFabricClient()

client.GetConfig().UserAgent = fmt.Sprintf("tf-acceptance-tests %v", client.GetConfig().UserAgent)

return client
}

// newFabricClient returns the base fabricv4 client that is then used for either the sdkv2 or framework
// implementations of the Terraform Provider with exported Methods
func (c *Config) newFabricClient() *fabricv4.APIClient {
Expand Down
11 changes: 0 additions & 11 deletions internal/resources/fabric/connection/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ import (
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func init() {
resource.AddTestSweepers("equinix_fabric_connection_PNFV", &resource.Sweeper{
Name: "equinix_fabric_connection",
F: testSweepConnections,
})
}

func testSweepConnections(region string) error {
return nil
}

func TestAccFabricCreatePort2SPConnection_PPDS(t *testing.T) {
ports := testing_helpers.GetFabricEnvPorts(t)
connectionsTestData := testing_helpers.GetFabricEnvConnectionTestData(t)
Expand Down
74 changes: 74 additions & 0 deletions internal/resources/fabric/connection/sweeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package connection

import (
"context"
"errors"
"fmt"
"github.com/equinix/equinix-sdk-go/services/fabricv4"
equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
"github.com/equinix/terraform-provider-equinix/internal/sweep"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"log"
)

func AddTestSweeper() {
resource.AddTestSweepers("equinix_fabric_connection", &resource.Sweeper{
Name: "equinix_fabric_connection",
Dependencies: []string{},
F: testSweepConnections,
})
}

func testSweepConnections(region string) error {
var errs []error
log.Printf("[DEBUG] Sweeping Fabric Connections")
ctx := context.Background()
meta, err := sweep.GetConfigForFabric()
if err != nil {
return fmt.Errorf("error getting configuration for sweeping Conections: %s", err)
}
meta.Load(ctx)

Check failure on line 30 in internal/resources/fabric/connection/sweeper.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `meta.Load` is not checked (errcheck)
fabric := meta.NewFabricClientForTesting()

name := fabricv4.SEARCHFIELDNAME_NAME
equinixStatus := fabricv4.SEARCHFIELDNAME_OPERATION_EQUINIX_STATUS
likeOperator := fabricv4.EXPRESSIONOPERATOR_LIKE
equalOperator := fabricv4.EXPRESSIONOPERATOR_EQUAL
limit := int32(100)
connectionsSearchRequest := fabricv4.SearchRequest{
Filter: &fabricv4.Expression{
And: []fabricv4.Expression{
{
Property: &name,
Operator: &likeOperator,
Values: sweep.FabricTestResourceSuffixes,
},
{
Property: &equinixStatus,
Operator: &equalOperator,
Values: []string{string(fabricv4.EQUINIXSTATUS_PROVISIONED)},
},
},
},
Pagination: &fabricv4.PaginationRequest{
Limit: &limit,
},
}

fabricConnections, _, err := fabric.ConnectionsApi.SearchConnections(ctx).SearchRequest(connectionsSearchRequest).Execute()
if err != nil {
return fmt.Errorf("error getting connections list for sweeping fabric connections: %s", err)
}

for _, connection := range fabricConnections.Data {
if sweep.IsSweepableFabricTestResource(connection.GetName()) {
log.Printf("[DEBUG] Deleting Connection: %s", connection.GetName())
_, resp, err := fabric.ConnectionsApi.DeleteConnectionByUuid(ctx, connection.GetUuid()).Execute()
if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil {
errs = append(errs, fmt.Errorf("error deleting fabric connection: %s", err))
}
}
}

return errors.Join(errs...)
}
43 changes: 40 additions & 3 deletions internal/sweep/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,57 @@ import (

const (
// duplicated from equinix_sweeoer_test.go
testResourcePrefix = "tfacc"
missingMetalToken = "to run sweepers of Equinix Metal Resources, you must set %s"
testResourcePrefix = "tfacc"
cannotConvertTimeoutToInt = "cannot convert value of '%s' env variable to int"
missingFabricSecrets = "missing fabric clientId - %s, and clientSecret - %s"
missingMetalToken = "to run sweepers of Equinix Metal Resources, you must set %s"
)

var (
FabricTestResourceSuffixes = []string{"_PFCR", "_PNFV", "_PPDS"}
)

func IsSweepableTestResource(namePrefix string) bool {
return strings.HasPrefix(namePrefix, testResourcePrefix)
}

func IsSweepableFabricTestResource(resourceName string) bool {
for _, suffix := range FabricTestResourceSuffixes {
if strings.HasSuffix(resourceName, suffix) {
return true
}
}
return false
}

func GetConfigForFabric() (*config.Config, error) {
endpoint := env.GetWithDefault(config.EndpointEnvVar, config.DefaultBaseURL)
clientId := env.GetWithDefault(config.ClientIDEnvVar, "")
clientSecret := env.GetWithDefault(config.ClientSecretEnvVar, "")
if clientId == "" || clientSecret == "" {
return nil, fmt.Errorf(missingFabricSecrets, config.ClientIDEnvVar, config.ClientSecretEnvVar)
}

clientTimeout := env.GetWithDefault(config.ClientTimeoutEnvVar, strconv.Itoa(config.DefaultTimeout))
clientTimeoutInt, err := strconv.Atoi(clientTimeout)
if err != nil {
return nil, fmt.Errorf(cannotConvertTimeoutToInt, config.ClientTimeoutEnvVar)
}

return &config.Config{
BaseURL: endpoint,
ClientID: clientId,
ClientSecret: clientSecret,
RequestTimeout: time.Duration(clientTimeoutInt) * time.Second,
}, nil
}

func GetConfigForMetal() (*config.Config, error) {
endpoint := env.GetWithDefault(config.EndpointEnvVar, config.DefaultBaseURL)
clientTimeout := env.GetWithDefault(config.ClientTimeoutEnvVar, strconv.Itoa(config.DefaultTimeout))
clientTimeoutInt, err := strconv.Atoi(clientTimeout)
if err != nil {
return nil, fmt.Errorf("cannot convert value of '%s' env variable to int", config.ClientTimeoutEnvVar)
return nil, fmt.Errorf(cannotConvertTimeoutToInt, config.ClientTimeoutEnvVar)
}
metalAuthToken := env.GetWithDefault(config.MetalAuthTokenEnvVar, "")

Expand Down
5 changes: 4 additions & 1 deletion internal/sweep/sweep_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package sweep_test

import (
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vlan"
"testing"

fabric_connection "github.com/equinix/terraform-provider-equinix/internal/resources/fabric/connection"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/connection"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/device"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/organization"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/project"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/ssh_key"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/user_api_key"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/virtual_circuit"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vlan"
"github.com/equinix/terraform-provider-equinix/internal/resources/metal/vrf"

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

Expand All @@ -25,6 +27,7 @@ func TestMain(m *testing.M) {
func addTestSweepers() {
connection.AddTestSweeper()
device.AddTestSweeper()
fabric_connection.AddTestSweeper()
organization.AddTestSweeper()
project.AddTestSweeper()
ssh_key.AddTestSweeper()
Expand Down

0 comments on commit 2633c91

Please sign in to comment.