github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/floatingips/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/floatingips" 9 th "github.com/huaweicloud/golangsdk/testhelper" 10 "github.com/huaweicloud/golangsdk/testhelper/client" 11 ) 12 13 // ListOutput is a sample response to a List call. 14 const ListOutput = ` 15 { 16 "floating_ips": [ 17 { 18 "fixed_ip": null, 19 "id": "1", 20 "instance_id": null, 21 "ip": "10.10.10.1", 22 "pool": "nova" 23 }, 24 { 25 "fixed_ip": "166.78.185.201", 26 "id": "2", 27 "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. 36 const GetOutput = ` 37 { 38 "floating_ip": { 39 "fixed_ip": "166.78.185.201", 40 "id": "2", 41 "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 49 const CreateOutput = ` 50 { 51 "floating_ip": { 52 "fixed_ip": null, 53 "id": "1", 54 "instance_id": null, 55 "ip": "10.10.10.1", 56 "pool": "nova" 57 } 58 } 59 ` 60 61 // CreateOutputWithNumericID is a sample response to a Post call 62 // with a legacy nova-network-based numeric ID. 63 const 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 75 // FirstFloatingIP is the first result in ListOutput. 76 var FirstFloatingIP = floatingips.FloatingIP{ 77 ID: "1", 78 IP: "10.10.10.1", 79 Pool: "nova", 80 } 81 82 // SecondFloatingIP is the first result in ListOutput. 83 var SecondFloatingIP = floatingips.FloatingIP{ 84 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. 93 var ExpectedFloatingIPsSlice = []floatingips.FloatingIP{FirstFloatingIP, SecondFloatingIP} 94 95 // CreatedFloatingIP is the parsed result from CreateOutput. 96 var CreatedFloatingIP = floatingips.FloatingIP{ 97 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. 103 func 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 115 func 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 127 func 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 142 // HandleCreateWithNumericIDSuccessfully configures the test server to respond to a Create request 143 // for a new floating ip 144 func 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 159 // HandleDeleteSuccessfully configures the test server to respond to a Delete request for a 160 // an existing floating ip 161 func 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 172 func 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 188 // HandleFixedAssociateSucessfully configures the test server to respond to a Post request 189 // to associate an allocated floating IP with a specific fixed IP address 190 func 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 207 // HandleDisassociateSuccessfully configures the test server to respond to a Post request 208 // to disassociate an allocated floating IP 209 func 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 }