Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 1 | // +build acceptance lbs |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
Jamie Hannaford | 82ce916 | 2014-11-10 12:18:35 +0100 | [diff] [blame] | 6 | "strconv" |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 7 | "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 | |
| 17 | func TestLBs(t *testing.T) { |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 18 | client := setup(t) |
| 19 | |
| 20 | ids := createLB(t, client, 3) |
Jamie Hannaford | 82ce916 | 2014-11-10 12:18:35 +0100 | [diff] [blame] | 21 | id := ids[0] |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 22 | |
| 23 | listLBProtocols(t, client) |
| 24 | |
| 25 | listLBAlgorithms(t, client) |
| 26 | |
| 27 | listLBs(t, client) |
| 28 | |
Jamie Hannaford | 82ce916 | 2014-11-10 12:18:35 +0100 | [diff] [blame] | 29 | getLB(t, client, id) |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 30 | |
Jamie Hannaford | 82ce916 | 2014-11-10 12:18:35 +0100 | [diff] [blame] | 31 | checkLBLogging(t, client, id) |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 32 | |
Jamie Hannaford | 82ce916 | 2014-11-10 12:18:35 +0100 | [diff] [blame] | 33 | checkErrorPage(t, client, id) |
| 34 | |
| 35 | getStats(t, client, id) |
| 36 | |
| 37 | updateLB(t, client, id) |
| 38 | |
| 39 | deleteLB(t, client, id) |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 40 | |
| 41 | batchDeleteLBs(t, client, ids[1:]) |
| 42 | } |
| 43 | |
| 44 | func 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 | |
| 70 | func 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 | |
| 83 | func 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 | |
| 97 | func 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 | |
| 111 | func 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 | |
| 127 | func 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 | |
| 134 | func updateLB(t *testing.T, client *gophercloud.ServiceClient, id int) { |
| 135 | opts := lbs.UpdateOpts{ |
| 136 | Name: tools.RandomString("new_", 5), |
| 137 | Protocol: "TCP", |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 138 | HalfClosed: gophercloud.Enabled, |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 139 | Algorithm: "RANDOM", |
| 140 | Port: 8080, |
| 141 | Timeout: 100, |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 142 | HTTPSRedirect: gophercloud.Disabled, |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 143 | } |
| 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 | |
| 153 | func deleteLB(t *testing.T, client *gophercloud.ServiceClient, id int) { |
| 154 | err := lbs.Delete(client, id).ExtractErr() |
| 155 | th.AssertNoErr(t, err) |
Jamie Hannaford | 82ce916 | 2014-11-10 12:18:35 +0100 | [diff] [blame] | 156 | t.Logf("Deleted LB %d", id) |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | func batchDeleteLBs(t *testing.T, client *gophercloud.ServiceClient, ids []int) { |
| 160 | err := lbs.BulkDelete(client, ids).ExtractErr() |
| 161 | th.AssertNoErr(t, err) |
Jamie Hannaford | 82ce916 | 2014-11-10 12:18:35 +0100 | [diff] [blame] | 162 | t.Logf("Deleted LB %s", intsToStr(ids)) |
| 163 | } |
| 164 | |
| 165 | func 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 Hannaford | b514bfd | 2014-11-10 15:39:15 +0100 | [diff] [blame] | 174 | waitForLB(client, id, lbs.ACTIVE) |
| 175 | |
Jamie Hannaford | 82ce916 | 2014-11-10 12:18:35 +0100 | [diff] [blame] | 176 | err = lbs.DisableLogging(client, id).ExtractErr() |
| 177 | th.AssertNoErr(t, err) |
| 178 | t.Logf("Disabled logging for LB %d", id) |
| 179 | } |
| 180 | |
| 181 | func 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 | |
| 193 | func 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 Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 200 | } |
Jamie Hannaford | b514bfd | 2014-11-10 15:39:15 +0100 | [diff] [blame] | 201 | |
| 202 | func 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 | } |