File: account_availability_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (91 lines) | stat: -rw-r--r-- 2,711 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package unit

import (
	"context"
	"fmt"
	"testing"

	"github.com/linode/linodego"
	"github.com/stretchr/testify/assert"
)

func TestAccountAvailabilities_List(t *testing.T) {
	fixtureData, err := fixtures.GetFixture("account_availability_list")
	assert.NoError(t, err)

	var base ClientBaseCase
	base.SetUp(t)
	defer base.TearDown(t)

	base.MockGet("account/availability", fixtureData)

	availabilities, err := base.Client.ListAccountAvailabilities(context.Background(), &linodego.ListOptions{})
	assert.NoError(t, err)

	// Check specific region "us-central"
	var usCentralAvailability *linodego.AccountAvailability
	for _, availability := range availabilities {
		if availability.Region == "us-central" {
			usCentralAvailability = &availability
			break
		}
	}
	if usCentralAvailability == nil {
		t.Errorf("Expected region 'us-central' to be in the response, but it was not found")
	} else {
		expectedAvailable := []string{"Linodes", "NodeBalancers", "Block Storage", "Kubernetes"}
		if !equalSlices(usCentralAvailability.Available, expectedAvailable) {
			t.Errorf("Expected available resources for 'us-central' to be %v, but got %v", expectedAvailable, usCentralAvailability.Available)
		}

		if len(usCentralAvailability.Unavailable) != 0 {
			t.Errorf("Expected no unavailable resources for 'us-central', but got %v", usCentralAvailability.Unavailable)
		}
	}

	expectedRegionsCount := 40
	if len(availabilities) != expectedRegionsCount {
		t.Errorf("Expected %d regions, but got %d", expectedRegionsCount, len(availabilities))
	}
}

func TestAccountAvailability_Get(t *testing.T) {
	fixtureData, err := fixtures.GetFixture("account_availability_get")
	assert.NoError(t, err)

	var base ClientBaseCase
	base.SetUp(t)
	defer base.TearDown(t)

	regionID := "us-east"

	base.MockGet(fmt.Sprintf("account/availability/%s", regionID), fixtureData)

	availability, err := base.Client.GetAccountAvailability(context.Background(), regionID)
	assert.NoError(t, err)

	assert.Equal(t, "us-east", availability.Region, "Expected region to be 'us-east'")

	expectedAvailable := []string{"Linodes", "NodeBalancers"}
	assert.ElementsMatch(t, expectedAvailable, availability.Available, "Available resources do not match the expected list")

	expectedUnavailable := []string{"Kubernetes", "Block Storage"}
	assert.ElementsMatch(t, expectedUnavailable, availability.Unavailable, "Unavailable resources do not match the expected list")
}

// Helper function to compare slices in assertion
func equalSlices(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	aMap := make(map[string]bool)
	for _, v := range a {
		aMap[v] = true
	}
	for _, v := range b {
		if !aMap[v] {
			return false
		}
	}
	return true
}