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