2020-08-25 19:42:54 +01: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.
|
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
package ipnlocal
|
2020-08-25 19:42:54 +01:00
|
|
|
|
|
|
|
import (
|
2020-09-04 16:36:07 +01:00
|
|
|
"reflect"
|
2020-08-25 19:42:54 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-04 21:12:42 +00:00
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/ipn/ipnstate"
|
2022-02-28 21:08:45 +00:00
|
|
|
"tailscale.com/ipn/store/mem"
|
2020-08-25 19:42:54 +01:00
|
|
|
"tailscale.com/logtail"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/tstest"
|
|
|
|
"tailscale.com/types/key"
|
2021-05-05 04:36:10 +01:00
|
|
|
"tailscale.com/types/logger"
|
2021-02-05 23:23:01 +00:00
|
|
|
"tailscale.com/types/persist"
|
2020-08-25 19:42:54 +01:00
|
|
|
"tailscale.com/wgengine"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestLocalLogLines tests to make sure that the log lines required for log parsing are
|
|
|
|
// being logged by the expected functions. Update these tests if moving log lines between
|
|
|
|
// functions.
|
|
|
|
func TestLocalLogLines(t *testing.T) {
|
2020-09-04 16:09:56 +01:00
|
|
|
logListen := tstest.NewLogLineTracker(t.Logf, []string{
|
2020-08-25 19:42:54 +01:00
|
|
|
"SetPrefs: %v",
|
2020-12-21 18:58:06 +00:00
|
|
|
"[v1] peer keys: %s",
|
|
|
|
"[v1] v%v peers: %v",
|
2020-08-25 19:42:54 +01:00
|
|
|
})
|
2021-01-09 00:50:03 +00:00
|
|
|
defer logListen.Close()
|
2020-08-25 19:42:54 +01:00
|
|
|
|
2021-05-05 04:36:10 +01:00
|
|
|
// Put a rate-limiter with a burst of 0 between the components below.
|
|
|
|
// This instructs the rate-limiter to eliminate all logging that
|
|
|
|
// isn't explicitly exempt from rate-limiting.
|
|
|
|
// This lets the logListen tracker verify that the rate-limiter allows these key lines.
|
|
|
|
logf := logger.RateLimitedFnWithClock(logListen.Logf, 5*time.Second, 0, 10, time.Now)
|
|
|
|
|
2020-08-25 19:42:54 +01:00
|
|
|
logid := func(hex byte) logtail.PublicID {
|
|
|
|
var ret logtail.PublicID
|
|
|
|
for i := 0; i < len(ret); i++ {
|
|
|
|
ret[i] = hex
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
idA := logid(0xaa)
|
|
|
|
|
|
|
|
// set up a LocalBackend, super bare bones. No functional data.
|
2022-02-28 21:08:45 +00:00
|
|
|
store := new(mem.Store)
|
2021-05-05 04:36:10 +01:00
|
|
|
e, err := wgengine.NewFakeUserspaceEngine(logf, 0)
|
2020-08-25 19:42:54 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-15 19:48:49 +01:00
|
|
|
t.Cleanup(e.Close)
|
2020-08-25 19:42:54 +01:00
|
|
|
|
2022-02-18 20:55:22 +00:00
|
|
|
lb, err := NewLocalBackend(logf, idA.String(), store, nil, e, 0)
|
2020-08-25 19:42:54 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-09-04 16:36:07 +01:00
|
|
|
defer lb.Shutdown()
|
2020-08-25 19:42:54 +01:00
|
|
|
|
|
|
|
// custom adjustments for required non-nil fields
|
2021-02-04 21:12:42 +00:00
|
|
|
lb.prefs = ipn.NewPrefs()
|
2020-08-25 19:42:54 +01:00
|
|
|
lb.hostinfo = &tailcfg.Hostinfo{}
|
|
|
|
// hacky manual override of the usual log-on-change behaviour of keylogf
|
|
|
|
lb.keyLogf = logListen.Logf
|
|
|
|
|
2020-09-04 16:36:07 +01:00
|
|
|
testWantRemain := func(wantRemain ...string) func(t *testing.T) {
|
2020-08-25 19:42:54 +01:00
|
|
|
return func(t *testing.T) {
|
2020-09-04 16:36:07 +01:00
|
|
|
if remain := logListen.Check(); !reflect.DeepEqual(remain, wantRemain) {
|
2021-05-05 04:36:10 +01:00
|
|
|
t.Helper()
|
2020-09-04 16:36:07 +01:00
|
|
|
t.Errorf("remain %q, want %q", remain, wantRemain)
|
2020-08-25 19:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log prefs line
|
2021-02-05 23:23:01 +00:00
|
|
|
persist := &persist.Persist{}
|
2021-02-04 21:12:42 +00:00
|
|
|
prefs := ipn.NewPrefs()
|
2020-08-25 19:42:54 +01:00
|
|
|
prefs.Persist = persist
|
|
|
|
lb.SetPrefs(prefs)
|
|
|
|
|
2020-12-21 18:58:06 +00:00
|
|
|
t.Run("after_prefs", testWantRemain("[v1] peer keys: %s", "[v1] v%v peers: %v"))
|
2020-08-25 19:42:54 +01:00
|
|
|
|
|
|
|
// log peers, peer keys
|
2021-05-05 04:36:10 +01:00
|
|
|
lb.mu.Lock()
|
|
|
|
lb.parseWgStatusLocked(&wgengine.Status{
|
2021-02-04 21:12:42 +00:00
|
|
|
Peers: []ipnstate.PeerStatusLite{{
|
2020-08-25 19:42:54 +01:00
|
|
|
TxBytes: 10,
|
|
|
|
RxBytes: 10,
|
|
|
|
LastHandshake: time.Now(),
|
2021-11-02 01:38:24 +00:00
|
|
|
NodeKey: key.NewNode().Public(),
|
2020-08-25 19:42:54 +01:00
|
|
|
}},
|
2021-05-05 04:36:10 +01:00
|
|
|
})
|
2020-10-29 22:26:10 +00:00
|
|
|
lb.mu.Unlock()
|
2020-08-25 19:42:54 +01:00
|
|
|
|
2020-09-04 16:36:07 +01:00
|
|
|
t.Run("after_peers", testWantRemain())
|
2021-05-05 04:36:10 +01:00
|
|
|
|
|
|
|
// Log it again with different stats to ensure it's not dup-suppressed.
|
|
|
|
logListen.Reset()
|
|
|
|
lb.mu.Lock()
|
|
|
|
lb.parseWgStatusLocked(&wgengine.Status{
|
|
|
|
Peers: []ipnstate.PeerStatusLite{{
|
|
|
|
TxBytes: 11,
|
|
|
|
RxBytes: 12,
|
|
|
|
LastHandshake: time.Now(),
|
2021-11-02 01:38:24 +00:00
|
|
|
NodeKey: key.NewNode().Public(),
|
2021-05-05 04:36:10 +01:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
lb.mu.Unlock()
|
|
|
|
t.Run("after_second_peer_status", testWantRemain("SetPrefs: %v"))
|
2020-08-25 19:42:54 +01:00
|
|
|
}
|