blob: 1a02b465279d3f5b28718b7e11b22c21fbcfcf53 [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
Jamie Hannaford8ad8c592014-10-23 16:58:27 +020033 err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020034 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
Jamie Hannaford8ad8c592014-10-23 16:58:27 +020057 th.AssertEquals(t, 1, count)
58 th.AssertNoErr(t, err)
Jamie Hannafordff08ef92014-10-20 16:10:12 +020059}
60
61func TestGet(t *testing.T) {
62 th.SetupHTTP()
63 defer th.TeardownHTTP()
64
65 os.MockGetResponse(t)
66
67 v, err := Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
68 th.AssertNoErr(t, err)
69
70 th.AssertEquals(t, v.Name, "snapshot-001")
71 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
72}
73
74func TestCreate(t *testing.T) {
75 th.SetupHTTP()
76 defer th.TeardownHTTP()
77
78 os.MockCreateResponse(t)
79
80 options := &CreateOpts{VolumeID: "1234", Name: "snapshot-001"}
81 n, err := Create(fake.ServiceClient(), options).Extract()
82 th.AssertNoErr(t, err)
83
84 th.AssertEquals(t, n.VolumeID, "1234")
85 th.AssertEquals(t, n.Name, "snapshot-001")
86 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
87}
88
89func TestDelete(t *testing.T) {
90 th.SetupHTTP()
91 defer th.TeardownHTTP()
92
93 os.MockDeleteResponse(t)
94
Jamie Hannafordc7589632014-10-27 11:39:17 +010095 res := Delete(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
96 th.AssertNoErr(t, res.Err)
Jamie Hannafordff08ef92014-10-20 16:10:12 +020097}