-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtargets_test.go
223 lines (188 loc) · 6.84 KB
/
targets_test.go
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
215
216
217
218
219
220
221
222
223
package gokong
import (
"testing"
// "time"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
)
func TestTargets_GetTargetsFromUpstreamId(t *testing.T) {
upstreamRequest := &UpstreamRequest{
Name: "upstream-" + uuid.NewV4().String(),
Slots: 10,
}
client := NewClient(NewDefaultConfig())
createdUpstream, err := client.Upstreams().Create(upstreamRequest)
assert.Nil(t, err)
assert.NotNil(t, createdUpstream)
targetRequest := &TargetRequest{
Target: "www.example.com:80",
Weight: 200,
}
createdTarget, err := client.Targets().CreateFromUpstreamId(createdUpstream.Id, targetRequest)
assert.Nil(t, err)
assert.NotNil(t, createdTarget)
result, err := client.Targets().GetTargetsFromUpstreamId(createdUpstream.Id)
assert.Nil(t, err)
assert.NotNil(t, result)
assert.Len(t, result, 1)
assert.Equal(t, createdTarget, result[0])
client.Targets().DeleteFromUpstreamById(createdUpstream.Id, *createdTarget.Id)
client.Upstreams().DeleteById(createdUpstream.Id)
}
func TestTargets_GetTargetsFromUpstreamName(t *testing.T) {
upstreamRequest := &UpstreamRequest{
Name: "upstream-" + uuid.NewV4().String(),
Slots: 10,
}
client := NewClient(NewDefaultConfig())
createdUpstream, err := client.Upstreams().Create(upstreamRequest)
assert.Nil(t, err)
assert.NotNil(t, createdUpstream)
targetRequest := &TargetRequest{
Target: "www.example.com:80",
Weight: 200,
}
createdTarget, err := client.Targets().CreateFromUpstreamName(createdUpstream.Name, targetRequest)
assert.Nil(t, err)
assert.NotNil(t, createdTarget)
result, err := client.Targets().GetTargetsFromUpstreamName(createdUpstream.Name)
assert.Nil(t, err)
assert.NotNil(t, result)
assert.Len(t, result, 1)
assert.Equal(t, createdTarget, result[0])
client.Targets().DeleteFromUpstreamById(createdUpstream.Name, *createdTarget.Target)
client.Upstreams().DeleteById(createdUpstream.Id)
}
func TestTargets_GetForNonExistentUpstream(t *testing.T) {
result, err := NewClient(NewDefaultConfig()).Targets().GetTargetsFromUpstreamName(uuid.NewV4().String())
assert.Nil(t, result)
assert.NotNil(t, err)
}
func TestTargets_Create(t *testing.T) {
upstreamRequest := &UpstreamRequest{
Name: "upstream-" + uuid.NewV4().String(),
Slots: 10,
}
client := NewClient(NewDefaultConfig())
createdUpstream, err := client.Upstreams().Create(upstreamRequest)
assert.Nil(t, err)
assert.NotNil(t, createdUpstream)
targetRequest := &TargetRequest{
Target: "www.example.com:80",
Weight: 200,
}
createdTarget, err := client.Targets().CreateFromUpstreamId(createdUpstream.Id, targetRequest)
assert.Nil(t, err)
assert.NotNil(t, createdTarget)
assert.True(t, *createdTarget.Id != "")
assert.Equal(t, *createdTarget.Target, targetRequest.Target)
assert.Equal(t, *createdTarget.Weight, targetRequest.Weight)
client.Targets().DeleteFromUpstreamById(createdUpstream.Id, *createdTarget.Id)
client.Upstreams().DeleteById(createdUpstream.Id)
}
func TestTargets_Delete(t *testing.T) {
upstreamRequest := &UpstreamRequest{
Name: "upstream-" + uuid.NewV4().String(),
Slots: 10,
}
client := NewClient(NewDefaultConfig())
createdUpstream, err := client.Upstreams().Create(upstreamRequest)
assert.Nil(t, err)
assert.NotNil(t, createdUpstream)
targetRequest := &TargetRequest{
Target: "www.example.com:80",
Weight: 200,
}
createdTarget, err := client.Targets().CreateFromUpstreamId(createdUpstream.Id, targetRequest)
assert.Nil(t, err)
assert.NotNil(t, createdTarget)
client.Targets().DeleteFromUpstreamByHostPort(createdUpstream.Name, *createdTarget.Target)
targets, err := client.Targets().GetTargetsFromUpstreamName(createdUpstream.Name)
assert.Nil(t, err)
assert.Len(t, targets, 0)
client.Upstreams().DeleteById(createdUpstream.Id)
}
// WWOM: The following test runs locally without issue and without need for hack contained therein
// However, on the build server there seems to be a timing issue of sorts whereby trhe Kong container
// hasn't completed the registration of a target and/or related health checks when we attempt to
// manually set the health status (to healthy/unhealthy) and subsequently check that it was set
// func TestTargets_SetTargetHealthFromUpstreamById(t *testing.T) {
// upstreamRequest := &UpstreamRequest{
// Name: "upstream-" + uuid.NewV4().String(),
// Slots: 10,
// HealthChecks: &UpstreamHealthCheck{
// Active: &UpstreamHealthCheckActive{
// Concurrency: 10,
// HttpPath: "/",
// Timeout: 1,
// Healthy: &ActiveHealthy{
// HttpStatuses: []int{200, 302},
// Interval: 1000,
// Successes: 10,
// },
// Unhealthy: &ActiveUnhealthy{
// HttpFailures: 10,
// HttpStatuses: []int{429, 404, 500, 501, 502, 503, 504, 505},
// Interval: 1000,
// TcpFailures: 10,
// Timeouts: 10,
// },
// },
// },
// }
//
// client := NewClient(NewDefaultConfig())
// createdUpstream, err := client.Upstreams().Create(upstreamRequest)
//
// assert.Nil(t, err)
// assert.NotNil(t, createdUpstream)
//
// targetRequest := &TargetRequest{
// Target: "www.example.com:80",
// Weight: 200,
// }
// createdTarget, err := client.Targets().CreateFromUpstreamName(createdUpstream.Name, targetRequest)
//
// assert.Nil(t, err)
// assert.NotNil(t, createdTarget)
//
// // HACK: This is a bit hack-y - but tests fail on the build occassionly as Kong hasn't setup the load balancer
// // and health checks for the upstream/targets by the time we start trying to set their health status below
// targets, err := client.Targets().GetTargetsWithHealthFromUpstreamName(createdUpstream.Name)
// retry := 1
// for (*targets[0].Health == "HEALTHCHECKS_OFF" || *targets[0].Health == "DNS_ERROR") && retry < 10 {
// t.Log("Health-checks still off on target. Sleep and try again until we have another status.")
// assert.Len(t, targets, 1)
//
// time.Sleep(2 * time.Second)
// targets, err = client.Targets().GetTargetsWithHealthFromUpstreamName(createdUpstream.Name)
// retry++
// }
//
// result := client.Targets().SetTargetFromUpstreamByIdAsUnhealthy(createdUpstream.Id, *createdTarget.Id)
//
// assert.Nil(t, result)
//
// targets, err = client.Targets().GetTargetsWithHealthFromUpstreamName(createdUpstream.Name)
//
// assert.Nil(t, err)
// assert.NotNil(t, targets)
// assert.Len(t, targets, 1)
// target := targets[0]
// assert.Equal(t, "UNHEALTHY", *target.Health)
//
// result = client.Targets().SetTargetFromUpstreamByIdAsHealthy(createdUpstream.Id, *createdTarget.Id)
//
// assert.Nil(t, result)
//
// targets, err = client.Targets().GetTargetsWithHealthFromUpstreamName(createdUpstream.Name)
//
// assert.Nil(t, err)
// assert.NotNil(t, targets)
// assert.Len(t, targets, 1)
// target = targets[0]
// assert.Equal(t, "HEALTHY", *target.Health)
//
// client.Targets().DeleteFromUpstreamById(createdUpstream.Name, *createdTarget.Target)
// client.Upstreams().DeleteById(createdUpstream.Id)
// }