forked from equinix/ne-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_device_type_test.go
140 lines (127 loc) · 5.29 KB
/
rest_device_type_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
package ne
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/equinix/ne-go/internal/api"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestGetDeviceTypes(t *testing.T) {
//Given
respBody := api.DeviceTypeResponse{}
if err := readJSONData("./test-fixtures/ne_device_types_get.json", &respBody); err != nil {
assert.Failf(t, "cannot read test response due to %s", err.Error())
}
limit := respBody.Pagination.Limit
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/ne/v1/deviceTypes?limit=%d", baseURL, limit),
func(r *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, respBody)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//When
c := NewClient(context.Background(), baseURL, testHc)
c.PageSize = limit
types, err := c.GetDeviceTypes()
//Then
assert.Nil(t, err, "Client should not return an error")
assert.NotNil(t, types, "Client should return a response")
assert.Equal(t, len(respBody.Data), len(types), "Number of objects matches")
for i := range respBody.Data {
verifyDeviceType(t, respBody.Data[i], types[i])
}
}
func TestGetDeviceSoftwareVersions(t *testing.T) {
//Given
respBody := api.DeviceTypeResponse{}
if err := readJSONData("./test-fixtures/ne_devices_types_csr1000v_get.json", &respBody); err != nil {
assert.Failf(t, "cannot read test response due to %s", err.Error())
}
limit := respBody.Pagination.Limit
deviceTypeCode := "CSR1000V"
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/ne/v1/deviceTypes?deviceTypeCode=%s&limit=%d", baseURL, deviceTypeCode, limit),
func(r *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, respBody)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//When
c := NewClient(context.Background(), baseURL, testHc)
c.PageSize = limit
versions, err := c.GetDeviceSoftwareVersions(deviceTypeCode)
//Then
assert.Nil(t, err, "Client should not return an error")
assert.NotNil(t, versions, "Client should return a response")
apiType := respBody.Data[0]
apiVerMap := make(map[string]api.DeviceTypeVersionDetails)
for _, pkg := range apiType.SoftwarePackages {
for _, ver := range pkg.VersionDetails {
if _, ok := apiVerMap[StringValue(ver.Version)]; !ok {
apiVerMap[StringValue(ver.Version)] = ver
}
}
}
assert.Equal(t, len(apiVerMap), len(versions), "Number of versions matches")
for _, ver := range versions {
apiVer := apiVerMap[StringValue(ver.Version)]
verifyDeviceSoftwareVersion(t, apiVer, ver)
}
}
func TestGetDevicePlatforms(t *testing.T) {
//Given
respBody := api.DeviceTypeResponse{}
if err := readJSONData("./test-fixtures/ne_devices_types_csr1000v_get.json", &respBody); err != nil {
assert.Failf(t, "cannot read test response due to %s", err.Error())
}
limit := respBody.Pagination.Limit
deviceTypeCode := "CSR1000V"
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/ne/v1/deviceTypes?deviceTypeCode=%s&limit=%d", baseURL, deviceTypeCode, limit),
func(r *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, respBody)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//When
c := NewClient(context.Background(), baseURL, testHc)
c.PageSize = limit
platforms, err := c.GetDevicePlatforms(deviceTypeCode)
//Then
assert.Nil(t, err, "Client should not return an error")
assert.NotNil(t, platforms, "Client should return a response")
assert.Equal(t, 3, len(platforms), "Number of platforms matches")
for _, version := range platforms {
assert.ElementsMatch(t, version.PackageCodes, []string{"APPX", "AX", "IPBASE", "SEC"}, "PackageCodes match")
assert.ElementsMatch(t, version.ManagementTypes, []string{"EQUINIX-CONFIGURED", "SELF-CONFIGURED"}, "ManagementTypes match")
assert.ElementsMatch(t, version.LicenseOptions, []string{"BYOL", "Sub"}, "LicenseOptions match")
}
}
func verifyDeviceType(t *testing.T, apiDeviceType api.DeviceType, deviceType DeviceType) {
assert.Equal(t, apiDeviceType.Name, deviceType.Name, "Name matches")
assert.Equal(t, apiDeviceType.Description, deviceType.Description, "Description matches")
assert.Equal(t, apiDeviceType.Code, deviceType.Code, "Code matches")
assert.Equal(t, apiDeviceType.Vendor, deviceType.Vendor, "Vendor matches")
assert.Equal(t, apiDeviceType.Category, deviceType.Category, "Category matches")
assert.Equal(t, len(apiDeviceType.AvailableMetros), len(deviceType.MetroCodes), "Number of available metros matches")
for i := range apiDeviceType.AvailableMetros {
assert.Equalf(t, *apiDeviceType.AvailableMetros[i].Code, deviceType.MetroCodes[i], "Code of available metro element %d matches", i)
}
}
func verifyDeviceSoftwareVersion(t *testing.T, apiVer api.DeviceTypeVersionDetails, ver DeviceSoftwareVersion) {
assert.Equal(t, apiVer.Version, ver.Version, "Version matches")
assert.Equal(t, apiVer.ImageName, ver.ImageName, "ImageName matches")
assert.Equal(t, apiVer.Date, ver.Date, "Date matches")
assert.Equal(t, apiVer.Status, ver.Status, "Status matches")
assert.Equal(t, apiVer.IsStable, ver.IsStable, "IsStable matches")
assert.Equal(t, apiVer.ReleaseNotesLink, ver.ReleaseNotesLink, "ReleaseNotesLink matches")
}