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