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