blob: ad4b88b8eab3260d92a8832e82a7886e7e2e3218 [file] [log] [blame]
jrperrittc5c590a2016-11-04 14:41:15 -05001package testing
2
3import (
4 "testing"
5
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02006 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/imageservice/v2/images"
7 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
8 th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper"
9 fakeclient "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client"
jrperrittc5c590a2016-11-04 14:41:15 -050010)
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,
Joe Topjian929e60b2017-02-20 15:31:15 -070072 Properties: map[string]string{
73 "architecture": "x86_64",
74 },
jrperrittc5c590a2016-11-04 14:41:15 -050075 Tags: []string{"ubuntu", "quantal"},
76 }).Extract()
77
78 th.AssertNoErr(t, err)
79
80 containerFormat := "bare"
81 diskFormat := "qcow2"
82 owner := "b4eedccc6fb74fa8a7ad6b08382b852b"
83 minDiskGigabytes := 0
84 minRAMMegabytes := 0
85 file := actualImage.File
86 createdDate := actualImage.CreatedAt
87 lastUpdate := actualImage.UpdatedAt
88 schema := "/v2/schemas/image"
89
90 expectedImage := images.Image{
91 ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
92 Name: "Ubuntu 12.10",
93 Tags: []string{"ubuntu", "quantal"},
94
95 Status: images.ImageStatusQueued,
96
97 ContainerFormat: containerFormat,
98 DiskFormat: diskFormat,
99
100 MinDiskGigabytes: minDiskGigabytes,
101 MinRAMMegabytes: minRAMMegabytes,
102
103 Owner: owner,
104
Joe Topjianfc6c80c2017-06-13 13:06:50 -0600105 Visibility: images.ImageVisibilityPrivate,
106 File: file,
107 CreatedAt: createdDate,
108 UpdatedAt: lastUpdate,
109 Schema: schema,
110 VirtualSize: 0,
111 Properties: map[string]interface{}{
112 "hw_disk_bus": "scsi",
113 "hw_disk_bus_model": "virtio-scsi",
114 "hw_scsi_model": "virtio-scsi",
115 },
jrperrittc5c590a2016-11-04 14:41:15 -0500116 }
117
118 th.AssertDeepEquals(t, &expectedImage, actualImage)
119}
120
121func TestCreateImageNulls(t *testing.T) {
122 th.SetupHTTP()
123 defer th.TeardownHTTP()
124
125 HandleImageCreationSuccessfullyNulls(t)
126
127 id := "e7db3b45-8db7-47ad-8109-3fb55c2c24fd"
128 name := "Ubuntu 12.10"
129
130 actualImage, err := images.Create(fakeclient.ServiceClient(), images.CreateOpts{
131 ID: id,
132 Name: name,
133 Tags: []string{"ubuntu", "quantal"},
134 }).Extract()
135
136 th.AssertNoErr(t, err)
137
138 containerFormat := "bare"
139 diskFormat := "qcow2"
140 owner := "b4eedccc6fb74fa8a7ad6b08382b852b"
141 minDiskGigabytes := 0
142 minRAMMegabytes := 0
143 file := actualImage.File
144 createdDate := actualImage.CreatedAt
145 lastUpdate := actualImage.UpdatedAt
146 schema := "/v2/schemas/image"
147
148 expectedImage := images.Image{
149 ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
150 Name: "Ubuntu 12.10",
151 Tags: []string{"ubuntu", "quantal"},
152
153 Status: images.ImageStatusQueued,
154
155 ContainerFormat: containerFormat,
156 DiskFormat: diskFormat,
157
158 MinDiskGigabytes: minDiskGigabytes,
159 MinRAMMegabytes: minRAMMegabytes,
160
161 Owner: owner,
162
163 Visibility: images.ImageVisibilityPrivate,
164 File: file,
165 CreatedAt: createdDate,
166 UpdatedAt: lastUpdate,
167 Schema: schema,
168 }
169
170 th.AssertDeepEquals(t, &expectedImage, actualImage)
171}
172
173func TestGetImage(t *testing.T) {
174 th.SetupHTTP()
175 defer th.TeardownHTTP()
176
177 HandleImageGetSuccessfully(t)
178
179 actualImage, err := images.Get(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27").Extract()
180
181 th.AssertNoErr(t, err)
182
183 checksum := "64d7c1cd2b6f60c92c14662941cb7913"
184 sizeBytes := int64(13167616)
185 containerFormat := "bare"
186 diskFormat := "qcow2"
187 minDiskGigabytes := 0
188 minRAMMegabytes := 0
189 owner := "5ef70662f8b34079a6eddb8da9d75fe8"
190 file := actualImage.File
191 createdDate := actualImage.CreatedAt
192 lastUpdate := actualImage.UpdatedAt
193 schema := "/v2/schemas/image"
194
195 expectedImage := images.Image{
196 ID: "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
197 Name: "cirros-0.3.2-x86_64-disk",
198 Tags: []string{},
199
200 Status: images.ImageStatusActive,
201
202 ContainerFormat: containerFormat,
203 DiskFormat: diskFormat,
204
205 MinDiskGigabytes: minDiskGigabytes,
206 MinRAMMegabytes: minRAMMegabytes,
207
208 Owner: owner,
209
210 Protected: false,
211 Visibility: images.ImageVisibilityPublic,
212
Joe Topjianfc6c80c2017-06-13 13:06:50 -0600213 Checksum: checksum,
214 SizeBytes: sizeBytes,
215 File: file,
216 CreatedAt: createdDate,
217 UpdatedAt: lastUpdate,
218 Schema: schema,
219 VirtualSize: 0,
220 Properties: map[string]interface{}{
221 "hw_disk_bus": "scsi",
222 "hw_disk_bus_model": "virtio-scsi",
223 "hw_scsi_model": "virtio-scsi",
224 },
jrperrittc5c590a2016-11-04 14:41:15 -0500225 }
226
227 th.AssertDeepEquals(t, &expectedImage, actualImage)
228}
229
230func TestDeleteImage(t *testing.T) {
231 th.SetupHTTP()
232 defer th.TeardownHTTP()
233
234 HandleImageDeleteSuccessfully(t)
235
236 result := images.Delete(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27")
237 th.AssertNoErr(t, result.Err)
238}
239
240func TestUpdateImage(t *testing.T) {
241 th.SetupHTTP()
242 defer th.TeardownHTTP()
243
244 HandleImageUpdateSuccessfully(t)
245
246 actualImage, err := images.Update(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", images.UpdateOpts{
247 images.ReplaceImageName{NewName: "Fedora 17"},
248 images.ReplaceImageTags{NewTags: []string{"fedora", "beefy"}},
249 }).Extract()
250
251 th.AssertNoErr(t, err)
252
253 sizebytes := int64(2254249)
254 checksum := "2cec138d7dae2aa59038ef8c9aec2390"
255 file := actualImage.File
256 createdDate := actualImage.CreatedAt
257 lastUpdate := actualImage.UpdatedAt
258 schema := "/v2/schemas/image"
259
260 expectedImage := images.Image{
261 ID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
262 Name: "Fedora 17",
263 Status: images.ImageStatusActive,
264 Visibility: images.ImageVisibilityPublic,
265
266 SizeBytes: sizebytes,
267 Checksum: checksum,
268
269 Tags: []string{
270 "fedora",
271 "beefy",
272 },
273
274 Owner: "",
275 MinRAMMegabytes: 0,
276 MinDiskGigabytes: 0,
277
278 DiskFormat: "",
279 ContainerFormat: "",
280 File: file,
281 CreatedAt: createdDate,
282 UpdatedAt: lastUpdate,
283 Schema: schema,
Joe Topjianfc6c80c2017-06-13 13:06:50 -0600284 VirtualSize: 0,
285 Properties: map[string]interface{}{
286 "hw_disk_bus": "scsi",
287 "hw_disk_bus_model": "virtio-scsi",
288 "hw_scsi_model": "virtio-scsi",
289 },
jrperrittc5c590a2016-11-04 14:41:15 -0500290 }
291
292 th.AssertDeepEquals(t, &expectedImage, actualImage)
293}