github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/qos/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/qos" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 fake "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 var createQoSExpected = qos.QoS{ 14 ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", 15 Name: "qos-001", 16 Consumer: "front-end", 17 Specs: map[string]string{ 18 "read_iops_sec": "20000", 19 }, 20 } 21 22 var getQoSExpected = qos.QoS{ 23 ID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", 24 Name: "qos-001", 25 Consumer: "front-end", 26 Specs: map[string]string{ 27 "read_iops_sec": "20000", 28 }, 29 } 30 31 func MockCreateResponse(t *testing.T) { 32 th.Mux.HandleFunc("/qos-specs", func(w http.ResponseWriter, r *http.Request) { 33 th.TestMethod(t, r, "POST") 34 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 35 th.TestHeader(t, r, "Content-Type", "application/json") 36 th.TestHeader(t, r, "Accept", "application/json") 37 th.TestJSONRequest(t, r, ` 38 { 39 "qos_specs": { 40 "name": "qos-001", 41 "consumer": "front-end", 42 "read_iops_sec": "20000" 43 } 44 } 45 `) 46 47 w.Header().Add("Content-Type", "application/json") 48 w.WriteHeader(http.StatusOK) 49 50 fmt.Fprintf(w, ` 51 { 52 "qos_specs": { 53 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 54 "name": "qos-001", 55 "consumer": "front-end", 56 "specs": { 57 "read_iops_sec": "20000" 58 } 59 } 60 } 61 `) 62 }) 63 } 64 65 func MockDeleteResponse(t *testing.T) { 66 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 67 th.TestMethod(t, r, "DELETE") 68 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 69 w.WriteHeader(http.StatusAccepted) 70 }) 71 } 72 73 func MockListResponse(t *testing.T) { 74 th.Mux.HandleFunc("/qos-specs", func(w http.ResponseWriter, r *http.Request) { 75 th.TestMethod(t, r, "GET") 76 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 77 78 w.Header().Add("Content-Type", "application/json") 79 r.ParseForm() 80 marker := r.Form.Get("marker") 81 switch marker { 82 case "": 83 fmt.Fprintf(w, ` 84 { 85 "qos_specs": [ 86 { 87 "consumer": "back-end", 88 "id": "1", 89 "name": "foo", 90 "specs": {} 91 }, 92 { 93 "consumer": "front-end", 94 "id": "2", 95 "name": "bar", 96 "specs" : { 97 "read_iops_sec" : "20000" 98 } 99 } 100 101 ], 102 "qos_specs_links": [ 103 { 104 "href": "%s/qos-specs?marker=2", 105 "rel": "next" 106 } 107 ] 108 } 109 `, th.Server.URL) 110 case "2": 111 fmt.Fprintf(w, `{ "qos_specs": [] }`) 112 default: 113 t.Fatalf("Unexpected marker: [%s]", marker) 114 } 115 }) 116 } 117 118 func MockGetResponse(t *testing.T) { 119 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 120 th.TestMethod(t, r, "GET") 121 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 122 w.Header().Add("Content-Type", "application/json") 123 w.WriteHeader(http.StatusOK) 124 125 fmt.Fprintf(w, ` 126 { 127 "qos_specs": { 128 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 129 "name": "qos-001", 130 "consumer": "front-end", 131 "specs": { 132 "read_iops_sec": "20000" 133 } 134 } 135 } 136 `) 137 }) 138 } 139 140 // UpdateBody provides a PUT result of the qos_specs for a QoS 141 const UpdateBody = ` 142 { 143 "qos_specs" : { 144 "consumer": "back-end", 145 "read_iops_sec": "40000", 146 "write_iops_sec": "40000" 147 } 148 } 149 ` 150 151 // UpdateQos is the expected qos_specs returned from PUT on a qos's qos_specs 152 var UpdateQos = map[string]string{ 153 "consumer": "back-end", 154 "read_iops_sec": "40000", 155 "write_iops_sec": "40000", 156 } 157 158 func MockUpdateResponse(t *testing.T) { 159 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 160 th.TestMethod(t, r, "PUT") 161 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 162 th.TestHeader(t, r, "Accept", "application/json") 163 th.TestJSONRequest(t, r, `{ 164 "qos_specs": { 165 "consumer": "back-end", 166 "read_iops_sec": "40000", 167 "write_iops_sec": "40000" 168 } 169 }`) 170 171 w.Header().Set("Content-Type", "application/json") 172 w.WriteHeader(http.StatusOK) 173 fmt.Fprintf(w, UpdateBody) 174 }) 175 } 176 177 func MockDeleteKeysResponse(t *testing.T) { 178 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/delete_keys", func(w http.ResponseWriter, r *http.Request) { 179 th.TestMethod(t, r, "PUT") 180 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 181 th.TestJSONRequest(t, r, `{ 182 "keys": [ 183 "read_iops_sec" 184 ] 185 }`) 186 187 w.WriteHeader(http.StatusAccepted) 188 }) 189 } 190 191 func MockAssociateResponse(t *testing.T) { 192 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/associate", func(w http.ResponseWriter, r *http.Request) { 193 th.TestMethod(t, r, "GET") 194 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 195 w.WriteHeader(http.StatusAccepted) 196 }) 197 } 198 199 func MockDisassociateResponse(t *testing.T) { 200 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/disassociate", func(w http.ResponseWriter, r *http.Request) { 201 th.TestMethod(t, r, "GET") 202 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 203 w.WriteHeader(http.StatusAccepted) 204 }) 205 } 206 207 func MockDisassociateAllResponse(t *testing.T) { 208 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/disassociate_all", func(w http.ResponseWriter, r *http.Request) { 209 th.TestMethod(t, r, "GET") 210 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 211 w.WriteHeader(http.StatusAccepted) 212 }) 213 } 214 215 func MockListAssociationsResponse(t *testing.T) { 216 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/associations", func(w http.ResponseWriter, r *http.Request) { 217 th.TestMethod(t, r, "GET") 218 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 219 w.Header().Add("Content-Type", "application/json") 220 fmt.Fprintf(w, ` 221 { 222 "qos_associations": [ 223 { 224 "name": "foo", 225 "id": "2f954bcf047c4ee9b09a37d49ae6db54", 226 "association_type": "volume_type" 227 } 228 ] 229 } 230 `) 231 }) 232 }