forked from stripe/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_test.go
181 lines (155 loc) · 4.77 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
package veneur
import (
"compress/zlib"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stripe/veneur/samplers"
)
// On the CI server, we can't be guaranteed that the port will be
// released immediately after the server is shut down. Instead, use
// a unique port for each test. As long as we don't have an insane number
// of integration tests, we should be fine.
var ProxyHTTPAddrPort = 8229
func generateProxyConfig() ProxyConfig {
port := ProxyHTTPAddrPort
ProxyHTTPAddrPort++
return ProxyConfig{
Debug: false,
ConsulRefreshInterval: "86400s",
ConsulForwardServiceName: "forwardServiceName",
ConsulTraceServiceName: "traceServiceName",
TraceAddress: "127.0.0.1:8128",
TraceAPIAddress: "127.0.0.1:8135",
HTTPAddress: fmt.Sprintf("127.0.0.1:%d", port),
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 TestMissingServices(t *testing.T) {
proxyConfig := generateProxyConfig()
proxyConfig.ConsulForwardServiceName = ""
proxyConfig.ConsulTraceServiceName = ""
_, error := NewProxyFromConfig(proxyConfig)
assert.Error(t, error, "No consul services means Proxy won't start")
}
func TestAcceptingBooleans(t *testing.T) {
proxyConfig := generateProxyConfig()
proxyConfig.ConsulTraceServiceName = ""
server, _ := NewProxyFromConfig(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(proxyConfig)
server.HTTPClient.Transport = transport
defer server.Shutdown()
server.Start()
go server.HTTPServe()
// 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 = fmt.Sprintf("http://%s", proxyConfig.HTTPAddress)
f := newFixture(t, config)
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")
}
}