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/pagination" |
| 14 | ) |
| 15 | |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 16 | func newClient() (*gophercloud.ServiceClient, error) { |
Jamie Hannaford | 390555a | 2014-10-22 17:04:03 +0200 | [diff] [blame] | 17 | ao, err := openstack.AuthOptionsFromEnv() |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 18 | 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 | |
| 32 | func 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 Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 38 | 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 Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 52 | if err != nil { |
| 53 | t.Error(err) |
| 54 | return |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 55 | } |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 56 | }() |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 57 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 58 | _, err = volumes.Update(client, cv.ID, &volumes.UpdateOpts{ |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 59 | Name: "gophercloud-updated-volume", |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 60 | }).Extract() |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 61 | if err != nil { |
| 62 | t.Error(err) |
| 63 | return |
| 64 | } |
| 65 | |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 66 | v, err := volumes.Get(client, cv.ID).Extract() |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 67 | if err != nil { |
| 68 | t.Error(err) |
| 69 | return |
| 70 | } |
| 71 | fmt.Printf("Got volume: %+v\n", v) |
| 72 | |
Jon Perritt | 97347a0 | 2014-09-21 13:34:48 -0500 | [diff] [blame] | 73 | 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 Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 77 | 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] | 78 | vols, err := volumes.ExtractVolumes(page) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 79 | if len(vols) != 1 { |
| 80 | t.Errorf("Expected 1 volume, got %d", len(vols)) |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 81 | } |
| 82 | return true, err |
| 83 | }) |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 84 | if err != nil { |
| 85 | t.Errorf("Error listing volumes: %v", err) |
| 86 | } |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 87 | } |