Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 1 | // +build acceptance blockstorage |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 6 | "fmt" |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 7 | "os" |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 8 | "testing" |
| 9 | |
| 10 | "github.com/rackspace/gophercloud" |
| 11 | "github.com/rackspace/gophercloud/openstack" |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/openstack/utils" |
| 14 | "github.com/rackspace/gophercloud/pagination" |
| 15 | ) |
| 16 | |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 17 | func newClient() (*gophercloud.ServiceClient, error) { |
| 18 | ao, err := utils.AuthOptions() |
| 19 | if err != nil { |
| 20 | return nil, err |
| 21 | } |
| 22 | |
| 23 | client, err := openstack.AuthenticatedClient(ao) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | |
| 28 | return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{ |
| 29 | Region: os.Getenv("OS_REGION_NAME"), |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | func TestVolumes(t *testing.T) { |
| 34 | client, err := newClient() |
| 35 | if err != nil { |
| 36 | t.Fatalf("Failed to create Block Storage v1 client: %v", err) |
| 37 | } |
| 38 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 39 | cv, err := volumes.Create(client, &volumes.CreateOpts{ |
| 40 | Size: 1, |
| 41 | Name: "gophercloud-test-volume", |
| 42 | }).Extract() |
| 43 | if err != nil { |
| 44 | t.Error(err) |
| 45 | return |
| 46 | } |
| 47 | defer func() { |
| 48 | err = volumes.WaitForStatus(client, cv.ID, "available", 60) |
| 49 | if err != nil { |
| 50 | t.Error(err) |
| 51 | } |
| 52 | err = volumes.Delete(client, cv.ID) |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 53 | if err != nil { |
| 54 | t.Error(err) |
| 55 | return |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 56 | } |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 57 | }() |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 58 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 59 | _, err = volumes.Update(client, cv.ID, &volumes.UpdateOpts{ |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 60 | Name: "gophercloud-updated-volume", |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 61 | }).Extract() |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 62 | if err != nil { |
| 63 | t.Error(err) |
| 64 | return |
| 65 | } |
| 66 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 67 | v, err := volumes.Get(client, cv.ID).Extract() |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 68 | if err != nil { |
| 69 | t.Error(err) |
| 70 | return |
| 71 | } |
| 72 | fmt.Printf("Got volume: %+v\n", v) |
| 73 | |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 74 | if v.Name != "gophercloud-updated-volume" { |
| 75 | t.Errorf("Unable to update volume: Expected name: gophercloud-updated-volume\nActual name: %s", v.Name) |
| 76 | } |
| 77 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 78 | err = volumes.List(client, &volumes.ListOpts{Name: "gophercloud-updated-volume"}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 79 | vols, err := volumes.ExtractVolumes(page) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 80 | if len(vols) != 1 { |
| 81 | t.Errorf("Expected 1 volume, got %d", len(vols)) |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 82 | } |
| 83 | return true, err |
| 84 | }) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 85 | if err != nil { |
| 86 | t.Errorf("Error listing volumes: %v", err) |
| 87 | } |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 88 | } |