From 2c328da094f06eaf1c946ca0156be40800037ca7 Mon Sep 17 00:00:00 2001 From: Denton Gentry Date: Sun, 10 Jan 2021 15:03:57 -0800 Subject: [PATCH] logtail: add a test to upload logs to local server Start an HTTP server to accept POST requests, and upload some logs to it. Check that uploaded logs were received. Code in logtail:drainPending was not being reliably exercised by other tests. This shows up in code coverage reports, as lines of code in drainPending are alternately added and subtracted from code coverage. This test will reliably exercise and verify this code. Signed-off-by: Denton Gentry --- logtail/logtail_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/logtail/logtail_test.go b/logtail/logtail_test.go index 5f435a8cd..5e3083cc3 100644 --- a/logtail/logtail_test.go +++ b/logtail/logtail_test.go @@ -6,6 +6,8 @@ package logtail import ( "context" + "net/http" + "net/http/httptest" "testing" "time" ) @@ -20,6 +22,27 @@ func TestFastShutdown(t *testing.T) { l.Shutdown(ctx) } +func TestUploadMessages(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + uploads := 0 + testServ := httptest.NewServer(http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + uploads += 1 + })) + + l := NewLogger(Config{BaseURL: testServ.URL}, t.Logf) + for i := 1; i < 10; i++ { + l.Write([]byte("log line")) + } + + l.Shutdown(ctx) + cancel() + if uploads == 0 { + t.Error("no log uploads") + } +} + var sink []byte func TestLoggerEncodeTextAllocs(t *testing.T) {