2021-01-21 20:33:54 +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 wglog_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-29 20:52:20 +00:00
|
|
|
"tailscale.com/types/wgkey"
|
2021-01-29 20:16:36 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
2021-01-21 20:33:54 +00:00
|
|
|
"tailscale.com/wgengine/wglog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogger(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-04-27 17:03:13 +00:00
|
|
|
format string
|
|
|
|
args []interface{}
|
|
|
|
want string
|
|
|
|
omit bool
|
2021-01-21 20:33:54 +00:00
|
|
|
}{
|
2021-04-27 17:03:13 +00:00
|
|
|
{"hi", nil, "hi", false},
|
|
|
|
{"Routine: starting", nil, "", true},
|
|
|
|
{"%v says it misses you", []interface{}{stringer("peer(IMTB…r7lM)")}, "[IMTBr] says it misses you", false},
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 17:03:13 +00:00
|
|
|
type log struct {
|
|
|
|
format string
|
|
|
|
args []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := make(chan log, 1)
|
2021-01-21 20:33:54 +00:00
|
|
|
logf := func(format string, args ...interface{}) {
|
|
|
|
select {
|
2021-04-27 17:03:13 +00:00
|
|
|
case c <- log{format, args}:
|
2021-01-21 20:33:54 +00:00
|
|
|
default:
|
2021-04-27 17:03:13 +00:00
|
|
|
t.Errorf("wrote %q, but shouldn't have", fmt.Sprintf(format, args...))
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x := wglog.NewLogger(logf)
|
2021-04-29 20:52:20 +00:00
|
|
|
key, err := wgkey.ParseHex("20c4c1ae54e1fd37cab6e9a532ca20646aff496796cc41d4519560e5e82bee53")
|
2021-01-21 20:33:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
x.SetPeers([]wgcfg.Peer{{PublicKey: key}})
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
if tt.omit {
|
|
|
|
// Write a message ourselves into the channel.
|
|
|
|
// Then if logf also attempts to write into the channel, it'll fail.
|
2021-04-27 17:03:13 +00:00
|
|
|
c <- log{}
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
2021-04-27 17:03:13 +00:00
|
|
|
x.DeviceLogger.Errorf(tt.format, tt.args...)
|
|
|
|
gotLog := <-c
|
2021-01-21 20:33:54 +00:00
|
|
|
if tt.omit {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-27 17:03:13 +00:00
|
|
|
if got := fmt.Sprintf(gotLog.format, gotLog.args...); got != tt.want {
|
|
|
|
t.Errorf("Printf(%q, %v) = %q want %q", tt.format, tt.args, got, tt.want)
|
2021-01-21 20:33:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 17:03:13 +00:00
|
|
|
|
|
|
|
func stringer(s string) stringerString {
|
|
|
|
return stringerString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type stringerString string
|
|
|
|
|
|
|
|
func (s stringerString) String() string { return string(s) }
|