blob: 3b59e0015adc153a8679b2d827393a8c5afdcf9e [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")
36 w.WriteHeader(http.StatusOK)
37 fmt.Fprintf(w, `[{'count': 0,'bytes': 0,'name': 'janeausten'},{'count': 1,'bytes': 14,'name': 'marktwain'}]`)
Jon Perritt50da9b42014-09-14 15:06:59 -050038 })
39
40 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -050041 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 Perritt50da9b42014-09-14 15:06:59 -050070 }
71}
72
73func 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 Perritt8c93a302014-09-28 22:35:57 -050081
82 w.Header().Set("Content-Type", "text/plain")
83 w.WriteHeader(http.StatusOK)
84 fmt.Fprintf(w, "")
Jon Perritt50da9b42014-09-14 15:06:59 -050085 })
86
87 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -050088 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 Perritt50da9b42014-09-14 15:06:59 -0500106 }
107}
108
109func 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 Perritt8c93a302014-09-28 22:35:57 -0500121 _, err := Create(client, "testContainer", CreateOpts{})
Jon Perritt50da9b42014-09-14 15:06:59 -0500122 if err != nil {
123 t.Fatalf("Unexpected error creating container: %v", err)
124 }
125}
126
127func 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 Perritt8c93a302014-09-28 22:35:57 -0500139 err := Delete(client, "testContainer")
Jon Perritt50da9b42014-09-14 15:06:59 -0500140 if err != nil {
141 t.Fatalf("Unexpected error deleting container: %v", err)
142 }
143}
144
145func 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 Perritt8c93a302014-09-28 22:35:57 -0500157 err := Update(client, "testContainer", UpdateOpts{})
Jon Perritt50da9b42014-09-14 15:06:59 -0500158 if err != nil {
159 t.Fatalf("Unexpected error updating container metadata: %v", err)
160 }
161}
162
163func 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 Perritt8c93a302014-09-28 22:35:57 -0500172 fmt.Fprintf(w, `
173
174 `)
Jon Perritt50da9b42014-09-14 15:06:59 -0500175 })
176
177 client := serviceClient()
Jon Perritt8c93a302014-09-28 22:35:57 -0500178 _, err := Get(client, "testContainer").ExtractMetadata()
Jon Perritt50da9b42014-09-14 15:06:59 -0500179 if err != nil {
180 t.Fatalf("Unexpected error getting container metadata: %v", err)
181 }
182}