blob: 1ab09a986630a237a98412cd97f598f0fa62083d [file] [log] [blame]
Jon Perritt50da9b42014-09-14 15:06:59 -05001package containers
2
3import (
Jon Perritt8c93a302014-09-28 22:35:57 -05004 "fmt"
Jon Perritt50da9b42014-09-14 15:06:59 -05005 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
Jon Perritt8c93a302014-09-28 22:35:57 -05009 "github.com/rackspace/gophercloud/pagination"
Jon Perritt50da9b42014-09-14 15:06:59 -050010 "github.com/rackspace/gophercloud/testhelper"
11)
12
Jon Perritt8c93a302014-09-28 22:35:57 -050013const (
Jon Perritt50da9b42014-09-14 15:06:59 -050014 tokenId = "abcabcabcabc"
15)
16
17var metadata = map[string]string{"gophercloud-test": "containers"}
18
19func serviceClient() *gophercloud.ServiceClient {
20 return &gophercloud.ServiceClient{
21 Provider: &gophercloud.ProviderClient{TokenID: tokenId},
22 Endpoint: testhelper.Endpoint(),
23 }
24}
25
26func 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 Perritt8c93a302014-09-28 22:35:57 -050034
Jon Perritt83c116b2014-09-30 16:30:24 -050035 w.Header().Set("Content-Type", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -050036 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 Perrittfdac6e52014-09-29 19:43:45 -050052 case "marktwain":
53 fmt.Fprintf(w, `[]`)
Jon Perritt8aa40262014-09-29 15:41:32 -050054 default:
55 t.Fatalf("Unexpected marker: [%s]", marker)
56 }
Jon Perritt50da9b42014-09-14 15:06:59 -050057 })
58
59 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -050060 count := 0
61 List(client, &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
62 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050063 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 Perritt8aa40262014-09-29 15:41:32 -050071 Count: 0,
72 Bytes: 0,
73 Name: "janeausten",
Jon Perritt8c93a302014-09-28 22:35:57 -050074 },
75 Container{
Jon Perritt8aa40262014-09-29 15:41:32 -050076 Count: 1,
77 Bytes: 14,
78 Name: "marktwain",
Jon Perritt8c93a302014-09-28 22:35:57 -050079 },
80 }
81
82 testhelper.CheckDeepEquals(t, expected, actual)
83
84 return true, nil
85 })
Jon Perrittde47eac2014-09-30 15:34:17 -050086
87 if count != 1 {
88 t.Errorf("Expected 1 page, got %d", count)
89 }
Jon Perritt50da9b42014-09-14 15:06:59 -050090}
91
92func 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 Perritt8c93a302014-09-28 22:35:57 -0500100
101 w.Header().Set("Content-Type", "text/plain")
Jon Perrittfdac6e52014-09-29 19:43:45 -0500102 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 Perritt50da9b42014-09-14 15:06:59 -0500112 })
113
114 client := serviceClient()
Jon Perrittde47eac2014-09-30 15:34:17 -0500115 count := 0
116 List(client, &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
117 count++
Jon Perritt8c93a302014-09-28 22:35:57 -0500118 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 Perrittfdac6e52014-09-29 19:43:45 -0500124 expected := []string{"janeausten", "marktwain"}
Jon Perritt8c93a302014-09-28 22:35:57 -0500125
126 testhelper.CheckDeepEquals(t, expected, actual)
127
128 return true, nil
129 })
Jon Perrittde47eac2014-09-30 15:34:17 -0500130
131 if count != 1 {
132 t.Errorf("Expected 1 page, got %d", count)
133 }
Jon Perritt50da9b42014-09-14 15:06:59 -0500134}
135
136func 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 Perritt5db08922014-09-30 21:32:48 -0500148 _, err := Create(client, "testContainer", nil).ExtractHeaders()
Jon Perritt50da9b42014-09-14 15:06:59 -0500149 if err != nil {
150 t.Fatalf("Unexpected error creating container: %v", err)
151 }
152}
153
154func 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 Perritt5db08922014-09-30 21:32:48 -0500166 _, err := Delete(client, "testContainer").ExtractHeaders()
Jon Perritt50da9b42014-09-14 15:06:59 -0500167 if err != nil {
168 t.Fatalf("Unexpected error deleting container: %v", err)
169 }
170}
171
172func 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 Perritt5db08922014-09-30 21:32:48 -0500184 _, err := Update(client, "testContainer", nil).ExtractHeaders()
Jon Perritt50da9b42014-09-14 15:06:59 -0500185 if err != nil {
186 t.Fatalf("Unexpected error updating container metadata: %v", err)
187 }
188}
189
190func 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 Perritt8c93a302014-09-28 22:35:57 -0500202 _, err := Get(client, "testContainer").ExtractMetadata()
Jon Perritt50da9b42014-09-14 15:06:59 -0500203 if err != nil {
204 t.Fatalf("Unexpected error getting container metadata: %v", err)
205 }
206}