File: snapshot_test.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 2,315 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// +build acceptance blockstorage snapshots

package v1

import (
	"testing"
	"time"

	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/pagination"
	"github.com/rackspace/gophercloud/rackspace/blockstorage/v1/snapshots"
	th "github.com/rackspace/gophercloud/testhelper"
)

func TestSnapshots(t *testing.T) {
	client := setup(t)
	volID := testVolumeCreate(t, client)

	t.Log("Creating snapshots")
	s := testSnapshotCreate(t, client, volID)
	id := s.ID

	t.Log("Listing snapshots")
	testSnapshotList(t, client)

	t.Logf("Getting snapshot %s", id)
	testSnapshotGet(t, client, id)

	t.Logf("Updating snapshot %s", id)
	testSnapshotUpdate(t, client, id)

	t.Logf("Deleting snapshot %s", id)
	testSnapshotDelete(t, client, id)
	s.WaitUntilDeleted(client, -1)

	t.Logf("Deleting volume %s", volID)
	testVolumeDelete(t, client, volID)
}

func testSnapshotCreate(t *testing.T, client *gophercloud.ServiceClient, volID string) *snapshots.Snapshot {
	opts := snapshots.CreateOpts{VolumeID: volID, Name: "snapshot-001"}
	s, err := snapshots.Create(client, opts).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Created snapshot %s", s.ID)

	t.Logf("Waiting for new snapshot to become available...")
	start := time.Now().Second()
	s.WaitUntilComplete(client, -1)
	t.Logf("Snapshot completed after %ds", time.Now().Second()-start)

	return s
}

func testSnapshotList(t *testing.T, client *gophercloud.ServiceClient) {
	snapshots.List(client).EachPage(func(page pagination.Page) (bool, error) {
		sList, err := snapshots.ExtractSnapshots(page)
		th.AssertNoErr(t, err)

		for _, s := range sList {
			t.Logf("Snapshot: ID [%s] Name [%s] Volume ID [%s] Progress [%s] Created [%s]",
				s.ID, s.Name, s.VolumeID, s.Progress, s.CreatedAt)
		}

		return true, nil
	})
}

func testSnapshotGet(t *testing.T, client *gophercloud.ServiceClient, id string) {
	_, err := snapshots.Get(client, id).Extract()
	th.AssertNoErr(t, err)
}

func testSnapshotUpdate(t *testing.T, client *gophercloud.ServiceClient, id string) {
	_, err := snapshots.Update(client, id, snapshots.UpdateOpts{Name: "new_name"}).Extract()
	th.AssertNoErr(t, err)
}

func testSnapshotDelete(t *testing.T, client *gophercloud.ServiceClient, id string) {
	res := snapshots.Delete(client, id)
	th.AssertNoErr(t, res.Err)
	t.Logf("Deleted snapshot %s", id)
}