mirror of
https://github.com/tailscale/tailscale.git
synced 2026-07-15 01:53:08 -04:00
Move all the FooForTest methods on LocalBackend to instead be methods on a new unexported forTest type which is then given out to callers in other packages via an exported ForTest method (panicking in non-test contexts) that returns that unexported type. This is unusual style (exported returning unexported) but declutters godoc and makes call sites both more explicit and easier to read without the "ForTest" suffix polluting the symbols. Now FooForTest() changes into ForTest().Foo(). This was motivated by a pending change moving a bunch of code out of LocalBackend into other packages that required adding more ForTest methods to LocalBackend to keep the tests (now in other packages) working. Instead, do this refactor now so the future change is prettier. Updates #12614 Updates #cleanup Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Change-Id: Ib25e6d76d48dc8622ac3a955e0b1220d582e63a8
63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package lapitest
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"tailscale.com/control/controlclient"
|
|
"tailscale.com/ipn/ipnlocal"
|
|
"tailscale.com/ipn/store/mem"
|
|
"tailscale.com/types/logid"
|
|
"tailscale.com/wgengine"
|
|
)
|
|
|
|
// NewBackend returns a new [ipnlocal.LocalBackend] for testing purposes.
|
|
// It fails the test if the specified options are invalid or if the backend cannot be created.
|
|
func NewBackend(tb testing.TB, opts ...Option) *ipnlocal.LocalBackend {
|
|
tb.Helper()
|
|
options, err := newOptions(tb, opts...)
|
|
if err != nil {
|
|
tb.Fatalf("NewBackend: %v", err)
|
|
}
|
|
return newBackend(options)
|
|
}
|
|
|
|
func newBackend(opts *options) *ipnlocal.LocalBackend {
|
|
tb := opts.TB()
|
|
tb.Helper()
|
|
|
|
sys := opts.Sys()
|
|
if _, ok := sys.StateStore.GetOK(); !ok {
|
|
sys.Set(&mem.Store{})
|
|
}
|
|
|
|
e, err := wgengine.NewFakeUserspaceEngine(opts.Logf(), sys.Set, sys.HealthTracker.Get(), sys.UserMetricsRegistry(), sys.Bus.Get())
|
|
if err != nil {
|
|
opts.tb.Fatalf("NewFakeUserspaceEngine: %v", err)
|
|
}
|
|
tb.Cleanup(e.Close)
|
|
sys.Set(e)
|
|
|
|
b, err := ipnlocal.NewLocalBackend(opts.Logf(), logid.PublicID{}, sys, 0)
|
|
if err != nil {
|
|
tb.Fatalf("NewLocalBackend: %v", err)
|
|
}
|
|
tb.Cleanup(b.Shutdown)
|
|
b.ForTest().SetControlClientGetter(opts.MakeControlClient)
|
|
return b
|
|
}
|
|
|
|
// NewUnreachableControlClient is a [NewControlFn] that creates
|
|
// a new [controlclient.Client] for an unreachable control server.
|
|
func NewUnreachableControlClient(tb testing.TB, opts controlclient.Options) (controlclient.Client, error) {
|
|
tb.Helper()
|
|
opts.ServerURL = "https://127.0.0.1:1"
|
|
cc, err := controlclient.New(opts)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
}
|
|
return cc, nil
|
|
}
|