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