From 125e168a91bf40783fdc27b3ba45e3936760b407 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Thu, 29 Jun 2023 12:18:56 +0200 Subject: [PATCH] Replace flaky server for log level handler test. (#9822) **What this PR does / why we need it**: I've noticed that the `TestLevelHandler` were flaky because the code wasn't waiting to the server to be running. Since the test is for a handler it's much simpler to call it directly with a response recorder. **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [ ] Documentation added - [x] Tests updated - [ ] `CHANGELOG.md` updated - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/upgrading/_index.md` - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](https://github.com/grafana/loki/commit/d10549e3ece02120974929894ee333d07755d213) --- pkg/util/log/log_test.go | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/pkg/util/log/log_test.go b/pkg/util/log/log_test.go index 3619524443..c96369925a 100644 --- a/pkg/util/log/log_test.go +++ b/pkg/util/log/log_test.go @@ -3,6 +3,7 @@ package log import ( "io" "net/http" + "net/http/httptest" "net/url" "strings" "testing" @@ -20,12 +21,6 @@ func TestLevelHandler(t *testing.T) { baseLogger: log.NewLogfmtLogger(io.Discard), } - // Start test http server - go func() { - err := http.ListenAndServe(":8080", LevelHandler(&lvl)) - assert.NoError(t, err) - }() - testCases := []struct { testName string targetLogLevel string @@ -42,22 +37,25 @@ func TestLevelHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.testName, func(t *testing.T) { var ( - resp *http.Response - err error + req *http.Request + err error ) if strings.HasPrefix(testCase.testName, "Get") { - resp, err = http.Get("http://localhost:8080/") + req, err = http.NewRequest("GET", "/", nil) } else if strings.HasPrefix(testCase.testName, "Post") { - resp, err = http.PostForm("http://localhost:8080/", url.Values{"log_level": {testCase.targetLogLevel}}) + form := url.Values{"log_level": {testCase.targetLogLevel}} + req, err = http.NewRequest("POST", "/", strings.NewReader(form.Encode())) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") } assert.NoError(t, err) - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - assert.NoError(t, err) - assert.JSONEq(t, testCase.expectedResponse, string(body)) - assert.Equal(t, testCase.expectedStatusCode, resp.StatusCode) + rr := httptest.NewRecorder() + handler := LevelHandler(&lvl) + handler.ServeHTTP(rr, req) + + assert.JSONEq(t, testCase.expectedResponse, rr.Body.String()) + assert.Equal(t, testCase.expectedStatusCode, rr.Code) assert.Equal(t, testCase.expectedLogLevel, lvl.String()) }) }