1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
package testing
import (
"fmt"
"net/http"
"testing"
th "github.com/gophercloud/gophercloud/testhelper"
fake "github.com/gophercloud/gophercloud/testhelper/client"
)
// MockListResponse provides mock responce for list snapshot API call
func MockListResponse(t *testing.T) {
th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
r.ParseForm()
marker := r.Form.Get("marker")
switch marker {
case "":
fmt.Fprintf(w, `
{
"snapshots": [
{
"id": "289da7f8-6440-407c-9fb4-7db01ec49164",
"name": "snapshot-001",
"volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
"description": "Daily Backup",
"status": "available",
"size": 30,
"created_at": "2017-05-30T03:35:03.000000"
},
{
"id": "96c3bda7-c82a-4f50-be73-ca7621794835",
"name": "snapshot-002",
"volume_id": "76b8950a-8594-4e5b-8dce-0dfa9c696358",
"description": "Weekly Backup",
"status": "available",
"size": 25,
"created_at": "2017-05-30T03:35:03.000000"
}
],
"snapshots_links": [
{
"href": "%s/snapshots?marker=1",
"rel": "next"
}]
}
`, th.Server.URL)
case "1":
fmt.Fprintf(w, `{"snapshots": []}`)
default:
t.Fatalf("Unexpected marker: [%s]", marker)
}
})
}
// MockGetResponse provides mock responce for get snapshot API call
func MockGetResponse(t *testing.T) {
th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"snapshot": {
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"name": "snapshot-001",
"description": "Daily backup",
"volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
"status": "available",
"size": 30,
"created_at": "2017-05-30T03:35:03.000000"
}
}
`)
})
}
// MockCreateResponse provides mock responce for create snapshot API call
func MockCreateResponse(t *testing.T) {
th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestHeader(t, r, "Accept", "application/json")
th.TestJSONRequest(t, r, `
{
"snapshot": {
"volume_id": "1234",
"name": "snapshot-001"
}
}
`)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, `
{
"snapshot": {
"volume_id": "1234",
"name": "snapshot-001",
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"description": "Daily backup",
"volume_id": "1234",
"status": "available",
"size": 30,
"created_at": "2017-05-30T03:35:03.000000"
}
}
`)
})
}
// MockUpdateMetadataResponse provides mock responce for update metadata snapshot API call
func MockUpdateMetadataResponse(t *testing.T) {
th.Mux.HandleFunc("/snapshots/123/metadata", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
th.TestHeader(t, r, "Content-Type", "application/json")
th.TestJSONRequest(t, r, `
{
"metadata": {
"key": "v1"
}
}
`)
fmt.Fprintf(w, `
{
"metadata": {
"key": "v1"
}
}
`)
})
}
// MockDeleteResponse provides mock responce for delete snapshot API call
func MockDeleteResponse(t *testing.T) {
th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "DELETE")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.WriteHeader(http.StatusNoContent)
})
}
// MockUpdateResponse provides mock responce for update snapshot API call
func MockUpdateResponse(t *testing.T) {
th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "PUT")
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `
{
"snapshot": {
"id": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
"name": "snapshot-002",
"description": "Daily backup 002",
"volume_id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
"status": "available",
"size": 30,
"created_at": "2017-05-30T03:35:03.000000",
"updated_at": "2017-05-30T03:35:03.000000"
}
}
`)
})
}
|