Skip to content

Commit

Permalink
test: Add RegionType to test region helper (#1548)
Browse files Browse the repository at this point in the history
* try # 1

* adding region type option to helper function

* make format

* add submodule checkout in e2e pr workflow

* adding any as an option for regionType

* giving default value for long running tests in integration_tests_pr.yml

* getting rid of grep in Makefile for proper exit codes

* Fix unit tests in cluster_test.go
  • Loading branch information
ykim-akamai authored Aug 13, 2024
1 parent 8bab035 commit 8cacb84
Show file tree
Hide file tree
Showing 47 changed files with 93 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/integration_tests_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ on:
run_long_tests:
description: 'Setting this value will skip long running tests (e.g. Database related cases): (true|false)'
required: false
default: false


name: Integration tests on PR
Expand Down Expand Up @@ -49,6 +50,8 @@ jobs:
uses: actions/checkout@v4
with:
ref: ${{ inputs.sha }}
fetch-depth: 0
submodules: 'recursive'

- run: make deps

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ COUNT?=1
PARALLEL?=10
PKG_NAME=linode/...
TIMEOUT?=240m
RUN_LONG_TESTS?=False
RUN_LONG_TESTS?=false
SWEEP?="tf_test,tf-test"
TEST_TAGS="integration"

Expand Down Expand Up @@ -61,7 +61,7 @@ test: fmt-check smoke-test unit-test int-test

.PHONY: unit-test
unit-test: fmt-check
go test -v --tags=unit ./$(PKG_NAME) | grep -v "\[no test files\]"
go test -v --tags=unit ./$(PKG_NAME)

.PHONY: int-test
int-test: fmt-check generate-ip-env-fw-e2e include-env
Expand All @@ -70,7 +70,7 @@ int-test: fmt-check generate-ip-env-fw-e2e include-env
RUN_LONG_TESTS=$(RUN_LONG_TESTS) \
TF_VAR_ipv4_addr=${PUBLIC_IPV4} \
TF_VAR_ipv6_addr=${PUBLIC_IPV6} \
go test --tags="$(TEST_TAGS)" -v ./$(PKG_NAME) -count $(COUNT) -timeout $(TIMEOUT) -ldflags="-X=github.com/linode/terraform-provider-linode/v2/version.ProviderVersion=acc" -parallel=$(PARALLEL) $(ARGS) | grep -v "\[no test files\]"
go test --tags="$(TEST_TAGS)" -v ./$(PKG_NAME) -count $(COUNT) -timeout $(TIMEOUT) -ldflags="-X=github.com/linode/terraform-provider-linode/v2/version.ProviderVersion=acc" -parallel=$(PARALLEL) $(ARGS)

.PHONY: include-env
include-env: $(IP_ENV_FILE)
Expand All @@ -87,7 +87,7 @@ smoke-test: fmt-check
TF_ACC=1 \
LINODE_API_VERSION="v4beta" \
RUN_LONG_TESTS=$(RUN_LONG_TESTS) \
go test -v -run smoke ./linode/... -count $(COUNT) -timeout $(TIMEOUT) -parallel=$(PARALLEL) -ldflags="-X=github.com/linode/terraform-provider-linode/v2/version.ProviderVersion=acc" | grep -v "\[no test files\]"
go test -v -run smoke ./linode/... -count $(COUNT) -timeout $(TIMEOUT) -parallel=$(PARALLEL) -ldflags="-X=github.com/linode/terraform-provider-linode/v2/version.ProviderVersion=acc"

.PHONY: docs-check
docs-check:
Expand Down
20 changes: 15 additions & 5 deletions linode/acceptance/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,12 @@ func ModifyProviderMeta(provider *schema.Provider, modifier ProviderMetaModifier
}
}

