github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v3/snapshots/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 9 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 10 ) 11 12 // MockListResponse provides mock response for list snapshot API call 13 func MockListResponse(t *testing.T) { 14 th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) { 15 th.TestMethod(t, r, "GET") 16 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 17 18 w.Header().Add("Content-Type", "application/json") 19 w.WriteHeader(http.StatusOK) 20 21 if err := r.ParseForm(); err != nil { 22 t.Errorf("Failed to parse request form %v", err) 23 } 24 marker := r.Form.Get("marker") 25 switch marker { 26 case "": 27 fmt.Fprintf(w, ` 28 { 29 "snapshots": [ 30 { 31 "id": "289da7f8-6440-407c-9fb4-7db01ec49164", 32 "name": "snapshot-001", 33 "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c", 34 "description": "Daily Backup", 35 "status": "available", 36 "size": 30, 37 "created_at": "2017-05-30T03:35:03.000000" 38 }, 39 { 40 "id": "96c3bda7-c82a-4f50-be73-ca7621794835", 41 "name": "snapshot-002", 42 "volume_id": "76b8950a-8594-4e5b-8dce-0dfa9c696358", 43 "description": "Weekly Backup", 44 "status": "available", 45 "size": 25, 46 "created_at": "2017-05-30T03:35:03.000000" 47 } 48 ], 49 "snapshots_links": [ 50 { 51 "href": "%s/snapshots?marker=1", 52 "rel": "next" 53 }] 54 } 55 `, th.Server.URL) 56 case "1": 57 fmt.Fprint(w, `{"snapshots": []}`) 58 default: 59 t.Fatalf("Unexpected marker: [%s]", marker) 60 } 61 }) 62 } 63 64 // MockGetResponse provides mock response for get snapshot API call 65 func MockGetResponse(t *testing.T) { 66 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 67 th.TestMethod(t, r, "GET") 68 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 69 70 w.Header().Add("Content-Type", "application/json") 71 w.WriteHeader(http.StatusOK) 72 fmt.Fprint(w, ` 73 { 74 "snapshot": { 75 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 76 "name": "snapshot-001", 77 "description": "Daily backup", 78 "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c", 79 "status": "available", 80 "size": 30, 81 "created_at": "2017-05-30T03:35:03.000000" 82 } 83 } 84 `) 85 }) 86 } 87 88 // MockCreateResponse provides mock response for create snapshot API call 89 func MockCreateResponse(t *testing.T) { 90 th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) { 91 th.TestMethod(t, r, "POST") 92 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 93 th.TestHeader(t, r, "Content-Type", "application/json") 94 th.TestHeader(t, r, "Accept", "application/json") 95 th.TestJSONRequest(t, r, ` 96 { 97 "snapshot": { 98 "volume_id": "1234", 99 "name": "snapshot-001" 100 } 101 } 102 `) 103 104 w.Header().Add("Content-Type", "application/json") 105 w.WriteHeader(http.StatusAccepted) 106 107 fmt.Fprint(w, ` 108 { 109 "snapshot": { 110 "volume_id": "1234", 111 "name": "snapshot-001", 112 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 113 "description": "Daily backup", 114 "volume_id": "1234", 115 "status": "available", 116 "size": 30, 117 "created_at": "2017-05-30T03:35:03.000000" 118 } 119 } 120 `) 121 }) 122 } 123 124 // MockUpdateMetadataResponse provides mock response for update metadata snapshot API call 125 func MockUpdateMetadataResponse(t *testing.T) { 126 th.Mux.HandleFunc("/snapshots/123/metadata", func(w http.ResponseWriter, r *http.Request) { 127 th.TestMethod(t, r, "PUT") 128 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 129 th.TestHeader(t, r, "Content-Type", "application/json") 130 th.TestJSONRequest(t, r, ` 131 { 132 "metadata": { 133 "key": "v1" 134 } 135 } 136 `) 137 138 fmt.Fprint(w, ` 139 { 140 "metadata": { 141 "key": "v1" 142 } 143 } 144 `) 145 }) 146 } 147 148 // MockDeleteResponse provides mock response for delete snapshot API call 149 func MockDeleteResponse(t *testing.T) { 150 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 151 th.TestMethod(t, r, "DELETE") 152 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 153 w.WriteHeader(http.StatusNoContent) 154 }) 155 } 156 157 // MockUpdateResponse provides mock response for update snapshot API call 158 func MockUpdateResponse(t *testing.T) { 159 th.Mux.HandleFunc("/snapshots/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 163 w.Header().Add("Content-Type", "application/json") 164 w.WriteHeader(http.StatusOK) 165 fmt.Fprint(w, ` 166 { 167 "snapshot": { 168 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 169 "name": "snapshot-002", 170 "description": "Daily backup 002", 171 "volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c", 172 "status": "available", 173 "size": 30, 174 "created_at": "2017-05-30T03:35:03.000000", 175 "updated_at": "2017-05-30T03:35:03.000000" 176 } 177 } 178 `) 179 }) 180 } 181 182 // MockResetStatusResponse provides mock response for reset snapshot status API call 183 func MockResetStatusResponse(t *testing.T) { 184 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22/action", func(w http.ResponseWriter, r *http.Request) { 185 th.TestMethod(t, r, "POST") 186 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 187 th.TestHeader(t, r, "Content-Type", "application/json") 188 th.TestJSONRequest(t, r, ` 189 { 190 "os-reset_status": { 191 "status": "error" 192 } 193 } 194 `) 195 w.WriteHeader(http.StatusAccepted) 196 }) 197 } 198 199 // MockUpdateStatusResponse provides mock response for update snapshot status API call 200 func MockUpdateStatusResponse(t *testing.T) { 201 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22/action", func(w http.ResponseWriter, r *http.Request) { 202 th.TestMethod(t, r, "POST") 203 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 204 th.TestHeader(t, r, "Content-Type", "application/json") 205 th.TestJSONRequest(t, r, ` 206 { 207 "os-update_snapshot_status": { 208 "status": "error", 209 "progress": "80%" 210 } 211 } 212 `) 213 w.WriteHeader(http.StatusAccepted) 214 }) 215 } 216 217 // MockForceDeleteResponse provides mock response for force delete snapshot API call 218 func MockForceDeleteResponse(t *testing.T) { 219 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22/action", func(w http.ResponseWriter, r *http.Request) { 220 th.TestMethod(t, r, "POST") 221 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 222 th.TestHeader(t, r, "Content-Type", "application/json") 223 th.TestJSONRequest(t, r, ` 224 { 225 "os-force_delete": {} 226 } 227 `) 228 w.WriteHeader(http.StatusAccepted) 229 }) 230 }