Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
| 3 | package floatingip |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | "github.com/rackspace/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
| 14 | // ListOutput is a sample response to a List call. |
| 15 | const ListOutput = ` |
| 16 | { |
| 17 | "floating_ips": [ |
| 18 | { |
| 19 | "fixed_ip": null, |
| 20 | "id": 1, |
| 21 | "instance_id": null, |
| 22 | "ip": "10.10.10.1", |
| 23 | "pool": "nova" |
| 24 | }, |
| 25 | { |
| 26 | "fixed_ip": "166.78.185.201", |
| 27 | "id": 2, |
| 28 | "instance_id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 29 | "ip": "10.10.10.2", |
| 30 | "pool": "nova" |
| 31 | } |
| 32 | ] |
| 33 | } |
| 34 | ` |
| 35 | |
| 36 | // GetOutput is a sample response to a Get call. |
| 37 | const GetOutput = ` |
| 38 | { |
| 39 | "floating_ip": { |
| 40 | "fixed_ip": "166.78.185.201", |
| 41 | "id": 2, |
| 42 | "instance_id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 43 | "ip": "10.10.10.2", |
| 44 | "pool": "nova" |
| 45 | } |
| 46 | } |
| 47 | ` |
| 48 | |
| 49 | // CreateOutput is a sample response to a Post call |
| 50 | const CreateOutput = ` |
| 51 | { |
| 52 | "floating_ip": { |
| 53 | "fixed_ip": null, |
| 54 | "id": 1, |
| 55 | "instance_id": null, |
| 56 | "ip": "10.10.10.1", |
| 57 | "pool": "nova" |
| 58 | } |
| 59 | } |
| 60 | ` |
| 61 | |
| 62 | // FirstFloatingIP is the first result in ListOutput. |
| 63 | var FirstFloatingIP = FloatingIP{ |
| 64 | ID: "1", |
| 65 | IP: "10.10.10.1", |
| 66 | Pool: "nova", |
| 67 | } |
| 68 | |
| 69 | // SecondFloatingIP is the first result in ListOutput. |
| 70 | var SecondFloatingIP = FloatingIP{ |
| 71 | FixedIP: "166.78.185.201", |
| 72 | ID: "2", |
| 73 | InstanceID: "4d8c3732-a248-40ed-bebc-539a6ffd25c0", |
| 74 | IP: "10.10.10.2", |
| 75 | Pool: "nova", |
| 76 | } |
| 77 | |
| 78 | // ExpectedFloatingIPsSlice is the slice of results that should be parsed |
| 79 | // from ListOutput, in the expected order. |
| 80 | var ExpectedFloatingIPsSlice = []FloatingIP{FirstFloatingIP, SecondFloatingIP} |
| 81 | |
| 82 | // CreatedFloatingIP is the parsed result from CreateOutput. |
| 83 | var CreatedFloatingIP = FloatingIP{ |
| 84 | ID: "1", |
| 85 | IP: "10.10.10.1", |
| 86 | Pool: "nova", |
| 87 | } |
| 88 | |
| 89 | // HandleListSuccessfully configures the test server to respond to a List request. |
| 90 | func HandleListSuccessfully(t *testing.T) { |
| 91 | th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) { |
| 92 | th.TestMethod(t, r, "GET") |
| 93 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 94 | |
| 95 | w.Header().Add("Content-Type", "application/json") |
| 96 | fmt.Fprintf(w, ListOutput) |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | // HandleGetSuccessfully configures the test server to respond to a Get request |
| 101 | // for an existing floating ip |
| 102 | func HandleGetSuccessfully(t *testing.T) { |
| 103 | th.Mux.HandleFunc("/os-floating-ips/2", func(w http.ResponseWriter, r *http.Request) { |
| 104 | th.TestMethod(t, r, "GET") |
| 105 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 106 | |
| 107 | w.Header().Add("Content-Type", "application/json") |
| 108 | fmt.Fprintf(w, GetOutput) |
| 109 | }) |
| 110 | } |
| 111 | |
| 112 | // HandleCreateSuccessfully configures the test server to respond to a Create request |
| 113 | // for a new floating ip |
| 114 | func HandleCreateSuccessfully(t *testing.T) { |
| 115 | th.Mux.HandleFunc("/os-floating-ips", func(w http.ResponseWriter, r *http.Request) { |
| 116 | th.TestMethod(t, r, "POST") |
| 117 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 118 | th.TestJSONRequest(t, r, ` |
| 119 | { |
| 120 | "pool": "nova" |
| 121 | } |
| 122 | `) |
| 123 | |
| 124 | w.Header().Add("Content-Type", "application/json") |
| 125 | fmt.Fprintf(w, CreateOutput) |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | // HandleDeleteSuccessfully configures the test server to respond to a Delete request for a |
| 130 | // an existing floating ip |
| 131 | func HandleDeleteSuccessfully(t *testing.T) { |
| 132 | th.Mux.HandleFunc("/os-floating-ips/1", func(w http.ResponseWriter, r *http.Request) { |
| 133 | th.TestMethod(t, r, "DELETE") |
| 134 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 135 | |
| 136 | w.WriteHeader(http.StatusAccepted) |
| 137 | }) |
| 138 | } |
| 139 | |
| 140 | // HandleAssociateSuccessfully configures the test server to respond to a Post request |
| 141 | // to associate an allocated floating IP |
| 142 | func HandleAssociateSuccessfully(t *testing.T) { |
| 143 | th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) { |
| 144 | th.TestMethod(t, r, "POST") |
| 145 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 146 | th.TestJSONRequest(t, r, ` |
| 147 | { |
| 148 | "addFloatingIp": { |
| 149 | "address": "10.10.10.2" |
| 150 | } |
| 151 | } |
| 152 | `) |
| 153 | |
| 154 | w.WriteHeader(http.StatusAccepted) |
| 155 | }) |
| 156 | } |
| 157 | |
| 158 | // HandleDisassociateSuccessfully configures the test server to respond to a Post request |
| 159 | // to disassociate an allocated floating IP |
| 160 | func HandleDisassociateSuccessfully(t *testing.T) { |
| 161 | th.Mux.HandleFunc("/servers/4d8c3732-a248-40ed-bebc-539a6ffd25c0/action", func(w http.ResponseWriter, r *http.Request) { |
| 162 | th.TestMethod(t, r, "POST") |
| 163 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 164 | th.TestJSONRequest(t, r, ` |
| 165 | { |
| 166 | "removeFloatingIp": { |
| 167 | "address": "10.10.10.2" |
| 168 | } |
| 169 | } |
| 170 | `) |
| 171 | |
| 172 | w.WriteHeader(http.StatusAccepted) |
| 173 | }) |
| 174 | } |