Skip to content

Commit

Permalink
chore(ci): refactor NE acceptance+sweep tests to avoid import cycles
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Jul 16, 2024
1 parent 97fa5b0 commit 20a6b6b
Show file tree
Hide file tree
Showing 19 changed files with 1,659 additions and 578 deletions.
39 changes: 0 additions & 39 deletions equinix/equinix_sweeper_test.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,11 @@
package equinix

import (
"fmt"
"strconv"
"strings"
"testing"
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"

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

const tstResourcePrefix = "tfacc"

func TestMain(m *testing.M) {
resource.TestMain(m)
}

func sharedConfigForRegion(region string) (*config.Config, error) {
endpoint := getFromEnvDefault(config.EndpointEnvVar, config.DefaultBaseURL)
clientToken := getFromEnvDefault(config.ClientTokenEnvVar, "")
clientID := getFromEnvDefault(config.ClientIDEnvVar, "")
clientSecret := getFromEnvDefault(config.ClientSecretEnvVar, "")
clientTimeout := getFromEnvDefault(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)
}
metalAuthToken := getFromEnvDefault(config.MetalAuthTokenEnvVar, "")

if clientToken == "" && (clientID == "" || clientSecret == "") && metalAuthToken == "" {
return nil, fmt.Errorf("To run acceptance tests sweeper, one of '%s' or pair '%s' - '%s' must be set for Equinix Fabric and Network Edge, and '%s' for Equinix Metal",
config.ClientTokenEnvVar, config.ClientIDEnvVar, config.ClientSecretEnvVar, config.MetalAuthTokenEnvVar)
}

return &config.Config{
AuthToken: metalAuthToken,
BaseURL: endpoint,
Token: clientToken,
ClientID: clientID,
ClientSecret: clientSecret,
RequestTimeout: time.Duration(clientTimeoutInt) * time.Second,
}, nil
}

func isSweepableTestResource(namePrefix string) bool {
return strings.HasPrefix(namePrefix, tstResourcePrefix)
}
31 changes: 0 additions & 31 deletions equinix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ var (
}
)

type testAccConfig struct {
ctx map[string]interface{}
config string
}

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
Expand Down Expand Up @@ -112,39 +107,13 @@ func testAccPreCheck(t *testing.T) {
}
}

func newTestAccConfig(ctx map[string]interface{}) *testAccConfig {
return &testAccConfig{
ctx: ctx,
config: "",
}
}

func (t *testAccConfig) build() string {
return t.config
}

func getFromEnv(varName string) (string, error) {
if v := os.Getenv(varName); v != "" {
return v, nil
}
return "", fmt.Errorf("environmental variable '%s' is not set", varName)
}

func getFromEnvDefault(varName string, defaultValue string) string {
if v := os.Getenv(varName); v != "" {
return v
}
return defaultValue
}

func copyMap(source map[string]interface{}) map[string]interface{} {
target := make(map[string]interface{})
for k, v := range source {
target[k] = v
}
return target
}

