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" |
| 7 | "strconv" |
| 8 | "testing" |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 9 | "time" |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 10 | |
| 11 | "github.com/rackspace/gophercloud" |
| 12 | "github.com/rackspace/gophercloud/openstack" |
| 13 | "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
| 14 | "github.com/rackspace/gophercloud/openstack/utils" |
| 15 | "github.com/rackspace/gophercloud/pagination" |
| 16 | ) |
| 17 | |
| 18 | var numVols = 1 |
| 19 | |
| 20 | func newClient() (*gophercloud.ServiceClient, error) { |
| 21 | ao, err := utils.AuthOptions() |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | |
| 26 | client, err := openstack.AuthenticatedClient(ao) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{ |
| 32 | Region: os.Getenv("OS_REGION_NAME"), |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | func TestVolumes(t *testing.T) { |
| 37 | client, err := newClient() |
| 38 | if err != nil { |
| 39 | t.Fatalf("Failed to create Block Storage v1 client: %v", err) |
| 40 | } |
| 41 | |
| 42 | for i := 0; i < numVols; i++ { |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 43 | cv, err := volumes.Create(client, volumes.VolumeOpts{ |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 44 | Size: 1, |
| 45 | Name: "gophercloud-test-volume-" + strconv.Itoa(i), |
| 46 | }) |
| 47 | if err != nil { |
| 48 | t.Error(err) |
| 49 | return |
Jon Perritt | 3db65cd | 2014-09-17 18:32:43 -0500 | [diff] [blame] | 50 | } |
| 51 | defer func() { |
| 52 | time.Sleep(10000 * time.Millisecond) |
| 53 | err = volumes.Delete(client, cv.ID) |
| 54 | if err != nil { |
| 55 | t.Error(err) |
| 56 | return |
| 57 | } |
| 58 | }() |
| 59 | |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | pager := volumes.List(client, volumes.ListOpts{}) |
| 63 | if err != nil { |
| 64 | t.Error(err) |
| 65 | return |
| 66 | } |
| 67 | err = pager.EachPage(func(page pagination.Page) (bool, error) { |
| 68 | vols, err := volumes.ExtractVolumes(page) |
| 69 | if len(vols) != numVols { |
| 70 | t.Errorf("Expected %d volumes, got %d", numVols, len(vols)) |
| 71 | } |
| 72 | return true, err |
| 73 | }) |
| 74 | } |