Skip to content

Commit e0f3b86

Browse files
Separate plan availability logic from getRegionsWithCaps (#558)
1 parent 0269a9a commit e0f3b86

18 files changed

+69
-53
lines changed

test/integration/images_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestImage_CreateUpload(t *testing.T) {
107107
defer teardown()
108108

109109
image, uploadURL, err := client.CreateImageUpload(context.Background(), ImageCreateUploadOptions{
110-
Region: getRegionsWithCaps(t, client, []string{"Metadata"}, []string{})[0],
110+
Region: getRegionsWithCaps(t, client, []string{"Metadata"})[0],
111111
Label: "linodego-image-create-upload",
112112
Description: "An image that does stuff.",
113113
CloudInit: true,
@@ -132,7 +132,7 @@ func TestImage_CloudInit(t *testing.T) {
132132
client, instance, teardown, err := setupInstance(
133133
t, "fixtures/TestImage_CloudInit", true,
134134
func(client *Client, options *InstanceCreateOptions) {
135-
options.Region = getRegionsWithCaps(t, client, []string{"Metadata"}, []string{})[0]
135+
options.Region = getRegionsWithCaps(t, client, []string{"Metadata"})[0]
136136
})
137137
if err != nil {
138138
t.Fatal(err)

test/integration/instance_config_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func setupInstanceWithVPCAndNATOneToOne(t *testing.T, fixturesYaml string) (
6767
t,
6868
fixturesYaml,
6969
func(client *Client, opts *InstanceCreateOptions) {
70-
opts.Region = getRegionsWithCaps(t, client, []string{"Linodes", "VPCs"}, []string{})[0]
70+
opts.Region = getRegionsWithCaps(t, client, []string{"Linodes", "VPCs"})[0]
7171
},
7272
)
7373
if err != nil {
@@ -110,7 +110,7 @@ func setupInstanceWith3Interfaces(t *testing.T, fixturesYaml string) (
110110
t,
111111
fixturesYaml,
112112
func(client *Client, opts *InstanceCreateOptions) {
113-
opts.Region = getRegionsWithCaps(t, client, []string{"Linodes", "VPCs"}, []string{})[0]
113+
opts.Region = getRegionsWithCaps(t, client, []string{"Linodes", "VPCs"})[0]
114114
},
115115
)
116116
if err != nil {
@@ -153,7 +153,7 @@ func TestInstance_ConfigInterfaces_AppendDelete(t *testing.T) {
153153
"fixtures/TestInstance_ConfigInterfaces_AppendDelete",
154154
func(client *Client, opts *InstanceCreateOptions) {
155155
// Ensure we're in a region that supports VLANs
156-
opts.Region = getRegionsWithCaps(t, client, []string{"vlans", "VPCs"}, []string{})[0]
156+
opts.Region = getRegionsWithCaps(t, client, []string{"vlans", "VPCs"})[0]
157157
},
158158
)
159159
defer teardown()
@@ -303,7 +303,7 @@ func TestInstance_ConfigInterfaces_Update(t *testing.T) {
303303
"fixtures/TestInstance_ConfigInterfaces_Update",
304304
func(client *Client, opts *InstanceCreateOptions) {
305305
// Ensure we're in a region that supports VLANs
306-
opts.Region = getRegionsWithCaps(t, client, []string{"vlans", "VPCs"}, []string{})[0]
306+
opts.Region = getRegionsWithCaps(t, client, []string{"vlans", "VPCs"})[0]
307307
},
308308
)
309309
defer teardown()
@@ -377,7 +377,7 @@ func TestInstance_ConfigInterface_Update(t *testing.T) {
377377
"fixtures/TestInstance_ConfigInterface_Update",
378378
func(client *Client, opts *InstanceCreateOptions) {
379379
// Ensure we're in a region that supports VLANs
380-
opts.Region = getRegionsWithCaps(t, client, []string{"vlans", "VPCs"}, []string{})[0]
380+
opts.Region = getRegionsWithCaps(t, client, []string{"vlans", "VPCs"})[0]
381381
},
382382
)
383383
defer teardown()

test/integration/instances_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func TestInstance_Rebuild(t *testing.T) {
354354
t,
355355
"fixtures/TestInstance_Rebuild", true,
356356
func(client *linodego.Client, options *linodego.InstanceCreateOptions) {
357-
options.Region = getRegionsWithCaps(t, client, []string{"Metadata"}, []string{})[0]
357+
options.Region = getRegionsWithCaps(t, client, []string{"Metadata"})[0]
358358
},
359359
)
360360
defer teardown()
@@ -392,7 +392,7 @@ func TestInstance_Clone(t *testing.T) {
392392
client, instance, teardownOriginalLinode, err := setupInstance(
393393
t, "fixtures/TestInstance_Clone", true,
394394
func(client *linodego.Client, options *linodego.InstanceCreateOptions) {
395-
targetRegion = getRegionsWithCaps(t, client, []string{"Metadata"}, []string{})[0]
395+
targetRegion = getRegionsWithCaps(t, client, []string{"Metadata"})[0]
396396

397397
options.Region = targetRegion
398398
})
@@ -470,7 +470,7 @@ func TestInstance_withMetadata(t *testing.T) {
470470
options.Metadata = &linodego.InstanceMetadataOptions{
471471
UserData: base64.StdEncoding.EncodeToString([]byte("reallycoolmetadata")),
472472
}
473-
options.Region = getRegionsWithCaps(t, client, []string{"Metadata"}, []string{})[0]
473+
options.Region = getRegionsWithCaps(t, client, []string{"Metadata"})[0]
474474
})
475475
if err != nil {
476476
t.Fatal(err)
@@ -519,7 +519,7 @@ func createInstance(t *testing.T, client *linodego.Client, enableCloudFirewall b
519519
createOpts := linodego.InstanceCreateOptions{
520520
Label: "go-test-ins-" + randLabel(),
521521
RootPass: randPassword(),
522-
Region: getRegionsWithCaps(t, client, []string{"linodes"}, []string{})[0],
522+
Region: getRegionsWithCaps(t, client, []string{"linodes"})[0],
523523
Type: "g6-nanode-1",
524524
Image: "linode/debian9",
525525
Booted: linodego.Pointer(false),
@@ -567,7 +567,7 @@ func createInstanceWithoutDisks(
567567

568568
createOpts := linodego.InstanceCreateOptions{
569569
Label: "go-test-ins-wo-disk-" + randLabel(),
570-
Region: getRegionsWithCaps(t, client, []string{"linodes"}, []string{})[0],
570+
Region: getRegionsWithCaps(t, client, []string{"linodes"})[0],
571571
Type: "g6-nanode-1",
572572
Booted: linodego.Pointer(false),
573573
}

test/integration/integration_suite_test.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/stretchr/testify/require"
15+
1416
"github.com/dnaeon/go-vcr/cassette"
1517
"github.com/dnaeon/go-vcr/recorder"
1618
"github.com/linode/linodego"
@@ -173,31 +175,18 @@ If the plans list is empty, it only checks for the capabilities.
173175
174176
Parameters:
175177
- capabilities: A list of required capabilities that regions must support.
176-
- plans (optional): A list of required plans that must be available in the regions.
177178
178179
Returns:
179-
- string values representing the IDs of regions that meet the given criteria.
180+
- string values representing the IDs of regions that have a given set of capabilities.
180181
*/
181-
func getRegionsWithCaps(t *testing.T, client *linodego.Client, capabilities, plans []string) []string {
182+
func getRegionsWithCaps(t *testing.T, client *linodego.Client, capabilities []string) []string {
182183
result := make([]string, 0)
183184

184185
regions, err := client.ListRegions(context.Background(), nil)
185186
if err != nil {
186187
t.Fatal(err)
187188
}
188189

189-
regionsAvailabilities, err := client.ListRegionsAvailability(context.Background(), nil)
190-
191-
type availKey struct {
192-
Region string
193-
Plan string
194-
}
195-
196-
availMap := make(map[availKey]linodego.RegionAvailability, len(regionsAvailabilities))
197-
for _, avail := range regionsAvailabilities {
198-
availMap[availKey{Region: avail.Region, Plan: avail.Plan}] = avail
199-
}
200-
201190
regionHasCaps := func(r linodego.Region) bool {
202191
capsMap := make(map[string]bool)
203192

@@ -214,12 +203,37 @@ func getRegionsWithCaps(t *testing.T, client *linodego.Client, capabilities, pla
214203
return true
215204
}
216205

217-
// Function to check if a region has the required plans available
218-
regionHasPlans := func(regionID string) bool {
219-
if len(plans) == 0 {
220-
return true
206+
for _, region := range regions {
207+
if region.Status != "ok" || !regionHasCaps(region) {
208+
continue
221209
}
222210

211+
result = append(result, region.ID)
212+
}
213+
214+
return result
215+
}
216+
217+
// getRegionWithCapsAndPlans resolves a list of regions that meet the given capabilities
218+
// and has availability for all the provided plans.
219+
func getRegionsWithCapsAndPlans(t *testing.T, client *linodego.Client, capabilities, plans []string) []string {
220+
regionsWithCaps := getRegionsWithCaps(t, client, capabilities)
221+
222+
regionsAvailabilities, err := client.ListRegionsAvailability(context.Background(), nil)
223+
require.NoError(t, err)
224+
225+
type availKey struct {
226+
Region string
227+
Plan string
228+
}
229+
230+
availMap := make(map[availKey]linodego.RegionAvailability, len(regionsAvailabilities))
231+
for _, avail := range regionsAvailabilities {
232+
availMap[availKey{Region: avail.Region, Plan: avail.Plan}] = avail
233+
}
234+
235+
// Function to check if a region has the required plans available
236+
regionHasPlans := func(regionID string) bool {
223237
for _, plan := range plans {
224238
if avail, ok := availMap[availKey{Region: regionID, Plan: plan}]; !ok || !avail.Available {
225239
return false
@@ -228,12 +242,14 @@ func getRegionsWithCaps(t *testing.T, client *linodego.Client, capabilities, pla
228242
return true
229243
}
230244

231-
for _, region := range regions {
232-
if region.Status != "ok" || !regionHasCaps(region) || !regionHasPlans(region.ID) {
245+
result := make([]string, 0, len(regionsWithCaps))
246+
247+
for _, region := range regionsWithCaps {
248+
if !regionHasPlans(region) {
233249
continue
234250
}
235251

236-
result = append(result, region.ID)
252+
result = append(result, region)
237253
}
238254

239255
return result

test/integration/lke_clusters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func setupLKECluster(t *testing.T, clusterModifiers []clusterModifier, fixturesY
256256

257257
createOpts := linodego.LKEClusterCreateOptions{
258258
Label: label,
259-
Region: getRegionsWithCaps(t, client, []string{"Kubernetes"}, []string{})[0],
259+
Region: getRegionsWithCaps(t, client, []string{"Kubernetes"})[0],
260260
K8sVersion: "1.29",
261261
Tags: []string{"testing"},
262262
NodePools: []linodego.LKENodePoolCreateOptions{{Count: 1, Type: "g6-standard-2", Tags: []string{"test"}}},

test/integration/lke_node_pools_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func TestLKENodePool_CreateWithLabelsAndTaints(t *testing.T) {
189189
Effect: linodego.LKENodePoolTaintEffectNoSchedule,
190190
}},
191191
Count: 1,
192-
Type: "g6-standard-1",
192+
Type: "g6-standard-1",
193193
}
194194
_, _, nodePool, teardown, err := setupLKENodePool(t, "fixtures/TestLKENodePool_CreateWithLabelsAndTaints", createOpts)
195195
if err != nil {

test/integration/mysql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func createMySQLDatabase(t *testing.T, client *linodego.Client,
180180

181181
createOpts := linodego.MySQLCreateOptions{
182182
Label: "go-mysql-test-def" + randLabel(),
183-
Region: getRegionsWithCaps(t, client, []string{"Managed Databases"}, []string{})[0],
183+
Region: getRegionsWithCaps(t, client, []string{"Managed Databases"})[0],
184184
Type: "g6-nanode-1",
185185
Engine: "mysql/8.0.30",
186186
Encrypted: false,

test/integration/nodebalancers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func setupNodeBalancer(t *testing.T, fixturesYaml string) (*linodego.Client, *li
8686
client, fixtureTeardown := createTestClient(t, fixturesYaml)
8787
createOpts := linodego.NodeBalancerCreateOptions{
8888
Label: &label,
89-
Region: getRegionsWithCaps(t, client, []string{"NodeBalancers"}, []string{})[0],
89+
Region: getRegionsWithCaps(t, client, []string{"NodeBalancers"})[0],
9090
ClientConnThrottle: &clientConnThrottle,
9191
FirewallID: GetFirewallID(),
9292
}

test/integration/object_storage_buckets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestObjectStorageBucket_Create_smoke(t *testing.T) {
4141
func TestObjectStorageBucket_Regional(t *testing.T) {
4242
// t.Skip("skipping region test before GA")
4343
client, teardown := createTestClient(t, "fixtures/TestObjectStorageBucket_Regional")
44-
regions := getRegionsWithCaps(t, client, []string{"Object Storage"}, []string{})
44+
regions := getRegionsWithCaps(t, client, []string{"Object Storage"})
4545
if len(regions) < 1 {
4646
t.Fatal("Can't get region with Object Storage capability")
4747
}

test/integration/object_storage_keys_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestObjectStorageKeys_Limited_NoAccess(t *testing.T) {
155155
func TestObjectStorageKeys_Regional_Limited(t *testing.T) {
156156
// t.Skip("skipping region test before GA")
157157
client, teardown := createTestClient(t, "fixtures/TestObjectStorageKeys_Regional_Limited")
158-
regions := getRegionsWithCaps(t, client, []string{"Object Storage"}, []string{})
158+
regions := getRegionsWithCaps(t, client, []string{"Object Storage"})
159159
if len(regions) < 1 {
160160
t.Fatal("Can't get region with Object Storage capability")
161161
}

0 commit comments

Comments
 (0)