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