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

log packages lint fixes and test for loki flushing at end #3196

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion log/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestFileHookFromConfigLine(t *testing.T) {
}

require.NoError(t, err)
assert.NotNil(t, res.(*fileHook).w)
assert.NotNil(t, res.(*fileHook).w) //nolint:forcetypeassert
})
}
}
Expand Down
55 changes: 53 additions & 2 deletions log/loki_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package log

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -69,23 +74,27 @@ func TestSyslogFromConfigLine(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.line, func(t *testing.T) {
t.Parallel()
// no parallel because this is way too fast and parallel will only slow it down

res, err := LokiFromConfigLine(nil, test.line)
if test.err {
require.Error(t, err)
return
}
hook, ok := res.(*lokiHook)
require.True(t, ok)
require.NoError(t, err)

test.res.client = res.(*lokiHook).client
test.res.ch = res.(*lokiHook).ch
test.res.client = hook.client
test.res.ch = hook.ch
require.Equal(t, &test.res, res)
})
}
}

func TestLogEntryMarshal(t *testing.T) {
t.Parallel()
entry := logEntry{
t: 9223372036854775807, // the actual max
msg: "something",
Expand All @@ -98,6 +107,7 @@ func TestLogEntryMarshal(t *testing.T) {
}

func TestFilterLabels(t *testing.T) {
t.Parallel()
cases := []struct {
allowedLabels []string
labels map[string]string
Expand Down Expand Up @@ -131,6 +141,7 @@ func TestFilterLabels(t *testing.T) {
for i, c := range cases {
c := c
t.Run(fmt.Sprint(i), func(t *testing.T) {
t.Parallel()
h := &lokiHook{}
h.allowedLabels = c.allowedLabels
result := h.filterLabels(c.labels, c.msg)
Expand All @@ -139,3 +150,43 @@ func TestFilterLabels(t *testing.T) {
})
}
}

func TestLokiFlushingOnStop(t *testing.T) {
t.Parallel()
receivedData := make(chan string, 1)
srv := httptest.NewServer(
http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
b, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
receivedData <- string(b)
close(receivedData) // this is mostly as this should never be called twice, so panicking here in that case
}),
)
configLine := fmt.Sprintf("loki=%s,pushPeriod=1h", srv.URL)
h, err := LokiFromConfigLine(nil, configLine)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
wg := new(sync.WaitGroup)
now := time.Now()
wg.Add(1)
go func() {
defer wg.Done()
err = h.Fire(&logrus.Entry{Time: now, Level: logrus.InfoLevel, Message: "test message"})
time.Sleep(time.Millisecond * 10)
olegbespalov marked this conversation as resolved.
Show resolved Hide resolved
cancel()
}()
h.Listen(ctx)
wg.Wait()
select {
case data := <-receivedData:
require.JSONEq(t,
fmt.Sprintf(
`{"streams":[{"stream":{"level":"info"},"values":[["%d","test message"]]}]}`, now.UnixNano()),
data)
default:
t.Fatal("No logs were received from loki before hook has finished")
}
}