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 | "time" |
| 6 | |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | "github.com/rackspace/gophercloud/rackspace/blockstorage/v1/snapshots" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | func TestSnapshots(t *testing.T) { |
| 14 | client := setup(t) |
| 15 | volID := testVolumeCreate(t, client) |
| 16 | |
| 17 | t.Log("Creating snapshots") |
| 18 | s := testSnapshotCreate(t, client, volID) |
| 19 | id := s.ID |
| 20 | |
| 21 | t.Log("Listing snapshots") |
| 22 | testSnapshotList(t, client) |
| 23 | |
| 24 | t.Logf("Getting snapshot %s", id) |
| 25 | testSnapshotGet(t, client, id) |
| 26 | |
| 27 | t.Logf("Updating snapshot %s", id) |
| 28 | testSnapshotUpdate(t, client, id) |
| 29 | |
| 30 | t.Logf("Deleting snapshot %s", id) |
| 31 | testSnapshotDelete(t, client, id) |
| 32 | s.WaitUntilDeleted(client, -1) |
| 33 | |
| 34 | t.Logf("Deleting volume %s", volID) |
| 35 | testVolumeDelete(t, client, volID) |
| 36 | } |
| 37 | |
| 38 | func testSnapshotCreate(t *testing.T, client *gophercloud.ServiceClient, volID string) *snapshots.Snapshot { |
| 39 | opts := snapshots.CreateOpts{VolumeID: volID, Name: "snapshot-001"} |
| 40 | s, err := snapshots.Create(client, opts).Extract() |
| 41 | th.AssertNoErr(t, err) |
| 42 | t.Logf("Created snapshot %s", s.ID) |
| 43 | |
| 44 | t.Logf("Waiting for new snapshot to become available...") |
| 45 | start := time.Now().Second() |
| 46 | s.WaitUntilComplete(client, -1) |
| 47 | t.Logf("Snapshot completed after %ds", time.Now().Second()-start) |
| 48 | |
| 49 | return s |
| 50 | } |
| 51 | |
| 52 | func testSnapshotList(t *testing.T, client *gophercloud.ServiceClient) { |
| 53 | snapshots.List(client).EachPage(func(page pagination.Page) (bool, error) { |
| 54 | sList, err := snapshots.ExtractSnapshots(page) |
| 55 | th.AssertNoErr(t, err) |
| 56 | |
| 57 | for _, s := range sList { |
| 58 | t.Logf("Snapshot: ID [%s] Name [%s] Volume ID [%s] Progress [%s] Created [%s]", |
| 59 | s.ID, s.Name, s.VolumeID, s.Progress, s.CreatedAt) |
| 60 | } |
| 61 | |
| 62 | return true, nil |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | func testSnapshotGet(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 67 | _, err := snapshots.Get(client, id).Extract() |
| 68 | th.AssertNoErr(t, err) |
| 69 | } |
| 70 | |
| 71 | func testSnapshotUpdate(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 72 | _, err := snapshots.Update(client, id, snapshots.UpdateOpts{Name: "new_name"}).Extract() |
| 73 | th.AssertNoErr(t, err) |
| 74 | } |
| 75 | |
| 76 | func testSnapshotDelete(t *testing.T, client *gophercloud.ServiceClient, id string) { |
| 77 | err := snapshots.Delete(client, id) |
| 78 | th.AssertNoErr(t, err) |
| 79 | t.Logf("Deleted snapshot %s", id) |
| 80 | } |