github.com/leeclow-ops/gophercloud@v1.2.1/acceptance/openstack/compute/v2/bootfromvolume_test.go (about) 1 //go:build acceptance || compute || bootfromvolume 2 // +build acceptance compute bootfromvolume 3 4 package v2 5 6 import ( 7 "testing" 8 9 "github.com/leeclow-ops/gophercloud/acceptance/clients" 10 blockstorage "github.com/leeclow-ops/gophercloud/acceptance/openstack/blockstorage/v2" 11 "github.com/leeclow-ops/gophercloud/acceptance/tools" 12 "github.com/leeclow-ops/gophercloud/openstack/compute/v2/extensions/bootfromvolume" 13 "github.com/leeclow-ops/gophercloud/openstack/compute/v2/extensions/volumeattach" 14 th "github.com/leeclow-ops/gophercloud/testhelper" 15 ) 16 17 func TestBootFromImage(t *testing.T) { 18 clients.RequireLong(t) 19 20 client, err := clients.NewComputeV2Client() 21 th.AssertNoErr(t, err) 22 23 choices, err := clients.AcceptanceTestChoicesFromEnv() 24 th.AssertNoErr(t, err) 25 26 blockDevices := []bootfromvolume.BlockDevice{ 27 { 28 BootIndex: 0, 29 DeleteOnTermination: true, 30 DestinationType: bootfromvolume.DestinationLocal, 31 SourceType: bootfromvolume.SourceImage, 32 UUID: choices.ImageID, 33 }, 34 } 35 36 server, err := CreateBootableVolumeServer(t, client, blockDevices) 37 th.AssertNoErr(t, err) 38 defer DeleteServer(t, client, server) 39 40 tools.PrintResource(t, server) 41 42 th.AssertEquals(t, server.Image["id"], choices.ImageID) 43 } 44 45 func TestBootFromNewVolume(t *testing.T) { 46 clients.RequireLong(t) 47 48 client, err := clients.NewComputeV2Client() 49 th.AssertNoErr(t, err) 50 51 choices, err := clients.AcceptanceTestChoicesFromEnv() 52 th.AssertNoErr(t, err) 53 54 blockDevices := []bootfromvolume.BlockDevice{ 55 { 56 DeleteOnTermination: true, 57 DestinationType: bootfromvolume.DestinationVolume, 58 SourceType: bootfromvolume.SourceImage, 59 UUID: choices.ImageID, 60 VolumeSize: 2, 61 }, 62 } 63 64 server, err := CreateBootableVolumeServer(t, client, blockDevices) 65 th.AssertNoErr(t, err) 66 defer DeleteServer(t, client, server) 67 68 attachPages, err := volumeattach.List(client, server.ID).AllPages() 69 th.AssertNoErr(t, err) 70 71 attachments, err := volumeattach.ExtractVolumeAttachments(attachPages) 72 th.AssertNoErr(t, err) 73 74 tools.PrintResource(t, server) 75 tools.PrintResource(t, attachments) 76 77 if server.Image != nil { 78 t.Fatalf("server image should be nil") 79 } 80 81 th.AssertEquals(t, len(attachments), 1) 82 83 // TODO: volumes_attached extension 84 } 85 86 func TestBootFromExistingVolume(t *testing.T) { 87 clients.RequireLong(t) 88 89 computeClient, err := clients.NewComputeV2Client() 90 th.AssertNoErr(t, err) 91 92 blockStorageClient, err := clients.NewBlockStorageV3Client() 93 th.AssertNoErr(t, err) 94 95 volume, err := blockstorage.CreateVolumeFromImage(t, blockStorageClient) 96 th.AssertNoErr(t, err) 97 98 tools.PrintResource(t, volume) 99 100 blockDevices := []bootfromvolume.BlockDevice{ 101 { 102 DeleteOnTermination: true, 103 DestinationType: bootfromvolume.DestinationVolume, 104 SourceType: bootfromvolume.SourceVolume, 105 UUID: volume.ID, 106 }, 107 } 108 109 server, err := CreateBootableVolumeServer(t, computeClient, blockDevices) 110 th.AssertNoErr(t, err) 111 defer DeleteServer(t, computeClient, server) 112 113 attachPages, err := volumeattach.List(computeClient, server.ID).AllPages() 114 th.AssertNoErr(t, err) 115 116 attachments, err := volumeattach.ExtractVolumeAttachments(attachPages) 117 th.AssertNoErr(t, err) 118 119 tools.PrintResource(t, server) 120 tools.PrintResource(t, attachments) 121 122 if server.Image != nil { 123 t.Fatalf("server image should be nil") 124 } 125 126 th.AssertEquals(t, len(attachments), 1) 127 th.AssertEquals(t, attachments[0].VolumeID, volume.ID) 128 // TODO: volumes_attached extension 129 } 130 131 func TestBootFromMultiEphemeralServer(t *testing.T) { 132 clients.RequireLong(t) 133 134 client, err := clients.NewComputeV2Client() 135 th.AssertNoErr(t, err) 136 137 choices, err := clients.AcceptanceTestChoicesFromEnv() 138 th.AssertNoErr(t, err) 139 140 blockDevices := []bootfromvolume.BlockDevice{ 141 { 142 BootIndex: 0, 143 DestinationType: bootfromvolume.DestinationLocal, 144 DeleteOnTermination: true, 145 SourceType: bootfromvolume.SourceImage, 146 UUID: choices.ImageID, 147 VolumeSize: 5, 148 }, 149 { 150 BootIndex: -1, 151 DestinationType: bootfromvolume.DestinationLocal, 152 DeleteOnTermination: true, 153 GuestFormat: "ext4", 154 SourceType: bootfromvolume.SourceBlank, 155 VolumeSize: 1, 156 }, 157 { 158 BootIndex: -1, 159 DestinationType: bootfromvolume.DestinationLocal, 160 DeleteOnTermination: true, 161 GuestFormat: "ext4", 162 SourceType: bootfromvolume.SourceBlank, 163 VolumeSize: 1, 164 }, 165 } 166 167 server, err := CreateMultiEphemeralServer(t, client, blockDevices) 168 th.AssertNoErr(t, err) 169 defer DeleteServer(t, client, server) 170 171 tools.PrintResource(t, server) 172 } 173 174 func TestAttachNewVolume(t *testing.T) { 175 clients.RequireLong(t) 176 177 client, err := clients.NewComputeV2Client() 178 th.AssertNoErr(t, err) 179 180 choices, err := clients.AcceptanceTestChoicesFromEnv() 181 th.AssertNoErr(t, err) 182 183 blockDevices := []bootfromvolume.BlockDevice{ 184 { 185 BootIndex: 0, 186 DeleteOnTermination: true, 187 DestinationType: bootfromvolume.DestinationLocal, 188 SourceType: bootfromvolume.SourceImage, 189 UUID: choices.ImageID, 190 }, 191 { 192 BootIndex: 1, 193 DeleteOnTermination: true, 194 DestinationType: bootfromvolume.DestinationVolume, 195 SourceType: bootfromvolume.SourceBlank, 196 VolumeSize: 2, 197 }, 198 } 199 200 server, err := CreateBootableVolumeServer(t, client, blockDevices) 201 th.AssertNoErr(t, err) 202 defer DeleteServer(t, client, server) 203 204 attachPages, err := volumeattach.List(client, server.ID).AllPages() 205 th.AssertNoErr(t, err) 206 207 attachments, err := volumeattach.ExtractVolumeAttachments(attachPages) 208 th.AssertNoErr(t, err) 209 210 tools.PrintResource(t, server) 211 tools.PrintResource(t, attachments) 212 213 th.AssertEquals(t, server.Image["id"], choices.ImageID) 214 th.AssertEquals(t, len(attachments), 1) 215 216 // TODO: volumes_attached extension 217 } 218 219 func TestAttachExistingVolume(t *testing.T) { 220 clients.RequireLong(t) 221 222 computeClient, err := clients.NewComputeV2Client() 223 th.AssertNoErr(t, err) 224 225 blockStorageClient, err := clients.NewBlockStorageV3Client() 226 th.AssertNoErr(t, err) 227 228 choices, err := clients.AcceptanceTestChoicesFromEnv() 229 th.AssertNoErr(t, err) 230 231 volume, err := blockstorage.CreateVolume(t, blockStorageClient) 232 th.AssertNoErr(t, err) 233 234 blockDevices := []bootfromvolume.BlockDevice{ 235 { 236 BootIndex: 0, 237 DeleteOnTermination: true, 238 DestinationType: bootfromvolume.DestinationLocal, 239 SourceType: bootfromvolume.SourceImage, 240 UUID: choices.ImageID, 241 }, 242 { 243 BootIndex: 1, 244 DeleteOnTermination: true, 245 DestinationType: bootfromvolume.DestinationVolume, 246 SourceType: bootfromvolume.SourceVolume, 247 UUID: volume.ID, 248 }, 249 } 250 251 server, err := CreateBootableVolumeServer(t, computeClient, blockDevices) 252 th.AssertNoErr(t, err) 253 defer DeleteServer(t, computeClient, server) 254 255 attachPages, err := volumeattach.List(computeClient, server.ID).AllPages() 256 th.AssertNoErr(t, err) 257 258 attachments, err := volumeattach.ExtractVolumeAttachments(attachPages) 259 th.AssertNoErr(t, err) 260 261 tools.PrintResource(t, server) 262 tools.PrintResource(t, attachments) 263 264 th.AssertEquals(t, server.Image["id"], choices.ImageID) 265 th.AssertEquals(t, len(attachments), 1) 266 th.AssertEquals(t, attachments[0].VolumeID, volume.ID) 267 268 // TODO: volumes_attached extension 269 } 270 271 func TestBootFromNewCustomizedVolume(t *testing.T) { 272 if testing.Short() { 273 t.Skip("Skipping test that requires server creation in short mode.") 274 } 275 276 client, err := clients.NewComputeV2Client() 277 if err != nil { 278 t.Fatalf("Unable to create a compute client: %v", err) 279 } 280 281 choices, err := clients.AcceptanceTestChoicesFromEnv() 282 if err != nil { 283 t.Fatal(err) 284 } 285 286 blockDevices := []bootfromvolume.BlockDevice{ 287 { 288 DeleteOnTermination: true, 289 DestinationType: bootfromvolume.DestinationVolume, 290 SourceType: bootfromvolume.SourceImage, 291 UUID: choices.ImageID, 292 VolumeSize: 2, 293 DeviceType: "disk", 294 DiskBus: "virtio", 295 }, 296 } 297 298 server, err := CreateBootableVolumeServer(t, client, blockDevices) 299 if err != nil { 300 t.Fatalf("Unable to create server: %v", err) 301 } 302 defer DeleteServer(t, client, server) 303 304 tools.PrintResource(t, server) 305 }