2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package logtail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-12 21:31:45 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2021-01-10 23:03:57 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-01-12 21:31:45 +00:00
|
|
|
"strings"
|
2020-02-05 22:16:58 +00:00
|
|
|
"testing"
|
2020-04-04 21:16:33 +00:00
|
|
|
"time"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFastShutdown(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
2021-01-11 14:22:35 +00:00
|
|
|
testServ := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {}))
|
|
|
|
defer testServ.Close()
|
|
|
|
|
2020-12-21 17:03:39 +00:00
|
|
|
l := NewLogger(Config{
|
2021-01-11 14:22:35 +00:00
|
|
|
BaseURL: testServ.URL,
|
Add tstest.PanicOnLog(), and fix various problems detected by this.
If a test calls log.Printf, 'go test' horrifyingly rearranges the
output to no longer be in chronological order, which makes debugging
virtually impossible. Let's stop that from happening by making
log.Printf panic if called from any module, no matter how deep, during
tests.
This required us to change the default error handler in at least one
http.Server, as well as plumbing a bunch of logf functions around,
especially in magicsock and wgengine, but also in logtail and backoff.
To add insult to injury, 'go test' also rearranges the output when a
parent test has multiple sub-tests (all the sub-test's t.Logf is always
printed after all the parent tests t.Logf), so we need to screw around
with a special Logf that can point at the "current" t (current_t.Logf)
in some places. Probably our entire way of using subtests is wrong,
since 'go test' would probably like to run them all in parallel if you
called t.Parallel(), but it definitely can't because the're all
manipulating the shared state created by the parent test. They should
probably all be separate toplevel tests instead, with common
setup/teardown logic. But that's a job for another time.
Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
2020-05-14 02:59:54 +00:00
|
|
|
}, t.Logf)
|
2020-02-05 22:16:58 +00:00
|
|
|
l.Shutdown(ctx)
|
|
|
|
}
|
2020-04-04 21:16:33 +00:00
|
|
|
|
2021-01-12 21:31:45 +00:00
|
|
|
// accumulate some logs before the server becomes available, exercise the drain path
|
|
|
|
func TestDrainPendingMessages(t *testing.T) {
|
2021-01-10 23:03:57 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-01-12 21:31:45 +00:00
|
|
|
uploaded := make(chan int)
|
|
|
|
bodytext := ""
|
2021-01-10 23:03:57 +00:00
|
|
|
testServ := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
2021-01-12 21:31:45 +00:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to read HTTP request")
|
|
|
|
}
|
|
|
|
bodytext += "\n" + string(body)
|
|
|
|
|
|
|
|
uploaded <- 0
|
2021-01-10 23:03:57 +00:00
|
|
|
}))
|
2021-01-11 14:22:35 +00:00
|
|
|
defer testServ.Close()
|
2021-01-10 23:03:57 +00:00
|
|
|
|
|
|
|
l := NewLogger(Config{BaseURL: testServ.URL}, t.Logf)
|
2021-01-12 21:31:45 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
2021-01-10 23:03:57 +00:00
|
|
|
l.Write([]byte("log line"))
|
|
|
|
}
|
|
|
|
|
2021-01-12 21:31:45 +00:00
|
|
|
select {
|
|
|
|
case <-uploaded:
|
|
|
|
if strings.Count(bodytext, "log line") == 3 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Errorf("Timed out waiting for log uploads")
|
|
|
|
}
|
|
|
|
|
2021-01-10 23:03:57 +00:00
|
|
|
l.Shutdown(ctx)
|
|
|
|
cancel()
|
2021-01-12 21:31:45 +00:00
|
|
|
if strings.Count(bodytext, "log line") != 3 {
|
|
|
|
t.Errorf("got %q; want: 3 copies of %q", bodytext, "log line")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncodeAndUploadMessages(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var jsonbody []byte
|
|
|
|
uploaded := make(chan int)
|
|
|
|
testServ := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to read HTTP request")
|
|
|
|
}
|
|
|
|
jsonbody = body
|
|
|
|
uploaded <- 0
|
|
|
|
}))
|
|
|
|
defer testServ.Close()
|
|
|
|
|
|
|
|
l := NewLogger(Config{BaseURL: testServ.URL}, t.Logf)
|
|
|
|
|
|
|
|
// There is always an initial "logtail started" message
|
|
|
|
<-uploaded
|
|
|
|
if !strings.Contains(string(jsonbody), "started") {
|
|
|
|
t.Errorf("initialize: got:%q; want:%q", string(jsonbody), "started")
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
log string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"plain text",
|
|
|
|
"log line",
|
|
|
|
"log line",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"simple JSON",
|
|
|
|
`{"text": "log line"}`,
|
|
|
|
"log line",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"escaped characters",
|
|
|
|
`\b\f\n\r\t"\\`,
|
|
|
|
`\b\f\n\r\t"\\`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
io.WriteString(l, tt.log)
|
|
|
|
<-uploaded
|
|
|
|
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
err := json.Unmarshal(jsonbody, &data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got, ok := data["text"]
|
|
|
|
if ok {
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%s: got %q; want %q", tt.name, got.(string), tt.want)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("no text in: %v", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
ltail, ok := data["logtail"]
|
|
|
|
if ok {
|
|
|
|
logtailmap := ltail.(map[string]interface{})
|
|
|
|
_, ok = logtailmap["client_time"]
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("%s: no client_time present", tt.name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("%s: no logtail map present", tt.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test some special cases
|
|
|
|
|
|
|
|
// JSON log message already contains a logtail field.
|
|
|
|
io.WriteString(l, `{"logtail": "LOGTAIL", "text": "text"}`)
|
|
|
|
<-uploaded
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
err := json.Unmarshal(jsonbody, &data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
error_has_logtail, ok := data["error_has_logtail"]
|
|
|
|
if ok {
|
|
|
|
if error_has_logtail.(string) != "LOGTAIL" {
|
|
|
|
t.Errorf("error_has_logtail: got:%q; want:%q",
|
|
|
|
error_has_logtail.(string), "LOGTAIL")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("no error_has_logtail field: %v", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// skipClientTime to omit the logtail metadata
|
|
|
|
l.skipClientTime = true
|
|
|
|
io.WriteString(l, "text")
|
|
|
|
<-uploaded
|
|
|
|
data = make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(jsonbody, &data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
_, ok = data["logtail"]
|
|
|
|
if ok {
|
|
|
|
t.Errorf("skipClientTime: unexpected logtail map present: %v", data)
|
2021-01-10 23:03:57 +00:00
|
|
|
}
|
2021-01-12 21:31:45 +00:00
|
|
|
|
|
|
|
// lowMem + long string
|
|
|
|
l.skipClientTime = false
|
|
|
|
l.lowMem = true
|
|
|
|
longStr := strings.Repeat("0", 512)
|
|
|
|
io.WriteString(l, longStr)
|
|
|
|
<-uploaded
|
|
|
|
data = make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(jsonbody, &data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
text, ok := data["text"]
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("lowMem: no text %v", data)
|
|
|
|
}
|
|
|
|
if len(text.(string)) > 300 {
|
|
|
|
t.Errorf("lowMem: got %d chars; want <300 chars", len(text.(string)))
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Shutdown(ctx)
|
|
|
|
cancel()
|
2021-01-10 23:03:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 21:16:33 +00:00
|
|
|
var sink []byte
|
|
|
|
|
|
|
|
func TestLoggerEncodeTextAllocs(t *testing.T) {
|
2020-12-21 17:03:39 +00:00
|
|
|
lg := &Logger{timeNow: time.Now}
|
2020-04-04 21:16:33 +00:00
|
|
|
inBuf := []byte("some text to encode")
|
|
|
|
n := testing.AllocsPerRun(1000, func() {
|
|
|
|
sink = lg.encodeText(inBuf, false)
|
|
|
|
})
|
|
|
|
if int(n) != 1 {
|
|
|
|
t.Logf("allocs = %d; want 1", int(n))
|
|
|
|
}
|
|
|
|
}
|
2020-07-25 10:40:18 +00:00
|
|
|
|
|
|
|
func TestLoggerWriteLength(t *testing.T) {
|
2020-12-21 17:03:39 +00:00
|
|
|
lg := &Logger{
|
2020-07-25 10:40:18 +00:00
|
|
|
timeNow: time.Now,
|
|
|
|
buffer: NewMemoryBuffer(1024),
|
|
|
|
}
|
|
|
|
inBuf := []byte("some text to encode")
|
|
|
|
n, err := lg.Write(inBuf)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if n != len(inBuf) {
|
|
|
|
t.Errorf("logger.Write wrote %d bytes, expected %d", n, len(inBuf))
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 21:31:45 +00:00
|
|
|
|
|
|
|
func TestParseAndRemoveLogLevel(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
log string
|
|
|
|
wantLevel int
|
|
|
|
wantLog string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no level",
|
|
|
|
0,
|
|
|
|
"no level",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"[v1] level 1",
|
|
|
|
1,
|
|
|
|
"level 1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"level 1 [v1] ",
|
|
|
|
1,
|
|
|
|
"level 1 ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"[v2] level 2",
|
|
|
|
2,
|
|
|
|
"level 2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"level [v2] 2",
|
|
|
|
2,
|
|
|
|
"level 2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"[v3] no level 3",
|
|
|
|
0,
|
|
|
|
"[v3] no level 3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
gotLevel, gotLog := parseAndRemoveLogLevel([]byte(tt.log))
|
|
|
|
if gotLevel != tt.wantLevel {
|
|
|
|
t.Errorf("%q: got:%d; want %d", tt.log, gotLevel, tt.wantLevel)
|
|
|
|
}
|
|
|
|
if string(gotLog) != tt.wantLog {
|
|
|
|
t.Errorf("%q: got:%q; want %q", tt.log, gotLog, tt.wantLog)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|