github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v2/volumes/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("/volumes/detail", 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 fmt.Fprintf(w, ` 21 { 22 "volumes": [ 23 { 24 "volume_type": "lvmdriver-1", 25 "created_at": "2015-09-17T03:35:03.000000", 26 "bootable": "false", 27 "name": "vol-001", 28 "os-vol-mig-status-attr:name_id": null, 29 "consistencygroup_id": null, 30 "source_volid": null, 31 "os-volume-replication:driver_data": null, 32 "multiattach": false, 33 "snapshot_id": null, 34 "replication_status": "disabled", 35 "os-volume-replication:extended_status": null, 36 "encrypted": false, 37 "os-vol-host-attr:host": null, 38 "availability_zone": "nova", 39 "attachments": [{ 40 "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a", 41 "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a", 42 "attached_at": "2016-08-06T14:48:20.000000", 43 "host_name": "foobar", 44 "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 45 "device": "/dev/vdc", 46 "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75" 47 }], 48 "id": "289da7f8-6440-407c-9fb4-7db01ec49164", 49 "size": 75, 50 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 51 "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459", 52 "os-vol-mig-status-attr:migstat": null, 53 "metadata": {"foo": "bar"}, 54 "status": "available", 55 "description": null 56 }, 57 { 58 "volume_type": "lvmdriver-1", 59 "created_at": "2015-09-17T03:32:29.000000", 60 "bootable": "false", 61 "name": "vol-002", 62 "os-vol-mig-status-attr:name_id": null, 63 "consistencygroup_id": null, 64 "source_volid": null, 65 "os-volume-replication:driver_data": null, 66 "multiattach": false, 67 "snapshot_id": null, 68 "replication_status": "disabled", 69 "os-volume-replication:extended_status": null, 70 "encrypted": false, 71 "os-vol-host-attr:host": null, 72 "availability_zone": "nova", 73 "attachments": [], 74 "id": "96c3bda7-c82a-4f50-be73-ca7621794835", 75 "size": 75, 76 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 77 "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459", 78 "os-vol-mig-status-attr:migstat": null, 79 "metadata": {}, 80 "status": "available", 81 "description": null 82 } 83 ] 84 } 85 `) 86 }) 87 } 88 89 func MockGetResponse(t *testing.T) { 90 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 91 th.TestMethod(t, r, "GET") 92 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 93 94 w.Header().Add("Content-Type", "application/json") 95 w.WriteHeader(http.StatusOK) 96 fmt.Fprintf(w, ` 97 { 98 "volume": { 99 "volume_type": "lvmdriver-1", 100 "created_at": "2015-09-17T03:32:29.000000", 101 "bootable": "false", 102 "name": "vol-001", 103 "os-vol-mig-status-attr:name_id": null, 104 "consistencygroup_id": null, 105 "source_volid": null, 106 "os-volume-replication:driver_data": null, 107 "multiattach": false, 108 "snapshot_id": null, 109 "replication_status": "disabled", 110 "os-volume-replication:extended_status": null, 111 "encrypted": false, 112 "os-vol-host-attr:host": null, 113 "availability_zone": "nova", 114 "attachments": [{ 115 "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a", 116 "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a", 117 "attached_at": "2016-08-06T14:48:20.000000", 118 "host_name": "foobar", 119 "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 120 "device": "/dev/vdc", 121 "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75" 122 }], 123 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 124 "size": 75, 125 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 126 "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459", 127 "os-vol-mig-status-attr:migstat": null, 128 "metadata": {}, 129 "status": "available", 130 "description": null 131 } 132 } 133 `) 134 }) 135 } 136 137 func MockCreateResponse(t *testing.T) { 138 th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) { 139 th.TestMethod(t, r, "POST") 140 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 141 th.TestHeader(t, r, "Content-Type", "application/json") 142 th.TestHeader(t, r, "Accept", "application/json") 143 th.TestJSONRequest(t, r, ` 144 { 145 "volume": { 146 "name": "vol-001", 147 "size": 75 148 } 149 } 150 `) 151 152 w.Header().Add("Content-Type", "application/json") 153 w.WriteHeader(http.StatusAccepted) 154 155 fmt.Fprintf(w, ` 156 { 157 "volume": { 158 "size": 75, 159 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 160 "metadata": {}, 161 "created_at": "2015-09-17T03:32:29.044216", 162 "encrypted": false, 163 "bootable": "false", 164 "availability_zone": "nova", 165 "attachments": [], 166 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 167 "status": "creating", 168 "description": null, 169 "volume_type": "lvmdriver-1", 170 "name": "vol-001", 171 "replication_status": "disabled", 172 "consistencygroup_id": null, 173 "source_volid": null, 174 "snapshot_id": null, 175 "multiattach": false 176 } 177 } 178 `) 179 }) 180 } 181 182 func MockDeleteResponse(t *testing.T) { 183 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 184 th.TestMethod(t, r, "DELETE") 185 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 186 w.WriteHeader(http.StatusAccepted) 187 }) 188 } 189 190 func MockUpdateResponse(t *testing.T) { 191 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 192 th.TestMethod(t, r, "PUT") 193 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 194 w.WriteHeader(http.StatusOK) 195 fmt.Fprintf(w, ` 196 { 197 "volume": { 198 "name": "vol-002" 199 } 200 } 201 `) 202 }) 203 }