blob: 21a47ac9b3210701d75d954ed64b8520763f4888 [file] [log] [blame]
Jon Perrittb71a28a2014-09-17 18:16:32 -05001// +build acceptance blockstorage
2
3package v1
4
5import (
Jon Perritt9b2bf7d2014-09-18 18:47:51 -05006 "fmt"
Jon Perrittb71a28a2014-09-17 18:16:32 -05007 "os"
Jon Perrittb71a28a2014-09-17 18:16:32 -05008 "testing"
9
10 "github.com/rackspace/gophercloud"
11 "github.com/rackspace/gophercloud/openstack"
Jon Perritt57ba7632014-10-02 20:32:22 -050012 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Jon Perrittb71a28a2014-09-17 18:16:32 -050013 "github.com/rackspace/gophercloud/openstack/utils"
14 "github.com/rackspace/gophercloud/pagination"
15)
16
Jon Perrittb71a28a2014-09-17 18:16:32 -050017func newClient() (*gophercloud.ServiceClient, error) {
18 ao, err := utils.AuthOptions()
19 if err != nil {
20 return nil, err
21 }
22
23 client, err := openstack.AuthenticatedClient(ao)
24 if err != nil {
25 return nil, err
26 }
27
28 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
29 Region: os.Getenv("OS_REGION_NAME"),
30 })
31}
32
33func TestVolumes(t *testing.T) {
34 client, err := newClient()
35 if err != nil {
36 t.Fatalf("Failed to create Block Storage v1 client: %v", err)
37 }
38
Jon Perritt57ba7632014-10-02 20:32:22 -050039 cv, err := volumes.Create(client, &volumes.CreateOpts{
40 Size: 1,
41 Name: "gophercloud-test-volume",
42 }).Extract()
43 if err != nil {
44 t.Error(err)
45 return
46 }
47 defer func() {
48 err = volumes.WaitForStatus(client, cv.ID, "available", 60)
49 if err != nil {
50 t.Error(err)
51 }
52 err = volumes.Delete(client, cv.ID)
Jon Perrittb71a28a2014-09-17 18:16:32 -050053 if err != nil {
54 t.Error(err)
55 return
Jon Perritt3db65cd2014-09-17 18:32:43 -050056 }
Jon Perritt57ba7632014-10-02 20:32:22 -050057 }()
Jon Perritt3db65cd2014-09-17 18:32:43 -050058
Jon Perritt57ba7632014-10-02 20:32:22 -050059 _, err = volumes.Update(client, cv.ID, &volumes.UpdateOpts{
Jon Perritt97347a02014-09-21 13:34:48 -050060 Name: "gophercloud-updated-volume",
Jon Perritt57ba7632014-10-02 20:32:22 -050061 }).Extract()
Jon Perritt97347a02014-09-21 13:34:48 -050062 if err != nil {
63 t.Error(err)
64 return
65 }
66
Jon Perritt57ba7632014-10-02 20:32:22 -050067 v, err := volumes.Get(client, cv.ID).Extract()
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050068 if err != nil {
69 t.Error(err)
70 return
71 }
72 fmt.Printf("Got volume: %+v\n", v)
73
Jon Perritt97347a02014-09-21 13:34:48 -050074 if v.Name != "gophercloud-updated-volume" {
75 t.Errorf("Unable to update volume: Expected name: gophercloud-updated-volume\nActual name: %s", v.Name)
76 }
77
Jon Perritt57ba7632014-10-02 20:32:22 -050078 err = volumes.List(client, &volumes.ListOpts{Name: "gophercloud-updated-volume"}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittb71a28a2014-09-17 18:16:32 -050079 vols, err := volumes.ExtractVolumes(page)
Jon Perritt57ba7632014-10-02 20:32:22 -050080 if len(vols) != 1 {
81 t.Errorf("Expected 1 volume, got %d", len(vols))
Jon Perrittb71a28a2014-09-17 18:16:32 -050082 }
83 return true, err
84 })
Jon Perritt57ba7632014-10-02 20:32:22 -050085 if err != nil {
86 t.Errorf("Error listing volumes: %v", err)
87 }
Jon Perrittb71a28a2014-09-17 18:16:32 -050088}