fixtures.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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", 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. fmt.Fprintf(w, `
  16. {
  17. "volumes": [
  18. {
  19. "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
  20. "display_name": "vol-001"
  21. },
  22. {
  23. "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
  24. "display_name": "vol-002"
  25. }
  26. ]
  27. }
  28. `)
  29. })
  30. }
  31. func MockGetResponse(t *testing.T) {
  32. th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
  33. th.TestMethod(t, r, "GET")
  34. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  35. w.Header().Add("Content-Type", "application/json")
  36. w.WriteHeader(http.StatusOK)
  37. fmt.Fprintf(w, `
  38. {
  39. "volume": {
  40. "id": "521752a6-acf6-4b2d-bc7a-119f9148cd8c",
  41. "display_name": "vol-001",
  42. "display_description": "Another volume.",
  43. "status": "active",
  44. "size": 30,
  45. "volume_type": "289da7f8-6440-407c-9fb4-7db01ec49164",
  46. "metadata": {
  47. "contents": "junk"
  48. },
  49. "availability_zone": "us-east1",
  50. "bootable": "false",
  51. "snapshot_id": null,
  52. "attachments": [
  53. {
  54. "attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa",
  55. "id": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf",
  56. "volume_id": "6c80f8ac-e3e2-480c-8e6e-f1db92fe4bfe",
  57. "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f",
  58. "host_name": "mitaka",
  59. "device": "/"
  60. }
  61. ],
  62. "created_at": "2012-02-14T20:53:07"
  63. }
  64. }
  65. `)
  66. })
  67. }
  68. func MockCreateResponse(t *testing.T) {
  69. th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
  70. th.TestMethod(t, r, "POST")
  71. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  72. th.TestHeader(t, r, "Content-Type", "application/json")
  73. th.TestHeader(t, r, "Accept", "application/json")
  74. th.TestJSONRequest(t, r, `
  75. {
  76. "volume": {
  77. "size": 75,
  78. "availability_zone": "us-east1"
  79. }
  80. }
  81. `)
  82. w.Header().Add("Content-Type", "application/json")
  83. w.WriteHeader(http.StatusCreated)
  84. fmt.Fprintf(w, `
  85. {
  86. "volume": {
  87. "size": 4,
  88. "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
  89. }
  90. }
  91. `)
  92. })
  93. }
  94. func MockDeleteResponse(t *testing.T) {
  95. th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
  96. th.TestMethod(t, r, "DELETE")
  97. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  98. w.WriteHeader(http.StatusNoContent)
  99. })
  100. }
  101. func MockUpdateResponse(t *testing.T) {
  102. th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
  103. th.TestMethod(t, r, "PUT")
  104. th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
  105. w.WriteHeader(http.StatusOK)
  106. fmt.Fprintf(w, `
  107. {
  108. "volume": {
  109. "display_name": "vol-002",
  110. "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
  111. }
  112. }
  113. `)
  114. })
  115. }