fixtures.go 6.2 KB

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