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