Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 1 | package snapshots |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
Jamie Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 7 | 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 Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 13 | const endpoint = "http://localhost:57909/v1/12345" |
| 14 | |
| 15 | func endpointClient() *gophercloud.ServiceClient { |
| 16 | return &gophercloud.ServiceClient{Endpoint: endpoint} |
| 17 | } |
| 18 | |
| 19 | func TestUpdateURL(t *testing.T) { |
| 20 | actual := updateURL(endpointClient(), "foo") |
| 21 | expected := endpoint + "snapshots/foo" |
| 22 | th.AssertEquals(t, expected, actual) |
| 23 | } |
| 24 | |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 25 | func TestList(t *testing.T) { |
| 26 | th.SetupHTTP() |
| 27 | defer th.TeardownHTTP() |
| 28 | |
| 29 | os.MockListResponse(t) |
| 30 | |
| 31 | count := 0 |
| 32 | |
Jamie Hannaford | 8ad8c59 | 2014-10-23 16:58:27 +0200 | [diff] [blame] | 33 | err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 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 | |
Jamie Hannaford | 8ad8c59 | 2014-10-23 16:58:27 +0200 | [diff] [blame] | 57 | th.AssertEquals(t, 1, count) |
| 58 | th.AssertNoErr(t, err) |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func 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 | |
| 74 | func 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 | |
| 89 | func TestDelete(t *testing.T) { |
| 90 | th.SetupHTTP() |
| 91 | defer th.TeardownHTTP() |
| 92 | |
| 93 | os.MockDeleteResponse(t) |
| 94 | |
Jamie Hannaford | c758963 | 2014-10-27 11:39:17 +0100 | [diff] [blame] | 95 | res := Delete(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 96 | th.AssertNoErr(t, res.Err) |
Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 97 | } |