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 | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/acceptance/tools" |
| 10 | "github.com/rackspace/gophercloud/pagination" |
| 11 | "github.com/rackspace/gophercloud/rackspace/lb/v1/lbs" |
| 12 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
| 13 | th "github.com/rackspace/gophercloud/testhelper" |
| 14 | ) |
| 15 | |
| 16 | func TestLBs(t *testing.T) { |
| 17 | return |
| 18 | client := setup(t) |
| 19 | |
| 20 | ids := createLB(t, client, 3) |
| 21 | |
| 22 | listLBProtocols(t, client) |
| 23 | |
| 24 | listLBAlgorithms(t, client) |
| 25 | |
| 26 | listLBs(t, client) |
| 27 | |
| 28 | getLB(t, client, ids[0]) |
| 29 | |
| 30 | updateLB(t, client, ids[0]) |
| 31 | |
| 32 | deleteLB(t, client, ids[0]) |
| 33 | |
| 34 | batchDeleteLBs(t, client, ids[1:]) |
| 35 | } |
| 36 | |
| 37 | func createLB(t *testing.T, client *gophercloud.ServiceClient, count int) []int { |
| 38 | ids := []int{} |
| 39 | |
| 40 | for i := 0; i < count; i++ { |
| 41 | opts := lbs.CreateOpts{ |
| 42 | Name: tools.RandomString("test_", 5), |
| 43 | Port: 80, |
| 44 | Protocol: "HTTP", |
| 45 | VIPs: []vips.VIP{ |
| 46 | vips.VIP{Type: vips.PUBLIC}, |
| 47 | }, |
| 48 | } |
| 49 | |
| 50 | lb, err := lbs.Create(client, opts).Extract() |
| 51 | th.AssertNoErr(t, err) |
| 52 | |
| 53 | t.Logf("Created LB %d - waiting for it to build...", lb.ID) |
| 54 | waitForLB(client, lb.ID, lbs.ACTIVE) |
| 55 | t.Logf("LB %d has reached ACTIVE state", lb.ID) |
| 56 | |
| 57 | ids = append(ids, lb.ID) |
| 58 | } |
| 59 | |
| 60 | return ids |
| 61 | } |
| 62 | |
| 63 | func waitForLB(client *gophercloud.ServiceClient, id int, state lbs.Status) { |
| 64 | gophercloud.WaitFor(60, func() (bool, error) { |
| 65 | lb, err := lbs.Get(client, id).Extract() |
| 66 | if err != nil { |
| 67 | return false, err |
| 68 | } |
| 69 | if lb.Status != state { |
| 70 | return false, nil |
| 71 | } |
| 72 | return true, nil |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | func listLBProtocols(t *testing.T, client *gophercloud.ServiceClient) { |
| 77 | err := lbs.ListProtocols(client).EachPage(func(page pagination.Page) (bool, error) { |
| 78 | pList, err := lbs.ExtractProtocols(page) |
| 79 | th.AssertNoErr(t, err) |
| 80 | |
| 81 | for _, p := range pList { |
| 82 | t.Logf("Listing protocol: Name [%s]", p.Name) |
| 83 | } |
| 84 | |
| 85 | return true, nil |
| 86 | }) |
| 87 | th.AssertNoErr(t, err) |
| 88 | } |
| 89 | |
| 90 | func listLBAlgorithms(t *testing.T, client *gophercloud.ServiceClient) { |
| 91 | err := lbs.ListAlgorithms(client).EachPage(func(page pagination.Page) (bool, error) { |
| 92 | aList, err := lbs.ExtractAlgorithms(page) |
| 93 | th.AssertNoErr(t, err) |
| 94 | |
| 95 | for _, a := range aList { |
| 96 | t.Logf("Listing algorithm: Name [%s]", a.Name) |
| 97 | } |
| 98 | |
| 99 | return true, nil |
| 100 | }) |
| 101 | th.AssertNoErr(t, err) |
| 102 | } |
| 103 | |
| 104 | func listLBs(t *testing.T, client *gophercloud.ServiceClient) { |
| 105 | err := lbs.List(client, lbs.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 106 | lbList, err := lbs.ExtractLBs(page) |
| 107 | th.AssertNoErr(t, err) |
| 108 | |
| 109 | for _, lb := range lbList { |
| 110 | t.Logf("Listing LB: ID [%d] Name [%s] Protocol [%s] Status [%s] Node count [%d] Port [%d]", |
| 111 | lb.ID, lb.Name, lb.Protocol, lb.Status, lb.NodeCount, lb.Port) |
| 112 | } |
| 113 | |
| 114 | return true, nil |
| 115 | }) |
| 116 | |
| 117 | th.AssertNoErr(t, err) |
| 118 | } |
| 119 | |
| 120 | func getLB(t *testing.T, client *gophercloud.ServiceClient, id int) { |
| 121 | lb, err := lbs.Get(client, id).Extract() |
| 122 | th.AssertNoErr(t, err) |
| 123 | t.Logf("Getting LB %d: Created [%s] VIPs [%#v] Logging [%#v] Persistence [%#v] SourceAddrs [%#v]", |
| 124 | lb.ID, lb.Created, lb.VIPs, lb.ConnectionLogging, lb.SessionPersistence, lb.SourceAddrs) |
| 125 | } |
| 126 | |
| 127 | func updateLB(t *testing.T, client *gophercloud.ServiceClient, id int) { |
| 128 | opts := lbs.UpdateOpts{ |
| 129 | Name: tools.RandomString("new_", 5), |
| 130 | Protocol: "TCP", |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 131 | HalfClosed: gophercloud.Enabled, |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 132 | Algorithm: "RANDOM", |
| 133 | Port: 8080, |
| 134 | Timeout: 100, |
Jamie Hannaford | bde7260 | 2014-11-10 10:32:26 +0100 | [diff] [blame] | 135 | HTTPSRedirect: gophercloud.Disabled, |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | err := lbs.Update(client, id, opts).ExtractErr() |
| 139 | th.AssertNoErr(t, err) |
| 140 | |
| 141 | t.Logf("Updating LB %d - waiting for it to finish", id) |
| 142 | waitForLB(client, id, lbs.ACTIVE) |
| 143 | t.Logf("LB %d has reached ACTIVE state", id) |
| 144 | } |
| 145 | |
| 146 | func deleteLB(t *testing.T, client *gophercloud.ServiceClient, id int) { |
| 147 | err := lbs.Delete(client, id).ExtractErr() |
| 148 | th.AssertNoErr(t, err) |
| 149 | t.Logf("Deleted %d", id) |
| 150 | } |
| 151 | |
| 152 | func batchDeleteLBs(t *testing.T, client *gophercloud.ServiceClient, ids []int) { |
| 153 | err := lbs.BulkDelete(client, ids).ExtractErr() |
| 154 | th.AssertNoErr(t, err) |
Jamie Hannaford | c9da4b4 | 2014-11-05 16:34:56 +0100 | [diff] [blame] | 155 | t.Logf("Deleted %s", intsToStr(ids)) |
Jamie Hannaford | bef5393 | 2014-11-05 12:39:30 +0100 | [diff] [blame] | 156 | } |