Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 4 | "fmt" |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | var metadata = map[string]string{"gophercloud-test": "containers"} |
| 14 | |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 15 | func TestListContainerInfo(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 16 | th.SetupHTTP() |
| 17 | defer th.TeardownHTTP() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 18 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 19 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 20 | th.TestMethod(t, r, "GET") |
| 21 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 22 | th.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 23 | |
Jon Perritt | 83c116b | 2014-09-30 16:30:24 -0500 | [diff] [blame] | 24 | w.Header().Set("Content-Type", "application/json") |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 25 | r.ParseForm() |
| 26 | marker := r.Form.Get("marker") |
| 27 | switch marker { |
| 28 | case "": |
| 29 | fmt.Fprintf(w, `[ |
| 30 | { |
| 31 | "count": 0, |
| 32 | "bytes": 0, |
| 33 | "name": "janeausten" |
| 34 | }, |
| 35 | { |
| 36 | "count": 1, |
| 37 | "bytes": 14, |
| 38 | "name": "marktwain" |
| 39 | } |
| 40 | ]`) |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 41 | case "marktwain": |
| 42 | fmt.Fprintf(w, `[]`) |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 43 | default: |
| 44 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 45 | } |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 46 | }) |
| 47 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 48 | count := 0 |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 49 | |
| 50 | List(fake.ServiceClient(), &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 51 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 52 | actual, err := ExtractInfo(page) |
| 53 | if err != nil { |
| 54 | t.Errorf("Failed to extract container info: %v", err) |
| 55 | return false, err |
| 56 | } |
| 57 | |
| 58 | expected := []Container{ |
| 59 | Container{ |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 60 | Count: 0, |
| 61 | Bytes: 0, |
| 62 | Name: "janeausten", |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 63 | }, |
| 64 | Container{ |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 65 | Count: 1, |
| 66 | Bytes: 14, |
| 67 | Name: "marktwain", |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 68 | }, |
| 69 | } |
| 70 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 71 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 72 | |
| 73 | return true, nil |
| 74 | }) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 75 | |
| 76 | if count != 1 { |
| 77 | t.Errorf("Expected 1 page, got %d", count) |
| 78 | } |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | func TestListContainerNames(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 82 | th.SetupHTTP() |
| 83 | defer th.TeardownHTTP() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 84 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 85 | th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 86 | th.TestMethod(t, r, "GET") |
| 87 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 88 | th.TestHeader(t, r, "Accept", "text/plain") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 89 | |
| 90 | w.Header().Set("Content-Type", "text/plain") |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 91 | r.ParseForm() |
| 92 | marker := r.Form.Get("marker") |
| 93 | switch marker { |
| 94 | case "": |
| 95 | fmt.Fprintf(w, "janeausten\nmarktwain\n") |
| 96 | case "marktwain": |
| 97 | fmt.Fprintf(w, ``) |
| 98 | default: |
| 99 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 100 | } |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 101 | }) |
| 102 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 103 | count := 0 |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 104 | |
| 105 | List(fake.ServiceClient(), &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 106 | count++ |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 107 | actual, err := ExtractNames(page) |
| 108 | if err != nil { |
| 109 | t.Errorf("Failed to extract container names: %v", err) |
| 110 | return false, err |
| 111 | } |
| 112 | |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 113 | expected := []string{"janeausten", "marktwain"} |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 114 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 115 | th.CheckDeepEquals(t, expected, actual) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 116 | |
| 117 | return true, nil |
| 118 | }) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 119 | |
| 120 | if count != 1 { |
| 121 | t.Errorf("Expected 1 page, got %d", count) |
| 122 | } |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | func TestCreateContainer(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 126 | th.SetupHTTP() |
| 127 | defer th.TeardownHTTP() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 128 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 129 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 130 | th.TestMethod(t, r, "PUT") |
| 131 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 132 | th.TestHeader(t, r, "Accept", "application/json") |
| 133 | |
| 134 | w.Header().Add("X-Container-Meta-Foo", "bar") |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 135 | w.WriteHeader(http.StatusNoContent) |
| 136 | }) |
| 137 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 138 | options := CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}} |
| 139 | headers, err := Create(fake.ServiceClient(), "testContainer", options).ExtractHeaders() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 140 | if err != nil { |
| 141 | t.Fatalf("Unexpected error creating container: %v", err) |
| 142 | } |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 143 | th.CheckEquals(t, "bar", headers["X-Container-Meta-Foo"][0]) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | func TestDeleteContainer(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 147 | th.SetupHTTP() |
| 148 | defer th.TeardownHTTP() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 149 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 150 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 151 | th.TestMethod(t, r, "DELETE") |
| 152 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 153 | th.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 154 | w.WriteHeader(http.StatusNoContent) |
| 155 | }) |
| 156 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 157 | _, err := Delete(fake.ServiceClient(), "testContainer").ExtractHeaders() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 158 | if err != nil { |
| 159 | t.Fatalf("Unexpected error deleting container: %v", err) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | func TestUpateContainer(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 164 | th.SetupHTTP() |
| 165 | defer th.TeardownHTTP() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 166 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 167 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 168 | th.TestMethod(t, r, "POST") |
| 169 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 170 | th.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 171 | w.WriteHeader(http.StatusNoContent) |
| 172 | }) |
| 173 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 174 | options := &UpdateOpts{Metadata: map[string]string{"foo": "bar"}} |
| 175 | _, err := Update(fake.ServiceClient(), "testContainer", options).ExtractHeaders() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 176 | if err != nil { |
| 177 | t.Fatalf("Unexpected error updating container metadata: %v", err) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func TestGetContainer(t *testing.T) { |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 182 | th.SetupHTTP() |
| 183 | defer th.TeardownHTTP() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 184 | |
Jon Perritt | 27caa2f | 2014-10-13 21:08:36 -0500 | [diff] [blame^] | 185 | th.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 186 | th.TestMethod(t, r, "HEAD") |
| 187 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 188 | th.TestHeader(t, r, "Accept", "application/json") |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 189 | w.WriteHeader(http.StatusNoContent) |
| 190 | }) |
| 191 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 192 | _, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 193 | if err != nil { |
| 194 | t.Fatalf("Unexpected error getting container metadata: %v", err) |
| 195 | } |
| 196 | } |