blob: 0d773dbd832695dce2b148d77527012c43fc8ede [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"
8 "strconv"
9 "testing"
Jon Perritt3db65cd2014-09-17 18:32:43 -050010 "time"
Jon Perrittb71a28a2014-09-17 18:16:32 -050011
12 "github.com/rackspace/gophercloud"
13 "github.com/rackspace/gophercloud/openstack"
14 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
15 "github.com/rackspace/gophercloud/openstack/utils"
16 "github.com/rackspace/gophercloud/pagination"
17)
18
19var numVols = 1
20
21func newClient() (*gophercloud.ServiceClient, error) {
22 ao, err := utils.AuthOptions()
23 if err != nil {
24 return nil, err
25 }
26
27 client, err := openstack.AuthenticatedClient(ao)
28 if err != nil {
29 return nil, err
30 }
31
32 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
33 Region: os.Getenv("OS_REGION_NAME"),
34 })
35}
36
37func TestVolumes(t *testing.T) {
38 client, err := newClient()
39 if err != nil {
40 t.Fatalf("Failed to create Block Storage v1 client: %v", err)
41 }
42
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050043 var cv *volumes.Volume
Jon Perrittb71a28a2014-09-17 18:16:32 -050044 for i := 0; i < numVols; i++ {
Jon Perritt97347a02014-09-21 13:34:48 -050045 cv, err = volumes.Create(client, volumes.CreateOpts{
Jon Perrittb71a28a2014-09-17 18:16:32 -050046 Size: 1,
47 Name: "gophercloud-test-volume-" + strconv.Itoa(i),
48 })
49 if err != nil {
50 t.Error(err)
51 return
Jon Perritt3db65cd2014-09-17 18:32:43 -050052 }
53 defer func() {
54 time.Sleep(10000 * time.Millisecond)
55 err = volumes.Delete(client, cv.ID)
56 if err != nil {
57 t.Error(err)
58 return
59 }
60 }()
61
Jon Perrittb71a28a2014-09-17 18:16:32 -050062 }
63
Jon Perritt97347a02014-09-21 13:34:48 -050064 _, err = volumes.Update(client, cv.ID, volumes.UpdateOpts{
65 Name: "gophercloud-updated-volume",
66 })
67 if err != nil {
68 t.Error(err)
69 return
70 }
71
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050072 gr, err := volumes.Get(client, cv.ID)
73 if err != nil {
74 t.Error(err)
75 return
76 }
77 v, err := volumes.ExtractVolume(gr)
78 if err != nil {
79 t.Error(err)
80 return
81 }
82 fmt.Printf("Got volume: %+v\n", v)
83
Jon Perritt97347a02014-09-21 13:34:48 -050084 if v.Name != "gophercloud-updated-volume" {
85 t.Errorf("Unable to update volume: Expected name: gophercloud-updated-volume\nActual name: %s", v.Name)
86 }
87
Jon Perrittb71a28a2014-09-17 18:16:32 -050088 pager := volumes.List(client, volumes.ListOpts{})
89 if err != nil {
90 t.Error(err)
91 return
92 }
93 err = pager.EachPage(func(page pagination.Page) (bool, error) {
94 vols, err := volumes.ExtractVolumes(page)
95 if len(vols) != numVols {
96 t.Errorf("Expected %d volumes, got %d", numVols, len(vols))
97 }
98 return true, err
99 })
100}