Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 1 | // +build acceptance blockstorage |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
| 6 | "os" |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/openstack" |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/pagination" |
| 13 | ) |
| 14 | |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 15 | func newClient() (*gophercloud.ServiceClient, error) { |
Jamie Hannaford | 390555a | 2014-10-22 17:04:03 +0200 | [diff] [blame] | 16 | ao, err := openstack.AuthOptionsFromEnv() |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 17 | 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 | |
| 31 | func 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 Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 37 | 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 Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 51 | if err != nil { |
| 52 | t.Error(err) |
| 53 | return |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 54 | } |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 55 | }() |
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 | _, err = volumes.Update(client, cv.ID, &volumes.UpdateOpts{ |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 58 | Name: "gophercloud-updated-volume", |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 59 | }).Extract() |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 60 | if err != nil { |
| 61 | t.Error(err) |
| 62 | return |
| 63 | } |
| 64 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 65 | v, err := volumes.Get(client, cv.ID).Extract() |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 66 | if err != nil { |
| 67 | t.Error(err) |
| 68 | return |
| 69 | } |
Ash Wilson | 9a9f5bc | 2014-10-24 14:55:40 -0400 | [diff] [blame^] | 70 | t.Logf("Got volume: %+v\n", v) |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 71 | |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 72 | 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 Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 76 | 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] | 77 | vols, err := volumes.ExtractVolumes(page) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 78 | if len(vols) != 1 { |
| 79 | t.Errorf("Expected 1 volume, got %d", len(vols)) |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 80 | } |
| 81 | return true, err |
| 82 | }) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 83 | if err != nil { |
| 84 | t.Errorf("Error listing volumes: %v", err) |
| 85 | } |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 86 | } |