Jon Perritt | e747a0f | 2014-09-29 19:54:55 -0500 | [diff] [blame] | 1 | package volumes |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | const TokenID = "123" |
| 14 | |
| 15 | func ServiceClient() *gophercloud.ServiceClient { |
| 16 | return &gophercloud.ServiceClient{ |
| 17 | Provider: &gophercloud.ProviderClient{ |
| 18 | TokenID: TokenID, |
| 19 | }, |
| 20 | Endpoint: th.Endpoint(), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestList(t *testing.T) { |
| 25 | th.SetupHTTP() |
| 26 | defer th.TeardownHTTP() |
| 27 | |
| 28 | th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) { |
| 29 | th.TestMethod(t, r, "GET") |
| 30 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 31 | |
| 32 | w.Header().Add("Content-Type", "application/json") |
| 33 | w.WriteHeader(http.StatusOK) |
| 34 | |
| 35 | fmt.Fprintf(w, ` |
| 36 | { |
| 37 | "volumes": [ |
| 38 | { |
| 39 | "id": "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 40 | "display_name": "vol-001" |
| 41 | }, |
| 42 | { |
| 43 | "id": "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 44 | "display_name": "vol-002" |
| 45 | } |
| 46 | ] |
| 47 | } |
| 48 | `) |
| 49 | }) |
| 50 | |
| 51 | client := ServiceClient() |
| 52 | count := 0 |
| 53 | |
| 54 | List(client, &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 55 | count++ |
| 56 | actual, err := ExtractVolumes(page) |
| 57 | if err != nil { |
| 58 | t.Errorf("Failed to extract volumes: %v", err) |
| 59 | return false, err |
| 60 | } |
| 61 | |
| 62 | expected := []Volume{ |
| 63 | Volume{ |
| 64 | ID: "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 65 | Name: "vol-001", |
| 66 | }, |
| 67 | Volume{ |
| 68 | ID: "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 69 | Name: "vol-002", |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | th.CheckDeepEquals(t, expected, actual) |
| 74 | |
| 75 | return true, nil |
| 76 | }) |
| 77 | |
| 78 | if count != 1 { |
| 79 | t.Errorf("Expected 1 page, got %d", count) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestGet(t *testing.T) { |
| 84 | th.SetupHTTP() |
| 85 | defer th.TeardownHTTP() |
| 86 | |
| 87 | th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 88 | th.TestMethod(t, r, "GET") |
| 89 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 90 | |
| 91 | w.Header().Add("Content-Type", "application/json") |
| 92 | w.WriteHeader(http.StatusOK) |
| 93 | fmt.Fprintf(w, ` |
| 94 | { |
| 95 | "volume": { |
| 96 | "display_name": "vol-001", |
| 97 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 98 | } |
| 99 | } |
| 100 | `) |
| 101 | }) |
| 102 | |
| 103 | v, err := Get(ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
| 104 | th.AssertNoErr(t, err) |
| 105 | |
| 106 | th.AssertEquals(t, v.Name, "vol-001") |
| 107 | th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 108 | } |
| 109 | |
| 110 | func TestCreate(t *testing.T) { |
| 111 | th.SetupHTTP() |
| 112 | defer th.TeardownHTTP() |
| 113 | |
| 114 | th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) { |
| 115 | th.TestMethod(t, r, "POST") |
| 116 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 117 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 118 | th.TestHeader(t, r, "Accept", "application/json") |
| 119 | th.TestJSONRequest(t, r, ` |
| 120 | { |
| 121 | "volume": { |
| 122 | "display_name": "vol-001" |
| 123 | } |
| 124 | } |
| 125 | `) |
| 126 | |
| 127 | w.Header().Add("Content-Type", "application/json") |
| 128 | w.WriteHeader(http.StatusCreated) |
| 129 | |
| 130 | fmt.Fprintf(w, ` |
| 131 | { |
| 132 | "volume": { |
| 133 | "display_name": "vol-001", |
| 134 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 135 | } |
| 136 | } |
| 137 | `) |
| 138 | }) |
| 139 | |
| 140 | options := &CreateOpts{Name: "vol-001"} |
| 141 | n, err := Create(ServiceClient(), options).Extract() |
| 142 | th.AssertNoErr(t, err) |
| 143 | |
| 144 | th.AssertEquals(t, n.Name, "vol-001") |
| 145 | th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 146 | } |
| 147 | |
| 148 | func TestDelete(t *testing.T) { |
| 149 | th.SetupHTTP() |
| 150 | defer th.TeardownHTTP() |
| 151 | |
| 152 | th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { |
| 153 | th.TestMethod(t, r, "DELETE") |
| 154 | th.TestHeader(t, r, "X-Auth-Token", TokenID) |
| 155 | w.WriteHeader(http.StatusNoContent) |
| 156 | }) |
| 157 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 158 | err := Delete(ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 159 | th.AssertNoErr(t, err) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 160 | } |