blob: 8db55d98aedf3bec0504a168bbc0814ccba837ad [file] [log] [blame]
Jon Perritte747a0f2014-09-29 19:54:55 -05001package snapshots
Jon Perritt6d5561b2014-10-01 21:42:15 -05002
3import (
Jon Perritt6d5561b2014-10-01 21:42:15 -05004 "testing"
5
Jon Perritt6d5561b2014-10-01 21:42:15 -05006 "github.com/rackspace/gophercloud/pagination"
7 th "github.com/rackspace/gophercloud/testhelper"
Ash Wilson407cfa32014-10-22 09:21:37 -04008 "github.com/rackspace/gophercloud/testhelper/client"
Jon Perritt6d5561b2014-10-01 21:42:15 -05009)
10
Jon Perritt6d5561b2014-10-01 21:42:15 -050011func TestList(t *testing.T) {
12 th.SetupHTTP()
13 defer th.TeardownHTTP()
14
Jamie Hannaford96c666d2014-10-20 16:09:10 +020015 MockListResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050016
Jon Perritt6d5561b2014-10-01 21:42:15 -050017 count := 0
Jamie Hannaford96c666d2014-10-20 16:09:10 +020018
Ash Wilson407cfa32014-10-22 09:21:37 -040019 List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt6d5561b2014-10-01 21:42:15 -050020 count++
21 actual, err := ExtractSnapshots(page)
22 if err != nil {
23 t.Errorf("Failed to extract snapshots: %v", err)
24 return false, err
25 }
26
27 expected := []Snapshot{
28 Snapshot{
29 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
30 Name: "snapshot-001",
31 },
32 Snapshot{
33 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
34 Name: "snapshot-002",
35 },
36 }
37
38 th.CheckDeepEquals(t, expected, actual)
39
40 return true, nil
41 })
42
43 if count != 1 {
44 t.Errorf("Expected 1 page, got %d", count)
45 }
46}
47
48func TestGet(t *testing.T) {
49 th.SetupHTTP()
50 defer th.TeardownHTTP()
51
Jamie Hannaford96c666d2014-10-20 16:09:10 +020052 MockGetResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050053
Ash Wilson407cfa32014-10-22 09:21:37 -040054 v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050055 th.AssertNoErr(t, err)
56
57 th.AssertEquals(t, v.Name, "snapshot-001")
58 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
59}
60
61func TestCreate(t *testing.T) {
62 th.SetupHTTP()
63 defer th.TeardownHTTP()
64
Jamie Hannaford96c666d2014-10-20 16:09:10 +020065 MockCreateResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -050066
Jamie Hannaford96c666d2014-10-20 16:09:10 +020067 options := CreateOpts{VolumeID: "1234", Name: "snapshot-001"}
Ash Wilson407cfa32014-10-22 09:21:37 -040068 n, err := Create(client.ServiceClient(), options).Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050069 th.AssertNoErr(t, err)
70
Jon Perritt1c2356b2014-10-13 19:56:43 -050071 th.AssertEquals(t, n.VolumeID, "1234")
Jon Perritt6d5561b2014-10-01 21:42:15 -050072 th.AssertEquals(t, n.Name, "snapshot-001")
73 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
74}
75
Jon Perritte357e3d2014-10-03 01:53:57 -050076func TestUpdateMetadata(t *testing.T) {
77 th.SetupHTTP()
78 defer th.TeardownHTTP()
79
Jamie Hannaford96c666d2014-10-20 16:09:10 +020080 MockUpdateMetadataResponse(t)
Jon Perritte357e3d2014-10-03 01:53:57 -050081
82 expected := map[string]interface{}{"key": "v1"}
83
84 options := &UpdateMetadataOpts{
85 Metadata: map[string]interface{}{
86 "key": "v1",
87 },
88 }
Jamie Hannaford96c666d2014-10-20 16:09:10 +020089
Ash Wilson407cfa32014-10-22 09:21:37 -040090 actual, err := UpdateMetadata(client.ServiceClient(), "123", options).ExtractMetadata()
Jon Perritte357e3d2014-10-03 01:53:57 -050091
92 th.AssertNoErr(t, err)
93 th.AssertDeepEquals(t, actual, expected)
94}
95
Jon Perritt6d5561b2014-10-01 21:42:15 -050096func TestDelete(t *testing.T) {
97 th.SetupHTTP()
98 defer th.TeardownHTTP()
99
Jamie Hannaford96c666d2014-10-20 16:09:10 +0200100 MockDeleteResponse(t)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500101
Ash Wilson407cfa32014-10-22 09:21:37 -0400102 err := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
Jon Perritt57ba7632014-10-02 20:32:22 -0500103 th.AssertNoErr(t, err)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500104}