blob: 25b2cfeeeb22e3336dde4313b415c2508d832495 [file] [log] [blame]
Jamie Hannaford4c2a1f22014-10-23 17:31:55 +02001// +build acceptance blockstorage snapshots
2
Jamie Hannafordaf209d12014-10-22 12:37:48 +02003package v1
Jamie Hannaford48a80ce2014-10-23 14:49:51 +02004
5import (
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
15func 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
40func 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
54func 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
68func testSnapshotGet(t *testing.T, client *gophercloud.ServiceClient, id string) {
69 _, err := snapshots.Get(client, id).Extract()
70 th.AssertNoErr(t, err)
71}
72
73func 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
78func testSnapshotDelete(t *testing.T, client *gophercloud.ServiceClient, id string) {
Jamie Hannaforde4e6dfe2014-10-27 11:50:29 +010079 res := snapshots.Delete(client, id)
80 th.AssertNoErr(t, res.Err)
Jamie Hannaford48a80ce2014-10-23 14:49:51 +020081 t.Logf("Deleted snapshot %s", id)
82}