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