jrperritt | c5c590a | 2016-11-04 14:41:15 -0500 | [diff] [blame] | 1 | package testing |
| 2 | |
| 3 | import ( |
| 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 | |
| 12 | func 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 | |
jrperritt | 2aaed7f | 2017-02-20 16:19:24 -0600 | [diff] [blame] | 47 | func 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 | |
jrperritt | c5c590a | 2016-11-04 14:41:15 -0500 | [diff] [blame] | 60 | func 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 Topjian | 929e60b | 2017-02-20 15:31:15 -0700 | [diff] [blame^] | 72 | Properties: map[string]string{ |
| 73 | "architecture": "x86_64", |
| 74 | }, |
jrperritt | c5c590a | 2016-11-04 14:41:15 -0500 | [diff] [blame] | 75 | 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 | |
| 105 | Visibility: images.ImageVisibilityPrivate, |
| 106 | File: file, |
| 107 | CreatedAt: createdDate, |
| 108 | UpdatedAt: lastUpdate, |
| 109 | Schema: schema, |
| 110 | } |
| 111 | |
| 112 | th.AssertDeepEquals(t, &expectedImage, actualImage) |
| 113 | } |
| 114 | |
| 115 | func TestCreateImageNulls(t *testing.T) { |
| 116 | th.SetupHTTP() |
| 117 | defer th.TeardownHTTP() |
| 118 | |
| 119 | HandleImageCreationSuccessfullyNulls(t) |
| 120 | |
| 121 | id := "e7db3b45-8db7-47ad-8109-3fb55c2c24fd" |
| 122 | name := "Ubuntu 12.10" |
| 123 | |
| 124 | actualImage, err := images.Create(fakeclient.ServiceClient(), images.CreateOpts{ |
| 125 | ID: id, |
| 126 | Name: name, |
| 127 | Tags: []string{"ubuntu", "quantal"}, |
| 128 | }).Extract() |
| 129 | |
| 130 | th.AssertNoErr(t, err) |
| 131 | |
| 132 | containerFormat := "bare" |
| 133 | diskFormat := "qcow2" |
| 134 | owner := "b4eedccc6fb74fa8a7ad6b08382b852b" |
| 135 | minDiskGigabytes := 0 |
| 136 | minRAMMegabytes := 0 |
| 137 | file := actualImage.File |
| 138 | createdDate := actualImage.CreatedAt |
| 139 | lastUpdate := actualImage.UpdatedAt |
| 140 | schema := "/v2/schemas/image" |
| 141 | |
| 142 | expectedImage := images.Image{ |
| 143 | ID: "e7db3b45-8db7-47ad-8109-3fb55c2c24fd", |
| 144 | Name: "Ubuntu 12.10", |
| 145 | Tags: []string{"ubuntu", "quantal"}, |
| 146 | |
| 147 | Status: images.ImageStatusQueued, |
| 148 | |
| 149 | ContainerFormat: containerFormat, |
| 150 | DiskFormat: diskFormat, |
| 151 | |
| 152 | MinDiskGigabytes: minDiskGigabytes, |
| 153 | MinRAMMegabytes: minRAMMegabytes, |
| 154 | |
| 155 | Owner: owner, |
| 156 | |
| 157 | Visibility: images.ImageVisibilityPrivate, |
| 158 | File: file, |
| 159 | CreatedAt: createdDate, |
| 160 | UpdatedAt: lastUpdate, |
| 161 | Schema: schema, |
| 162 | } |
| 163 | |
| 164 | th.AssertDeepEquals(t, &expectedImage, actualImage) |
| 165 | } |
| 166 | |
| 167 | func TestGetImage(t *testing.T) { |
| 168 | th.SetupHTTP() |
| 169 | defer th.TeardownHTTP() |
| 170 | |
| 171 | HandleImageGetSuccessfully(t) |
| 172 | |
| 173 | actualImage, err := images.Get(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27").Extract() |
| 174 | |
| 175 | th.AssertNoErr(t, err) |
| 176 | |
| 177 | checksum := "64d7c1cd2b6f60c92c14662941cb7913" |
| 178 | sizeBytes := int64(13167616) |
| 179 | containerFormat := "bare" |
| 180 | diskFormat := "qcow2" |
| 181 | minDiskGigabytes := 0 |
| 182 | minRAMMegabytes := 0 |
| 183 | owner := "5ef70662f8b34079a6eddb8da9d75fe8" |
| 184 | file := actualImage.File |
| 185 | createdDate := actualImage.CreatedAt |
| 186 | lastUpdate := actualImage.UpdatedAt |
| 187 | schema := "/v2/schemas/image" |
| 188 | |
| 189 | expectedImage := images.Image{ |
| 190 | ID: "1bea47ed-f6a9-463b-b423-14b9cca9ad27", |
| 191 | Name: "cirros-0.3.2-x86_64-disk", |
| 192 | Tags: []string{}, |
| 193 | |
| 194 | Status: images.ImageStatusActive, |
| 195 | |
| 196 | ContainerFormat: containerFormat, |
| 197 | DiskFormat: diskFormat, |
| 198 | |
| 199 | MinDiskGigabytes: minDiskGigabytes, |
| 200 | MinRAMMegabytes: minRAMMegabytes, |
| 201 | |
| 202 | Owner: owner, |
| 203 | |
| 204 | Protected: false, |
| 205 | Visibility: images.ImageVisibilityPublic, |
| 206 | |
| 207 | Checksum: checksum, |
| 208 | SizeBytes: sizeBytes, |
| 209 | File: file, |
| 210 | CreatedAt: createdDate, |
| 211 | UpdatedAt: lastUpdate, |
| 212 | Schema: schema, |
| 213 | } |
| 214 | |
| 215 | th.AssertDeepEquals(t, &expectedImage, actualImage) |
| 216 | } |
| 217 | |
| 218 | func TestDeleteImage(t *testing.T) { |
| 219 | th.SetupHTTP() |
| 220 | defer th.TeardownHTTP() |
| 221 | |
| 222 | HandleImageDeleteSuccessfully(t) |
| 223 | |
| 224 | result := images.Delete(fakeclient.ServiceClient(), "1bea47ed-f6a9-463b-b423-14b9cca9ad27") |
| 225 | th.AssertNoErr(t, result.Err) |
| 226 | } |
| 227 | |
| 228 | func TestUpdateImage(t *testing.T) { |
| 229 | th.SetupHTTP() |
| 230 | defer th.TeardownHTTP() |
| 231 | |
| 232 | HandleImageUpdateSuccessfully(t) |
| 233 | |
| 234 | actualImage, err := images.Update(fakeclient.ServiceClient(), "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", images.UpdateOpts{ |
| 235 | images.ReplaceImageName{NewName: "Fedora 17"}, |
| 236 | images.ReplaceImageTags{NewTags: []string{"fedora", "beefy"}}, |
| 237 | }).Extract() |
| 238 | |
| 239 | th.AssertNoErr(t, err) |
| 240 | |
| 241 | sizebytes := int64(2254249) |
| 242 | checksum := "2cec138d7dae2aa59038ef8c9aec2390" |
| 243 | file := actualImage.File |
| 244 | createdDate := actualImage.CreatedAt |
| 245 | lastUpdate := actualImage.UpdatedAt |
| 246 | schema := "/v2/schemas/image" |
| 247 | |
| 248 | expectedImage := images.Image{ |
| 249 | ID: "da3b75d9-3f4a-40e7-8a2c-bfab23927dea", |
| 250 | Name: "Fedora 17", |
| 251 | Status: images.ImageStatusActive, |
| 252 | Visibility: images.ImageVisibilityPublic, |
| 253 | |
| 254 | SizeBytes: sizebytes, |
| 255 | Checksum: checksum, |
| 256 | |
| 257 | Tags: []string{ |
| 258 | "fedora", |
| 259 | "beefy", |
| 260 | }, |
| 261 | |
| 262 | Owner: "", |
| 263 | MinRAMMegabytes: 0, |
| 264 | MinDiskGigabytes: 0, |
| 265 | |
| 266 | DiskFormat: "", |
| 267 | ContainerFormat: "", |
| 268 | File: file, |
| 269 | CreatedAt: createdDate, |
| 270 | UpdatedAt: lastUpdate, |
| 271 | Schema: schema, |
| 272 | } |
| 273 | |
| 274 | th.AssertDeepEquals(t, &expectedImage, actualImage) |
| 275 | } |