-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d65baf
commit 6bb118c
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package toxiproxy | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Shopify/toxiproxy/v2/collectors" | ||
"github.com/Shopify/toxiproxy/v2/stream" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
func TestProxyMetricsReceivedSentBytes(t *testing.T) { | ||
expectedMetrics := []string{ | ||
`toxiproxy_proxy_received_bytes_total{direction="upstream",listener="localhost:0",proxy="test",upstream="upstream"} 5`, | ||
`toxiproxy_proxy_sent_bytes_total{direction="upstream",listener="localhost:0",proxy="test",upstream="upstream"} 5`, | ||
} | ||
srv := NewServer(NewMetricsContainer(prometheus.NewRegistry())) | ||
srv.Metrics.ProxyMetrics = collectors.NewProxyMetricCollectors() | ||
proxy := NewProxy(srv) | ||
proxy.Name = "test" | ||
proxy.Listen = "localhost:0" | ||
proxy.Upstream = "upstream" | ||
r := bufio.NewReader(bytes.NewBufferString("hello")) | ||
w := &testWriteCloser{ | ||
bufio.NewWriter(bytes.NewBuffer([]byte{})), | ||
} | ||
proxy.Toxics.StartLink(srv, "testupstream", r, w, stream.Upstream) | ||
gotMetrics := prometheusOutput(t, srv, "toxiproxy") | ||
if !reflect.DeepEqual(gotMetrics, expectedMetrics) { | ||
t.Fatalf("expected: %v got: %v", expectedMetrics, gotMetrics) | ||
} | ||
} | ||
func TestRuntimeMetricsBuildInfo(t *testing.T) { | ||
expectedMetrics := []string{ | ||
`go_build_info{checksum="unknown",path="unknown",version="unknown"} 1`, | ||
} | ||
srv := NewServer(NewMetricsContainer(prometheus.NewRegistry())) | ||
srv.Metrics.RuntimeMetrics = collectors.NewRuntimeMetricCollectors() | ||
|
||
gotMetrics := prometheusOutput(t, srv, "go_build_info") | ||
if !reflect.DeepEqual(gotMetrics, expectedMetrics) { | ||
t.Fatalf("expected: %v got: %v", expectedMetrics, gotMetrics) | ||
} | ||
} | ||
|
||
type testWriteCloser struct { | ||
*bufio.Writer | ||
} | ||
|
||
func (t *testWriteCloser) Close() error { | ||
return t.Flush() | ||
} | ||
|
||
func prometheusOutput(t *testing.T, apiServer *ApiServer, prefix string) []string { | ||
t.Helper() | ||
|
||
testServer := httptest.NewServer(apiServer.Metrics.handler()) | ||
defer testServer.Close() | ||
resp, err := http.Get(testServer.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var selected []string | ||
s := bufio.NewScanner(resp.Body) | ||
for s.Scan() { | ||
if strings.HasPrefix(s.Text(), prefix) { | ||
selected = append(selected, s.Text()) | ||
} | ||
} | ||
return selected | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters