-
Notifications
You must be signed in to change notification settings - Fork 14
/
privatenetworks_test.go
199 lines (157 loc) · 7.12 KB
/
privatenetworks_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package glesys
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPrivateNetworksCreate(t *testing.T) {
c := &mockClient{body: `{ "response": { "privatenetwork":
{ "id": "pn-123ab", "name": "mynetwork", "ipv6aggregate": "2001:db8::/48"}}}`}
n := PrivateNetworkService{client: c}
networkname := "mynetwork"
net, _ := n.Create(context.Background(), networkname)
assert.Equal(t, "POST", c.lastMethod, "method is used correct")
assert.Equal(t, "privatenetwork/create", c.lastPath, "path used is correct")
assert.Equal(t, "pn-123ab", net.ID, "ID is correct")
assert.Equal(t, "mynetwork", net.Name, "Name is correct")
assert.Equal(t, "2001:db8::/48", net.IPv6Aggregate, "IPv6Aggregate is correct")
}
func TestPrivateNetworksDestroy(t *testing.T) {
c := &mockClient{}
n := PrivateNetworkService{client: c}
n.Destroy(context.Background(), "pn-123ab")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "privatenetwork/delete", c.lastPath, "path used is correct")
}
func TestPrivateNetworksDetails(t *testing.T) {
c := &mockClient{body: `{ "response": { "privatenetwork": {
"id": "pn-123ab",
"name": "mynetwork",
"ipv6aggregate": "2001:db8::/48"
} } }`}
n := PrivateNetworkService{client: c}
net, _ := n.Details(context.Background(), "pn-123ab")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "privatenetwork/details", c.lastPath, "path used is correct")
assert.Equal(t, "mynetwork", net.Name, "Name is correct")
assert.Equal(t, "pn-123ab", net.ID, "ID is correct")
assert.Equal(t, "2001:db8::/48", net.IPv6Aggregate, "IPv6Aggregate is correct")
}
func TestPrivateNetworksList(t *testing.T) {
c := &mockClient{body: `{ "response": { "privatenetworks": [{
"id": "pn-123ab",
"name": "mynetwork",
"ipv6aggregate": "2001:db8:1::/48"
}, {
"id": "pn-456cd",
"name": "othernet",
"ipv6aggregate": "2001:db8:2::/48" } ] } }`}
n := PrivateNetworkService{client: c}
net, _ := n.List(context.Background())
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "privatenetwork/list", c.lastPath, "path used is correct")
assert.Equal(t, "mynetwork", (*net)[0].Name, "Name is correct")
assert.Equal(t, "pn-123ab", (*net)[0].ID, "ID is correct")
assert.Equal(t, "2001:db8:1::/48", (*net)[0].IPv6Aggregate, "IPv6Aggregate is correct")
assert.Equal(t, "othernet", (*net)[1].Name, "Name is correct")
assert.Equal(t, "pn-456cd", (*net)[1].ID, "ID is correct")
assert.Equal(t, "2001:db8:2::/48", (*net)[1].IPv6Aggregate, "IPv6Aggregate is correct")
}
func TestPrivateNetworksEdit(t *testing.T) {
c := &mockClient{body: `{ "response": { "privatenetwork":
{ "name": "newnetwork-1", "id": "pn-123ab"}}}`}
n := PrivateNetworkService{client: c}
params := EditPrivateNetworkParams{
Name: "newnetwork-1",
ID: "pn-123ab",
}
net, _ := n.Edit(context.Background(), params)
assert.Equal(t, "POST", c.lastMethod, "method is used correct")
assert.Equal(t, "privatenetwork/edit", c.lastPath, "path used is correct")
assert.Equal(t, "pn-123ab", net.ID, "ID is correct")
assert.Equal(t, "newnetwork-1", net.Name, "Name is correct")
}
func TestPrivateNetworksEstimatedcost(t *testing.T) {
c := &mockClient{body: `{ "response": { "billing":
{ "currency": "SEK", "price": 50.0,
"discount": 12.5, "total": 37.5}}}`}
n := PrivateNetworkService{client: c}
netid := "pn-123ab"
net, _ := n.EstimatedCost(context.Background(), netid)
assert.Equal(t, "POST", c.lastMethod, "method is used correct")
assert.Equal(t, "privatenetwork/estimatedcost", c.lastPath, "path used is correct")
assert.Equal(t, 50.0, net.Price, "Price is correct")
assert.Equal(t, 12.5, net.Discount, "Discount is correct")
assert.Equal(t, 37.5, net.Total, "Total price is correct")
}
func TestPrivateNetworkSegmentsCreate(t *testing.T) {
c := &mockClient{body: `{ "response": { "privatenetworksegment":
{ "id": "266979ab-1e05-4fbc-b9e0-577f31c0d2e9",
"name": "mysegment", "ipv6subnet": "2001:db8:0::/64",
"ipv4subnet": "192.0.2.0/24", "datacenter": "dc-fbg1",
"platform": "kvm"}}}`}
n := PrivateNetworkService{client: c}
params := CreatePrivateNetworkSegmentParams{
Name: "mysegment",
Platform: "kvm",
Datacenter: "dc-fbg1",
IPv4Subnet: "192.0.2.0/24",
}
segment, _ := n.CreateSegment(context.Background(), params)
assert.Equal(t, "POST", c.lastMethod, "method is used correct")
assert.Equal(t, "privatenetwork/createsegment", c.lastPath, "path used is correct")
assert.Equal(t, "266979ab-1e05-4fbc-b9e0-577f31c0d2e9", segment.ID, "ID is correct")
assert.Equal(t, "mysegment", segment.Name, "Name is correct")
assert.Equal(t, "2001:db8:0::/64", segment.IPv6Subnet, "IPv6Subnet is correct")
assert.Equal(t, "192.0.2.0/24", segment.IPv4Subnet, "IPv4Subnet is correct")
}
func TestPrivateNetworkSegmentsDestroy(t *testing.T) {
c := &mockClient{}
n := PrivateNetworkService{client: c}
n.DestroySegment(context.Background(), "266979ab-1e05-4fbc-b9e0-577f31c0d2e9")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "privatenetwork/deletesegment", c.lastPath, "path used is correct")
}
func TestPrivateNetworkSegmentsList(t *testing.T) {
c := &mockClient{body: `
{ "response": { "privatenetworksegments": [{
"id": "fb34a19a-392a-43ec-ab3f-0c5b73ad1234",
"name": "mysegment",
"platform": "kvm",
"datacenter": "dc-fbg1",
"ipv4subnet": "192.0.2.0/24",
"ipv6subnet": "2001:db8:0::/64"
}, {
"id": "fb34a19a-392a-43ec-ab3f-0c5b73ad5678",
"name": "othersegment",
"platform": "kvm",
"datacenter": "dc-fbg1",
"ipv4subnet": "192.0.2.0/24",
"ipv6subnet": "2001:db8:1::/64" } ] } }
`}
n := PrivateNetworkService{client: c}
segments, _ := n.ListSegments(context.Background(), "pn-123ab")
assert.Equal(t, "POST", c.lastMethod, "method used is correct")
assert.Equal(t, "privatenetwork/listsegments", c.lastPath, "path used is correct")
assert.Equal(t, "mysegment", (*segments)[0].Name, "Name is correct")
assert.Equal(t, "fb34a19a-392a-43ec-ab3f-0c5b73ad1234", (*segments)[0].ID, "ID is correct")
assert.Equal(t, "2001:db8:0::/64", (*segments)[0].IPv6Subnet, "IPv6Aggregate is correct")
assert.Equal(t, "othersegment", (*segments)[1].Name, "Name is correct")
assert.Equal(t, "fb34a19a-392a-43ec-ab3f-0c5b73ad5678", (*segments)[1].ID, "ID is correct")
assert.Equal(t, "kvm", (*segments)[1].Platform, "Platform is correct")
assert.Equal(t, "2001:db8:1::/64", (*segments)[1].IPv6Subnet, "IPv6Aggregate is correct")
}
func TestPrivateNetworkSegmentsEdit(t *testing.T) {
c := &mockClient{body: `{ "response": { "privatenetworksegment":
{ "name": "segmentname-2", "id": "fb34a19a-392a-43ec-ab3f-0c5b73ad1234"}}}`}
n := PrivateNetworkService{client: c}
params := EditPrivateNetworkSegmentParams{
Name: "segmentname-2",
ID: "fb34a19a-392a-43ec-ab3f-0c5b73ad1234",
}
segment, _ := n.EditSegment(context.Background(), params)
assert.Equal(t, "POST", c.lastMethod, "method is used correct")
assert.Equal(t, "privatenetwork/editsegment", c.lastPath, "path used is correct")
assert.Equal(t, "fb34a19a-392a-43ec-ab3f-0c5b73ad1234", segment.ID, "ID is correct")
assert.Equal(t, "segmentname-2", segment.Name, "Name is correct")
}