blob: 7760427f08c02fbae082a5f6d1a79761b3eb5e73 [file] [log] [blame]
Jon Perrittb71a28a2014-09-17 18:16:32 -05001// +build acceptance blockstorage
2
3package v1
4
5import (
6 "os"
Jon Perrittb71a28a2014-09-17 18:16:32 -05007 "testing"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/openstack"
Jon Perritt57ba7632014-10-02 20:32:22 -050011 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Jon Perrittb71a28a2014-09-17 18:16:32 -050012 "github.com/rackspace/gophercloud/pagination"
jrperritt0bfb0992014-10-28 10:38:07 -050013 th "github.com/rackspace/gophercloud/testhelper"
Jon Perrittb71a28a2014-09-17 18:16:32 -050014)
15
Jon Perritt46be1a12014-11-03 09:18:24 -060016func newClient(t *testing.T) (*gophercloud.ServiceClient, error) {
Jamie Hannaford390555a2014-10-22 17:04:03 +020017 ao, err := openstack.AuthOptionsFromEnv()
jrperritt0bfb0992014-10-28 10:38:07 -050018 th.AssertNoErr(t, err)
Jon Perrittb71a28a2014-09-17 18:16:32 -050019
20 client, err := openstack.AuthenticatedClient(ao)
jrperritt0bfb0992014-10-28 10:38:07 -050021 th.AssertNoErr(t, err)
Jon Perrittb71a28a2014-09-17 18:16:32 -050022
23 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
24 Region: os.Getenv("OS_REGION_NAME"),
25 })
26}
27
28func TestVolumes(t *testing.T) {
Jon Perritt46be1a12014-11-03 09:18:24 -060029 client, err := newClient(t)
jrperritt0bfb0992014-10-28 10:38:07 -050030 th.AssertNoErr(t, err)
Jon Perrittb71a28a2014-09-17 18:16:32 -050031
Jon Perritt57ba7632014-10-02 20:32:22 -050032 cv, err := volumes.Create(client, &volumes.CreateOpts{
33 Size: 1,
34 Name: "gophercloud-test-volume",
35 }).Extract()
jrperritt0bfb0992014-10-28 10:38:07 -050036 th.AssertNoErr(t, err)
Jon Perritt57ba7632014-10-02 20:32:22 -050037 defer func() {
38 err = volumes.WaitForStatus(client, cv.ID, "available", 60)
jrperritt0bfb0992014-10-28 10:38:07 -050039 th.AssertNoErr(t, err)
Jon Perrittba2395e2014-10-27 15:23:21 -050040 err = volumes.Delete(client, cv.ID).ExtractErr()
jrperritt0bfb0992014-10-28 10:38:07 -050041 th.AssertNoErr(t, err)
Jon Perritt57ba7632014-10-02 20:32:22 -050042 }()
Jon Perritt3db65cd2014-09-17 18:32:43 -050043
Jon Perritt57ba7632014-10-02 20:32:22 -050044 _, err = volumes.Update(client, cv.ID, &volumes.UpdateOpts{
Jon Perritt97347a02014-09-21 13:34:48 -050045 Name: "gophercloud-updated-volume",
Jon Perritt57ba7632014-10-02 20:32:22 -050046 }).Extract()
jrperritt0bfb0992014-10-28 10:38:07 -050047 th.AssertNoErr(t, err)
Jon Perritt97347a02014-09-21 13:34:48 -050048
Jon Perritt57ba7632014-10-02 20:32:22 -050049 v, err := volumes.Get(client, cv.ID).Extract()
jrperritt0bfb0992014-10-28 10:38:07 -050050 th.AssertNoErr(t, err)
Ash Wilson9a9f5bc2014-10-24 14:55:40 -040051 t.Logf("Got volume: %+v\n", v)
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050052
Jon Perritt97347a02014-09-21 13:34:48 -050053 if v.Name != "gophercloud-updated-volume" {
54 t.Errorf("Unable to update volume: Expected name: gophercloud-updated-volume\nActual name: %s", v.Name)
55 }
56
Jon Perritt57ba7632014-10-02 20:32:22 -050057 err = volumes.List(client, &volumes.ListOpts{Name: "gophercloud-updated-volume"}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittb71a28a2014-09-17 18:16:32 -050058 vols, err := volumes.ExtractVolumes(page)
jrperritt0bfb0992014-10-28 10:38:07 -050059 th.CheckEquals(t, 1, len(vols))
Jon Perrittb71a28a2014-09-17 18:16:32 -050060 return true, err
61 })
jrperritt0bfb0992014-10-28 10:38:07 -050062 th.AssertNoErr(t, err)
Jon Perrittb71a28a2014-09-17 18:16:32 -050063}