Jamie Hannaford | af209d1 | 2014-10-22 12:37:48 +0200 | [diff] [blame] | 1 | package v1 |
Jamie Hannaford | 48a80ce | 2014-10-23 14:49:51 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | "github.com/rackspace/gophercloud/rackspace/blockstorage/v1/volumes" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | func TestVolumes(t *testing.T) { |
| 14 | client := setup(t) |
| 15 | |
| 16 | t.Logf("Listing volumes") |
| 17 | testVolumeList(t, client) |
| 18 | |
| 19 | t.Logf("Creating volume") |
| 20 | volumeID := testVolumeCreate(t, client) |
| 21 | |
| 22 | t.Logf("Getting volume %s", volumeID) |
| 23 | testVolumeGet(t, client, volumeID) |
| 24 | |
| 25 | t.Logf("Updating volume %s", volumeID) |
| 26 | testVolumeUpdate(t, client, volumeID) |
| 27 | |
| 28 | t.Logf("Deleting volume %s", volumeID) |
| 29 | testVolumeDelete(t, client, volumeID) |
| 30 | } |
| 31 | |
| 32 | func testVolumeList(t *testing.T, client *gophercloud.ServiceClient) { |
| 33 | volumes.List(client).EachPage(func(page pagination.Page) (bool, error) { |
| 34 | vList, err := volumes.ExtractVolumes(page) |
| 35 | th.AssertNoErr(t, err) |
| 36 | |
| 37 | for _, v := range vList { |
| 38 | t.Logf("Volume: ID [%s] Name [%s] Type [%s] Created [%s]", v.ID, v.Name, |
| 39 | v.VolumeType, v.CreatedAt) |
| 40 | } |
| 41 | |
| 42 | return true, nil |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | func testVolumeCreate(t *testing.T, client *gophercloud.ServiceClient) string { |
| 47 | vol, err := volumes.Create(client, os.CreateOpts{Size: 75}).Extract() |
| 48 | th.AssertNoErr(t, err) |
| 49 | t.Logf("Created volume: ID [%s] Size [%s]", vol.ID, vol.Size) |
| 50 | return vol.ID |
| 51 | } |
| 52 | |
| 53 | func testVolumeGet(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 54 | vol, err := volumes.Get(client, id).Extract() |
| 55 | th.AssertNoErr(t, err) |
| 56 | t.Logf("Created volume: ID [%s] Size [%s]", vol.ID, vol.Size) |
| 57 | } |
| 58 | |
| 59 | func testVolumeUpdate(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 60 | vol, err := volumes.Update(client, id, volumes.UpdateOpts{Name: "new_name"}).Extract() |
| 61 | th.AssertNoErr(t, err) |
| 62 | t.Logf("Created volume: ID [%s] Name [%s]", vol.ID, vol.Name) |
| 63 | } |
| 64 | |
| 65 | func testVolumeDelete(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 66 | err := volumes.Delete(client, id) |
| 67 | th.AssertNoErr(t, err) |
| 68 | t.Logf("Deleted volume %s", id) |
| 69 | } |