blob: 45ab0ac8c87382d4835d4f2846563934c9ef1a7d [file] [log] [blame]
Jamie Hannafordff08ef92014-10-20 16:10:12 +02001package snapshots
2
3import (
4 "testing"
5
6 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
7 "github.com/rackspace/gophercloud/pagination"
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10)
11
12func TestList(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15
16 os.MockListResponse(t)
17
18 count := 0
19
20 List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
21 count++
22 actual, err := ExtractSnapshots(page)
23 if err != nil {
24 t.Errorf("Failed to extract snapshots: %v", err)
25 return false, err
26 }
27
28 expected := []Snapshot{
29 Snapshot{
30 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
31 Name: "snapshot-001",
32 },
33 Snapshot{
34 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
35 Name: "snapshot-002",
36 },
37 }
38
39 th.CheckDeepEquals(t, expected, actual)
40
41 return true, nil
42 })
43
44 if count != 1 {
45 t.Errorf("Expected 1 page, got %d", count)
46 }
47}
48
49func TestGet(t *testing.T) {
50 th.SetupHTTP()
51 defer th.TeardownHTTP()
52
53 os.MockGetResponse(t)
54
55 v, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
56 th.AssertNoErr(t, err)
57
58 th.AssertEquals(t, v.Name, "snapshot-001")
59 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
60}
61
62func TestCreate(t *testing.T) {
63 th.SetupHTTP()
64 defer th.TeardownHTTP()
65
66 os.MockCreateResponse(t)
67
68 options := &CreateOpts{VolumeID: "1234", Name: "snapshot-001"}
69 n, err := Create(fake.ServiceClient(), options).Extract()
70 th.AssertNoErr(t, err)
71
72 th.AssertEquals(t, n.VolumeID, "1234")
73 th.AssertEquals(t, n.Name, "snapshot-001")
74 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
75}
76
77func TestDelete(t *testing.T) {
78 th.SetupHTTP()
79 defer th.TeardownHTTP()
80
81 os.MockDeleteResponse(t)
82
83 err := Delete(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
84 th.AssertNoErr(t, err)
85}