blob: 95626767aa7db893d22b8ff73b7f508e83373975 [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
Jon Perritt8c93a302014-09-28 22:35:57 -05008 "github.com/rackspace/gophercloud/pagination"
Jon Perritt27caa2f2014-10-13 21:08:36 -05009 th "github.com/rackspace/gophercloud/testhelper"
Jamie Hannaford4ff79962014-10-06 16:28:23 +020010 fake "github.com/rackspace/gophercloud/testhelper/client"
Jon Perritt50da9b42014-09-14 15:06:59 -050011)
12
13var metadata = map[string]string{"gophercloud-test": "containers"}
14
Jon Perritt50da9b42014-09-14 15:06:59 -050015func TestListContainerInfo(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050016 th.SetupHTTP()
17 defer th.TeardownHTTP()
Jon Perritt50da9b42014-09-14 15:06:59 -050018
Jon Perritt27caa2f2014-10-13 21:08:36 -050019 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 Perritt8c93a302014-09-28 22:35:57 -050023
Jon Perritt83c116b2014-09-30 16:30:24 -050024 w.Header().Set("Content-Type", "application/json")
Jon Perritt8aa40262014-09-29 15:41:32 -050025 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 Perrittfdac6e52014-09-29 19:43:45 -050041 case "marktwain":
42 fmt.Fprintf(w, `[]`)
Jon Perritt8aa40262014-09-29 15:41:32 -050043 default:
44 t.Fatalf("Unexpected marker: [%s]", marker)
45 }
Jon Perritt50da9b42014-09-14 15:06:59 -050046 })
47
Jon Perrittde47eac2014-09-30 15:34:17 -050048 count := 0
Jamie Hannaford4ff79962014-10-06 16:28:23 +020049
50 List(fake.ServiceClient(), &ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -050051 count++
Jon Perritt8c93a302014-09-28 22:35:57 -050052 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 Perritt8aa40262014-09-29 15:41:32 -050060 Count: 0,
61 Bytes: 0,
62 Name: "janeausten",
Jon Perritt8c93a302014-09-28 22:35:57 -050063 },
64 Container{
Jon Perritt8aa40262014-09-29 15:41:32 -050065 Count: 1,
66 Bytes: 14,
67 Name: "marktwain",
Jon Perritt8c93a302014-09-28 22:35:57 -050068 },
69 }
70
Jon Perritt27caa2f2014-10-13 21:08:36 -050071 th.CheckDeepEquals(t, expected, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -050072
73 return true, nil
74 })
Jon Perrittde47eac2014-09-30 15:34:17 -050075
76 if count != 1 {
77 t.Errorf("Expected 1 page, got %d", count)
78 }
Jon Perritt50da9b42014-09-14 15:06:59 -050079}
80
81func TestListContainerNames(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -050082 th.SetupHTTP()
83 defer th.TeardownHTTP()
Jon Perritt50da9b42014-09-14 15:06:59 -050084
Jon Perritt27caa2f2014-10-13 21:08:36 -050085 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 Perritt8c93a302014-09-28 22:35:57 -050089
90 w.Header().Set("Content-Type", "text/plain")
Jon Perrittfdac6e52014-09-29 19:43:45 -050091 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 Perritt50da9b42014-09-14 15:06:59 -0500101 })
102
Jon Perrittde47eac2014-09-30 15:34:17 -0500103 count := 0
Jamie Hannaford4ff79962014-10-06 16:28:23 +0200104
105 List(fake.ServiceClient(), &ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittde47eac2014-09-30 15:34:17 -0500106 count++
Jon Perritt8c93a302014-09-28 22:35:57 -0500107 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 Perrittfdac6e52014-09-29 19:43:45 -0500113 expected := []string{"janeausten", "marktwain"}
Jon Perritt8c93a302014-09-28 22:35:57 -0500114
Jon Perritt27caa2f2014-10-13 21:08:36 -0500115 th.CheckDeepEquals(t, expected, actual)
Jon Perritt8c93a302014-09-28 22:35:57 -0500116
117 return true, nil
118 })
Jon Perrittde47eac2014-09-30 15:34:17 -0500119
120 if count != 1 {
121 t.Errorf("Expected 1 page, got %d", count)
122 }
Jon Perritt50da9b42014-09-14 15:06:59 -0500123}
124
125func TestCreateContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -0500126 th.SetupHTTP()
127 defer th.TeardownHTTP()
Jon Perritt50da9b42014-09-14 15:06:59 -0500128
Jon Perritt27caa2f2014-10-13 21:08:36 -0500129 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 Perritt50da9b42014-09-14 15:06:59 -0500135 w.WriteHeader(http.StatusNoContent)
136 })
137
Jon Perritt27caa2f2014-10-13 21:08:36 -0500138 options := CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}}
139 headers, err := Create(fake.ServiceClient(), "testContainer", options).ExtractHeaders()
Jon Perritt50da9b42014-09-14 15:06:59 -0500140 if err != nil {
141 t.Fatalf("Unexpected error creating container: %v", err)
142 }
Jon Perritt27caa2f2014-10-13 21:08:36 -0500143 th.CheckEquals(t, "bar", headers["X-Container-Meta-Foo"][0])
Jon Perritt50da9b42014-09-14 15:06:59 -0500144}
145
146func TestDeleteContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -0500147 th.SetupHTTP()
148 defer th.TeardownHTTP()
Jon Perritt50da9b42014-09-14 15:06:59 -0500149
Jon Perritt27caa2f2014-10-13 21:08:36 -0500150 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 Perritt50da9b42014-09-14 15:06:59 -0500154 w.WriteHeader(http.StatusNoContent)
155 })
156
Jamie Hannaford4ff79962014-10-06 16:28:23 +0200157 _, err := Delete(fake.ServiceClient(), "testContainer").ExtractHeaders()
Jon Perritt50da9b42014-09-14 15:06:59 -0500158 if err != nil {
159 t.Fatalf("Unexpected error deleting container: %v", err)
160 }
161}
162
163func TestUpateContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -0500164 th.SetupHTTP()
165 defer th.TeardownHTTP()
Jon Perritt50da9b42014-09-14 15:06:59 -0500166
Jon Perritt27caa2f2014-10-13 21:08:36 -0500167 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 Perritt50da9b42014-09-14 15:06:59 -0500171 w.WriteHeader(http.StatusNoContent)
172 })
173
Jon Perritt27caa2f2014-10-13 21:08:36 -0500174 options := &UpdateOpts{Metadata: map[string]string{"foo": "bar"}}
175 _, err := Update(fake.ServiceClient(), "testContainer", options).ExtractHeaders()
Jon Perritt50da9b42014-09-14 15:06:59 -0500176 if err != nil {
177 t.Fatalf("Unexpected error updating container metadata: %v", err)
178 }
179}
180
181func TestGetContainer(t *testing.T) {
Jon Perritt27caa2f2014-10-13 21:08:36 -0500182 th.SetupHTTP()
183 defer th.TeardownHTTP()
Jon Perritt50da9b42014-09-14 15:06:59 -0500184
Jon Perritt27caa2f2014-10-13 21:08:36 -0500185 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 Perritt50da9b42014-09-14 15:06:59 -0500189 w.WriteHeader(http.StatusNoContent)
190 })
191
Jamie Hannaford4ff79962014-10-06 16:28:23 +0200192 _, err := Get(fake.ServiceClient(), "testContainer").ExtractMetadata()
Jon Perritt50da9b42014-09-14 15:06:59 -0500193 if err != nil {
194 t.Fatalf("Unexpected error getting container metadata: %v", err)
195 }
196}