blob: 68c2b6a7ef545fae81cff27273fa8ee296a89360 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001// +build acceptance
2
3package openstack
4
5import (
Jon Perrittee6074f2014-04-30 18:42:32 -05006 blockstorage "github.com/rackspace/gophercloud/openstack/blockstorage/v1"
7 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
8 "github.com/rackspace/gophercloud/openstack/identity"
9 "github.com/rackspace/gophercloud/openstack/utils"
10 "os"
Jon Perritt94963ad2014-05-05 12:14:39 -050011 "strconv"
Jon Perrittee6074f2014-04-30 18:42:32 -050012 "testing"
Jon Perritt94963ad2014-05-05 12:14:39 -050013 "time"
Jon Perrittee6074f2014-04-30 18:42:32 -050014)
15
Jon Perritt94963ad2014-05-05 12:14:39 -050016var numVols = 2
17
Jon Perrittee6074f2014-04-30 18:42:32 -050018func getClient() (*blockstorage.Client, error) {
19 ao, err := utils.AuthOptions()
20 if err != nil {
21 return nil, err
22 }
23
24 r, err := identity.Authenticate(ao)
25 if err != nil {
26 return nil, err
27 }
28
29 sc, err := identity.GetServiceCatalog(r)
30 if err != nil {
31 return nil, err
32 }
33
34 ces, err := sc.CatalogEntries()
35 if err != nil {
36 return nil, err
37 }
38
39 var eps []identity.Endpoint
40 for _, ce := range ces {
41 if ce.Type == "volume" {
42 eps = ce.Endpoints
43 }
44 }
45
46 region := os.Getenv("OS_REGION_NAME")
47 rep := ""
48 for _, ep := range eps {
49 if ep.Region == region {
50 rep = ep.PublicURL
51 }
52 }
53
54 client := blockstorage.NewClient(rep, r, ao)
55
56 return client, nil
57
58}
59
60func TestVolumes(t *testing.T) {
61 client, err := getClient()
62 if err != nil {
63 t.Error(err)
64 return
65 }
Jon Perritt70dd47d2014-05-01 13:51:53 -050066
Jon Perritt94963ad2014-05-05 12:14:39 -050067 var cv volumes.Volume
68 for i := 0; i < numVols; i++ {
69 cv, err := volumes.Create(client, volumes.CreateOpts{
70 "size": 1,
71 "display_name": "test-volume" + strconv.Itoa(i),
Jon Perritte77b9b22014-05-01 13:11:12 -050072 })
Jon Perritt94963ad2014-05-05 12:14:39 -050073 if err != nil {
74 t.Error(err)
75 return
76 }
77 defer func() {
78 time.Sleep(10000 * time.Millisecond)
79 err = volumes.Delete(client, volumes.DeleteOpts{
80 "id": cv.Id,
81 })
82 if err != nil {
83 t.Error(err)
84 return
85 }
86 }()
87 }
Jon Perritt70dd47d2014-05-01 13:51:53 -050088
Jon Perritt94963ad2014-05-05 12:14:39 -050089 vols, err := volumes.List(client, volumes.ListOpts{
90 "full": true,
Jon Perritt70dd47d2014-05-01 13:51:53 -050091 })
92 if err != nil {
93 t.Error(err)
94 return
95 }
Jon Perritt94963ad2014-05-05 12:14:39 -050096 if len(vols) != numVols {
97 t.Errorf("Expected %d volumes, got %d", numVols, len(vols))
98 return
99 }
100
101 vols, err = volumes.List(client, volumes.ListOpts{
102 "full": false,
103 })
104 if err != nil {
105 t.Error(err)
106 return
107 }
108 if len(vols) != numVols {
109 t.Errorf("Expected %d volumes, got %d", numVols, len(vols))
110 return
111 }
112
113 _, err = volumes.Get(client, volumes.GetOpts{
114 "id": cv.Id,
115 })
116 if err != nil {
117 t.Error(err)
118 return
119 }
120
Jon Perrittee6074f2014-04-30 18:42:32 -0500121}