blob: 788eec49aa5aba9c07a804fa7664f119536810eb [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
47func TestCreateImage(t *testing.T) {
48 th.SetupHTTP()
49 defer th.TeardownHTTP()
50
51 HandleImageCreationSuccessfully(t)
52
53 id := "e7db3b45-8db7-47ad-8109-3fb55c2c24fd"
54 name := "Ubuntu 12.10"
55
56 actualImage, err := images.Create(fakeclient.ServiceClient(), images.CreateOpts{
57 ID: id,
58 Name: name,
59 Tags: []string{"ubuntu", "quantal"},
60 }).Extract()
61
62 th.AssertNoErr(t, err)
63
64 containerFormat := "bare"
65 diskFormat := "qcow2"
66 owner := "b4eedccc6fb74fa8a7ad6b08382b852b"
67 minDiskGigabytes := 0
68 minRAMMegabytes := 0
69 file := actualImage.File
70 createdDate := actualImage.CreatedAt
71 lastUpdate := actualImage.UpdatedAt
72 schema := "/v2/schemas/image"
73
74 expectedImage := images.Image{
75 ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
76 Name: "Ubuntu 12.10",
77 Tags: []string{"ubuntu", "quantal"},
78
79 Status: images.ImageStatusQueued,
80
81 ContainerFormat: containerFormat,
82 DiskFormat: diskFormat,
83
84 MinDiskGigabytes: minDiskGigabytes,
85 MinRAMMegabytes: minRAMMegabytes,
86
87 Owner: owner,
88
89 Visibility: images.ImageVisibilityPrivate,
90 File: file,
91 CreatedAt: createdDate,
92 UpdatedAt: lastUpdate,
93 Schema: schema,
94 }
95
96 th.AssertDeepEquals(t, &expectedImage, actualImage)
97}
98
99func TestCreateImageNulls(t *testing.T) {
100 th.SetupHTTP()
101 defer th.TeardownHTTP()
102
103 HandleImageCreationSuccessfullyNulls(t)
104
105 id := "e7db3b45-8db7-47ad-8109-3fb55c2c24fd"
106 name := "Ubuntu 12.10"
107
108 actualImage, err := images.Create(fakeclient.ServiceClient(), images.CreateOpts{
109 ID: id,
110 Name: name,
111 Tags: []string{"ubuntu", "quantal"},
112 }).Extract()
113
114 th.AssertNoErr(t, err)
115
116 containerFormat := "bare"
117 diskFormat := "qcow2"
118 owner := "b4eedccc6fb74fa8a7ad6b08382b852b"
119 minDiskGigabytes := 0
120 minRAMMegabytes := 0
121 file := actualImage.File
122 createdDate := actualImage.CreatedAt
123 lastUpdate := actualImage.UpdatedAt
124 schema := "/v2/schemas/image"
125
126 expectedImage := images.Image{
127 ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
128 Name: "Ubuntu 12.10",
129 Tags: []string{"ubuntu", "quantal"},
130
131 Status: images.ImageStatusQueued,
132
133 ContainerFormat: containerFormat,
134 DiskFormat: diskFormat,
135
136 MinDiskGigabytes: minDiskGigabytes,
137 MinRAMMegabytes: minRAMMegabytes,
138
139 Owner: owner,
140
141 Visibility: images.ImageVisibilityPrivate,
142 File: file,
143 CreatedAt: createdDate,
144 UpdatedAt: lastUpdate,
145 Schema: schema,
146 }
147
148 th.AssertDeepEquals(t, &expectedImage, actualImage)
149}
150
151func TestGetImage(t *testing.T) {
152 th.SetupHTTP()
153 defer th.TeardownHTTP()
154
155 HandleImageGetSuccessfully(t)
156
157 actualImage, err := images.Get(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27").Extract()
158
159 th.AssertNoErr(t, err)
160
161 checksum := "64d7c1cd2b6f60c92c14662941cb7913"
162 sizeBytes := int64(13167616)
163 containerFormat := "bare"
164 diskFormat := "qcow2"
165 minDiskGigabytes := 0
166 minRAMMegabytes := 0
167 owner := "5ef70662f8b34079a6eddb8da9d75fe8"
168 file := actualImage.File
169 createdDate := actualImage.CreatedAt
170 lastUpdate := actualImage.UpdatedAt
171 schema := "/v2/schemas/image"
172
173 expectedImage := images.Image{
174 ID: "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
175 Name: "cirros-0.3.2-x86_64-disk",
176 Tags: []string{},
177
178 Status: images.ImageStatusActive,
179
180 ContainerFormat: containerFormat,
181 DiskFormat: diskFormat,
182
183 MinDiskGigabytes: minDiskGigabytes,
184 MinRAMMegabytes: minRAMMegabytes,
185
186 Owner: owner,
187
188 Protected: false,
189 Visibility: images.ImageVisibilityPublic,
190
191 Checksum: checksum,
192 SizeBytes: sizeBytes,
193 File: file,
194 CreatedAt: createdDate,
195 UpdatedAt: lastUpdate,
196 Schema: schema,
197 }
198
199 th.AssertDeepEquals(t, &expectedImage, actualImage)
200}
201
202func TestDeleteImage(t *testing.T) {
203 th.SetupHTTP()
204 defer th.TeardownHTTP()
205
206 HandleImageDeleteSuccessfully(t)
207
208 result := images.Delete(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27")
209 th.AssertNoErr(t, result.Err)
210}
211
212func TestUpdateImage(t *testing.T) {
213 th.SetupHTTP()
214 defer th.TeardownHTTP()
215
216 HandleImageUpdateSuccessfully(t)
217
218 actualImage, err := images.Update(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", images.UpdateOpts{
219 images.ReplaceImageName{NewName: "Fedora 17"},
220 images.ReplaceImageTags{NewTags: []string{"fedora", "beefy"}},
221 }).Extract()
222
223 th.AssertNoErr(t, err)
224
225 sizebytes := int64(2254249)
226 checksum := "2cec138d7dae2aa59038ef8c9aec2390"
227 file := actualImage.File
228 createdDate := actualImage.CreatedAt
229 lastUpdate := actualImage.UpdatedAt
230 schema := "/v2/schemas/image"
231
232 expectedImage := images.Image{
233 ID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
234 Name: "Fedora 17",
235 Status: images.ImageStatusActive,
236 Visibility: images.ImageVisibilityPublic,
237
238 SizeBytes: sizebytes,
239 Checksum: checksum,
240
241 Tags: []string{
242 "fedora",
243 "beefy",
244 },
245
246 Owner: "",
247 MinRAMMegabytes: 0,
248 MinDiskGigabytes: 0,
249
250 DiskFormat: "",
251 ContainerFormat: "",
252 File: file,
253 CreatedAt: createdDate,
254 UpdatedAt: lastUpdate,
255 Schema: schema,
256 }
257
258 th.AssertDeepEquals(t, &expectedImage, actualImage)
259}