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 | |
| 35 | w.Header().Add("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 | ]`) |
| 52 | default: |
| 53 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 54 | } |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 55 | }) |
| 56 | |
| 57 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 58 | List(client, ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 59 | actual, err := ExtractInfo(page) |
| 60 | if err != nil { |
| 61 | t.Errorf("Failed to extract container info: %v", err) |
| 62 | return false, err |
| 63 | } |
| 64 | |
| 65 | expected := []Container{ |
| 66 | Container{ |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame^] | 67 | Count: 0, |
| 68 | Bytes: 0, |
| 69 | Name: "janeausten", |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 70 | }, |
| 71 | Container{ |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame^] | 72 | Count: 1, |
| 73 | Bytes: 14, |
| 74 | Name: "marktwain", |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 75 | }, |
| 76 | } |
| 77 | |
| 78 | testhelper.CheckDeepEquals(t, expected, actual) |
| 79 | |
| 80 | return true, nil |
| 81 | }) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | func TestListContainerNames(t *testing.T) { |
| 85 | testhelper.SetupHTTP() |
| 86 | defer testhelper.TeardownHTTP() |
| 87 | |
| 88 | testhelper.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 89 | testhelper.TestMethod(t, r, "GET") |
| 90 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 91 | testhelper.TestHeader(t, r, "Accept", "text/plain") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 92 | |
| 93 | w.Header().Set("Content-Type", "text/plain") |
| 94 | w.WriteHeader(http.StatusOK) |
| 95 | fmt.Fprintf(w, "") |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 96 | }) |
| 97 | |
| 98 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 99 | List(client, ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 100 | actual, err := ExtractNames(page) |
| 101 | if err != nil { |
| 102 | t.Errorf("Failed to extract container names: %v", err) |
| 103 | return false, err |
| 104 | } |
| 105 | |
| 106 | expected := []string{"janeausten, marktwain"} |
| 107 | |
| 108 | testhelper.CheckDeepEquals(t, expected, actual) |
| 109 | |
| 110 | return true, nil |
| 111 | }) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | func TestCreateContainer(t *testing.T) { |
| 115 | testhelper.SetupHTTP() |
| 116 | defer testhelper.TeardownHTTP() |
| 117 | |
| 118 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 119 | testhelper.TestMethod(t, r, "PUT") |
| 120 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 121 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 122 | w.WriteHeader(http.StatusNoContent) |
| 123 | }) |
| 124 | |
| 125 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 126 | _, err := Create(client, "testContainer", CreateOpts{}) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 127 | if err != nil { |
| 128 | t.Fatalf("Unexpected error creating container: %v", err) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestDeleteContainer(t *testing.T) { |
| 133 | testhelper.SetupHTTP() |
| 134 | defer testhelper.TeardownHTTP() |
| 135 | |
| 136 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 137 | testhelper.TestMethod(t, r, "DELETE") |
| 138 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 139 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 140 | w.WriteHeader(http.StatusNoContent) |
| 141 | }) |
| 142 | |
| 143 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 144 | err := Delete(client, "testContainer") |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 145 | if err != nil { |
| 146 | t.Fatalf("Unexpected error deleting container: %v", err) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestUpateContainer(t *testing.T) { |
| 151 | testhelper.SetupHTTP() |
| 152 | defer testhelper.TeardownHTTP() |
| 153 | |
| 154 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 155 | testhelper.TestMethod(t, r, "POST") |
| 156 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 157 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 158 | w.WriteHeader(http.StatusNoContent) |
| 159 | }) |
| 160 | |
| 161 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 162 | err := Update(client, "testContainer", UpdateOpts{}) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 163 | if err != nil { |
| 164 | t.Fatalf("Unexpected error updating container metadata: %v", err) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | func TestGetContainer(t *testing.T) { |
| 169 | testhelper.SetupHTTP() |
| 170 | defer testhelper.TeardownHTTP() |
| 171 | |
| 172 | testhelper.Mux.HandleFunc("/testContainer", func(w http.ResponseWriter, r *http.Request) { |
| 173 | testhelper.TestMethod(t, r, "HEAD") |
| 174 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenId) |
| 175 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 176 | w.WriteHeader(http.StatusNoContent) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 177 | fmt.Fprintf(w, ` |
| 178 | |
| 179 | `) |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 180 | }) |
| 181 | |
| 182 | client := serviceClient() |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 183 | _, err := Get(client, "testContainer").ExtractMetadata() |
Jon Perritt | 50da9b4 | 2014-09-14 15:06:59 -0500 | [diff] [blame] | 184 | if err != nil { |
| 185 | t.Fatalf("Unexpected error getting container metadata: %v", err) |
| 186 | } |
| 187 | } |