blob: c67ddecad9332cb1d70efeb1f75e144ec402b5e8 [file] [log] [blame]
Jamie Hannafordbef53932014-11-05 12:39:30 +01001// +build acceptance lbs
2
3package v1
4
5import (
Jamie Hannaford82ce9162014-11-10 12:18:35 +01006 "strconv"
Jamie Hannafordbef53932014-11-05 12:39:30 +01007 "testing"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/acceptance/tools"
11 "github.com/rackspace/gophercloud/pagination"
12 "github.com/rackspace/gophercloud/rackspace/lb/v1/lbs"
13 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
14 th "github.com/rackspace/gophercloud/testhelper"
15)
16
17func TestLBs(t *testing.T) {
Jamie Hannafordbef53932014-11-05 12:39:30 +010018 client := setup(t)
19
20 ids := createLB(t, client, 3)
Jamie Hannaford82ce9162014-11-10 12:18:35 +010021 id := ids[0]
Jamie Hannafordbef53932014-11-05 12:39:30 +010022
23 listLBProtocols(t, client)
24
25 listLBAlgorithms(t, client)
26
27 listLBs(t, client)
28
Jamie Hannaford82ce9162014-11-10 12:18:35 +010029 getLB(t, client, id)
Jamie Hannafordbef53932014-11-05 12:39:30 +010030
Jamie Hannaford82ce9162014-11-10 12:18:35 +010031 checkLBLogging(t, client, id)
Jamie Hannafordbef53932014-11-05 12:39:30 +010032
Jamie Hannaford82ce9162014-11-10 12:18:35 +010033 checkErrorPage(t, client, id)
34
35 getStats(t, client, id)
36
37 updateLB(t, client, id)
38
39 deleteLB(t, client, id)
Jamie Hannafordbef53932014-11-05 12:39:30 +010040
41 batchDeleteLBs(t, client, ids[1:])
42}
43
44func createLB(t *testing.T, client *gophercloud.ServiceClient, count int) []int {
45 ids := []int{}
46
47 for i := 0; i < count; i++ {
48 opts := lbs.CreateOpts{
49 Name: tools.RandomString("test_", 5),
50 Port: 80,
51 Protocol: "HTTP",
52 VIPs: []vips.VIP{
53 vips.VIP{Type: vips.PUBLIC},
54 },
55 }
56
57 lb, err := lbs.Create(client, opts).Extract()
58 th.AssertNoErr(t, err)
59
60 t.Logf("Created LB %d - waiting for it to build...", lb.ID)
61 waitForLB(client, lb.ID, lbs.ACTIVE)
62 t.Logf("LB %d has reached ACTIVE state", lb.ID)
63
64 ids = append(ids, lb.ID)
65 }
66
67 return ids
68}
69
70func waitForLB(client *gophercloud.ServiceClient, id int, state lbs.Status) {
71 gophercloud.WaitFor(60, func() (bool, error) {
72 lb, err := lbs.Get(client, id).Extract()
73 if err != nil {
74 return false, err
75 }
76 if lb.Status != state {
77 return false, nil
78 }
79 return true, nil
80 })
81}
82
83func listLBProtocols(t *testing.T, client *gophercloud.ServiceClient) {
84 err := lbs.ListProtocols(client).EachPage(func(page pagination.Page) (bool, error) {
85 pList, err := lbs.ExtractProtocols(page)
86 th.AssertNoErr(t, err)
87
88 for _, p := range pList {
89 t.Logf("Listing protocol: Name [%s]", p.Name)
90 }
91
92 return true, nil
93 })
94 th.AssertNoErr(t, err)
95}
96
97func listLBAlgorithms(t *testing.T, client *gophercloud.ServiceClient) {
98 err := lbs.ListAlgorithms(client).EachPage(func(page pagination.Page) (bool, error) {
99 aList, err := lbs.ExtractAlgorithms(page)
100 th.AssertNoErr(t, err)
101
102 for _, a := range aList {
103 t.Logf("Listing algorithm: Name [%s]", a.Name)
104 }
105
106 return true, nil
107 })
108 th.AssertNoErr(t, err)
109}
110
111func listLBs(t *testing.T, client *gophercloud.ServiceClient) {
112 err := lbs.List(client, lbs.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
113 lbList, err := lbs.ExtractLBs(page)
114 th.AssertNoErr(t, err)
115
116 for _, lb := range lbList {
117 t.Logf("Listing LB: ID [%d] Name [%s] Protocol [%s] Status [%s] Node count [%d] Port [%d]",
118 lb.ID, lb.Name, lb.Protocol, lb.Status, lb.NodeCount, lb.Port)
119 }
120
121 return true, nil
122 })
123
124 th.AssertNoErr(t, err)
125}
126
127func getLB(t *testing.T, client *gophercloud.ServiceClient, id int) {
128 lb, err := lbs.Get(client, id).Extract()
129 th.AssertNoErr(t, err)
130 t.Logf("Getting LB %d: Created [%s] VIPs [%#v] Logging [%#v] Persistence [%#v] SourceAddrs [%#v]",
131 lb.ID, lb.Created, lb.VIPs, lb.ConnectionLogging, lb.SessionPersistence, lb.SourceAddrs)
132}
133
134func updateLB(t *testing.T, client *gophercloud.ServiceClient, id int) {
135 opts := lbs.UpdateOpts{
136 Name: tools.RandomString("new_", 5),
137 Protocol: "TCP",
Jamie Hannafordbde72602014-11-10 10:32:26 +0100138 HalfClosed: gophercloud.Enabled,
Jamie Hannafordbef53932014-11-05 12:39:30 +0100139 Algorithm: "RANDOM",
140 Port: 8080,
141 Timeout: 100,
Jamie Hannafordbde72602014-11-10 10:32:26 +0100142 HTTPSRedirect: gophercloud.Disabled,
Jamie Hannafordbef53932014-11-05 12:39:30 +0100143 }
144
145 err := lbs.Update(client, id, opts).ExtractErr()
146 th.AssertNoErr(t, err)
147
148 t.Logf("Updating LB %d - waiting for it to finish", id)
149 waitForLB(client, id, lbs.ACTIVE)
150 t.Logf("LB %d has reached ACTIVE state", id)
151}
152
153func deleteLB(t *testing.T, client *gophercloud.ServiceClient, id int) {
154 err := lbs.Delete(client, id).ExtractErr()
155 th.AssertNoErr(t, err)
Jamie Hannaford82ce9162014-11-10 12:18:35 +0100156 t.Logf("Deleted LB %d", id)
Jamie Hannafordbef53932014-11-05 12:39:30 +0100157}
158
159func batchDeleteLBs(t *testing.T, client *gophercloud.ServiceClient, ids []int) {
160 err := lbs.BulkDelete(client, ids).ExtractErr()
161 th.AssertNoErr(t, err)
Jamie Hannaford82ce9162014-11-10 12:18:35 +0100162 t.Logf("Deleted LB %s", intsToStr(ids))
163}
164
165func checkLBLogging(t *testing.T, client *gophercloud.ServiceClient, id int) {
166 err := lbs.EnableLogging(client, id).ExtractErr()
167 th.AssertNoErr(t, err)
168 t.Logf("Enabled logging for LB %d", id)
169
170 res, err := lbs.IsLoggingEnabled(client, id)
171 th.AssertNoErr(t, err)
172 t.Logf("LB %d log enabled? %s", id, strconv.FormatBool(res))
173
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100174 waitForLB(client, id, lbs.ACTIVE)
175
Jamie Hannaford82ce9162014-11-10 12:18:35 +0100176 err = lbs.DisableLogging(client, id).ExtractErr()
177 th.AssertNoErr(t, err)
178 t.Logf("Disabled logging for LB %d", id)
179}
180
181func checkErrorPage(t *testing.T, client *gophercloud.ServiceClient, id int) {
182 content, err := lbs.SetErrorPage(client, id, "<html>New content!</html>").Extract()
183 t.Logf("Set error page for LB %d", id)
184
185 content, err = lbs.GetErrorPage(client, id).Extract()
186 th.AssertNoErr(t, err)
187 t.Logf("Error page for LB %d: %s", id, content)
188
189 err = lbs.DeleteErrorPage(client, id).ExtractErr()
190 t.Logf("Deleted error page for LB %d", id)
191}
192
193func getStats(t *testing.T, client *gophercloud.ServiceClient, id int) {
194 waitForLB(client, id, lbs.ACTIVE)
195
196 stats, err := lbs.GetStats(client, id).Extract()
197 th.AssertNoErr(t, err)
198
199 t.Logf("Stats for LB %d: %#v", id, stats)
Jamie Hannafordbef53932014-11-05 12:39:30 +0100200}
Jamie Hannafordb514bfd2014-11-10 15:39:15 +0100201
202func checkCaching(t *testing.T, client *gophercloud.ServiceClient, id int) {
203 err := lbs.EnableCaching(client, id).ExtractErr()
204 th.AssertNoErr(t, err)
205 t.Logf("Enabled caching for LB %d", id)
206
207 res, err := lbs.IsContentCached(client, id)
208 th.AssertNoErr(t, err)
209 t.Logf("Is caching enabled for LB? %s", strconv.FormatBool(res))
210
211 err = lbs.DisableCaching(client, id).ExtractErr()
212 th.AssertNoErr(t, err)
213 t.Logf("Disabled caching for LB %d", id)
214}