github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/imageservice/v2/imageservice.go (about)

     1  // Package v2 contains common functions for creating imageservice resources
     2  // for use in acceptance tests. See the `*_test.go` files for example usages.
     3  package v2
     4  
     5  import (
     6  	"io"
     7  	"net/http"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/gophercloud/gophercloud"
    12  	"github.com/gophercloud/gophercloud/internal/acceptance/tools"
    13  	"github.com/gophercloud/gophercloud/openstack/imageservice/v2/imagedata"
    14  	"github.com/gophercloud/gophercloud/openstack/imageservice/v2/imageimport"
    15  	"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
    16  	"github.com/gophercloud/gophercloud/openstack/imageservice/v2/tasks"
    17  	th "github.com/gophercloud/gophercloud/testhelper"
    18  )
    19  
    20  // CreateEmptyImage will create an image, but with no actual image data.
    21  // An error will be returned if an image was unable to be created.
    22  func CreateEmptyImage(t *testing.T, client *gophercloud.ServiceClient) (*images.Image, error) {
    23  	var image *images.Image
    24  
    25  	name := tools.RandomString("ACPTTEST", 16)
    26  	t.Logf("Attempting to create image: %s", name)
    27  
    28  	protected := false
    29  	visibility := images.ImageVisibilityPrivate
    30  	createOpts := &images.CreateOpts{
    31  		Name:            name,
    32  		ContainerFormat: "bare",
    33  		DiskFormat:      "qcow2",
    34  		MinDisk:         0,
    35  		MinRAM:          0,
    36  		Protected:       &protected,
    37  		Visibility:      &visibility,
    38  		Properties: map[string]string{
    39  			"architecture": "x86_64",
    40  		},
    41  		Tags: []string{"foo", "bar", "baz"},
    42  	}
    43  
    44  	image, err := images.Create(client, createOpts).Extract()
    45  	if err != nil {
    46  		return image, err
    47  	}
    48  
    49  	newImage, err := images.Get(client, image.ID).Extract()
    50  	if err != nil {
    51  		return image, err
    52  	}
    53  
    54  	t.Logf("Created image %s: %#v", name, newImage)
    55  
    56  	th.CheckEquals(t, newImage.Name, name)
    57  	th.CheckEquals(t, newImage.Properties["architecture"], "x86_64")
    58  	return newImage, nil
    59  }
    60  
    61  // DeleteImage deletes an image.
    62  // A fatal error will occur if the image failed to delete. This works best when
    63  // used as a deferred function.
    64  func DeleteImage(t *testing.T, client *gophercloud.ServiceClient, image *images.Image) {
    65  	err := images.Delete(client, image.ID).ExtractErr()
    66  	if err != nil {
    67  		t.Fatalf("Unable to delete image %s: %v", image.ID, err)
    68  	}
    69  
    70  	t.Logf("Deleted image: %s", image.ID)
    71  }
    72  
    73  // ImportImageURL contains an URL of a test image that can be imported.
    74  const ImportImageURL = "http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img"
    75  
    76  // CreateTask will create a task to import the CirrOS image.
    77  // An error will be returned if a task couldn't be created.
    78  func CreateTask(t *testing.T, client *gophercloud.ServiceClient, imageURL string) (*tasks.Task, error) {
    79  	t.Logf("Attempting to create an Imageservice import task with image: %s", imageURL)
    80  	opts := tasks.CreateOpts{
    81  		Type: "import",
    82  		Input: map[string]interface{}{
    83  			"image_properties": map[string]interface{}{
    84  				"container_format": "bare",
    85  				"disk_format":      "raw",
    86  			},
    87  			"import_from_format": "raw",
    88  			"import_from":        imageURL,
    89  		},
    90  	}
    91  	task, err := tasks.Create(client, opts).Extract()
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	newTask, err := tasks.Get(client, task.ID).Extract()
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	return newTask, nil
   102  }
   103  
   104  // GetImportInfo will retrieve Import API information.
   105  func GetImportInfo(t *testing.T, client *gophercloud.ServiceClient) (*imageimport.ImportInfo, error) {
   106  	t.Log("Attempting to get the Imageservice Import API information")
   107  	importInfo, err := imageimport.Get(client).Extract()
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	return importInfo, nil
   113  }
   114  
   115  // StageImage will stage local image file to the referenced remote queued image.
   116  func StageImage(t *testing.T, client *gophercloud.ServiceClient, filepath, imageID string) error {
   117  	imageData, err := os.Open(filepath)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	defer imageData.Close()
   122  
   123  	return imagedata.Stage(client, imageID, imageData).ExtractErr()
   124  }
   125  
   126  // DownloadImageFileFromURL will download an image from the specified URL and
   127  // place it into the specified path.
   128  func DownloadImageFileFromURL(t *testing.T, url, filepath string) error {
   129  	file, err := os.Create(filepath)
   130  	if err != nil {
   131  		return err
   132  	}
   133  	defer file.Close()
   134  
   135  	t.Logf("Attempting to download image from %s", url)
   136  	resp, err := http.Get(url)
   137  	if err != nil {
   138  		return err
   139  	}
   140  	defer resp.Body.Close()
   141  
   142  	size, err := io.Copy(file, resp.Body)
   143  	if err != nil {
   144  		return err
   145  	}
   146  
   147  	t.Logf("Downloaded image with size of %d bytes in %s", size, filepath)
   148  	return nil
   149  }
   150  
   151  // DeleteImageFile will delete local image file.
   152  func DeleteImageFile(t *testing.T, filepath string) {
   153  	err := os.Remove(filepath)
   154  	if err != nil {
   155  		t.Fatalf("Unable to delete image file %s", filepath)
   156  	}
   157  
   158  	t.Logf("Successfully deleted image file %s", filepath)
   159  }
   160  
   161  // ImportImage will import image data from the remote source to the Imageservice.
   162  func ImportImage(t *testing.T, client *gophercloud.ServiceClient, imageID string) error {
   163  	importOpts := imageimport.CreateOpts{
   164  		Name: imageimport.WebDownloadMethod,
   165  		URI:  ImportImageURL,
   166  	}
   167  
   168  	t.Logf("Attempting to import image data for %s from %s", imageID, importOpts.URI)
   169  	return imageimport.Create(client, imageID, importOpts).ExtractErr()
   170  }