blob: 145fde07b2dc784eb2c235d53cb116b7407223d8 [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 Perritt9b2bf7d2014-09-18 18:47:51 -050045 cv, err = volumes.Create(client, volumes.VolumeOpts{
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 Perritt9b2bf7d2014-09-18 18:47:51 -050064 gr, err := volumes.Get(client, cv.ID)
65 if err != nil {
66 t.Error(err)
67 return
68 }
69 v, err := volumes.ExtractVolume(gr)
70 if err != nil {
71 t.Error(err)
72 return
73 }
74 fmt.Printf("Got volume: %+v\n", v)
75
Jon Perrittb71a28a2014-09-17 18:16:32 -050076 pager := volumes.List(client, volumes.ListOpts{})
77 if err != nil {
78 t.Error(err)
79 return
80 }
81 err = pager.EachPage(func(page pagination.Page) (bool, error) {
82 vols, err := volumes.ExtractVolumes(page)
83 if len(vols) != numVols {
84 t.Errorf("Expected %d volumes, got %d", numVols, len(vols))
85 }
86 return true, err
87 })
88}