// GetRegionsWithCaps returns a list of regions that support the given capabilities.
func GetRegionsWithCaps(capabilities []string, filters ...RegionFilterFunc) ([]string, error) {
// GetRegionsWithCaps returns a list of region IDs that support the given capabilities
// Parameters:
// - capabilities: Required capabilities that the regions must support.
// - siteType: The site type to filter by ("core" or "distributed" or "any").
// - filters: Optional custom filters for additional criteria.
func GetRegionsWithCaps(capabilities []string, regionType string, filters ...RegionFilterFunc) ([]string, error) {
client, err := GetTestClient()
if err != nil {
return nil, err
Expand All @@ -582,8 +586,14 @@ func GetRegionsWithCaps(capabilities []string, filters ...RegionFilterFunc) ([]s
return nil, err
}

// Filter on capabilities
// Filter on capabilities and site type
regionsWithCaps := slices.DeleteFunc(regions, func(region linodego.Region) bool {
// Check if the site type matches
// Skip site type check if "any" is passed
if !strings.EqualFold(regionType, "any") && !strings.EqualFold(region.SiteType, regionType) {
return true
}

capsMap := make(map[string]bool)

for _, c := range region.Capabilities {
Expand Down Expand Up @@ -620,8 +630,8 @@ func GetRegionsWithCaps(capabilities []string, filters ...RegionFilterFunc) ([]s
}

// GetRandomRegionWithCaps gets a random region given a list of region capabilities.
func GetRandomRegionWithCaps(capabilities []string, filters ...RegionFilterFunc) (string, error) {
regions, err := GetRegionsWithCaps(capabilities, filters...)
func GetRandomRegionWithCaps(capabilities []string, regionType string, filters ...RegionFilterFunc) (string, error) {
regions, err := GetRegionsWithCaps(capabilities, regionType, filters...)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion linode/accountavailability/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccDataSourceNodeBalancers_basic(t *testing.T) {

resourceName := "data.linode_account_availability.foobar"

region, err := acceptance.GetRandomRegionWithCaps(nil)
region, err := acceptance.GetRandomRegionWithCaps(nil, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/backup/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps(nil)
region, err := acceptance.GetRandomRegionWithCaps(nil, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/databaseaccesscontrols/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {

postgresEngineVersion = v.ID

region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/databasebackups/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {

engineVersion = v.ID

region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/databasemysql/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {

engineVersion = v.ID

region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/databasemysqlbackups/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {

engineVersion = v.ID

region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/databasepostgresql/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {

engineVersion = v.ID

region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/databases/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {

engineVersion = v.ID

region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Managed Databases"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/firewall/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
F: sweep,
})

region, err := acceptance.GetRandomRegionWithCaps([]string{"Cloud Firewall", "NodeBalancers"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Cloud Firewall", "NodeBalancers"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/firewalldevice/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps([]string{"Cloud Firewall", "NodeBalancers"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Cloud Firewall", "NodeBalancers"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/firewalls/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const testFirewallDataName = "data.linode_firewalls.test"
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps([]string{"Linodes"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Linodes"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/image/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func init() {
F: sweep,
})

region, err := acceptance.GetRandomRegionWithCaps(nil)
region, err := acceptance.GetRandomRegionWithCaps(nil, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/images/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps(nil)
region, err := acceptance.GetRandomRegionWithCaps(nil, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions linode/instance/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {

region, err := acceptance.GetRandomRegionWithCaps([]string{
linodego.CapabilityVlans, linodego.CapabilityVPCs, linodego.CapabilityDiskEncryption,
})
}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -2063,7 +2063,7 @@ func TestAccResourceInstance_userData(t *testing.T) {
var instance linodego.Instance
instanceName := acctest.RandomWithPrefix("tf_test")

region, err := acceptance.GetRandomRegionWithCaps([]string{"Metadata"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Metadata"}, "core")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -2165,7 +2165,7 @@ func TestAccResourceInstance_firewallOnCreation(t *testing.T) {
var instance linodego.Instance
instanceName := acctest.RandomWithPrefix("tf_test")

region, err := acceptance.GetRandomRegionWithCaps([]string{"Cloud Firewall"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Cloud Firewall"}, "core")
rootPass := acctest.RandString(12)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -2329,7 +2329,7 @@ func TestAccResourceInstance_migration(t *testing.T) {

// Resolve a region to migrate to
targetRegion, err := acceptance.GetRandomRegionWithCaps(
[]string{"Linodes"},
[]string{"Linodes"}, "core",
func(v linodego.Region) bool {
return v.ID != testRegion
},
Expand Down Expand Up @@ -2386,7 +2386,7 @@ func TestAccResourceInstance_withPG(t *testing.T) {

// Resolve a region with support for PGs
targetRegion, err := acceptance.GetRandomRegionWithCaps(
[]string{"Linodes", "Placement Group"},
[]string{"Linodes", "Placement Group"}, "core",
)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -2433,7 +2433,7 @@ func TestAccResourceInstance_pgAssignment(t *testing.T) {

// Resolve a region with support for PGs
testRegion, err := acceptance.GetRandomRegionWithCaps(
[]string{"Linodes", "Placement Group"},
[]string{"Linodes", "Placement Group"}, "core",
)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -2523,7 +2523,7 @@ func TestAccResourceInstance_diskEncryption(t *testing.T) {

// Resolve a region that supports disk encryption
targetRegion, err := acceptance.GetRandomRegionWithCaps(
[]string{linodego.CapabilityLinodes, linodego.CapabilityDiskEncryption},
[]string{linodego.CapabilityLinodes, linodego.CapabilityDiskEncryption}, "core",
)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion linode/instanceconfig/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps([]string{"vlans", "VPCs"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"vlans", "VPCs"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/instancedisk/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps(nil)
region, err := acceptance.GetRandomRegionWithCaps(nil, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/instanceip/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const testInstanceIPResName = "linode_instance_ip.test"
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps(nil)
region, err := acceptance.GetRandomRegionWithCaps(nil, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/instancenetworking/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const testInstanceNetworkResName = "data.linode_instance_networking.test"
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps([]string{"VPCs", "Linodes"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"VPCs", "Linodes"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion linode/ipv6ranges/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var testRegion string

func init() {
region, err := acceptance.GetRandomRegionWithCaps([]string{"Linodes"})
region, err := acceptance.GetRandomRegionWithCaps([]string{"Linodes"}, "core")
if err != nil {
log.Fatal(err)
}
Expand Down
Loading

0 comments on commit 8cacb84

Please sign in to comment.