forked from stripe/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
229 lines (198 loc) · 6.13 KB
/
proxy_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
224
225
226
227
228
229
package veneur
import (
"compress/zlib"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stripe/veneur/samplers"
)
func generateProxyConfig() ProxyConfig {
return ProxyConfig{
Debug: false,
ConsulRefreshInterval: "86400s",
ConsulForwardServiceName: "forwardServiceName",
ConsulTraceServiceName: "traceServiceName",
TraceAddress: "127.0.0.1:8128",
TraceAPIAddress: "127.0.0.1:8135",
HTTPAddress: "127.0.0.1:0",
StatsAddress: "127.0.0.1:8201",
}
}
type ConsulTwoMetricRoundTripper struct {
t *testing.T
wg *sync.WaitGroup
aReceived bool
bReceived bool
mtx sync.Mutex
}
func (rt *ConsulTwoMetricRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// Ensure that only one RoundTrip is happening at once
// to prevent dataraces on aReceived and bReceived
rt.mtx.Lock()
defer rt.mtx.Unlock()
rec := httptest.NewRecorder()
if req.URL.Path == "/v1/health/service/forwardServiceName" {
resp, _ := ioutil.ReadFile("fixtures/consul/health_service_two.json")
rec.Write(resp)
rec.Code = http.StatusOK
} else if req.URL.Path == "/v1/health/service/traceServiceName" {
resp, _ := ioutil.ReadFile("fixtures/consul/health_service_two.json")
rec.Write(resp)
rec.Code = http.StatusOK
} else if req.URL.Path == "/api/v1/series" {
// Just make the datadog bit work
rec.Code = http.StatusOK
} else if req.URL.Path == "/import" && req.Host == "10.1.10.12:8000" {
z, _ := zlib.NewReader(req.Body)
body, _ := ioutil.ReadAll(z)
defer req.Body.Close()
if strings.Contains(string(body), "a.b.c") {
rt.aReceived = true
}
rec.Code = http.StatusOK
} else if req.URL.Path == "/import" && req.Host == "10.1.10.13:8000" {
z, _ := zlib.NewReader(req.Body)
body, _ := ioutil.ReadAll(z)
defer req.Body.Close()
if strings.Contains(string(body), "x.b.c") {
rt.bReceived = true
}
rec.Code = http.StatusOK
} else {
assert.Fail(rt.t, "Received an unexpected request: %s %s", req.Host, req.URL.Path)
}
// If we've gotten all of them, fire!
if rt.aReceived && rt.bReceived {
rt.wg.Done()
}
return rec.Result(), nil
}
func TestAllowStaticServices(t *testing.T) {
proxyConfig := generateProxyConfig()
proxyConfig.ConsulForwardServiceName = ""
proxyConfig.ConsulTraceServiceName = ""
proxyConfig.ForwardAddress = "localhost:1234"
proxyConfig.TraceAddress = "localhost:1234"
server, error := NewProxyFromConfig(logrus.New(), proxyConfig)
assert.NoError(t, error, "Should start with just static services")
assert.False(t, server.usingConsul, "Server isn't using consul")
}
func TestMissingServices(t *testing.T) {
proxyConfig := generateProxyConfig()
proxyConfig.ForwardAddress = ""
proxyConfig.TraceAddress = ""
proxyConfig.ConsulForwardServiceName = ""
proxyConfig.ConsulTraceServiceName = ""
_, error := NewProxyFromConfig(logrus.New(), proxyConfig)
assert.Error(t, error, "No consul services means Proxy won't start")
}
func TestAcceptingBooleans(t *testing.T) {
proxyConfig := generateProxyConfig()
proxyConfig.ConsulTraceServiceName = ""
proxyConfig.TraceAddress = ""
server, _ := NewProxyFromConfig(logrus.New(), proxyConfig)
assert.True(t, server.AcceptingForwards, "Server accepts forwards")
assert.False(t, server.AcceptingTraces, "Server does not forward traces")
}
func TestConsistentForward(t *testing.T) {
// We need to set up a proxy, have a local veneur send to it, then verify
// that the proxy forwards to two downstream fake globals.
// TIME FOR SOME GAME THEORY
// Make the proxy
proxyConfig := generateProxyConfig()
proxyConfig.ForwardAddress = "localhost:1234"
proxyConfig.ConsulForwardServiceName = "forwardServiceName"
proxyConfig.Debug = true
wg := sync.WaitGroup{}
wg.Add(1)
transport := &ConsulTwoMetricRoundTripper{
t: t,
wg: &wg,
}
server, _ := NewProxyFromConfig(logrus.New(), proxyConfig)
server.HTTPClient.Transport = transport
defer server.Shutdown()
server.Start()
srv := httptest.NewServer(server.Handler())
defer srv.Close()
// Make sure we're sane first
assert.Len(t, server.ForwardDestinations.Members(), 2, "Incorrect host count in ring")
// Cool, now let's make a veneur to process some bits!
config := localConfig()
config.ForwardAddress = srv.URL
f := newFixture(t, config, nil, nil)
defer f.Close()
f.server.Workers[0].ProcessMetric(&samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "a.b.c",
Type: "histogram",
},
Value: float64(100),
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
})
f.server.Workers[0].ProcessMetric(&samplers.UDPMetric{
MetricKey: samplers.MetricKey{
Name: "x.b.c",
Type: "histogram",
},
Value: float64(100),
Digest: 12345,
SampleRate: 1.0,
Scope: samplers.MixedScope,
})
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
fmt.Println("GOT 'IM")
case <-time.After(3 * time.Second):
assert.Fail(t, "Failed to receive all metrics before timeout")
}
}
func TestTimeout(t *testing.T) {
cfg := generateProxyConfig()
cfg.ConsulTraceServiceName = ""
cfg.ConsulForwardServiceName = ""
never := make(chan struct{})
defer close(never)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-never
}))
defer ts.Close()
cfg.ForwardAddress = ts.URL
cfg.ForwardTimeout = "1ns" // just really really short
server, _ := NewProxyFromConfig(logrus.New(), cfg)
ctr := samplers.Counter{Name: "foo", Tags: []string{}}
ctr.Sample(20.0, 1.0)
jsonCtr, err := ctr.Export()
require.NoError(t, err)
metrics := []samplers.JSONMetric{jsonCtr}
// Now send the metrics to a server that never responds; we
// expect this to return before our (sufficiently long)
// timeout:
ch := make(chan struct{})
go func() {
server.ProxyMetrics(context.Background(), metrics, "foo.com")
close(ch)
}()
select {
case <-ch:
t.Log("Returned quickly, great.")
case <-time.After(3 * time.Second):
t.Fatal("Proxy took too long to time out. Is the deadline behavior working?")
}
}