PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/rackspace/gophercloud/acceptance/rackspace/lb/v1/lb_test.go

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