blob: 6739a99707ff2420f983d6b1396034d7cf7ee2c7 [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"
13)
14
Jon Perrittb71a28a2014-09-17 18:16:32 -050015func newClient() (*gophercloud.ServiceClient, error) {
Jamie Hannaford390555a2014-10-22 17:04:03 +020016 ao, err := openstack.AuthOptionsFromEnv()
Jon Perrittb71a28a2014-09-17 18:16:32 -050017 if err != nil {
18 return nil, err
19 }
20
21 client, err := openstack.AuthenticatedClient(ao)
22 if err != nil {
23 return nil, err
24 }
25
26 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
27 Region: os.Getenv("OS_REGION_NAME"),
28 })
29}
30
31func TestVolumes(t *testing.T) {
32 client, err := newClient()
33 if err != nil {
34 t.Fatalf("Failed to create Block Storage v1 client: %v", err)
35 }
36
Jon Perritt57ba7632014-10-02 20:32:22 -050037 cv, err := volumes.Create(client, &volumes.CreateOpts{
38 Size: 1,
39 Name: "gophercloud-test-volume",
40 }).Extract()
41 if err != nil {
42 t.Error(err)
43 return
44 }
45 defer func() {
46 err = volumes.WaitForStatus(client, cv.ID, "available", 60)
47 if err != nil {
48 t.Error(err)
49 }
50 err = volumes.Delete(client, cv.ID)
Jon Perrittb71a28a2014-09-17 18:16:32 -050051 if err != nil {
52 t.Error(err)
53 return
Jon Perritt3db65cd2014-09-17 18:32:43 -050054 }
Jon Perritt57ba7632014-10-02 20:32:22 -050055 }()
Jon Perritt3db65cd2014-09-17 18:32:43 -050056
Jon Perritt57ba7632014-10-02 20:32:22 -050057 _, err = volumes.Update(client, cv.ID, &volumes.UpdateOpts{
Jon Perritt97347a02014-09-21 13:34:48 -050058 Name: "gophercloud-updated-volume",
Jon Perritt57ba7632014-10-02 20:32:22 -050059 }).Extract()
Jon Perritt97347a02014-09-21 13:34:48 -050060 if err != nil {
61 t.Error(err)
62 return
63 }
64
Jon Perritt57ba7632014-10-02 20:32:22 -050065 v, err := volumes.Get(client, cv.ID).Extract()
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050066 if err != nil {
67 t.Error(err)
68 return
69 }
Ash Wilson9a9f5bc2014-10-24 14:55:40 -040070 t.Logf("Got volume: %+v\n", v)
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050071
Jon Perritt97347a02014-09-21 13:34:48 -050072 if v.Name != "gophercloud-updated-volume" {
73 t.Errorf("Unable to update volume: Expected name: gophercloud-updated-volume\nActual name: %s", v.Name)
74 }
75
Jon Perritt57ba7632014-10-02 20:32:22 -050076 err = volumes.List(client, &volumes.ListOpts{Name: "gophercloud-updated-volume"}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittb71a28a2014-09-17 18:16:32 -050077 vols, err := volumes.ExtractVolumes(page)
Jon Perritt57ba7632014-10-02 20:32:22 -050078 if len(vols) != 1 {
79 t.Errorf("Expected 1 volume, got %d", len(vols))
Jon Perrittb71a28a2014-09-17 18:16:32 -050080 }
81 return true, err
82 })
Jon Perritt57ba7632014-10-02 20:32:22 -050083 if err != nil {
84 t.Errorf("Error listing volumes: %v", err)
85 }
Jon Perrittb71a28a2014-09-17 18:16:32 -050086}