forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floating_ips_actions_test.go
167 lines (135 loc) · 4.42 KB
/
floating_ips_actions_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
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestFloatingIPsActions_Assign(t *testing.T) {
setup()
defer teardown()
dropletID := 12345
assignRequest := &ActionRequest{
"droplet_id": float64(dropletID), // encoding/json decodes numbers as floats
"type": "assign",
}
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, "POST")
if !reflect.DeepEqual(v, assignRequest) {
t.Errorf("Request body = %#v, expected %#v", v, assignRequest)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
assign, _, err := client.FloatingIPActions.Assign("192.168.0.1", 12345)
if err != nil {
t.Errorf("FloatingIPsActions.Assign returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(assign, expected) {
t.Errorf("FloatingIPsActions.Assign returned %+v, expected %+v", assign, expected)
}
}
func TestFloatingIPsActions_Unassign(t *testing.T) {
setup()
defer teardown()
unassignRequest := &ActionRequest{
"type": "unassign",
}
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
v := new(ActionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, "POST")
if !reflect.DeepEqual(v, unassignRequest) {
t.Errorf("Request body = %+v, expected %+v", v, unassignRequest)
}
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.FloatingIPActions.Unassign("192.168.0.1")
if err != nil {
t.Errorf("FloatingIPsActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("FloatingIPsActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestFloatingIPsActions_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions/456", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprintf(w, `{"action":{"status":"in-progress"}}`)
})
action, _, err := client.FloatingIPActions.Get("192.168.0.1", 456)
if err != nil {
t.Errorf("FloatingIPsActions.Get returned error: %v", err)
}
expected := &Action{Status: "in-progress"}
if !reflect.DeepEqual(action, expected) {
t.Errorf("FloatingIPsActions.Get returned %+v, expected %+v", action, expected)
}
}
func TestFloatingIPsActions_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprintf(w, `{"actions":[{"status":"in-progress"}]}`)
})
actions, _, err := client.FloatingIPActions.List("192.168.0.1", nil)
if err != nil {
t.Errorf("FloatingIPsActions.List returned error: %v", err)
}
expected := []Action{Action{Status: "in-progress"}}
if !reflect.DeepEqual(actions, expected) {
t.Errorf("FloatingIPsActions.List returned %+v, expected %+v", actions, expected)
}
}
func TestFloatingIPsActions_ListMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"actions":[{"status":"in-progress"}], "links":{"pages":{"next":"http://example.com/v2/floating_ips/192.168.0.1/actions?page=2"}}}`)
})
_, resp, err := client.FloatingIPActions.List("192.168.0.1", nil)
if err != nil {
t.Errorf("FloatingIPsActions.List returned error: %v", err)
}
checkCurrentPage(t, resp, 1)
}
func TestFloatingIPsActions_ListPageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"actions":[{"status":"in-progress"}],
"links":{
"pages":{
"next":"http://example.com/v2/regions/?page=3",
"prev":"http://example.com/v2/regions/?page=1",
"last":"http://example.com/v2/regions/?page=3",
"first":"http://example.com/v2/regions/?page=1"
}
}
}`
mux.HandleFunc("/v2/floating_ips/192.168.0.1/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, resp, err := client.FloatingIPActions.List("192.168.0.1", opt)
if err != nil {
t.Errorf("FloatingIPsActions.List returned error: %v", err)
}
checkCurrentPage(t, resp, 2)
}