func setSchemaValueIfNotEmpty(key string, value interface{}, d *schema.ResourceData) error {
if !comparisons.IsEmpty(value) {
return d.Set(key, value)
Expand Down
15 changes: 0 additions & 15 deletions internal/acceptance/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"sync"
"testing"

"github.com/equinix/terraform-provider-equinix/equinix"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/env"
"github.com/equinix/terraform-provider-equinix/internal/provider"
"github.com/equinix/terraform-provider-equinix/version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
terraformsdk "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -33,19 +31,6 @@ var (
testAccProviderConfigure sync.Once
)

func init() {
TestAccProvider = equinix.Provider()
TestAccProviders = map[string]*schema.Provider{
"equinix": TestAccProvider,
}
TestExternalProviders = map[string]resource.ExternalProvider{
"random": {
Source: "hashicorp/random",
},
}
TestAccFrameworkProvider = provider.CreateFrameworkProvider(version.ProviderVersion).(*provider.FrameworkProvider)
}

func TestAccPreCheck(t *testing.T) {
var err error

Expand Down
22 changes: 22 additions & 0 deletions internal/acceptance/acceptance_acc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package acceptance

import (
"github.com/equinix/terraform-provider-equinix/equinix"
"github.com/equinix/terraform-provider-equinix/internal/provider"
"github.com/equinix/terraform-provider-equinix/version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func init() {
TestAccProvider = equinix.Provider()
TestAccProviders = map[string]*schema.Provider{
"equinix": TestAccProvider,
}
TestExternalProviders = map[string]resource.ExternalProvider{
"random": {
Source: "hashicorp/random",
},
}
TestAccFrameworkProvider = provider.CreateFrameworkProvider(version.ProviderVersion).(*provider.FrameworkProvider)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import (
"fmt"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/acceptance"
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

const (
networkDeviceMetroEnvVar = "TF_ACC_NETWORK_DEVICE_METRO"
)

func TestAccDataSourceNetworkAccount_basic(t *testing.T) {
metro, _ := schema.EnvDefaultFunc(networkDeviceMetroEnvVar, "SV")()
context := map[string]interface{}{
Expand All @@ -19,8 +24,8 @@ func TestAccDataSourceNetworkAccount_basic(t *testing.T) {
}
resourceName := fmt.Sprintf("data.equinix_network_account.%s", context["resourceName"].(string))
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheck(t) },
Providers: acceptance.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceNetworkAccountConfig_basic(context),
Expand Down
24 changes: 19 additions & 5 deletions internal/resources/networkedge/acl_template/resource_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,38 @@ import (
"log"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/acceptance"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
"github.com/equinix/terraform-provider-equinix/internal/sweep"

"github.com/equinix/ne-go"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

const (
tstResourcePrefix = "tfacc"
)

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

func copyMap(source map[string]interface{}) map[string]interface{} {
target := make(map[string]interface{})
for k, v := range source {
target[k] = v
}
return target
}

func testSweepNetworkACLTemplate(region string) error {
config, err := sharedConfigForRegion(region)
config, err := sweep.SharedConfigForRegion(region)
if err != nil {
return fmt.Errorf("[INFO][SWEEPER_LOG] Error getting configuration for sweeping Network ACL Templates: %s", err)
}
Expand All @@ -38,7 +52,7 @@ func testSweepNetworkACLTemplate(region string) error {
}
nonSweepableCount := 0
for _, template := range templates {
if !isSweepableTestResource(ne.StringValue(template.Name)) {
if !sweep.IsSweepableTestResource(ne.StringValue(template.Name)) {
nonSweepableCount++
continue
}
Expand Down Expand Up @@ -82,8 +96,8 @@ func TestAccNetworkACLTemplate(t *testing.T) {
resourceName := "equinix_network_acl_template." + context["resourceName"].(string)
var template ne.ACLTemplate
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheck(t) },
Providers: acceptance.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetworkACLTemplate(context),
Expand Down Expand Up @@ -147,7 +161,7 @@ func testAccNetworkACLTemplateExists(resourceName string, template *ne.ACLTempla
if !ok {
return fmt.Errorf("resource not found: %s", resourceName)
}
client := testAccProvider.Meta().(*config.Config).Ne
client := acceptance.TestAccProvider.Meta().(*config.Config).Ne
if rs.Primary.ID == "" {
return fmt.Errorf("resource has no ID attribute set")
}
Expand Down
7 changes: 4 additions & 3 deletions internal/resources/networkedge/bgp/resource_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/equinix/terraform-provider-equinix/internal/acceptance"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/nprintf"

Expand Down Expand Up @@ -68,8 +69,8 @@ func TestAccNetworkBGP_CSR1000V_Single_AWS(t *testing.T) {
resourceName := fmt.Sprintf("equinix_network_bgp.%s", context["bgp-resourceName"].(string))
var bgpConfig ne.BGPConfiguration
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { acceptance.TestAccPreCheck(t) },
Providers: acceptance.TestAccProviders,
Steps: []resource.TestStep{
{
Config: newTestAccConfig(context).withDevice().withSSHUser().withVDConnection().withBGP().build(),
Expand Down Expand Up @@ -142,7 +143,7 @@ func testAccNeBGPExists(resourceName string, bgpConfig *ne.BGPConfiguration) res
if !ok {
return fmt.Errorf("resource not found: %s", resourceName)
}
client := testAccProvider.Meta().(*config.Config).Ne
client := acceptance.TestAccProvider.Meta().(*config.Config).Ne
if rs.Primary.ID == "" {
return fmt.Errorf("resource has no ID attribute set")
}
Expand Down
Loading

0 comments on commit 20a6b6b

Please sign in to comment.