Jon Perritt | e747a0f | 2014-09-29 19:54:55 -0500 | [diff] [blame] | 1 | package snapshots |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 4 | "testing" |
| 5 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | th "github.com/rackspace/gophercloud/testhelper" |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/testhelper/client" |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 9 | ) |
| 10 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 11 | func TestList(t *testing.T) { |
| 12 | th.SetupHTTP() |
| 13 | defer th.TeardownHTTP() |
| 14 | |
Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame^] | 15 | MockListResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 16 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 17 | count := 0 |
Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame^] | 18 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 19 | List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 20 | 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 | |
| 48 | func TestGet(t *testing.T) { |
| 49 | th.SetupHTTP() |
| 50 | defer th.TeardownHTTP() |
| 51 | |
Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame^] | 52 | MockGetResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 53 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 54 | v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 55 | 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 | |
| 61 | func TestCreate(t *testing.T) { |
| 62 | th.SetupHTTP() |
| 63 | defer th.TeardownHTTP() |
| 64 | |
Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame^] | 65 | MockCreateResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 66 | |
Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame^] | 67 | options := CreateOpts{VolumeID: "1234", Name: "snapshot-001"} |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 68 | n, err := Create(client.ServiceClient(), options).Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 69 | th.AssertNoErr(t, err) |
| 70 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 71 | th.AssertEquals(t, n.VolumeID, "1234") |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 72 | th.AssertEquals(t, n.Name, "snapshot-001") |
| 73 | th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 74 | } |
| 75 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 76 | func TestUpdateMetadata(t *testing.T) { |
| 77 | th.SetupHTTP() |
| 78 | defer th.TeardownHTTP() |
| 79 | |
Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame^] | 80 | MockUpdateMetadataResponse(t) |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 81 | |
| 82 | expected := map[string]interface{}{"key": "v1"} |
| 83 | |
| 84 | options := &UpdateMetadataOpts{ |
| 85 | Metadata: map[string]interface{}{ |
| 86 | "key": "v1", |
| 87 | }, |
| 88 | } |
Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame^] | 89 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 90 | actual, err := UpdateMetadata(client.ServiceClient(), "123", options).ExtractMetadata() |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 91 | |
| 92 | th.AssertNoErr(t, err) |
| 93 | th.AssertDeepEquals(t, actual, expected) |
| 94 | } |
| 95 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 96 | func TestDelete(t *testing.T) { |
| 97 | th.SetupHTTP() |
| 98 | defer th.TeardownHTTP() |
| 99 | |
Jamie Hannaford | 96c666d | 2014-10-20 16:09:10 +0200 | [diff] [blame^] | 100 | MockDeleteResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 101 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 102 | err := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 103 | th.AssertNoErr(t, err) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 104 | } |