1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
//go:build acceptance || imageservice || images
// +build acceptance imageservice images
package v2
import (
"sort"
"testing"
"time"
"github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/pagination"
th "github.com/gophercloud/gophercloud/testhelper"
)
func TestImagesListEachPage(t *testing.T) {
client, err := clients.NewImageServiceV2Client()
th.AssertNoErr(t, err)
listOpts := images.ListOpts{
Limit: 1,
}
pager := images.List(client, listOpts)
err = pager.EachPage(func(page pagination.Page) (bool, error) {
images, err := images.ExtractImages(page)
if err != nil {
t.Fatalf("Unable to extract images: %v", err)
}
for _, image := range images {
tools.PrintResource(t, image)
tools.PrintResource(t, image.Properties)
}
return true, nil
})
}
func TestImagesListAllPages(t *testing.T) {
client, err := clients.NewImageServiceV2Client()
th.AssertNoErr(t, err)
image, err := CreateEmptyImage(t, client)
th.AssertNoErr(t, err)
defer DeleteImage(t, client, image)
listOpts := images.ListOpts{}
allPages, err := images.List(client, listOpts).AllPages()
th.AssertNoErr(t, err)
allImages, err := images.ExtractImages(allPages)
th.AssertNoErr(t, err)
var found bool
for _, i := range allImages {
tools.PrintResource(t, i)
tools.PrintResource(t, i.Properties)
if i.Name == image.Name {
found = true
}
}
th.AssertEquals(t, found, true)
}
func TestImagesListByDate(t *testing.T) {
client, err := clients.NewImageServiceV2Client()
th.AssertNoErr(t, err)
date := time.Date(2014, 1, 1, 1, 1, 1, 0, time.UTC)
listOpts := images.ListOpts{
Limit: 1,
CreatedAtQuery: &images.ImageDateQuery{
Date: date,
Filter: images.FilterGTE,
},
}
allPages, err := images.List(client, listOpts).AllPages()
th.AssertNoErr(t, err)
allImages, err := images.ExtractImages(allPages)
th.AssertNoErr(t, err)
if len(allImages) == 0 {
t.Fatalf("Query resulted in no results")
}
for _, image := range allImages {
tools.PrintResource(t, image)
tools.PrintResource(t, image.Properties)
}
date = time.Date(2049, 1, 1, 1, 1, 1, 0, time.UTC)
listOpts = images.ListOpts{
Limit: 1,
CreatedAtQuery: &images.ImageDateQuery{
Date: date,
Filter: images.FilterGTE,
},
}
allPages, err = images.List(client, listOpts).AllPages()
th.AssertNoErr(t, err)
allImages, err = images.ExtractImages(allPages)
th.AssertNoErr(t, err)
if len(allImages) > 0 {
t.Fatalf("Expected 0 images, got %d", len(allImages))
}
}
func TestImagesFilter(t *testing.T) {
client, err := clients.NewImageServiceV2Client()
th.AssertNoErr(t, err)
image, err := CreateEmptyImage(t, client)
th.AssertNoErr(t, err)
defer DeleteImage(t, client, image)
listOpts := images.ListOpts{
Tags: []string{"foo", "bar"},
ContainerFormat: "bare",
DiskFormat: "qcow2",
}
allPages, err := images.List(client, listOpts).AllPages()
th.AssertNoErr(t, err)
allImages, err := images.ExtractImages(allPages)
th.AssertNoErr(t, err)
if len(allImages) == 0 {
t.Fatalf("Query resulted in no results")
}
}
func TestImagesUpdate(t *testing.T) {
client, err := clients.NewImageServiceV2Client()
th.AssertNoErr(t, err)
image, err := CreateEmptyImage(t, client)
th.AssertNoErr(t, err)
defer DeleteImage(t, client, image)
newTags := []string{"foo", "bar"}
updateOpts := images.UpdateOpts{
images.ReplaceImageName{NewName: image.Name + "foo"},
images.ReplaceImageTags{NewTags: newTags},
images.ReplaceImageMinDisk{NewMinDisk: 21},
images.ReplaceImageProtected{NewProtected: true},
images.UpdateImageProperty{
Op: images.AddOp,
Name: "hw_disk_bus",
Value: "scsi",
},
images.UpdateImageProperty{
Op: images.RemoveOp,
Name: "architecture",
},
}
newImage, err := images.Update(client, image.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, newImage)
tools.PrintResource(t, newImage.Properties)
th.AssertEquals(t, newImage.Name, image.Name+"foo")
th.AssertEquals(t, newImage.Protected, true)
sort.Strings(newTags)
sort.Strings(newImage.Tags)
th.AssertDeepEquals(t, newImage.Tags, newTags)
// Because OpenStack is now adding additional properties automatically,
// it's not possible to do an easy AssertDeepEquals.
th.AssertEquals(t, newImage.Properties["hw_disk_bus"], "scsi")
if _, ok := newImage.Properties["architecture"]; ok {
t.Fatal("architecture property still exists")
}
// Now change image protection back to false or delete will fail
updateOpts = images.UpdateOpts{
images.ReplaceImageProtected{NewProtected: false},
}
_, err = images.Update(client, image.ID, updateOpts).Extract()
th.AssertNoErr(t, err)
}
|