blob: 6866e265defcc5a45c101216467348f2776b1e00 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Joe Topjiandee32222015-02-09 23:56:26 +00002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
jrperritt3d966162016-06-06 14:08:54 -05008 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
Jon Perritt27249f42016-02-18 10:35:59 -06009 th "github.com/gophercloud/gophercloud/testhelper"
10 "github.com/gophercloud/gophercloud/testhelper/client"
Joe Topjiandee32222015-02-09 23:56:26 +000011)
12
13// ListOutput is a sample response to a List call.
14const ListOutput = `
15{
16 "floating_ips": [
17 {
18 "fixed_ip": null,
Jon Perritt12395212016-02-24 10:41:17 -060019 "id": "1",
Joe Topjiandee32222015-02-09 23:56:26 +000020 "instance_id": null,
21 "ip": "10.10.10.1",
22 "pool": "nova"
23 },
24 {
25 "fixed_ip": "166.78.185.201",
Jon Perritt12395212016-02-24 10:41:17 -060026 "id": "2",
Joe Topjiandee32222015-02-09 23:56:26 +000027 "instance_id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
28 "ip": "10.10.10.2",
29 "pool": "nova"
30 }
31 ]
32}
33`
34
35// GetOutput is a sample response to a Get call.
36const GetOutput = `
37{
38 "floating_ip": {
39 "fixed_ip": "166.78.185.201",
Jon Perritt12395212016-02-24 10:41:17 -060040 "id": "2",
Joe Topjiandee32222015-02-09 23:56:26 +000041 "instance_id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
42 "ip": "10.10.10.2",
43 "pool": "nova"
44 }
45}
46`
47
48// CreateOutput is a sample response to a Post call
49const CreateOutput = `
50{
51 "floating_ip": {
52 "fixed_ip": null,
Jon Perritt12395212016-02-24 10:41:17 -060053 "id": "1",
Joe Topjiandee32222015-02-09 23:56:26 +000054 "instance_id": null,
55 "ip": "10.10.10.1",
56 "pool": "nova"
57 }
58}
59`
60
Joe Topjian4d2266c2017-01-19 16:57:50 -070061// CreateOutputWithNumericID is a sample response to a Post call
62// with a legacy nova-network-based numeric ID.
63const CreateOutputWithNumericID = `
64{
65 "floating_ip": {
66 "fixed_ip": null,
67 "id": 1,
68 "instance_id": null,
69 "ip": "10.10.10.1",
70 "pool": "nova"
71 }
72}
73`
74
Joe Topjiandee32222015-02-09 23:56:26 +000075// FirstFloatingIP is the first result in ListOutput.
jrperritt3d966162016-06-06 14:08:54 -050076var FirstFloatingIP = floatingips.FloatingIP{
Joe Topjiandee32222015-02-09 23:56:26 +000077 ID: "1",
78 IP: "10.10.10.1",
79 Pool: "nova",
80}
81
82// SecondFloatingIP is the first result in ListOutput.
jrperritt3d966162016-06-06 14:08:54 -050083var SecondFloatingIP = floatingips.FloatingIP{
Joe Topjiandee32222015-02-09 23:56:26 +000084 FixedIP: "166.78.185.201",
85 ID: "2",
86 InstanceID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
87 IP: "10.10.10.2",
88 Pool: "nova",
89}
90
91// ExpectedFloatingIPsSlice is the slice of results that should be parsed
92// from ListOutput, in the expected order.
jrperritt3d966162016-06-06 14:08:54 -050093var ExpectedFloatingIPsSlice = []floatingips.FloatingIP{FirstFloatingIP, SecondFloatingIP}
Joe Topjiandee32222015-02-09 23:56:26 +000094
95// CreatedFloatingIP is the parsed result from CreateOutput.
jrperritt3d966162016-06-06 14:08:54 -050096var CreatedFloatingIP = floatingips.FloatingIP{
Joe Topjiandee32222015-02-09 23:56:26 +000097 ID: "1",
98 IP: "10.10.10.1",
99 Pool: "nova",
100}
101
102// HandleListSuccessfully configures the test server to respond to a List request.
103func HandleListSuccessfully(t *testing.T) {
104 th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) {
105 th.TestMethod(t, r, "GET")
106 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
107
108 w.Header().Add("Content-Type", "application/json")
109 fmt.Fprintf(w, ListOutput)
110 })
111}
112
113// HandleGetSuccessfully configures the test server to respond to a Get request
114// for an existing floating ip
115func HandleGetSuccessfully(t *testing.T) {
116 th.Mux.HandleFunc("/os-floating-ips/2", func(w http.ResponseWriter, r *http.Request) {
117 th.TestMethod(t, r, "GET")
118 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
119
120 w.Header().Add("Content-Type", "application/json")
121 fmt.Fprintf(w, GetOutput)
122 })
123}
124
125// HandleCreateSuccessfully configures the test server to respond to a Create request
126// for a new floating ip
127func HandleCreateSuccessfully(t *testing.T) {
128 th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) {
129 th.TestMethod(t, r, "POST")
130 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
131 th.TestJSONRequest(t, r, `
132{
133 "pool": "nova"
134}
135`)
136
137 w.Header().Add("Content-Type", "application/json")
138 fmt.Fprintf(w, CreateOutput)
139 })
140}
141
Joe Topjian4d2266c2017-01-19 16:57:50 -0700142// HandleCreateWithNumericIDSuccessfully configures the test server to respond to a Create request
143// for a new floating ip
144func HandleCreateWithNumericIDSuccessfully(t *testing.T) {
145 th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) {
146 th.TestMethod(t, r, "POST")
147 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
148 th.TestJSONRequest(t, r, `
149{
150 "pool": "nova"
151}
152`)
153
154 w.Header().Add("Content-Type", "application/json")
155 fmt.Fprintf(w, CreateOutputWithNumericID)
156 })
157}
158
Joe Topjiandee32222015-02-09 23:56:26 +0000159// HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
160// an existing floating ip
161func HandleDeleteSuccessfully(t *testing.T) {
162 th.Mux.HandleFunc("/os-floating-ips/1", func(w http.ResponseWriter, r *http.Request) {
163 th.TestMethod(t, r, "DELETE")
164 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
165
166 w.WriteHeader(http.StatusAccepted)
167 })
168}
169
170// HandleAssociateSuccessfully configures the test server to respond to a Post request
171// to associate an allocated floating IP
172func HandleAssociateSuccessfully(t *testing.T) {
173 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) {
174 th.TestMethod(t, r, "POST")
175 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
176 th.TestJSONRequest(t, r, `
177{
178 "addFloatingIp": {
179 "address": "10.10.10.2"
180 }
181}
182`)
183
184 w.WriteHeader(http.StatusAccepted)
185 })
186}
187
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000188// HandleFixedAssociateSucessfully configures the test server to respond to a Post request
189// to associate an allocated floating IP with a specific fixed IP address
190func HandleAssociateFixedSuccessfully(t *testing.T) {
191 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) {
192 th.TestMethod(t, r, "POST")
193 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
194 th.TestJSONRequest(t, r, `
195{
196 "addFloatingIp": {
197 "address": "10.10.10.2",
198 "fixed_address": "166.78.185.201"
199 }
200}
201`)
202
203 w.WriteHeader(http.StatusAccepted)
204 })
205}
206
Joe Topjiandee32222015-02-09 23:56:26 +0000207// HandleDisassociateSuccessfully configures the test server to respond to a Post request
208// to disassociate an allocated floating IP
209func HandleDisassociateSuccessfully(t *testing.T) {
210 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) {
211 th.TestMethod(t, r, "POST")
212 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
213 th.TestJSONRequest(t, r, `
214{
215 "removeFloatingIp": {
216 "address": "10.10.10.2"
217 }
218}
219`)
220
221 w.WriteHeader(http.StatusAccepted)
222 })
223}