-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlocation_integration_test.go
145 lines (114 loc) · 4.64 KB
/
location_integration_test.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package megaport
import (
"context"
"log/slog"
"os"
"testing"
"github.com/stretchr/testify/suite"
)
// LocationIntegrationTestSuite is the integration test suite for the Location service
type LocationIntegrationTestSuite IntegrationTestSuite
func TestLocationIntegrationTestSuite(t *testing.T) {
t.Parallel()
if *runIntegrationTests {
suite.Run(t, new(LocationIntegrationTestSuite))
}
}
func (suite *LocationIntegrationTestSuite) SetupSuite() {
accessKey := os.Getenv("MEGAPORT_ACCESS_KEY")
secretKey := os.Getenv("MEGAPORT_SECRET_KEY")
handler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel})
programLevel.Set(slog.LevelDebug)
megaportClient, err := New(nil, WithBaseURL(MEGAPORTURL), WithLogHandler(handler), WithCredentials(accessKey, secretKey))
if err != nil {
suite.FailNowf("", "could not initialize megaport test client: %s", err.Error())
}
_, err = megaportClient.Authorize(ctx)
if err != nil {
suite.FailNowf("", "could not authorize megaport test client: %s", err.Error())
}
suite.client = megaportClient
}
// TestBadID tests the GetLocationByID method with an invalid ID.
func (suite *LocationIntegrationTestSuite) TestBadID() {
ctx := context.Background()
// Make sure that an id with no record returns an error as expected.
_, idErr := suite.client.LocationService.GetLocationByID(ctx, -999999)
suite.Equal(ErrLocationNotFound, idErr)
}
// TestBadName tests the GetLocationByName method with an invalid name.
func (suite *LocationIntegrationTestSuite) TestBadName() {
ctx := context.Background()
// Make sure that a name with no record returns an error as expected.
_, nameErr := suite.client.LocationService.GetLocationByName(ctx, "DefinitelyNotARealName")
suite.Equal(ErrLocationNotFound, nameErr)
}
// TestGetLocationByID tests the GetLocationByID method.
func (suite *LocationIntegrationTestSuite) TestGetLocationByID() {
ctx := context.Background()
// Make sure our location by id works as expected
byId, idErr := suite.client.LocationService.GetLocationByID(ctx, 137)
suite.Nil(idErr)
suite.Equal("3DC/Telecity Sofia", byId.Name)
byId, idErr = suite.client.LocationService.GetLocationByID(ctx, 383)
suite.Nil(idErr)
suite.Equal("NextDC B2", byId.Name)
}
// TestGetLocationByName tests the GetLocationByName method.
func (suite *LocationIntegrationTestSuite) TestGetLocationByName() {
ctx := context.Background()
// Make sure our location by name works as expected
byName, nameErr := suite.client.LocationService.GetLocationByName(ctx, "3DC/Telecity Sofia")
suite.Nil(nameErr)
suite.Equal(137, byName.ID)
byName, nameErr = suite.client.LocationService.GetLocationByName(ctx, "NextDC B2")
suite.Nil(nameErr)
suite.Equal(383, byName.ID)
byName, nameErr = suite.client.LocationService.GetLocationByName(ctx, "Equinix SY3")
suite.Nil(nameErr)
suite.Equal(6, byName.ID)
}
// TestGetLocationByNameFuzzy tests the GetLocationByNameFuzzy method.
func (suite *LocationIntegrationTestSuite) TestGetLocationByNameFuzzy() {
ctx := context.Background()
byFuzzy, fuzzyErr := suite.client.LocationService.GetLocationByNameFuzzy(ctx, "NextDC")
suite.True(len(byFuzzy) > 0)
suite.NoError(fuzzyErr)
failFuzzy, failFuzzyErr := suite.client.LocationService.GetLocationByNameFuzzy(ctx, "definitely not a location name at all")
suite.True(len(failFuzzy) == 0)
suite.Error(ErrNoMatchingLocations, failFuzzyErr)
}
// TestListCountries tests the ListCountries method
// first one should always be Australia
func (suite *LocationIntegrationTestSuite) TestListCountries() {
ctx := context.Background()
countries, countriesErr := suite.client.LocationService.ListCountries(ctx)
suite.NoError(countriesErr)
suite.Equal("Australia", countries[0].Name)
suite.Equal("AUS", countries[0].Code)
suite.Equal("AU", countries[0].Prefix)
suite.Greater(countries[0].SiteCount, 0)
}
// TestListMarketCodes tests the ListMarketCodes method
func (suite *LocationIntegrationTestSuite) TestMarketCodes() {
ctx := context.Background()
marketCodes, _ := suite.client.LocationService.ListMarketCodes(ctx)
found := false
for i := 0; i < len(marketCodes); i++ {
if marketCodes[i] == "AU" {
found = true
}
}
suite.True(found)
}
// TestFilterLocationsByMarketCode tests the FilterLocationsByMarketCode method
func (suite *LocationIntegrationTestSuite) TestFilterLocationsByMarketCode() {
ctx := context.Background()
locations, err := suite.client.LocationService.ListLocations(ctx)
suite.NoError(err)
currentCount := len(locations)
filtered, filterErr := suite.client.LocationService.FilterLocationsByMarketCode(ctx, "AU", locations)
suite.NoError(filterErr)
suite.Less(len(filtered), currentCount)
suite.Equal("AU", filtered[0].Market)
}