PageRenderTime 137ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/JamesClonk/machine
Go | 175 lines | 136 code | 38 blank | 1 comment | 12 complexity | 5992e5463903235c2d1c390c1cfd2ec5 MD5 | raw file
  1. // +build acceptance lbs
  2. package v1
  3. import (
  4. "os"
  5. "testing"
  6. "github.com/rackspace/gophercloud"
  7. "github.com/rackspace/gophercloud/acceptance/tools"
  8. "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/diskconfig"
  9. "github.com/rackspace/gophercloud/pagination"
  10. "github.com/rackspace/gophercloud/rackspace/compute/v2/servers"
  11. "github.com/rackspace/gophercloud/rackspace/lb/v1/lbs"
  12. "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes"
  13. th "github.com/rackspace/gophercloud/testhelper"
  14. )
  15. func TestNodes(t *testing.T) {
  16. client := setup(t)
  17. serverIP := findServer(t)
  18. ids := createLB(t, client, 1)
  19. lbID := ids[0]
  20. nodeID := addNodes(t, client, lbID, serverIP)
  21. listNodes(t, client, lbID)
  22. getNode(t, client, lbID, nodeID)
  23. updateNode(t, client, lbID, nodeID)
  24. listEvents(t, client, lbID)
  25. deleteNode(t, client, lbID, nodeID)
  26. waitForLB(client, lbID, lbs.ACTIVE)
  27. deleteLB(t, client, lbID)
  28. }
  29. func findServer(t *testing.T) string {
  30. var serverIP string
  31. client, err := newComputeClient()
  32. th.AssertNoErr(t, err)
  33. err = servers.List(client, nil).EachPage(func(page pagination.Page) (bool, error) {
  34. sList, err := servers.ExtractServers(page)
  35. th.AssertNoErr(t, err)
  36. for _, s := range sList {
  37. serverIP = s.AccessIPv4
  38. t.Logf("Found an existing server: ID [%s] Public IP [%s]", s.ID, serverIP)
  39. break
  40. }
  41. return true, nil
  42. })
  43. th.AssertNoErr(t, err)
  44. if serverIP == "" {
  45. t.Log("No server found, creating one")
  46. imageRef := os.Getenv("RS_IMAGE_ID")
  47. if imageRef == "" {
  48. t.Fatalf("OS var RS_IMAGE_ID undefined")
  49. }
  50. flavorRef := os.Getenv("RS_FLAVOR_ID")
  51. if flavorRef == "" {
  52. t.Fatalf("OS var RS_FLAVOR_ID undefined")
  53. }
  54. opts := &servers.CreateOpts{
  55. Name: tools.RandomString("lb_test_", 5),
  56. ImageRef: imageRef,
  57. FlavorRef: flavorRef,
  58. DiskConfig: diskconfig.Manual,
  59. }
  60. s, err := servers.Create(client, opts).Extract()
  61. th.AssertNoErr(t, err)
  62. serverIP = s.AccessIPv4
  63. t.Logf("Created server %s, waiting for it to build", s.ID)
  64. err = servers.WaitForStatus(client, s.ID, "ACTIVE", 300)
  65. th.AssertNoErr(t, err)
  66. t.Logf("Server created successfully.")
  67. }
  68. return serverIP
  69. }
  70. func addNodes(t *testing.T, client *gophercloud.ServiceClient, lbID int, serverIP string) int {
  71. opts := nodes.CreateOpts{
  72. nodes.CreateOpt{
  73. Address: serverIP,
  74. Port: 80,
  75. Condition: nodes.ENABLED,
  76. Type: nodes.PRIMARY,
  77. },
  78. }
  79. page := nodes.Create(client, lbID, opts)
  80. nodeList, err := page.ExtractNodes()
  81. th.AssertNoErr(t, err)
  82. var nodeID int
  83. for _, n := range nodeList {
  84. nodeID = n.ID
  85. }
  86. if nodeID == 0 {
  87. t.Fatalf("nodeID could not be extracted from create response")
  88. }
  89. t.Logf("Added node %d to LB %d", nodeID, lbID)
  90. waitForLB(client, lbID, lbs.ACTIVE)
  91. return nodeID
  92. }
  93. func listNodes(t *testing.T, client *gophercloud.ServiceClient, lbID int) {
  94. err := nodes.List(client, lbID, nil).EachPage(func(page pagination.Page) (bool, error) {
  95. nodeList, err := nodes.ExtractNodes(page)
  96. th.AssertNoErr(t, err)
  97. for _, n := range nodeList {
  98. t.Logf("Listing node: ID [%d] Address [%s:%d] Status [%s]", n.ID, n.Address, n.Port, n.Status)
  99. }
  100. return true, nil
  101. })
  102. th.AssertNoErr(t, err)
  103. }
  104. func getNode(t *testing.T, client *gophercloud.ServiceClient, lbID int, nodeID int) {
  105. node, err := nodes.Get(client, lbID, nodeID).Extract()
  106. th.AssertNoErr(t, err)
  107. t.Logf("Getting node %d: Type [%s] Weight [%d]", nodeID, node.Type, node.Weight)
  108. }
  109. func updateNode(t *testing.T, client *gophercloud.ServiceClient, lbID int, nodeID int) {
  110. opts := nodes.UpdateOpts{
  111. Weight: gophercloud.IntToPointer(10),
  112. Condition: nodes.DRAINING,
  113. Type: nodes.SECONDARY,
  114. }
  115. err := nodes.Update(client, lbID, nodeID, opts).ExtractErr()
  116. th.AssertNoErr(t, err)
  117. t.Logf("Updated node %d", nodeID)
  118. waitForLB(client, lbID, lbs.ACTIVE)
  119. }
  120. func listEvents(t *testing.T, client *gophercloud.ServiceClient, lbID int) {
  121. pager := nodes.ListEvents(client, lbID, nodes.ListEventsOpts{})
  122. err := pager.EachPage(func(page pagination.Page) (bool, error) {
  123. eventList, err := nodes.ExtractNodeEvents(page)
  124. th.AssertNoErr(t, err)
  125. for _, e := range eventList {
  126. t.Logf("Listing events for node %d: Type [%s] Msg [%s] Severity [%s] Date [%s]",
  127. e.NodeID, e.Type, e.DetailedMessage, e.Severity, e.Created)
  128. }
  129. return true, nil
  130. })
  131. th.AssertNoErr(t, err)
  132. }
  133. func deleteNode(t *testing.T, client *gophercloud.ServiceClient, lbID int, nodeID int) {
  134. err := nodes.Delete(client, lbID, nodeID).ExtractErr()
  135. th.AssertNoErr(t, err)
  136. t.Logf("Deleted node %d", nodeID)
  137. }