blob: 93747be27735ef793efbf5ebf8665c3c0a83746c [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
35 w.Header().Add("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 ]`)
52 default:
53 t.Fatalf("Unexpected marker: [%s]", marker)
54 }
Jon Perritt50da9b42014-09-14 15:06:59 -050055 })
56
57 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -050058 List(client, ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -050059 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 Perritt8aa40262014-09-29 15:41:32 -050067 Count: 0,
68 Bytes: 0,
69 Name: "janeausten",
Jon Perritt8c93a302014-09-28 22:35:57 -050070 },
71 Container{
Jon Perritt8aa40262014-09-29 15:41:32 -050072 Count: 1,
73 Bytes: 14,
74 Name: "marktwain",
Jon Perritt8c93a302014-09-28 22:35:57 -050075 },
76 }
77
78 testhelper.CheckDeepEquals(t, expected, actual)
79
80 return true, nil
81 })
Jon Perritt50da9b42014-09-14 15:06:59 -050082}
83
84func 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 Perritt8c93a302014-09-28 22:35:57 -050092
93 w.Header().Set("Content-Type", "text/plain")
94 w.WriteHeader(http.StatusOK)
95 fmt.Fprintf(w, "")
Jon Perritt50da9b42014-09-14 15:06:59 -050096 })
97
98 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -050099 List(client, ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -0500100 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 Perritt50da9b42014-09-14 15:06:59 -0500112}
113
114func 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 Perritt8c93a302014-09-28 22:35:57 -0500126 _, err := Create(client, "testContainer", CreateOpts{})
Jon Perritt50da9b42014-09-14 15:06:59 -0500127 if err != nil {
128 t.Fatalf("Unexpected error creating container: %v", err)
129 }
130}
131
132func 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 Perritt8c93a302014-09-28 22:35:57 -0500144 err := Delete(client, "testContainer")
Jon Perritt50da9b42014-09-14 15:06:59 -0500145 if err != nil {
146 t.Fatalf("Unexpected error deleting container: %v", err)
147 }
148}
149
150func 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 Perritt8c93a302014-09-28 22:35:57 -0500162 err := Update(client, "testContainer", UpdateOpts{})
Jon Perritt50da9b42014-09-14 15:06:59 -0500163 if err != nil {
164 t.Fatalf("Unexpected error updating container metadata: %v", err)
165 }
166}
167
168func 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 Perritt8c93a302014-09-28 22:35:57 -0500177 fmt.Fprintf(w, `
178
179 `)
Jon Perritt50da9b42014-09-14 15:06:59 -0500180 })
181
182 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500183 _, err := Get(client, "testContainer").ExtractMetadata()
Jon Perritt50da9b42014-09-14 15:06:59 -0500184 if err != nil {
185 t.Fatalf("Unexpected error getting container metadata: %v", err)
186 }
187}