Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add fail case for Proxy remote write #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions internal/remotewrite/proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package remotewrite

import (
"bytes"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/observatorium/observatorium/internal"
"github.com/prometheus/client_golang/prometheus"
)

func TestProxy(t *testing.T) {
logger := internal.NewLogger("debug", "logfmt", "test")

// remoteWriteMain is the primary remote write endpoint that always returns 403 Forbidden.
remoteWriteMain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Log("msg", "remote write main")
w.WriteHeader(http.StatusForbidden)
}))
defer remoteWriteMain.Close()

// remoteWriteMirror is a secondary remote write endpoint that always returns 403 Forbidden.
remoteWriteMirror := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Log("msg", "remote write mirror")
w.WriteHeader(http.StatusForbidden)
}))
defer remoteWriteMirror.Close()

reg := prometheus.NewRegistry()

writeURL, err := url.Parse(remoteWriteMain.URL)
if err != nil {
t.Fatal(err)
}

endpoints := []Endpoint{
{
Name: "mirror",
URL: remoteWriteMirror.URL,
},
}
gateway := httptest.NewServer(Proxy(writeURL, endpoints, logger, reg))
defer gateway.Close()

req, err := http.NewRequest(http.MethodPost, gateway.URL, bytes.NewBufferString("some metrics here :)"))
if err != nil {
t.Fatal(err)
}

client := http.DefaultClient

res, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
_ = res.Body.Close()
if res.StatusCode != http.StatusForbidden {
t.Fatalf("expected status code %d, got %d", http.StatusForbidden, res.StatusCode)
}
}