lapitest

package
v1.84.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2025 License: BSD-3-Clause Imports: 26 Imported by: 0

Documentation

Overview

Package lapitest provides utilities for black-box testing of LocalAPI (ipnserver).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBackend

func NewBackend(tb testing.TB, opts ...Option) *ipnlocal.LocalBackend

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 NewUnreachableControlClient

func NewUnreachableControlClient(tb testing.TB, opts controlclient.Options) (controlclient.Client, error)

NewUnreachableControlClient is a NewControlFn that creates a new controlclient.Client for an unreachable control server.

Types

type Client

type Client struct {

	// Client is the underlying [local.Client] wrapped by the test client.
	// It is configured to send requests to the test server on behalf of the actor.
	*local.Client
	// Actor represents the user on whose behalf this client is making requests.
	// The server uses it to determine the client's identity and permissions.
	// The test can mutate the user to alter the actor's identity or permissions
	// before making a new request. It is typically an [ipnauth.TestActor],
	// unless the [Client] was created with s specific actor using [Server.ClientFor].
	Actor ipnauth.Actor
	// contains filtered or unexported fields
}

Client wraps a local.Client for testing purposes. It can be created using Server.Client, Server.ClientWithName, or Server.ClientFor and sends requests as the specified actor to the associated Server.

func (*Client) Username

func (c *Client) Username() string

Username returns username of the client's owner.

func (*Client) WatchIPNBus

WatchIPNBus is like local.Client.WatchIPNBus but returns a local.IPNBusWatcher that is closed when the test ends and a cancel function that stops the watcher. It fails the test if the underlying WatchIPNBus returns an error.

type NewControlFn

type NewControlFn func(tb testing.TB, opts controlclient.Options) (controlclient.Client, error)

NewControlFn is any function that creates a new controlclient.Client with the specified options.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is any optional configuration that can be passed to NewServer or NewBackend.

func WithBackend

func WithBackend(backend *ipnlocal.LocalBackend) Option

WithBackend returns an Option that configures the server to use the specified ipnlocal.LocalBackend instead of creating a new one. It is mutually exclusive with WithControlClient.

func WithContext

func WithContext(ctx context.Context) Option

WithContext returns an Option that sets the base context to be used by the Server.

func WithControlClient

func WithControlClient(newControl NewControlFn) Option

WithControlClient returns an option that specifies a function to be used by the ipnlocal.LocalBackend when creating a new controlclient.Client. It is mutually exclusive with WithBackend and is only used if no backend has been provided.

func WithLogging

func WithLogging(enableLogging bool) Option

WithLogging returns an Option that enables or disables logging.

func WithSys

func WithSys(sys *tsd.System) Option

WithSys returns an Option that sets the tsd.System to be used when creating a new ipnlocal.LocalBackend.

type Server

type Server struct {
	// contains filtered or unexported fields
}

A Server is an in-process LocalAPI server that can be used in end-to-end tests.

func NewServer

func NewServer(tb testing.TB, opts ...Option) *Server

NewServer starts and returns a new Server with the specified options.

func NewUnstartedServer

func NewUnstartedServer(tb testing.TB, opts ...Option) *Server

NewUnstartedServer returns a new Server with the specified options without starting it.

func (*Server) Backend

func (s *Server) Backend() *ipnlocal.LocalBackend

Backend returns the underlying ipnlocal.LocalBackend.

func (*Server) BlockWhileInUse

func (s *Server) BlockWhileInUse(ctx context.Context) error

BlockWhileInUse blocks until the server becomes idle (no active requests), or the context is done. It returns the context's error if it is done. It is used in tests only.

func (*Server) BlockWhileInUseByOther

func (s *Server) BlockWhileInUseByOther(ctx context.Context, actor ipnauth.Actor) error

BlockWhileInUseByOther blocks while the specified actor can't connect to the server due to another actor being connected. It is used in tests only.

func (*Server) CheckCurrentUser

func (s *Server) CheckCurrentUser(want ipnauth.Actor)

CheckCurrentUser fails the test if the current user does not match the expected user. It is only used on Windows and will be removed as we progress on tailscale/corp#18342.

func (*Server) Client

func (s *Server) Client() *Client

Client returns a new Client configured for making requests to the server as a new ipnauth.TestActor with a unique username and ipnauth.ClientID.

func (*Server) ClientFor

func (s *Server) ClientFor(actor ipnauth.Actor) *Client

ClientFor returns a new Client configured for making requests to the server as the specified actor.

func (*Server) ClientWithName

func (s *Server) ClientWithName(name string) *Client

ClientWithName returns a new Client configured for making requests to the server as a new ipnauth.TestActor with the specified name and a unique ipnauth.ClientID.

func (*Server) Close

func (s *Server) Close()

Close shuts down the server and blocks until all outstanding requests on this server have completed.

func (*Server) MakeTestActor

func (s *Server) MakeTestActor(name string, clientID string) *ipnauth.TestActor

MakeTestActor returns a new ipnauth.TestActor with the specified name and client ID. If the name is empty, a unique sequential name is generated. Likewise, if clientID is empty, a unique sequential client ID is generated.

func (*Server) Start

func (s *Server) Start()

Start starts the server from NewUnstartedServer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL