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