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