Skip to content

test: Update getRegionsWithCaps function to also accept a list of plans that must be available in the identified region#555

Merged
ykim-akamai merged 4 commits intolinode:mainfrom
ykim-akamai:test/add_optional_filter_to_helper_function
Jul 19, 2024
Merged

test: Update getRegionsWithCaps function to also accept a list of plans that must be available in the identified region#555
ykim-akamai merged 4 commits intolinode:mainfrom
ykim-akamai:test/add_optional_filter_to_helper_function

Conversation

@ykim-akamai
Copy link
Contributor

📝 Description

A helper function has been updated for the logic to resolve integration test regions that also accepts a list of plans which must be available in the resolved region

Note: the function takes list value of plans as an optional parameter

✔️ How to Test

make testint

📷 Preview

If applicable, include a screenshot or code snippet of this change. Otherwise, please remove this section.

@ykim-akamai ykim-akamai requested a review from a team as a code owner July 19, 2024 00:53
@ykim-akamai ykim-akamai requested review from yec-akamai and zliang-akamai and removed request for a team July 19, 2024 00:53
}

// Function to check if a region has the required plans available
regionHasPlans := func(regionID string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice use of closures!

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 {
Copy link
Contributor

@lgarber-akamai lgarber-akamai Jul 19, 2024

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):

regionsAvailabilities, err := client.ListRegionsAvailability(context.Background(), nil)
require.NoError(t, err)

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
}

// ...

regionHasPlans := func(regionID string) bool {
	for _, plan := range plans {
		if avail, ok := availMap[availKey{Region: regionID, Plan: plan}]; !ok || !avail.Available {
			return false
		}
	}

	return true
}

Since this is just for tests I don't think it's a big deal either way; just thought I'd share another approach 👍

Copy link
Contributor Author

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 🚀

Copy link
Contributor

@yec-akamai yec-akamai left a comment

Choose a reason for hiding this comment

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

Tests works well locally, nice work!


// Function to check if a region has the required plans available
regionHasPlans := func(regionID string) bool {
if len(plans) == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

@lgarber-akamai lgarber-akamai left a comment

Choose a reason for hiding this comment

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

Looks great once fixtures are re-run, nice work!

@ykim-akamai ykim-akamai merged commit 81546f3 into linode:main Jul 19, 2024
@jriddle-linode jriddle-linode added the new-feature for new features in the changelog. label Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature for new features in the changelog.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants