ipn/{ipnext,ipnlocal}: add a SafeBackend interface

Updates #12614

Change-Id: I197e673666e86ea74c19e3935ed71aec269b6c94
Co-authored-by: Nick Khyl <nickk@tailscale.com>
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2025-04-24 13:55:39 -07:00
committed by Brad Fitzpatrick
parent 25c4dc5fd7
commit 3d8533b5d0
8 changed files with 76 additions and 32 deletions

View File

@@ -27,7 +27,9 @@ import (
"tailscale.com/tailcfg"
"tailscale.com/tsd"
"tailscale.com/tstest"
"tailscale.com/tstime"
"tailscale.com/types/key"
"tailscale.com/types/lazy"
"tailscale.com/types/persist"
"tailscale.com/util/must"
)
@@ -284,7 +286,7 @@ func TestNewExtensionHost(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
logf := tstest.WhileTestRunningLogger(t)
h, err := NewExtensionHost(logf, tsd.NewSystem(), &testBackend{}, tt.defs...)
h, err := NewExtensionHostForTest(logf, &testBackend{}, tt.defs...)
if gotErr := err != nil; gotErr != tt.wantErr {
t.Errorf("NewExtensionHost: gotErr %v(%v); wantErr %v", gotErr, err, tt.wantErr)
}
@@ -1095,7 +1097,7 @@ func newExtensionHostForTest[T ipnext.Extension](t *testing.T, b Backend, initia
}
defs[i] = ipnext.DefinitionForTest(ext)
}
h, err := NewExtensionHost(logf, tsd.NewSystem(), b, defs...)
h, err := NewExtensionHostForTest(logf, b, defs...)
if err != nil {
t.Fatalf("NewExtensionHost: %v", err)
}
@@ -1320,6 +1322,7 @@ func (q *testExecQueue) Wait(context.Context) error { return nil }
// testBackend implements [ipnext.Backend] for testing purposes
// by calling the provided hooks when its methods are called.
type testBackend struct {
lazySys lazy.SyncValue[*tsd.System]
switchToBestProfileHook func(reason string)
// mu protects the backend state.
@@ -1328,6 +1331,13 @@ type testBackend struct {
mu sync.Mutex
}
func (b *testBackend) Clock() tstime.Clock { return tstime.StdClock{} }
func (b *testBackend) Sys() *tsd.System {
return b.lazySys.Get(tsd.NewSystem)
}
func (b *testBackend) SendNotify(ipn.Notify) { panic("not implemented") }
func (b *testBackend) TailscaleVarRoot() string { panic("not implemented") }
func (b *testBackend) SwitchToBestProfile(reason string) {
b.mu.Lock()
defer b.mu.Unlock()