blob: 5fdeadd416328d8347c84912f4c03cdaa5b415d0 [file] [log] [blame]
Jamie Hannafordb6927c12014-11-03 10:31:26 +01001package nodes
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +01002
3import (
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
11const (
Jamie Hannaford16bebfc2014-11-03 12:52:30 +010012 lbID = 12345
13 nodeID = 67890
14 nodeID2 = 67891
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010015)
16
17func 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 Hannaforded8b89a2014-11-03 12:24:19 +010059
60func 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 Hannaford16bebfc2014-11-03 12:52:30 +0100109
110func 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 Hannaford51175a02014-11-03 13:29:44 +0100121
122func 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}