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