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