blob: a964af8f90d45b6feb6a98e93e167b35945dbba7 [file] [log] [blame]
Jamie Hannafordb6927c12014-11-03 10:31:26 +01001package nodes
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01002
3import (
4 "testing"
5
Jamie Hannafordcfe2f282014-11-07 15:11:21 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01007 "github.com/rackspace/gophercloud/pagination"
8 th "github.com/rackspace/gophercloud/testhelper"
9 "github.com/rackspace/gophercloud/testhelper/client"
10)
11
12const (
Jamie Hannaford16bebfc2014-11-03 12:52:30 +010013 lbID = 12345
14 nodeID = 67890
15 nodeID2 = 67891
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010016)
17
18func 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 Hannaforded8b89a2014-11-03 12:24:19 +010060
61func 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 Hannaford16bebfc2014-11-03 12:52:30 +0100110
Ash Wilsonae551432015-06-24 09:40:40 -0400111func TestCreateErr(t *testing.T) {
112 th.SetupHTTP()
113 defer th.TeardownHTTP()
114
115 mockCreateErrResponse(t, lbID)
116
117 opts := CreateOpts{
118 CreateOpt{
119 Address: "10.2.2.3",
120 Port: 80,
121 Condition: ENABLED,
122 Type: PRIMARY,
123 },
124 CreateOpt{
125 Address: "10.2.2.4",
126 Port: 81,
127 Condition: ENABLED,
128 Type: SECONDARY,
129 },
130 }
131
132 page := Create(client.ServiceClient(), lbID, opts)
133
134 actual, err := page.ExtractNodes()
135 if err == nil {
136 t.Fatal("Did not receive expected error from ExtractNodes")
137 }
138 if actual != nil {
139 t.Fatalf("Received non-nil result from failed ExtractNodes: %#v", actual)
140 }
141}
142
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100143func TestBulkDelete(t *testing.T) {
144 th.SetupHTTP()
145 defer th.TeardownHTTP()
146
147 ids := []int{nodeID, nodeID2}
148
149 mockBatchDeleteResponse(t, lbID, ids)
150
151 err := BulkDelete(client.ServiceClient(), lbID, ids).ExtractErr()
152 th.AssertNoErr(t, err)
153}
Jamie Hannaford51175a02014-11-03 13:29:44 +0100154
155func TestGet(t *testing.T) {
156 th.SetupHTTP()
157 defer th.TeardownHTTP()
158
159 mockGetResponse(t, lbID, nodeID)
160
161 node, err := Get(client.ServiceClient(), lbID, nodeID).Extract()
162 th.AssertNoErr(t, err)
163
164 expected := &Node{
165 ID: 410,
166 Address: "10.1.1.1",
167 Port: 80,
168 Condition: ENABLED,
169 Status: ONLINE,
170 Weight: 12,
171 Type: PRIMARY,
172 }
173
174 th.AssertDeepEquals(t, expected, node)
175}
Jamie Hannaford00222d72014-11-03 13:58:52 +0100176
177func TestUpdate(t *testing.T) {
178 th.SetupHTTP()
179 defer th.TeardownHTTP()
180
181 mockUpdateResponse(t, lbID, nodeID)
182
183 opts := UpdateOpts{
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100184 Weight: gophercloud.IntToPointer(10),
Jamie Hannaford00222d72014-11-03 13:58:52 +0100185 Condition: DRAINING,
186 Type: SECONDARY,
187 }
188
189 err := Update(client.ServiceClient(), lbID, nodeID, opts).ExtractErr()
190 th.AssertNoErr(t, err)
191}
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100192
193func TestDelete(t *testing.T) {
194 th.SetupHTTP()
195 defer th.TeardownHTTP()
196
197 mockDeleteResponse(t, lbID, nodeID)
198
199 err := Delete(client.ServiceClient(), lbID, nodeID).ExtractErr()
200 th.AssertNoErr(t, err)
201}
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100202
203func TestListEvents(t *testing.T) {
204 th.SetupHTTP()
205 defer th.TeardownHTTP()
206
Jamie Hannaford703527e2014-11-05 12:38:15 +0100207 mockListEventsResponse(t, lbID)
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100208
209 count := 0
210
Jamie Hannaford703527e2014-11-05 12:38:15 +0100211 pager := ListEvents(client.ServiceClient(), lbID, ListEventsOpts{})
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100212
213 err := pager.EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100214 count++
215 actual, err := ExtractNodeEvents(page)
216 th.AssertNoErr(t, err)
217
218 expected := []NodeEvent{
219 NodeEvent{
220 DetailedMessage: "Node is ok",
221 NodeID: 373,
222 ID: 7,
223 Type: "UPDATE_NODE",
224 Description: "Node '373' status changed to 'ONLINE' for load balancer '323'",
225 Category: "UPDATE",
226 Severity: "INFO",
227 RelativeURI: "/406271/loadbalancers/323/nodes/373/events",
228 AccountID: 406271,
229 LoadBalancerID: 323,
230 Title: "Node Status Updated",
231 Author: "Rackspace Cloud",
232 Created: "10-30-2012 10:18:23",
233 },
234 }
235
236 th.CheckDeepEquals(t, expected, actual)
237
238 return true, nil
239 })
240
241 th.AssertNoErr(t, err)
242 th.AssertEquals(t, 1, count)
243}