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

Attempt to fix flaky TestGetRoundTripper* tests #4738

Merged
merged 6 commits into from
Sep 9, 2023
Merged
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
71 changes: 43 additions & 28 deletions plugin/metrics/prometheus/metricsstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ package metricsstore

import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -518,6 +518,29 @@ func TestWarningResponse(t *testing.T) {
assert.Len(t, exp.GetSpans(), 1, "HTTP request was traced and span reported")
}

type fakePromServer struct {
*httptest.Server
authReceived atomic.Pointer[string]
}

func newFakePromServer(t *testing.T) *fakePromServer {
s := &fakePromServer{}
s.Server = httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
t.Logf("Request to fake Prometheus server %+v", r)
h := r.Header.Get("Authorization")
s.authReceived.Store(&h)
},
),
)
return s
}

func (s *fakePromServer) getAuth() string {
return *s.authReceived.Load()
}

func TestGetRoundTripperTLSConfig(t *testing.T) {
for _, tc := range []struct {
name string
Expand All @@ -537,13 +560,7 @@ func TestGetRoundTripperTLSConfig(t *testing.T) {
}, logger)
require.NoError(t, err)

server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "Bearer foo", r.Header.Get("Authorization"))
},
),
)
server := newFakePromServer(t)
defer server.Close()

req, err := http.NewRequestWithContext(
Expand All @@ -556,7 +573,9 @@ func TestGetRoundTripperTLSConfig(t *testing.T) {

resp, err := rt.RoundTrip(req)
require.NoError(t, err)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "Bearer foo", server.getAuth())
})
}
}
Expand All @@ -578,14 +597,10 @@ func TestGetRoundTripperTokenFile(t *testing.T) {
TokenOverrideFromContext: false,
}, nil)
require.NoError(t, err)
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "Bearer "+wantBearer, r.Header.Get("Authorization"))
},
),
)

server := newFakePromServer(t)
defer server.Close()

ctx := bearertoken.ContextWithBearerToken(context.Background(), "tokenFromRequest")
req, err := http.NewRequestWithContext(
ctx,
Expand All @@ -594,9 +609,12 @@ func TestGetRoundTripperTokenFile(t *testing.T) {
nil,
)
require.NoError(t, err)

resp, err := rt.RoundTrip(req)
require.NoError(t, err)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "Bearer "+wantBearer, server.getAuth())
}

func TestGetRoundTripperTokenFromContext(t *testing.T) {
Expand All @@ -613,14 +631,10 @@ func TestGetRoundTripperTokenFromContext(t *testing.T) {
TokenOverrideFromContext: true,
}, nil)
require.NoError(t, err)
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "Bearer tokenFromRequest", r.Header.Get("Authorization"))
},
),
)

server := newFakePromServer(t)
defer server.Close()

ctx := bearertoken.ContextWithBearerToken(context.Background(), "tokenFromRequest")
req, err := http.NewRequestWithContext(
ctx,
Expand All @@ -629,9 +643,12 @@ func TestGetRoundTripperTokenFromContext(t *testing.T) {
nil,
)
require.NoError(t, err)

resp, err := rt.RoundTrip(req)
require.NoError(t, err)

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "Bearer tokenFromRequest", server.getAuth())
}

func TestGetRoundTripperTokenError(t *testing.T) {
Expand Down Expand Up @@ -685,14 +702,12 @@ func startMockPrometheusServer(t *testing.T, wantPromQlQuery string, wantWarning
}))
}

func sendResponse(t *testing.T, w http.ResponseWriter, responseFile string) error {
func sendResponse(t *testing.T, w http.ResponseWriter, responseFile string) {
bytes, err := os.ReadFile(responseFile)
if err != nil {
return err
}
require.NoError(t, err)

_, err = fmt.Fprintln(w, string(bytes))
return err
_, err = w.Write(bytes)
require.NoError(t, err)
}

func buildTestBaseQueryParametersFrom(tc metricsTestCase) metricsstore.BaseQueryParameters {
Expand Down