Jon Perritt | e747a0f | 2014-09-29 19:54:55 -0500 | [diff] [blame] | 1 | package volumes |
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" |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 5 | "time" |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 6 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 7 | "github.com/gophercloud/gophercloud" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | fixtures "github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes/testing" |
| 9 | "github.com/gophercloud/gophercloud/pagination" |
| 10 | th "github.com/gophercloud/gophercloud/testhelper" |
| 11 | "github.com/gophercloud/gophercloud/testhelper/client" |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 12 | ) |
| 13 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 14 | func TestList(t *testing.T) { |
| 15 | th.SetupHTTP() |
| 16 | defer th.TeardownHTTP() |
| 17 | |
Sreekanth Pothanis | 07400f3 | 2015-09-08 00:26:14 -0700 | [diff] [blame] | 18 | fixtures.MockListResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 19 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 20 | count := 0 |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 21 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 22 | List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 23 | count++ |
| 24 | actual, err := ExtractVolumes(page) |
| 25 | if err != nil { |
| 26 | t.Errorf("Failed to extract volumes: %v", err) |
| 27 | return false, err |
| 28 | } |
| 29 | |
| 30 | expected := []Volume{ |
| 31 | Volume{ |
| 32 | ID: "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 33 | Name: "vol-001", |
| 34 | }, |
| 35 | Volume{ |
| 36 | ID: "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 37 | Name: "vol-002", |
| 38 | }, |
| 39 | } |
| 40 | |
| 41 | th.CheckDeepEquals(t, expected, actual) |
| 42 | |
| 43 | return true, nil |
| 44 | }) |
| 45 | |
| 46 | if count != 1 { |
| 47 | t.Errorf("Expected 1 page, got %d", count) |
| 48 | } |
| 49 | } |
| 50 | |
Jon Perritt | e7017d6 | 2015-02-18 10:53:53 -0700 | [diff] [blame] | 51 | func TestListAll(t *testing.T) { |
| 52 | th.SetupHTTP() |
| 53 | defer th.TeardownHTTP() |
| 54 | |
Sreekanth Pothanis | 07400f3 | 2015-09-08 00:26:14 -0700 | [diff] [blame] | 55 | fixtures.MockListResponse(t) |
Jon Perritt | e7017d6 | 2015-02-18 10:53:53 -0700 | [diff] [blame] | 56 | |
| 57 | allPages, err := List(client.ServiceClient(), &ListOpts{}).AllPages() |
| 58 | th.AssertNoErr(t, err) |
| 59 | actual, err := ExtractVolumes(allPages) |
| 60 | th.AssertNoErr(t, err) |
| 61 | |
| 62 | expected := []Volume{ |
| 63 | Volume{ |
| 64 | ID: "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 65 | Name: "vol-001", |
| 66 | }, |
| 67 | Volume{ |
| 68 | ID: "96c3bda7-c82a-4f50-be73-ca7621794835", |
| 69 | Name: "vol-002", |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | th.CheckDeepEquals(t, expected, actual) |
| 74 | |
| 75 | } |
| 76 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 77 | func TestGet(t *testing.T) { |
| 78 | th.SetupHTTP() |
| 79 | defer th.TeardownHTTP() |
| 80 | |
Sreekanth Pothanis | 07400f3 | 2015-09-08 00:26:14 -0700 | [diff] [blame] | 81 | fixtures.MockGetResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 82 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 83 | actual, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 84 | th.AssertNoErr(t, err) |
| 85 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 86 | expected := &Volume{ |
| 87 | Status: "active", |
| 88 | Name: "vol-001", |
| 89 | Attachments: []map[string]interface{}{ |
| 90 | { |
| 91 | "attachment_id": "03987cd1-0ad5-40d1-9b2a-7cc48295d4fa", |
| 92 | "id": "47e9ecc5-4045-4ee3-9a4b-d859d546a0cf", |
| 93 | "volume_id": "6c80f8ac-e3e2-480c-8e6e-f1db92fe4bfe", |
| 94 | "server_id": "d1c4788b-9435-42e2-9b81-29f3be1cd01f", |
| 95 | "host_name": "mitaka", |
| 96 | "device": "/", |
| 97 | }, |
| 98 | }, |
| 99 | AvailabilityZone: "us-east1", |
| 100 | Bootable: "false", |
| 101 | CreatedAt: gophercloud.JSONRFC3339Milli(time.Date(2012, 2, 14, 20, 53, 07, 0, time.UTC)), |
| 102 | Description: "Another volume.", |
| 103 | VolumeType: "289da7f8-6440-407c-9fb4-7db01ec49164", |
| 104 | SnapshotID: "", |
| 105 | SourceVolID: "", |
| 106 | Metadata: map[string]string{ |
| 107 | "contents": "junk", |
| 108 | }, |
| 109 | ID: "521752a6-acf6-4b2d-bc7a-119f9148cd8c", |
| 110 | Size: 30, |
| 111 | } |
| 112 | |
| 113 | th.AssertDeepEquals(t, expected, actual) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | func TestCreate(t *testing.T) { |
| 117 | th.SetupHTTP() |
| 118 | defer th.TeardownHTTP() |
| 119 | |
Sreekanth Pothanis | 07400f3 | 2015-09-08 00:26:14 -0700 | [diff] [blame] | 120 | fixtures.MockCreateResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 121 | |
Jamie Hannaford | 59f2207 | 2014-10-23 17:00:59 +0200 | [diff] [blame] | 122 | options := &CreateOpts{Size: 75} |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 123 | n, err := Create(client.ServiceClient(), options).Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 124 | th.AssertNoErr(t, err) |
| 125 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 126 | th.AssertEquals(t, n.Size, 4) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 127 | th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 128 | } |
| 129 | |
| 130 | func TestDelete(t *testing.T) { |
| 131 | th.SetupHTTP() |
| 132 | defer th.TeardownHTTP() |
| 133 | |
Sreekanth Pothanis | 07400f3 | 2015-09-08 00:26:14 -0700 | [diff] [blame] | 134 | fixtures.MockDeleteResponse(t) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 135 | |
Jamie Hannaford | ce9f908 | 2014-10-27 11:27:12 +0100 | [diff] [blame] | 136 | res := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 137 | th.AssertNoErr(t, res.Err) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 138 | } |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 139 | |
| 140 | func TestUpdate(t *testing.T) { |
| 141 | th.SetupHTTP() |
| 142 | defer th.TeardownHTTP() |
| 143 | |
Sreekanth Pothanis | 07400f3 | 2015-09-08 00:26:14 -0700 | [diff] [blame] | 144 | fixtures.MockUpdateResponse(t) |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 145 | |
Jamie Hannaford | 449c4ac | 2014-10-21 09:58:02 +0200 | [diff] [blame] | 146 | options := UpdateOpts{Name: "vol-002"} |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 147 | v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 148 | th.AssertNoErr(t, err) |
| 149 | th.CheckEquals(t, "vol-002", v.Name) |
| 150 | } |