File: lb_test.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (214 lines) | stat: -rw-r--r-- 5,645 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// +build acceptance lbs

package v1

import (
	"strconv"
	"testing"

	"github.com/rackspace/gophercloud"
	"github.com/rackspace/gophercloud/acceptance/tools"
	"github.com/rackspace/gophercloud/pagination"
	"github.com/rackspace/gophercloud/rackspace/lb/v1/lbs"
	"github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
	th "github.com/rackspace/gophercloud/testhelper"
)

func TestLBs(t *testing.T) {
	client := setup(t)

	ids := createLB(t, client, 3)
	id := ids[0]

	listLBProtocols(t, client)

	listLBAlgorithms(t, client)

	listLBs(t, client)

	getLB(t, client, id)

	checkLBLogging(t, client, id)

	checkErrorPage(t, client, id)

	getStats(t, client, id)

	updateLB(t, client, id)

	deleteLB(t, client, id)

	batchDeleteLBs(t, client, ids[1:])
}

func createLB(t *testing.T, client *gophercloud.ServiceClient, count int) []int {
	ids := []int{}

	for i := 0; i < count; i++ {
		opts := lbs.CreateOpts{
			Name:     tools.RandomString("test_", 5),
			Port:     80,
			Protocol: "HTTP",
			VIPs: []vips.VIP{
				vips.VIP{Type: vips.PUBLIC},
			},
		}

		lb, err := lbs.Create(client, opts).Extract()
		th.AssertNoErr(t, err)

		t.Logf("Created LB %d - waiting for it to build...", lb.ID)
		waitForLB(client, lb.ID, lbs.ACTIVE)
		t.Logf("LB %d has reached ACTIVE state", lb.ID)

		ids = append(ids, lb.ID)
	}

	return ids
}

func waitForLB(client *gophercloud.ServiceClient, id int, state lbs.Status) {
	gophercloud.WaitFor(60, func() (bool, error) {
		lb, err := lbs.Get(client, id).Extract()
		if err != nil {
			return false, err
		}
		if lb.Status != state {
			return false, nil
		}
		return true, nil
	})
}

func listLBProtocols(t *testing.T, client *gophercloud.ServiceClient) {
	err := lbs.ListProtocols(client).EachPage(func(page pagination.Page) (bool, error) {
		pList, err := lbs.ExtractProtocols(page)
		th.AssertNoErr(t, err)

		for _, p := range pList {
			t.Logf("Listing protocol: Name [%s]", p.Name)
		}

		return true, nil
	})
	th.AssertNoErr(t, err)
}

func listLBAlgorithms(t *testing.T, client *gophercloud.ServiceClient) {
	err := lbs.ListAlgorithms(client).EachPage(func(page pagination.Page) (bool, error) {
		aList, err := lbs.ExtractAlgorithms(page)
		th.AssertNoErr(t, err)

		for _, a := range aList {
			t.Logf("Listing algorithm: Name [%s]", a.Name)
		}

		return true, nil
	})
	th.AssertNoErr(t, err)
}

func listLBs(t *testing.T, client *gophercloud.ServiceClient) {
	err := lbs.List(client, lbs.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
		lbList, err := lbs.ExtractLBs(page)
		th.AssertNoErr(t, err)

		for _, lb := range lbList {
			t.Logf("Listing LB: ID [%d] Name [%s] Protocol [%s] Status [%s] Node count [%d] Port [%d]",
				lb.ID, lb.Name, lb.Protocol, lb.Status, lb.NodeCount, lb.Port)
		}

		return true, nil
	})

	th.AssertNoErr(t, err)
}

func getLB(t *testing.T, client *gophercloud.ServiceClient, id int) {
	lb, err := lbs.Get(client, id).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Getting LB %d: Created [%s] VIPs [%#v] Logging [%#v] Persistence [%#v] SourceAddrs [%#v]",
		lb.ID, lb.Created, lb.VIPs, lb.ConnectionLogging, lb.SessionPersistence, lb.SourceAddrs)
}

func updateLB(t *testing.T, client *gophercloud.ServiceClient, id int) {
	opts := lbs.UpdateOpts{
		Name:          tools.RandomString("new_", 5),
		Protocol:      "TCP",
		HalfClosed:    gophercloud.Enabled,
		Algorithm:     "RANDOM",
		Port:          8080,
		Timeout:       100,
		HTTPSRedirect: gophercloud.Disabled,
	}

	err := lbs.Update(client, id, opts).ExtractErr()
	th.AssertNoErr(t, err)

	t.Logf("Updating LB %d - waiting for it to finish", id)
	waitForLB(client, id, lbs.ACTIVE)
	t.Logf("LB %d has reached ACTIVE state", id)
}

func deleteLB(t *testing.T, client *gophercloud.ServiceClient, id int) {
	err := lbs.Delete(client, id).ExtractErr()
	th.AssertNoErr(t, err)
	t.Logf("Deleted LB %d", id)
}

func batchDeleteLBs(t *testing.T, client *gophercloud.ServiceClient, ids []int) {
	err := lbs.BulkDelete(client, ids).ExtractErr()
	th.AssertNoErr(t, err)
	t.Logf("Deleted LB %s", intsToStr(ids))
}

func checkLBLogging(t *testing.T, client *gophercloud.ServiceClient, id int) {
	err := lbs.EnableLogging(client, id).ExtractErr()
	th.AssertNoErr(t, err)
	t.Logf("Enabled logging for LB %d", id)

	res, err := lbs.IsLoggingEnabled(client, id)
	th.AssertNoErr(t, err)
	t.Logf("LB %d log enabled? %s", id, strconv.FormatBool(res))

	waitForLB(client, id, lbs.ACTIVE)

	err = lbs.DisableLogging(client, id).ExtractErr()
	th.AssertNoErr(t, err)
	t.Logf("Disabled logging for LB %d", id)
}

func checkErrorPage(t *testing.T, client *gophercloud.ServiceClient, id int) {
	content, err := lbs.SetErrorPage(client, id, "<html>New content!</html>").Extract()
	t.Logf("Set error page for LB %d", id)

	content, err = lbs.GetErrorPage(client, id).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Error page for LB %d: %s", id, content)

	err = lbs.DeleteErrorPage(client, id).ExtractErr()
	t.Logf("Deleted error page for LB %d", id)
}

func getStats(t *testing.T, client *gophercloud.ServiceClient, id int) {
	waitForLB(client, id, lbs.ACTIVE)

	stats, err := lbs.GetStats(client, id).Extract()
	th.AssertNoErr(t, err)

	t.Logf("Stats for LB %d: %#v", id, stats)
}

func checkCaching(t *testing.T, client *gophercloud.ServiceClient, id int) {
	err := lbs.EnableCaching(client, id).ExtractErr()
	th.AssertNoErr(t, err)
	t.Logf("Enabled caching for LB %d", id)

	res, err := lbs.IsContentCached(client, id)
	th.AssertNoErr(t, err)
	t.Logf("Is caching enabled for LB? %s", strconv.FormatBool(res))

	err = lbs.DisableCaching(client, id).ExtractErr()
	th.AssertNoErr(t, err)
	t.Logf("Disabled caching for LB %d", id)
}