blob: e372606f58c4b37b706719ddc6af0636337023ac [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
61// FirstFloatingIP is the first result in ListOutput.
jrperritt3d966162016-06-06 14:08:54 -050062var FirstFloatingIP = floatingips.FloatingIP{
Joe Topjiandee32222015-02-09 23:56:26 +000063 ID: "1",
64 IP: "10.10.10.1",
65 Pool: "nova",
66}
67
68// SecondFloatingIP is the first result in ListOutput.
jrperritt3d966162016-06-06 14:08:54 -050069var SecondFloatingIP = floatingips.FloatingIP{
Joe Topjiandee32222015-02-09 23:56:26 +000070 FixedIP: "166.78.185.201",
71 ID: "2",
72 InstanceID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
73 IP: "10.10.10.2",
74 Pool: "nova",
75}
76
77// ExpectedFloatingIPsSlice is the slice of results that should be parsed
78// from ListOutput, in the expected order.
jrperritt3d966162016-06-06 14:08:54 -050079var ExpectedFloatingIPsSlice = []floatingips.FloatingIP{FirstFloatingIP, SecondFloatingIP}
Joe Topjiandee32222015-02-09 23:56:26 +000080
81// CreatedFloatingIP is the parsed result from CreateOutput.
jrperritt3d966162016-06-06 14:08:54 -050082var CreatedFloatingIP = floatingips.FloatingIP{
Joe Topjiandee32222015-02-09 23:56:26 +000083 ID: "1",
84 IP: "10.10.10.1",
85 Pool: "nova",
86}
87
88// HandleListSuccessfully configures the test server to respond to a List request.
89func HandleListSuccessfully(t *testing.T) {
90 th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) {
91 th.TestMethod(t, r, "GET")
92 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
93
94 w.Header().Add("Content-Type", "application/json")
95 fmt.Fprintf(w, ListOutput)
96 })
97}
98
99// HandleGetSuccessfully configures the test server to respond to a Get request
100// for an existing floating ip
101func HandleGetSuccessfully(t *testing.T) {
102 th.Mux.HandleFunc("/os-floating-ips/2", func(w http.ResponseWriter, r *http.Request) {
103 th.TestMethod(t, r, "GET")
104 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
105
106 w.Header().Add("Content-Type", "application/json")
107 fmt.Fprintf(w, GetOutput)
108 })
109}
110
111// HandleCreateSuccessfully configures the test server to respond to a Create request
112// for a new floating ip
113func HandleCreateSuccessfully(t *testing.T) {
114 th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) {
115 th.TestMethod(t, r, "POST")
116 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
117 th.TestJSONRequest(t, r, `
118{
119 "pool": "nova"
120}
121`)
122
123 w.Header().Add("Content-Type", "application/json")
124 fmt.Fprintf(w, CreateOutput)
125 })
126}
127
128// HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
129// an existing floating ip
130func HandleDeleteSuccessfully(t *testing.T) {
131 th.Mux.HandleFunc("/os-floating-ips/1", func(w http.ResponseWriter, r *http.Request) {
132 th.TestMethod(t, r, "DELETE")
133 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
134
135 w.WriteHeader(http.StatusAccepted)
136 })
137}
138
139// HandleAssociateSuccessfully configures the test server to respond to a Post request
140// to associate an allocated floating IP
141func HandleAssociateSuccessfully(t *testing.T) {
142 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) {
143 th.TestMethod(t, r, "POST")
144 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
145 th.TestJSONRequest(t, r, `
146{
147 "addFloatingIp": {
148 "address": "10.10.10.2"
149 }
150}
151`)
152
153 w.WriteHeader(http.StatusAccepted)
154 })
155}
156
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000157// HandleFixedAssociateSucessfully configures the test server to respond to a Post request
158// to associate an allocated floating IP with a specific fixed IP address
159func HandleAssociateFixedSuccessfully(t *testing.T) {
160 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) {
161 th.TestMethod(t, r, "POST")
162 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
163 th.TestJSONRequest(t, r, `
164{
165 "addFloatingIp": {
166 "address": "10.10.10.2",
167 "fixed_address": "166.78.185.201"
168 }
169}
170`)
171
172 w.WriteHeader(http.StatusAccepted)
173 })
174}
175
Joe Topjiandee32222015-02-09 23:56:26 +0000176// HandleDisassociateSuccessfully configures the test server to respond to a Post request
177// to disassociate an allocated floating IP
178func HandleDisassociateSuccessfully(t *testing.T) {
179 th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) {
180 th.TestMethod(t, r, "POST")
181 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
182 th.TestJSONRequest(t, r, `
183{
184 "removeFloatingIp": {
185 "address": "10.10.10.2"
186 }
187}
188`)
189
190 w.WriteHeader(http.StatusAccepted)
191 })
192}