github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/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/vnpaycloud-console/gophercloud/v2/openstack/blockstorage/v3/qos" 9 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 10 fake "github.com/vnpaycloud-console/gophercloud/v2/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.Fprint(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 if err := r.ParseForm(); err != nil { 80 t.Errorf("Failed to parse request form %v", err) 81 } 82 marker := r.Form.Get("marker") 83 switch marker { 84 case "": 85 fmt.Fprintf(w, ` 86 { 87 "qos_specs": [ 88 { 89 "consumer": "back-end", 90 "id": "1", 91 "name": "foo", 92 "specs": {} 93 }, 94 { 95 "consumer": "front-end", 96 "id": "2", 97 "name": "bar", 98 "specs" : { 99 "read_iops_sec" : "20000" 100 } 101 } 102 103 ], 104 "qos_specs_links": [ 105 { 106 "href": "%s/qos-specs?marker=2", 107 "rel": "next" 108 } 109 ] 110 } 111 `, th.Server.URL) 112 case "2": 113 fmt.Fprint(w, `{ "qos_specs": [] }`) 114 default: 115 t.Fatalf("Unexpected marker: [%s]", marker) 116 } 117 }) 118 } 119 120 func MockGetResponse(t *testing.T) { 121 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 122 th.TestMethod(t, r, "GET") 123 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 124 w.Header().Add("Content-Type", "application/json") 125 w.WriteHeader(http.StatusOK) 126 127 fmt.Fprint(w, ` 128 { 129 "qos_specs": { 130 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 131 "name": "qos-001", 132 "consumer": "front-end", 133 "specs": { 134 "read_iops_sec": "20000" 135 } 136 } 137 } 138 `) 139 }) 140 } 141 142 // UpdateBody provides a PUT result of the qos_specs for a QoS 143 const UpdateBody = ` 144 { 145 "qos_specs" : { 146 "consumer": "back-end", 147 "read_iops_sec": "40000", 148 "write_iops_sec": "40000" 149 } 150 } 151 ` 152 153 // UpdateQos is the expected qos_specs returned from PUT on a qos's qos_specs 154 var UpdateQos = map[string]string{ 155 "consumer": "back-end", 156 "read_iops_sec": "40000", 157 "write_iops_sec": "40000", 158 } 159 160 func MockUpdateResponse(t *testing.T) { 161 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 162 th.TestMethod(t, r, "PUT") 163 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 164 th.TestHeader(t, r, "Accept", "application/json") 165 th.TestJSONRequest(t, r, `{ 166 "qos_specs": { 167 "consumer": "back-end", 168 "read_iops_sec": "40000", 169 "write_iops_sec": "40000" 170 } 171 }`) 172 173 w.Header().Set("Content-Type", "application/json") 174 w.WriteHeader(http.StatusOK) 175 fmt.Fprint(w, UpdateBody) 176 }) 177 } 178 179 func MockDeleteKeysResponse(t *testing.T) { 180 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/delete_keys", func(w http.ResponseWriter, r *http.Request) { 181 th.TestMethod(t, r, "PUT") 182 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 183 th.TestJSONRequest(t, r, `{ 184 "keys": [ 185 "read_iops_sec" 186 ] 187 }`) 188 189 w.WriteHeader(http.StatusAccepted) 190 }) 191 } 192 193 func MockAssociateResponse(t *testing.T) { 194 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/associate", func(w http.ResponseWriter, r *http.Request) { 195 th.TestMethod(t, r, "GET") 196 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 197 w.WriteHeader(http.StatusAccepted) 198 }) 199 } 200 201 func MockDisassociateResponse(t *testing.T) { 202 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/disassociate", func(w http.ResponseWriter, r *http.Request) { 203 th.TestMethod(t, r, "GET") 204 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 205 w.WriteHeader(http.StatusAccepted) 206 }) 207 } 208 209 func MockDisassociateAllResponse(t *testing.T) { 210 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/disassociate_all", func(w http.ResponseWriter, r *http.Request) { 211 th.TestMethod(t, r, "GET") 212 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 213 w.WriteHeader(http.StatusAccepted) 214 }) 215 } 216 217 func MockListAssociationsResponse(t *testing.T) { 218 th.Mux.HandleFunc("/qos-specs/d32019d3-bc6e-4319-9c1d-6722fc136a22/associations", func(w http.ResponseWriter, r *http.Request) { 219 th.TestMethod(t, r, "GET") 220 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 221 w.Header().Add("Content-Type", "application/json") 222 fmt.Fprint(w, ` 223 { 224 "qos_associations": [ 225 { 226 "name": "foo", 227 "id": "2f954bcf047c4ee9b09a37d49ae6db54", 228 "association_type": "volume_type" 229 } 230 ] 231 } 232 `) 233 }) 234 }