blob: 8aaeeb74b880d0c27dcc2ec1b165ea66224f5d1c [file] [log] [blame]
Joe Topjian929e60b2017-02-20 15:31:15 -07001// Package v2 contains common functions for creating imageservice resources
2// for use in acceptance tests. See the `*_test.go` files for example usages.
3package v2
4
5import (
6 "testing"
7
8 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/acceptance/tools"
10 "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
11)
12
13// CreateEmptyImage will create an image, but with no actual image data.
14// An error will be returned if an image was unable to be created.
15func CreateEmptyImage(t *testing.T, client *gophercloud.ServiceClient) (*images.Image, error) {
16 var image *images.Image
17
18 name := tools.RandomString("ACPTTEST", 16)
19 t.Logf("Attempting to create image: %s", name)
20
21 protected := false
22 visibility := images.ImageVisibilityPrivate
23 createOpts := &images.CreateOpts{
24 Name: name,
25 ContainerFormat: "bare",
26 DiskFormat: "qcow2",
27 MinDisk: 0,
28 MinRAM: 0,
29 Protected: &protected,
30 Visibility: &visibility,
31 Properties: map[string]string{
32 "architecture": "x86_64",
33 },
34 }
35
36 image, err := images.Create(client, createOpts).Extract()
37 if err != nil {
38 return image, err
39 }
40
41 t.Logf("Created image %s: %#v", name, image)
42 return image, nil
43}
44
45// DeleteImage deletes an image.
46// A fatal error will occur if the image failed to delete. This works best when
47// used as a deferred function.
48func DeleteImage(t *testing.T, client *gophercloud.ServiceClient, image *images.Image) {
49 err := images.Delete(client, image.ID).ExtractErr()
50 if err != nil {
51 t.Fatalf("Unable to delete image %s: %v", image.ID, err)
52 }
53
54 t.Logf("Deleted image: %s", image.ID)
55}