-
Notifications
You must be signed in to change notification settings - Fork 81
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: Update getRegionsWithCaps function to also accept a list of plans that must be available in the identified region #555
Merged
ykim-1
merged 4 commits into
linode:main
from
ykim-1:test/add_optional_filter_to_helper_function
Jul 19, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8d8c73b
add plans filter for helper function getRegionsWithCaps
ykim-1 0784e5d
addressing pr comment, putting into availability map to increase effi…
ykim-1 7c812a6
Merge branch 'main' into test/add_optional_filter_to_helper_function
ykim-1 9fd0e32
Merge branch 'main' into test/add_optional_filter_to_helper_function
ykim-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,15 +166,38 @@ func transportRecorderWrapper(t *testing.T, fixtureYaml string) (transport.Wrapp | |
}, teardown | ||
} | ||
|
||
// getRegionsWithCaps returns a list of regions that support the given capabilities. | ||
func getRegionsWithCaps(t *testing.T, client *linodego.Client, capabilities []string) []string { | ||
/* | ||
Helper function getRegionsWithCaps returns a list of regions that support the given capabilities and plans. | ||
It filters regions based on their capabilities and the availability of specified plans. | ||
If the plans list is empty, it only checks for the capabilities. | ||
|
||
Parameters: | ||
- capabilities: A list of required capabilities that regions must support. | ||
- plans (optional): A list of required plans that must be available in the regions. | ||
|
||
Returns: | ||
- string values representing the IDs of regions that meet the given criteria. | ||
*/ | ||
func getRegionsWithCaps(t *testing.T, client *linodego.Client, capabilities, plans []string) []string { | ||
result := make([]string, 0) | ||
|
||
regions, err := client.ListRegions(context.Background(), nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
regionsAvailabilities, err := client.ListRegionsAvailability(context.Background(), nil) | ||
|
||
type availKey struct { | ||
Region string | ||
Plan string | ||
} | ||
|
||
availMap := make(map[availKey]linodego.RegionAvailability, len(regionsAvailabilities)) | ||
for _, avail := range regionsAvailabilities { | ||
availMap[availKey{Region: avail.Region, Plan: avail.Plan}] = avail | ||
} | ||
|
||
regionHasCaps := func(r linodego.Region) bool { | ||
capsMap := make(map[string]bool) | ||
|
||
|
@@ -191,8 +214,22 @@ func getRegionsWithCaps(t *testing.T, client *linodego.Client, capabilities []st | |
return true | ||
} | ||
|
||
// Function to check if a region has the required plans available | ||
regionHasPlans := func(regionID string) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of closures! |
||
if len(plans) == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still left this check in to avoid any unnecessary map lookups when there are no plans to check. |
||
return true | ||
} | ||
|
||
for _, plan := range plans { | ||
if avail, ok := availMap[availKey{Region: regionID, Plan: plan}]; !ok || !avail.Available { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
for _, region := range regions { | ||
if region.Status != "ok" || !regionHasCaps(region) { | ||
if region.Status != "ok" || !regionHasCaps(region) || !regionHasPlans(region.ID) { | ||
continue | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: We can considerably reduce the number of iterations we need to run by aggregating all availabilities into a map hashing on (region, plan):
Since this is just for tests I don't think it's a big deal either way; just thought I'd share another approach 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great suggestion! I didn't think of putting it into a hash map but this will for sure be faster than my previous solution 🚀