blob: fdd84027379abaa3ab4d42b218ca7d634598c880 [file] [log] [blame]
jrperrittc5c590a2016-11-04 14:41:15 -05001package testing
2
3import (
4 "testing"
5
6 "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
7 "github.com/gophercloud/gophercloud/pagination"
8 th "github.com/gophercloud/gophercloud/testhelper"
9 fakeclient "github.com/gophercloud/gophercloud/testhelper/client"
10)
11
12func TestListImage(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15
16 HandleImageListSuccessfully(t)
17
18 t.Logf("Test setup %+v\n", th.Server)
19
20 t.Logf("Id\tName\tOwner\tChecksum\tSizeBytes")
21
22 pager := images.List(fakeclient.ServiceClient(), images.ListOpts{Limit: 1})
23 t.Logf("Pager state %v", pager)
24 count, pages := 0, 0
25 err := pager.EachPage(func(page pagination.Page) (bool, error) {
26 pages++
27 t.Logf("Page %v", page)
28 images, err := images.ExtractImages(page)
29 if err != nil {
30 return false, err
31 }
32
33 for _, i := range images {
34 t.Logf("%s\t%s\t%s\t%s\t%v\t\n", i.ID, i.Name, i.Owner, i.Checksum, i.SizeBytes)
35 count++
36 }
37
38 return true, nil
39 })
40 th.AssertNoErr(t, err)
41
42 t.Logf("--------\n%d images listed on %d pages.\n", count, pages)
43 th.AssertEquals(t, 3, pages)
44 th.AssertEquals(t, 3, count)
45}
46
jrperritt2aaed7f2017-02-20 16:19:24 -060047func TestAllPagesImage(t *testing.T) {
48 th.SetupHTTP()
49 defer th.TeardownHTTP()
50
51 HandleImageListSuccessfully(t)
52
53 pages, err := images.List(fakeclient.ServiceClient(), nil).AllPages()
54 th.AssertNoErr(t, err)
55 images, err := images.ExtractImages(pages)
56 th.AssertNoErr(t, err)
57 th.AssertEquals(t, 3, len(images))
58}
59
jrperrittc5c590a2016-11-04 14:41:15 -050060func TestCreateImage(t *testing.T) {
61 th.SetupHTTP()
62 defer th.TeardownHTTP()
63
64 HandleImageCreationSuccessfully(t)
65
66 id := "e7db3b45-8db7-47ad-8109-3fb55c2c24fd"
67 name := "Ubuntu 12.10"
68
69 actualImage, err := images.Create(fakeclient.ServiceClient(), images.CreateOpts{
70 ID: id,
71 Name: name,
72 Tags: []string{"ubuntu", "quantal"},
73 }).Extract()
74
75 th.AssertNoErr(t, err)
76
77 containerFormat := "bare"
78 diskFormat := "qcow2"
79 owner := "b4eedccc6fb74fa8a7ad6b08382b852b"
80 minDiskGigabytes := 0
81 minRAMMegabytes := 0
82 file := actualImage.File
83 createdDate := actualImage.CreatedAt
84 lastUpdate := actualImage.UpdatedAt
85 schema := "/v2/schemas/image"
86
87 expectedImage := images.Image{
88 ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
89 Name: "Ubuntu 12.10",
90 Tags: []string{"ubuntu", "quantal"},
91
92 Status: images.ImageStatusQueued,
93
94 ContainerFormat: containerFormat,
95 DiskFormat: diskFormat,
96
97 MinDiskGigabytes: minDiskGigabytes,
98 MinRAMMegabytes: minRAMMegabytes,
99
100 Owner: owner,
101
102 Visibility: images.ImageVisibilityPrivate,
103 File: file,
104 CreatedAt: createdDate,
105 UpdatedAt: lastUpdate,
106 Schema: schema,
107 }
108
109 th.AssertDeepEquals(t, &expectedImage, actualImage)
110}
111
112func TestCreateImageNulls(t *testing.T) {
113 th.SetupHTTP()
114 defer th.TeardownHTTP()
115
116 HandleImageCreationSuccessfullyNulls(t)
117
118 id := "e7db3b45-8db7-47ad-8109-3fb55c2c24fd"
119 name := "Ubuntu 12.10"
120
121 actualImage, err := images.Create(fakeclient.ServiceClient(), images.CreateOpts{
122 ID: id,
123 Name: name,
124 Tags: []string{"ubuntu", "quantal"},
125 }).Extract()
126
127 th.AssertNoErr(t, err)
128
129 containerFormat := "bare"
130 diskFormat := "qcow2"
131 owner := "b4eedccc6fb74fa8a7ad6b08382b852b"
132 minDiskGigabytes := 0
133 minRAMMegabytes := 0
134 file := actualImage.File
135 createdDate := actualImage.CreatedAt
136 lastUpdate := actualImage.UpdatedAt
137 schema := "/v2/schemas/image"
138
139 expectedImage := images.Image{
140 ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
141 Name: "Ubuntu 12.10",
142 Tags: []string{"ubuntu", "quantal"},
143
144 Status: images.ImageStatusQueued,
145
146 ContainerFormat: containerFormat,
147 DiskFormat: diskFormat,
148
149 MinDiskGigabytes: minDiskGigabytes,
150 MinRAMMegabytes: minRAMMegabytes,
151
152 Owner: owner,
153
154 Visibility: images.ImageVisibilityPrivate,
155 File: file,
156 CreatedAt: createdDate,
157 UpdatedAt: lastUpdate,
158 Schema: schema,
159 }
160
161 th.AssertDeepEquals(t, &expectedImage, actualImage)
162}
163
164func TestGetImage(t *testing.T) {
165 th.SetupHTTP()
166 defer th.TeardownHTTP()
167
168 HandleImageGetSuccessfully(t)
169
170 actualImage, err := images.Get(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27").Extract()
171
172 th.AssertNoErr(t, err)
173
174 checksum := "64d7c1cd2b6f60c92c14662941cb7913"
175 sizeBytes := int64(13167616)
176 containerFormat := "bare"
177 diskFormat := "qcow2"
178 minDiskGigabytes := 0
179 minRAMMegabytes := 0
180 owner := "5ef70662f8b34079a6eddb8da9d75fe8"
181 file := actualImage.File
182 createdDate := actualImage.CreatedAt
183 lastUpdate := actualImage.UpdatedAt
184 schema := "/v2/schemas/image"
185
186 expectedImage := images.Image{
187 ID: "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
188 Name: "cirros-0.3.2-x86_64-disk",
189 Tags: []string{},
190
191 Status: images.ImageStatusActive,
192
193 ContainerFormat: containerFormat,
194 DiskFormat: diskFormat,
195
196 MinDiskGigabytes: minDiskGigabytes,
197 MinRAMMegabytes: minRAMMegabytes,
198
199 Owner: owner,
200
201 Protected: false,
202 Visibility: images.ImageVisibilityPublic,
203
204 Checksum: checksum,
205 SizeBytes: sizeBytes,
206 File: file,
207 CreatedAt: createdDate,
208 UpdatedAt: lastUpdate,
209 Schema: schema,
210 }
211
212 th.AssertDeepEquals(t, &expectedImage, actualImage)
213}
214
215func TestDeleteImage(t *testing.T) {
216 th.SetupHTTP()
217 defer th.TeardownHTTP()
218
219 HandleImageDeleteSuccessfully(t)
220
221 result := images.Delete(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27")
222 th.AssertNoErr(t, result.Err)
223}
224
225func TestUpdateImage(t *testing.T) {
226 th.SetupHTTP()
227 defer th.TeardownHTTP()
228
229 HandleImageUpdateSuccessfully(t)
230
231 actualImage, err := images.Update(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", images.UpdateOpts{
232 images.ReplaceImageName{NewName: "Fedora 17"},
233 images.ReplaceImageTags{NewTags: []string{"fedora", "beefy"}},
234 }).Extract()
235
236 th.AssertNoErr(t, err)
237
238 sizebytes := int64(2254249)
239 checksum := "2cec138d7dae2aa59038ef8c9aec2390"
240 file := actualImage.File
241 createdDate := actualImage.CreatedAt
242 lastUpdate := actualImage.UpdatedAt
243 schema := "/v2/schemas/image"
244
245 expectedImage := images.Image{
246 ID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
247 Name: "Fedora 17",
248 Status: images.ImageStatusActive,
249 Visibility: images.ImageVisibilityPublic,
250
251 SizeBytes: sizebytes,
252 Checksum: checksum,
253
254 Tags: []string{
255 "fedora",
256 "beefy",
257 },
258
259 Owner: "",
260 MinRAMMegabytes: 0,
261 MinDiskGigabytes: 0,
262
263 DiskFormat: "",
264 ContainerFormat: "",
265 File: file,
266 CreatedAt: createdDate,
267 UpdatedAt: lastUpdate,
268 Schema: schema,
269 }
270
271 th.AssertDeepEquals(t, &expectedImage, actualImage)
272}