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" |
| 14 | "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
| 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 | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame^] | 45 | cv, err = volumes.Create(client, volumes.VolumeOpts{ |
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 | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame^] | 64 | gr, err := volumes.Get(client, cv.ID) |
| 65 | if err != nil { |
| 66 | t.Error(err) |
| 67 | return |
| 68 | } |
| 69 | v, err := volumes.ExtractVolume(gr) |
| 70 | if err != nil { |
| 71 | t.Error(err) |
| 72 | return |
| 73 | } |
| 74 | fmt.Printf("Got volume: %+v\n", v) |
| 75 | |
Jon Perritt | b71a28a | 2014-09-17 18:16:32 -0500 | [diff] [blame] | 76 | pager := volumes.List(client, volumes.ListOpts{}) |
| 77 | if err != nil { |
| 78 | t.Error(err) |
| 79 | return |
| 80 | } |
| 81 | err = pager.EachPage(func(page pagination.Page) (bool, error) { |
| 82 | vols, err := volumes.ExtractVolumes(page) |
| 83 | if len(vols) != numVols { |
| 84 | t.Errorf("Expected %d volumes, got %d", numVols, len(vols)) |
| 85 | } |
| 86 | return true, err |
| 87 | }) |
| 88 | } |