blob: 22d965347bc73b0586668fff5f46896800c4b464 [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"
Jon Perritt57b056b2014-09-29 21:57:20 -050014 "github.com/rackspace/gophercloud/openstack/blockStorage/v1/volumes"
Jon Perrittb71a28a2014-09-17 18:16:32 -050015 "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 Perritt03cb46d2014-09-22 20:46:20 -050072 v, err := volumes.Get(client, cv.ID).ExtractVolume()
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050073 if err != nil {
74 t.Error(err)
75 return
76 }
77 fmt.Printf("Got volume: %+v\n", v)
78
Jon Perritt97347a02014-09-21 13:34:48 -050079 if v.Name != "gophercloud-updated-volume" {
80 t.Errorf("Unable to update volume: Expected name: gophercloud-updated-volume\nActual name: %s", v.Name)
81 }
82
Jon Perrittb71a28a2014-09-17 18:16:32 -050083 pager := volumes.List(client, volumes.ListOpts{})
84 if err != nil {
85 t.Error(err)
86 return
87 }
88 err = pager.EachPage(func(page pagination.Page) (bool, error) {
89 vols, err := volumes.ExtractVolumes(page)
90 if len(vols) != numVols {
91 t.Errorf("Expected %d volumes, got %d", numVols, len(vols))
92 }
93 return true, err
94 })
95}