File: nodebalancer_config_nodes_test.go

package info (click to toggle)
golang-github-linode-linodego 1.47.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,032 kB
  • sloc: makefile: 95; sh: 52; python: 24
file content (203 lines) | stat: -rw-r--r-- 6,409 bytes parent folder | download
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
package integration

import (
	"context"
	"testing"

	"github.com/linode/linodego"
)

var (
	testNodePort                   = "8080"
	testNodeLabel                  = "go-node-test-def"
	testNodeWeight                 = 10
	testNodeBalancerNodeCreateOpts = linodego.NodeBalancerNodeCreateOptions{
		Label:  testNodeLabel,
		Weight: testNodeWeight,
		Mode:   linodego.ModeAccept,
	}
)

func TestNodeBalancerNode_Create_smoke(t *testing.T) {
	_, _, _, node, teardown, err := setupNodeBalancerNode(t, "fixtures/TestNodeBalancerNode_Create")
	defer teardown()

	if err != nil {
		t.Errorf("Error creating NodeBalancer Node, got error %v", err)
	}

	expected := testNodeBalancerNodeCreateOpts

	if node.Label != expected.Label ||
		node.Weight != expected.Weight ||
		node.Mode != expected.Mode {
		t.Errorf("NodeBalancerNode did not match CreateOptions - %v", node)
	}
}

func TestNodeBalancerNode_Update(t *testing.T) {
	client, nodebalancer, config, node, teardown, err := setupNodeBalancerNode(t, "fixtures/TestNodeBalancerNode_Update")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	updateOpts := linodego.NodeBalancerNodeUpdateOptions{
		Mode:   linodego.ModeDrain,
		Weight: testNodeWeight + 90,
		Label:  testNodeLabel + "_r",
	}
	nodeUpdated, err := client.UpdateNodeBalancerNode(context.Background(), nodebalancer.ID, config.ID, node.ID, updateOpts)
	if err != nil {
		t.Errorf("Error updating NodeBalancer Node, %s", err)
	}

	// fixture sanitization breaks predictability for this test, verify the prefix
	if string(updateOpts.Mode) != string(nodeUpdated.Mode) ||
		updateOpts.Label != nodeUpdated.Label ||
		updateOpts.Weight != nodeUpdated.Weight {
		t.Errorf("NodeBalancerNode did not match UpdateOptions")
	}
}

func TestNodeBalancerNodes_List(t *testing.T) {
	client, nodebalancer, config, _, teardown, err := setupNodeBalancerNode(t, "fixtures/TestNodeBalancerNodes_List")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	listOpts := linodego.NewListOptions(0, "")
	nodes, err := client.ListNodeBalancerNodes(context.Background(), nodebalancer.ID, config.ID, listOpts)
	if err != nil {
		t.Errorf("Error listing nodebalancers nodes, expected array, got error %v", err)
	}
	if len(nodes) != listOpts.Results {
		t.Errorf("Expected ListNodeBalancerNodes to match API result count")
	}
}

func TestNodeBalancerNodes_ListMultiplePages(t *testing.T) {
	// TODO: replace hand crafted fixtures with unit tests
	// This fixture was hand-crafted to render an empty page 1 result, with a single result on page 2
	// "results:1,data:[],page:1,pages:2"  .. "results:1,data[{...}],page:2,pages:2"
	client, nodebalancer, config, _, teardown, err := setupNodeBalancerNode(t, "fixtures/TestNodeBalancerNodes_ListMultiplePages")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	listOpts := linodego.NewListOptions(0, "")
	nodes, err := client.ListNodeBalancerNodes(context.Background(), nodebalancer.ID, config.ID, listOpts)
	if err != nil {
		t.Errorf("Error listing nodebalancers configs, expected array, got error %v", err)
	}
	if len(nodes) != listOpts.Results {
		t.Errorf("Expected ListNodeBalancerNodes count to match API results count")
	}
}

func TestNodeBalancerNode_Get(t *testing.T) {
	client, nodebalancer, config, node, teardown, err := setupNodeBalancerNode(t, "fixtures/TestNodeBalancerNode_Get")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	nodeGot, err := client.GetNodeBalancerNode(context.Background(), nodebalancer.ID, config.ID, node.ID)
	if nodeGot.Address != node.Address {
		t.Errorf("GetNodeBalancerNode did not get the expected node")
	}
	if err != nil {
		t.Errorf("Error getting nodebalancer %d, got error %v", nodebalancer.ID, err)
	}
}

func TestNodeBalancer_Rebuild(t *testing.T) {
	client, nodebalancer, config, node, teardown, err := setupNodeBalancerNode(t, "fixtures/TestNodeBalancer_Rebuild")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	nbcRebuildOpts := config.GetRebuildOptions()
	nbcRebuildOpts.Nodes = append(
		nbcRebuildOpts.Nodes,
		linodego.NodeBalancerConfigRebuildNodeOptions{
			NodeBalancerNodeCreateOptions: node.GetCreateOptions(),
			ID:                            node.ID,
		},
	)

	nbcGot, err := client.RebuildNodeBalancerConfig(
		context.Background(),
		nodebalancer.ID,
		config.ID,
		nbcRebuildOpts,
	)
	if err != nil {
		t.Errorf("Error rebuilding nodebalancer config %d: %v", config.ID, err)
	}
	if nbcGot.Port != config.Port {
		t.Errorf("RebuildNodeBalancerConfig did not return the expected port")
	}

	newNodes, err := client.ListNodeBalancerNodes(
		context.Background(),
		nodebalancer.ID,
		nbcGot.ID,
		nil,
	)
	if err != nil {
		t.Fatal(err)
	}

	if newNodes[0].ID != node.ID {
		t.Fatalf("expected node ID to match; %d != %d", newNodes[0].ID, node.ID)
	}
}

func setupNodeBalancerNode(t *testing.T, fixturesYaml string) (*linodego.Client, *linodego.NodeBalancer, *linodego.NodeBalancerConfig, *linodego.NodeBalancerNode, func(), error) {
	t.Helper()
	var fixtureTeardown func()
	client, nodebalancer, config, fixtureTeardown, err := setupNodeBalancerConfig(t, fixturesYaml)
	if err != nil {
		t.Fatalf("Error creating nodebalancer config, got error %v", err)
	}

	instance, err := createInstance(t, client, true)
	if err != nil {
		t.Errorf("failed to create test instance: %s", err)
	}

	instanceIP, err := client.AddInstanceIPAddress(context.Background(), instance.ID, false)
	if err != nil {
		t.Fatal(err)
	}

	createOpts := testNodeBalancerNodeCreateOpts
	createOpts.Address = instanceIP.Address + ":" + testNodePort
	node, err := client.CreateNodeBalancerNode(context.Background(), nodebalancer.ID, config.ID, createOpts)
	if err != nil {
		t.Fatalf("Error creating NodeBalancer Config Node, got error %v", err)
	}

	teardown := func() {
		// delete the NodeBalancerNode to exercise the code
		if err := client.DeleteNodeBalancerNode(context.Background(), nodebalancer.ID, config.ID, node.ID); err != nil {
			e, ok := err.(*linodego.Error)
			// Tollerate 404 because Rebuild testing will delete all Nodes
			if !ok || e.Code != 404 {
				t.Fatalf("Expected to delete a NodeBalancer Config Node, but got %v", err)
			}
		}
		// delete the instance
		if err := client.DeleteInstance(context.Background(), instance.ID); err != nil {
			if t != nil {
				t.Errorf("Error deleting test Instance: %s", err)
			}
		}
		fixtureTeardown()
	}
	return client, nodebalancer, config, node, teardown, err
}