Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Fabric Connections test sweeper #720

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -121,7 +121,7 @@
}

authClient.Timeout = c.requestTimeout()
authClient.Transport = logging.NewTransport("Equinix", authClient.Transport)

Check failure on line 124 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: logging.NewTransport is deprecated: This will log the content of every http request/response at `[DEBUG]` level, without any filtering. Any sensitive information will appear as-is in your logs. Please use NewSubsystemLoggingHTTPTransport instead. (staticcheck)
c.authClient = authClient
neClient := ne.NewClient(ctx, c.BaseURL, authClient)

Expand Down Expand Up @@ -149,10 +149,20 @@
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 {
transport := logging.NewTransport("Equinix Fabric (fabricv4)", c.authClient.Transport)

Check failure on line 165 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: logging.NewTransport is deprecated: This will log the content of every http request/response at `[DEBUG]` level, without any filtering. Any sensitive information will appear as-is in your logs. Please use NewSubsystemLoggingHTTPTransport instead. (staticcheck)

retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Transport = transport
Expand Down Expand Up @@ -183,7 +193,7 @@
// Deprecated: migrate to NewMetalClientForSdk or NewMetalClientForFramework instead
func (c *Config) NewMetalClient() *packngo.Client {
transport := http.DefaultTransport
transport = logging.NewTransport("Equinix Metal (packngo)", transport)

Check failure on line 196 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: logging.NewTransport is deprecated: This will log the content of every http request/response at `[DEBUG]` level, without any filtering. Any sensitive information will appear as-is in your logs. Please use NewSubsystemLoggingHTTPTransport instead. (staticcheck)
retryClient := retryablehttp.NewClient()
retryClient.HTTPClient.Transport = transport
retryClient.RetryMax = c.MaxRetries
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant for this PR or even for the Fabric team but the existing Metal sweepers in this file should all be renamed to metal_<resource>.

organization.AddTestSweeper()
project.AddTestSweeper()
ssh_key.AddTestSweeper()
Expand Down
Loading