Skip to content

Commit

Permalink
Fix flaky TestAccessLogOnFailedRequest (#2573)
Browse files Browse the repository at this point in the history
Fixes #2571

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
  • Loading branch information
AlexanderYastrebov authored Sep 4, 2023
1 parent 96565f4 commit 7fe3412
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 50 deletions.
30 changes: 9 additions & 21 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2069,14 +2069,12 @@ func TestEnableAccessLogWithFilter(t *testing.T) {
}

func TestAccessLogOnFailedRequest(t *testing.T) {
buf := NewLockedBuffer()
logging.Init(logging.Options{
AccessLogOutput: buf})
testLog := NewTestLog()
defer testLog.Close()

s := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
s.Close()
logging.Init(logging.Options{AccessLogOutput: testLog})

p, err := newTestProxy(fmt.Sprintf(`* -> "%s"`, s.URL), 0)
p, err := newTestProxy(`* -> "http://bad-gateway.test"`, FlagsNone)
if err != nil {
t.Fatalf("Failed to create test proxy: %v", err)
return
Expand All @@ -2086,31 +2084,21 @@ func TestAccessLogOnFailedRequest(t *testing.T) {
ps := httptest.NewServer(p.proxy)
defer ps.Close()

rsp, err := http.Get(ps.URL)
rsp, err := ps.Client().Get(ps.URL)
if err != nil {
t.Fatalf("Failed to GET: %v", err)
return
}

defer rsp.Body.Close()

if rsp.StatusCode != http.StatusBadGateway {
t.Errorf("failed to return 502 Bad Gateway on failing backend connection: %d", rsp.StatusCode)
}

time.Sleep(time.Millisecond)
output := buf.String()

proxyURL, err := url.Parse(ps.URL)
if err != nil {
t.Fatalf("Failed to parse url: %v", err)
return
}

expected := fmt.Sprintf(`"GET / HTTP/1.1" %d %d "-" "Go-http-client/1.1"`, http.StatusBadGateway, len(http.StatusText(http.StatusBadGateway))+1)
if !strings.Contains(output, expected) || !strings.Contains(output, proxyURL.Host) {
t.Errorf("Failed to get accesslog '%v' '%v'", output, expected)
t.Logf("%s", cmp.Diff(output, expected))
const expected = `"GET / HTTP/1.1" 502 12 "-" "Go-http-client/1.1"`
if err = testLog.WaitFor(expected, 100*time.Millisecond); err != nil {
t.Errorf("Failed to get accesslog %v: %v", expected, err)
t.Logf("%s", cmp.Diff(testLog.String(), expected))
}
}

Expand Down
29 changes: 0 additions & 29 deletions proxy/utils.go

This file was deleted.

0 comments on commit 7fe3412

Please sign in to comment.