-
Notifications
You must be signed in to change notification settings - Fork 0
/
floating_ips_test.go
151 lines (119 loc) · 3.68 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
package cloudscale
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestFloatingIPs_IP(t *testing.T) {
expected := "192.0.2.123"
floatingIP := FloatingIP{
Network: "192.0.2.123/32",
}
if ip := floatingIP.IP(); ip != expected {
t.Errorf("FloatingIP.IP got=%s\nwant=%s", ip, expected)
}
}
func TestFloatingIPs_Create(t *testing.T) {
setup()
defer teardown()
floatingIPrequest := &FloatingIPCreateRequest{
IPVersion: 6,
Server: "47cec963-fcd2-482f-bdb6-24461b2d47b1",
}
mux.HandleFunc("/v1/floating-ips", func(w http.ResponseWriter, r *http.Request) {
expected := map[string]interface{}{
"ip_version": float64(6),
"server": "47cec963-fcd2-482f-bdb6-24461b2d47b1",
}
var v map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
if !reflect.DeepEqual(v, expected) {
t.Errorf("Request body\n got=%#v\nwant=%#v", v, expected)
}
fmt.Fprintf(w, `{"network": "2001:db8::cafe/128"}`)
})
floatingIP, err := client.FloatingIPs.Create(ctx, floatingIPrequest)
if err != nil {
t.Errorf("FloatingIPs.Create returned error: %v", err)
}
if network := floatingIP.Network; network != "2001:db8::cafe/128" {
t.Errorf("expected network '%s', received '%s'", floatingIP.Network, network)
}
}
func TestFloatingIPs_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/floating-ips/192.0.2.123", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"network": "192.0.2.123/32"}`)
})
floatingIP, err := client.FloatingIPs.Get(ctx, "192.0.2.123")
if err != nil {
t.Errorf("FloatingIPs.Get returned error: %v", err)
}
expected := &FloatingIP{Network: "192.0.2.123/32"}
if !reflect.DeepEqual(floatingIP, expected) {
t.Errorf("FloatingIPs.Get\n got=%#v\nwant=%#v", floatingIP, expected)
}
}
func TestFloatingIPs_Update(t *testing.T) {
setup()
defer teardown()
updateRequest := &FloatingIPUpdateRequest{
Server: "47777777-fcd2-482f-bdb6-24461b2d47b1",
}
mux.HandleFunc("/v1/floating-ips/192.0.2.123", func(w http.ResponseWriter, r *http.Request) {
expected := map[string]interface{}{
"server": "47777777-fcd2-482f-bdb6-24461b2d47b1",
}
var v map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
if !reflect.DeepEqual(v, expected) {
t.Errorf("Request body = %#v, expected %#v", v, expected)
}
fmt.Fprintf(w, `{"network": "192.0.2.123/32"}`)
})
floatingIP, err := client.FloatingIPs.Update(ctx, "192.0.2.123", updateRequest)
if err != nil {
t.Errorf("FloatingIps.Update returned error: %v", err)
} else {
if network := floatingIP.Network; network != "192.0.2.123/32" {
t.Errorf("expected network '%s', received '%s'", "192.0.2.123/32", network)
}
}
}
func TestFloatingIPs_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/floating-ips/192.0.2.123", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodDelete)
})
err := client.FloatingIPs.Delete(ctx, "192.0.2.123")
if err != nil {
t.Errorf("FloatingIPs.Delete returned error: %v", err)
}
}
func TestFloatingIPs_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/floating-ips", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodGet)
fmt.Fprint(w, `[{"network": "192.0.2.123/32"}]`)
})
servers, err := client.FloatingIPs.List(ctx)
if err != nil {
t.Errorf("Servers.List returned error: %v", err)
}
expected := []FloatingIP{{Network: "192.0.2.123/32"}}
if !reflect.DeepEqual(servers, expected) {
t.Errorf("FloatingIPs.List\n got=%#v\nwant=%#v", servers, expected)
}
}