blob: f84f5cb90771ce6a483223c278f50b17efa39b90 [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"
Jon Perrittb71a28a2014-09-17 18:16:32 -05008 "testing"
9
10 "github.com/rackspace/gophercloud"
11 "github.com/rackspace/gophercloud/openstack"
Jon Perritt57ba7632014-10-02 20:32:22 -050012 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Jon Perrittb71a28a2014-09-17 18:16:32 -050013 "github.com/rackspace/gophercloud/pagination"
14)
15
Jon Perrittb71a28a2014-09-17 18:16:32 -050016func newClient() (*gophercloud.ServiceClient, error) {
Jamie Hannaford390555a2014-10-22 17:04:03 +020017 ao, err := openstack.AuthOptionsFromEnv()
Jon Perrittb71a28a2014-09-17 18:16:32 -050018 if err != nil {
19 return nil, err
20 }
21
22 client, err := openstack.AuthenticatedClient(ao)
23 if err != nil {
24 return nil, err
25 }
26
27 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
28 Region: os.Getenv("OS_REGION_NAME"),
29 })
30}
31
32func TestVolumes(t *testing.T) {
33 client, err := newClient()
34 if err != nil {
35 t.Fatalf("Failed to create Block Storage v1 client: %v", err)
36 }
37
Jon Perritt57ba7632014-10-02 20:32:22 -050038 cv, err := volumes.Create(client, &volumes.CreateOpts{
39 Size: 1,
40 Name: "gophercloud-test-volume",
41 }).Extract()
42 if err != nil {
43 t.Error(err)
44 return
45 }
46 defer func() {
47 err = volumes.WaitForStatus(client, cv.ID, "available", 60)
48 if err != nil {
49 t.Error(err)
50 }
51 err = volumes.Delete(client, cv.ID)
Jon Perrittb71a28a2014-09-17 18:16:32 -050052 if err != nil {
53 t.Error(err)
54 return
Jon Perritt3db65cd2014-09-17 18:32:43 -050055 }
Jon Perritt57ba7632014-10-02 20:32:22 -050056 }()
Jon Perritt3db65cd2014-09-17 18:32:43 -050057
Jon Perritt57ba7632014-10-02 20:32:22 -050058 _, err = volumes.Update(client, cv.ID, &volumes.UpdateOpts{
Jon Perritt97347a02014-09-21 13:34:48 -050059 Name: "gophercloud-updated-volume",
Jon Perritt57ba7632014-10-02 20:32:22 -050060 }).Extract()
Jon Perritt97347a02014-09-21 13:34:48 -050061 if err != nil {
62 t.Error(err)
63 return
64 }
65
Jon Perritt57ba7632014-10-02 20:32:22 -050066 v, err := volumes.Get(client, cv.ID).Extract()
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050067 if err != nil {
68 t.Error(err)
69 return
70 }
71 fmt.Printf("Got volume: %+v\n", v)
72
Jon Perritt97347a02014-09-21 13:34:48 -050073 if v.Name != "gophercloud-updated-volume" {
74 t.Errorf("Unable to update volume: Expected name: gophercloud-updated-volume\nActual name: %s", v.Name)
75 }
76
Jon Perritt57ba7632014-10-02 20:32:22 -050077 err = volumes.List(client, &volumes.ListOpts{Name: "gophercloud-updated-volume"}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittb71a28a2014-09-17 18:16:32 -050078 vols, err := volumes.ExtractVolumes(page)
Jon Perritt57ba7632014-10-02 20:32:22 -050079 if len(vols) != 1 {
80 t.Errorf("Expected 1 volume, got %d", len(vols))
Jon Perrittb71a28a2014-09-17 18:16:32 -050081 }
82 return true, err
83 })
Jon Perritt57ba7632014-10-02 20:32:22 -050084 if err != nil {
85 t.Errorf("Error listing volumes: %v", err)
86 }
Jon Perrittb71a28a2014-09-17 18:16:32 -050087}