blob: 003d347c073baf26053cf3cdcfa6a843d063a35c [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
111func 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 Hannaford51175a02014-11-03 13:29:44 +0100122
123func 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 Hannaford00222d72014-11-03 13:58:52 +0100144
145func TestUpdate(t *testing.T) {
146 th.SetupHTTP()
147 defer th.TeardownHTTP()
148
149 mockUpdateResponse(t, lbID, nodeID)
150
151 opts := UpdateOpts{
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100152 Weight: gophercloud.IntToPointer(10),
Jamie Hannaford00222d72014-11-03 13:58:52 +0100153 Condition: DRAINING,
154 Type: SECONDARY,
155 }
156
157 err := Update(client.ServiceClient(), lbID, nodeID, opts).ExtractErr()
158 th.AssertNoErr(t, err)
159}
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100160
161func 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 Hannaford1fac9dd2014-11-03 14:22:40 +0100170
171func TestListEvents(t *testing.T) {
172 th.SetupHTTP()
173 defer th.TeardownHTTP()
174
Jamie Hannaford703527e2014-11-05 12:38:15 +0100175 mockListEventsResponse(t, lbID)
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100176
177 count := 0
178
Jamie Hannaford703527e2014-11-05 12:38:15 +0100179 pager := ListEvents(client.ServiceClient(), lbID, ListEventsOpts{})
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100180
181 err := pager.EachPage(func(page pagination.Page) (bool, error) {
Jamie Hannaford1fac9dd2014-11-03 14:22:40 +0100182 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}