Jamie Hannaford | b6927c1 | 2014-11-03 10:31:26 +0100 | [diff] [blame] | 1 | package nodes |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | th "github.com/rackspace/gophercloud/testhelper" |
| 9 | "github.com/rackspace/gophercloud/testhelper/client" |
| 10 | ) |
| 11 | |
| 12 | const ( |
Jamie Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 13 | lbID = 12345 |
| 14 | nodeID = 67890 |
| 15 | nodeID2 = 67891 |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | func TestList(t *testing.T) { |
| 19 | th.SetupHTTP() |
| 20 | defer th.TeardownHTTP() |
| 21 | |
| 22 | mockListResponse(t, lbID) |
| 23 | |
| 24 | count := 0 |
| 25 | |
| 26 | err := List(client.ServiceClient(), lbID, nil).EachPage(func(page pagination.Page) (bool, error) { |
| 27 | count++ |
| 28 | actual, err := ExtractNodes(page) |
| 29 | th.AssertNoErr(t, err) |
| 30 | |
| 31 | expected := []Node{ |
| 32 | Node{ |
| 33 | ID: 410, |
| 34 | Address: "10.1.1.1", |
| 35 | Port: 80, |
| 36 | Condition: ENABLED, |
| 37 | Status: ONLINE, |
| 38 | Weight: 3, |
| 39 | Type: PRIMARY, |
| 40 | }, |
| 41 | Node{ |
| 42 | ID: 411, |
| 43 | Address: "10.1.1.2", |
| 44 | Port: 80, |
| 45 | Condition: ENABLED, |
| 46 | Status: ONLINE, |
| 47 | Weight: 8, |
| 48 | Type: SECONDARY, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | th.CheckDeepEquals(t, expected, actual) |
| 53 | |
| 54 | return true, nil |
| 55 | }) |
| 56 | |
| 57 | th.AssertNoErr(t, err) |
| 58 | th.AssertEquals(t, 1, count) |
| 59 | } |
Jamie Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 60 | |
| 61 | func TestCreate(t *testing.T) { |
| 62 | th.SetupHTTP() |
| 63 | defer th.TeardownHTTP() |
| 64 | |
| 65 | mockCreateResponse(t, lbID) |
| 66 | |
| 67 | opts := CreateOpts{ |
| 68 | CreateOpt{ |
| 69 | Address: "10.2.2.3", |
| 70 | Port: 80, |
| 71 | Condition: ENABLED, |
| 72 | Type: PRIMARY, |
| 73 | }, |
| 74 | CreateOpt{ |
| 75 | Address: "10.2.2.4", |
| 76 | Port: 81, |
| 77 | Condition: ENABLED, |
| 78 | Type: SECONDARY, |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | page := Create(client.ServiceClient(), lbID, opts) |
| 83 | |
| 84 | actual, err := page.ExtractNodes() |
| 85 | th.AssertNoErr(t, err) |
| 86 | |
| 87 | expected := []Node{ |
| 88 | Node{ |
| 89 | ID: 185, |
| 90 | Address: "10.2.2.3", |
| 91 | Port: 80, |
| 92 | Condition: ENABLED, |
| 93 | Status: ONLINE, |
| 94 | Weight: 1, |
| 95 | Type: PRIMARY, |
| 96 | }, |
| 97 | Node{ |
| 98 | ID: 186, |
| 99 | Address: "10.2.2.4", |
| 100 | Port: 81, |
| 101 | Condition: ENABLED, |
| 102 | Status: ONLINE, |
| 103 | Weight: 1, |
| 104 | Type: SECONDARY, |
| 105 | }, |
| 106 | } |
| 107 | |
| 108 | th.CheckDeepEquals(t, expected, actual) |
| 109 | } |
Jamie Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 110 | |
| 111 | func TestBulkDelete(t *testing.T) { |
| 112 | th.SetupHTTP() |
| 113 | defer th.TeardownHTTP() |
| 114 | |
| 115 | ids := []int{nodeID, nodeID2} |
| 116 | |
| 117 | mockBatchDeleteResponse(t, lbID, ids) |
| 118 | |
| 119 | err := BulkDelete(client.ServiceClient(), lbID, ids).ExtractErr() |
| 120 | th.AssertNoErr(t, err) |
| 121 | } |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 122 | |
| 123 | func TestGet(t *testing.T) { |
| 124 | th.SetupHTTP() |
| 125 | defer th.TeardownHTTP() |
| 126 | |
| 127 | mockGetResponse(t, lbID, nodeID) |
| 128 | |
| 129 | node, err := Get(client.ServiceClient(), lbID, nodeID).Extract() |
| 130 | th.AssertNoErr(t, err) |
| 131 | |
| 132 | expected := &Node{ |
| 133 | ID: 410, |
| 134 | Address: "10.1.1.1", |
| 135 | Port: 80, |
| 136 | Condition: ENABLED, |
| 137 | Status: ONLINE, |
| 138 | Weight: 12, |
| 139 | Type: PRIMARY, |
| 140 | } |
| 141 | |
| 142 | th.AssertDeepEquals(t, expected, node) |
| 143 | } |
Jamie Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 144 | |
| 145 | func TestUpdate(t *testing.T) { |
| 146 | th.SetupHTTP() |
| 147 | defer th.TeardownHTTP() |
| 148 | |
| 149 | mockUpdateResponse(t, lbID, nodeID) |
| 150 | |
| 151 | opts := UpdateOpts{ |
| 152 | Address: "1.2.3.4", |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame^] | 153 | Weight: gophercloud.IntToPointer(10), |
Jamie Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 154 | Condition: DRAINING, |
| 155 | Type: SECONDARY, |
| 156 | } |
| 157 | |
| 158 | err := Update(client.ServiceClient(), lbID, nodeID, opts).ExtractErr() |
| 159 | th.AssertNoErr(t, err) |
| 160 | } |
Jamie Hannaford | 9f4870f | 2014-11-03 14:03:16 +0100 | [diff] [blame] | 161 | |
| 162 | func TestDelete(t *testing.T) { |
| 163 | th.SetupHTTP() |
| 164 | defer th.TeardownHTTP() |
| 165 | |
| 166 | mockDeleteResponse(t, lbID, nodeID) |
| 167 | |
| 168 | err := Delete(client.ServiceClient(), lbID, nodeID).ExtractErr() |
| 169 | th.AssertNoErr(t, err) |
| 170 | } |
Jamie Hannaford | 1fac9dd | 2014-11-03 14:22:40 +0100 | [diff] [blame] | 171 | |
| 172 | func TestListEvents(t *testing.T) { |
| 173 | th.SetupHTTP() |
| 174 | defer th.TeardownHTTP() |
| 175 | |
Jamie Hannaford | 703527e | 2014-11-05 12:38:15 +0100 | [diff] [blame] | 176 | mockListEventsResponse(t, lbID) |
Jamie Hannaford | 1fac9dd | 2014-11-03 14:22:40 +0100 | [diff] [blame] | 177 | |
| 178 | count := 0 |
| 179 | |
Jamie Hannaford | 703527e | 2014-11-05 12:38:15 +0100 | [diff] [blame] | 180 | pager := ListEvents(client.ServiceClient(), lbID, ListEventsOpts{}) |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 181 | |
| 182 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
Jamie Hannaford | 1fac9dd | 2014-11-03 14:22:40 +0100 | [diff] [blame] | 183 | count++ |
| 184 | actual, err := ExtractNodeEvents(page) |
| 185 | th.AssertNoErr(t, err) |
| 186 | |
| 187 | expected := []NodeEvent{ |
| 188 | NodeEvent{ |
| 189 | DetailedMessage: "Node is ok", |
| 190 | NodeID: 373, |
| 191 | ID: 7, |
| 192 | Type: "UPDATE_NODE", |
| 193 | Description: "Node '373' status changed to 'ONLINE' for load balancer '323'", |
| 194 | Category: "UPDATE", |
| 195 | Severity: "INFO", |
| 196 | RelativeURI: "/406271/loadbalancers/323/nodes/373/events", |
| 197 | AccountID: 406271, |
| 198 | LoadBalancerID: 323, |
| 199 | Title: "Node Status Updated", |
| 200 | Author: "Rackspace Cloud", |
| 201 | Created: "10-30-2012 10:18:23", |
| 202 | }, |
| 203 | } |
| 204 | |
| 205 | th.CheckDeepEquals(t, expected, actual) |
| 206 | |
| 207 | return true, nil |
| 208 | }) |
| 209 | |
| 210 | th.AssertNoErr(t, err) |
| 211 | th.AssertEquals(t, 1, count) |
| 212 | } |