forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floating_ips_test.go
156 lines (127 loc) · 4.47 KB
/
floating_ips_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
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestFloatingIPs_ListFloatingIPs(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"floating_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}],"meta":{"total":2}}`)
})
floatingIPs, resp, err := client.FloatingIPs.List(ctx, nil)
if err != nil {
t.Errorf("FloatingIPs.List returned error: %v", err)
}
expectedFloatingIPs := []FloatingIP{
{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"},
{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 2}, IP: "192.168.0.2"},
}
if !reflect.DeepEqual(floatingIPs, expectedFloatingIPs) {
t.Errorf("FloatingIPs.List returned floating IPs %+v, expected %+v", floatingIPs, expectedFloatingIPs)
}
expectedMeta := &Meta{
Total: 2,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("FloatingIPs.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestFloatingIPs_ListFloatingIPsMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"floating_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}], "links":{"pages":{"next":"http://example.com/v2/floating_ips/?page=2"}}}`)
})
_, resp, err := client.FloatingIPs.List(ctx, nil)
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 1)
}
func TestFloatingIPs_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"floating_ips": [{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"},{"region":{"slug":"nyc3"},"droplet":{"id":2},"ip":"192.168.0.2"}],
"links":{
"pages":{
"next":"http://example.com/v2/floating_ips/?page=3",
"prev":"http://example.com/v2/floating_ips/?page=1",
"last":"http://example.com/v2/floating_ips/?page=3",
"first":"http://example.com/v2/floating_ips/?page=1"
}
}
}`
mux.HandleFunc("/v2/floating_ips", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, resp, err := client.FloatingIPs.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 2)
}
func TestFloatingIPs_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"floating_ip":{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"}}`)
})
floatingIP, _, err := client.FloatingIPs.Get(ctx, "192.168.0.1")
if err != nil {
t.Errorf("domain.Get returned error: %v", err)
}
expected := &FloatingIP{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"}
if !reflect.DeepEqual(floatingIP, expected) {
t.Errorf("FloatingIPs.Get returned %+v, expected %+v", floatingIP, expected)
}
}
func TestFloatingIPs_Create(t *testing.T) {
setup()
defer teardown()
createRequest := &FloatingIPCreateRequest{
Region: "nyc3",
DropletID: 1,
}
mux.HandleFunc("/v2/floating_ips", func(w http.ResponseWriter, r *http.Request) {
v := new(FloatingIPCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprint(w, `{"floating_ip":{"region":{"slug":"nyc3"},"droplet":{"id":1},"ip":"192.168.0.1"}}`)
})
floatingIP, _, err := client.FloatingIPs.Create(ctx, createRequest)
if err != nil {
t.Errorf("FloatingIPs.Create returned error: %v", err)
}
expected := &FloatingIP{Region: &Region{Slug: "nyc3"}, Droplet: &Droplet{ID: 1}, IP: "192.168.0.1"}
if !reflect.DeepEqual(floatingIP, expected) {
t.Errorf("FloatingIPs.Create returned %+v, expected %+v", floatingIP, expected)
}
}
func TestFloatingIPs_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.FloatingIPs.Delete(ctx, "192.168.0.1")
if err != nil {
t.Errorf("FloatingIPs.Delete returned error: %v", err)
}
}