tstest
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package tstest provides utilities for use in unit tests.
Index¶
- func CheckIsZero[T IsZeroable](t testing.TB, nonzeroValues map[reflect.Type]any)
- func FixLogs(t *testing.T)
- func KernelVersion() (major, minor, patch int)
- func MinAllocsPerRun(t *testing.T, target uint64, f func()) error
- func PanicOnLog()
- func Parallel(t *testing.T)
- func Replace[T any](t testing.TB, target *T, val T)
- func ResourceCheck(tb testing.TB)
- func Shard(t testing.TB)
- func SkipOnKernelVersions(t testing.TB, issue string, versions ...string)
- func SkipOnUnshardedCI(t testing.TB)
- func UnfixLogs(t *testing.T)
- func WaitFor(maxWait time.Duration, try func() error) error
- func WhileTestRunningLogger(t testenv.TB) logger.Logf
- type Clock
- func (c *Clock) Advance(d time.Duration) time.Time
- func (c *Clock) AdvanceTo(t time.Time)
- func (c *Clock) AfterFunc(d time.Duration, f func()) tstime.TimerController
- func (c *Clock) GetStart() time.Time
- func (c *Clock) GetStep() time.Duration
- func (c *Clock) NewTicker(d time.Duration) (tstime.TickerController, <-chan time.Time)
- func (c *Clock) NewTimer(d time.Duration) (tstime.TimerController, <-chan time.Time)
- func (c *Clock) Now() time.Time
- func (c *Clock) PeekNow() time.Time
- func (c *Clock) SetStep(d time.Duration)
- func (c *Clock) SetTimerChannelSize(n int)
- func (c *Clock) Since(t time.Time) time.Duration
- type ClockOpts
- type IsZeroable
- type LogLineTracker
- type MemLogger
- type Ticker
- type Timer
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
funcCheckIsZero¶added inv1.74.0
CheckIsZero checks that the IsZero method of a given type functionscorrectly, by instantiating a new value of that type, changing a field, andthen checking that the IsZero method returns false.
The nonzeroValues map should contain non-zero values for each type thatexists in the type T or any contained types. Basic types like string, bool,and numeric types are handled automatically.
funcKernelVersion¶added inv1.92.0
func KernelVersion() (major, minor, patchint)
KernelVersion returns the major, minor, and patch version of the Linux kernel.It returns (0, 0, 0) if the version cannot be determined.
funcMinAllocsPerRun¶added inv1.18.0
MinAllocsPerRun asserts that f can run with no more than target allocations.It runs f up to 1000 times or 5s, whichever happens first.If f has executed more than target allocations on every run, it returns a non-nil error.
MinAllocsPerRun sets GOMAXPROCS to 1 during its measurement and restoresit before returning.
funcPanicOnLog¶
func PanicOnLog()
PanicOnLog modifies the standard library log package's default output toan io.Writer that panics, to root out code that's not plumbing their loggingthrough explicit tailscale.com/logger.Logf paths.
funcReplace¶added inv1.38.0
Replace replaces the value of target with val.The old value is restored when the test ends.
funcResourceCheck¶
ResourceCheck takes a snapshot of the current goroutines and registers acleanup on tb to verify that after the rest, all goroutines created by thetest go away. (well, at least that the count matches. Maybe in the future itcan look at specific routines).
It panics if called from a parallel test.
funcShard¶added inv1.52.0
Shard skips t if it's not running if the TS_TEST_SHARD test shard is set to"n/m" and this test execution number in the process mod m is not equal to n-1.That is, to run with 4 shards, set TS_TEST_SHARD=1/4, ..., TS_TEST_SHARD=4/4for the four jobs.
funcSkipOnKernelVersions¶added inv1.92.0
SkipOnKernelVersions skips the test if the currentkernel version is in the specified list.
funcSkipOnUnshardedCI¶added inv1.52.0
SkipOnUnshardedCI skips t if we're in CI and the TS_TEST_SHARDenvironment variable isn't set.
Types¶
typeClock¶
type Clock struct {// contains filtered or unexported fields}Clock is a testing clock that advances every time its Now method iscalled, beginning at its start time. If no start time is specified usingClockBuilder, an arbitrary start time will be selected when the Clock iscreated and can be retrieved by calling Clock.Start().
funcNewClock¶added inv1.46.0
NewClock creates a Clock with the specified settings. To create aClock with only the default settings, new(Clock) is equivalent, except thatthe start time will not be computed until one of the receivers is called.
func (*Clock)Advance¶added inv1.0.0
Advance moves simulated time forward or backwards by a relative amount. AnyTimer or Ticker that is waiting will fire at the requested point in simulatedtime. Advance returns the new simulated time. If this Clock follows real timethen the next call to Now will equal the return value of Advance + theelapsed time since calling Advance. Otherwise, the next call to Now willequal the return value of Advance, regardless of the current step.
func (*Clock)AdvanceTo¶added inv1.46.0
AdvanceTo moves simulated time to a new absolute value. Any Timer or Tickerthat is waiting will fire at the requested point in simulated time. If thisClock follows real time then the next call to Now will equal t + the elapsedtime since calling Advance. Otherwise, the next call to Now will equal t,regardless of the configured step.
func (*Clock)AfterFunc¶added inv1.46.0
func (c *Clock) AfterFunc(dtime.Duration, f func())tstime.TimerController
AfterFunc returns a Timer that calls f when it fires, using this Clock foraccessing the current time.
func (*Clock)GetStart¶added inv1.46.0
GetStart returns the initial simulated time when this Clock was created.
func (*Clock)GetStep¶added inv1.46.0
GetStep returns the amount that simulated time advances on every call to Now.
func (*Clock)NewTicker¶added inv1.46.0
NewTicker returns a Ticker that uses this Clock for accessing the currenttime.
func (*Clock)NewTimer¶added inv1.46.0
NewTimer returns a Timer that uses this Clock for accessing the currenttime.
func (*Clock)Now¶
Now returns the virtual clock's current time, and advances itaccording to its step configuration.
func (*Clock)PeekNow¶added inv1.46.0
PeekNow returns the last time reported by Now. If Now has never been called,PeekNow returns the same value as GetStart.
func (*Clock)SetStep¶added inv1.46.0
SetStep updates the amount that simulated time advances on every call to Now.
func (*Clock)SetTimerChannelSize¶added inv1.46.0
SetTimerChannelSize changes the channel size for any Timer or Ticker createdin the future. It does not affect those that were already created.
typeClockOpts¶added inv1.46.0
type ClockOpts struct {// Start is the starting time for the Clock. When FollowRealTime is false,// Start is also the value that will be returned by the first call// to Clock.Now.Starttime.Time// Step is the amount of time the Clock will advance whenever Clock.Now is// called. If set to zero, the Clock will only advance when Clock.Advance is// called and/or if FollowRealTime is true.//// FollowRealTime and Step cannot be enabled at the same time.Steptime.Duration// TimerChannelSize configures the maximum buffered ticks that are// permitted in the channel of any Timer and Ticker created by this Clock.// The special value 0 means to use the default of 1. The buffer may need to// be increased if time is advanced by more than a single tick and proper// functioning of the test requires that the ticks are not lost.TimerChannelSizeint// FollowRealTime makes the simulated time increment along with real time.// It is a compromise between determinism and the difficulty of explicitly// managing the simulated time via Step or Clock.Advance. When// FollowRealTime is set, calls to Now() and PeekNow() will add the// elapsed real-world time to the simulated time.//// FollowRealTime and Step cannot be enabled at the same time.FollowRealTimebool}ClockOpts is used to configure the initial settings for a Clock. Once thesettings are configured as desired, call NewClock to get the resulting Clock.
typeIsZeroable¶added inv1.74.0
type IsZeroable interface {IsZero()bool}IsZeroable is the interface for things with an IsZero method.
typeLogLineTracker¶added inv1.2.0
type LogLineTracker struct {// contains filtered or unexported fields}LogLineTracker is a logger that tracks which log format patterns it'sseen and can report which expected ones were not seen later.
funcNewLogLineTracker¶added inv1.2.0
func NewLogLineTracker(logflogger.Logf, expectedFormatStrings []string) *LogLineTracker
NewLogLineTracker produces a LogLineTracker wrapping a given logf that tracks whether expectedFormatStrings were seen.
func (*LogLineTracker)Check¶added inv1.2.0
func (lt *LogLineTracker) Check() []string
Check returns which format strings haven't been logged yet.
func (*LogLineTracker)Close¶added inv1.4.0
func (lt *LogLineTracker) Close()
Close closes lt. After calling Close, calls to Logf become no-ops.
func (*LogLineTracker)Logf¶added inv1.2.0
func (lt *LogLineTracker) Logf(formatstring, args ...any)
Logf logs to its underlying logger and also tracks that the given format pattern has been seen.
func (*LogLineTracker)Reset¶added inv1.8.0
func (lt *LogLineTracker) Reset()
Reset forgets everything that it's seen.
typeMemLogger¶added inv1.14.6
MemLogger is a bytes.Buffer with a Logf method for tests that wantto log to a buffer.
typeTicker¶added inv1.46.0
type Ticker struct {C <-chantime.Time// The channel on which ticks are delivered.// contains filtered or unexported fields}Ticker is a time.Ticker lookalike for use in tests that need to control whenevents fire. Ticker could be made standalone in future but for now isexpected to be paired with a Clock and created by Clock.NewTicker.
func (*Ticker)Fire¶added inv1.46.0
Fire triggers the ticker. curTime is the timestamp to write to the channel.The next trigger time for the ticker is updated to the last computed triggertime + the ticker period (set at creation or using Reset). The next triggertime is computed this way to match standard time.Ticker behavior, whichprevents accumulation of long term drift caused by delays in event execution.
func (*Ticker)Reset¶added inv1.46.0
Reset adjusts the Ticker's period to d and reschedules the next fire time tothe current simulated time + d.
func (*Ticker)ResetAbsolute¶added inv1.46.0
ResetAbsolute adjusts the Ticker's period to d and reschedules the next firetime to nextTrigger.
typeTimer¶added inv1.46.0
type Timer struct {C <-chantime.Time// The channel on which ticks are delivered.// contains filtered or unexported fields}Timer is a time.Timer lookalike for use in tests that need to control whenevents fire. Timer could be made standalone in future but for now must bepaired with a Clock and created by Clock.NewTimer.
func (*Timer)Fire¶added inv1.46.0
Fire triggers the ticker. curTime is the timestamp to write to the channel.The next trigger time for the ticker is updated to the last computed triggertime + the ticker period (set at creation or using Reset). The next triggertime is computed this way to match standard time.Ticker behavior, whichprevents accumulation of long term drift caused by delays in event execution.
func (*Timer)Reset¶added inv1.46.0
Reset reschedules the next fire time to the current simulated time + d.Reset reports whether the timer was still active before the reset.
func (*Timer)ResetAbsolute¶added inv1.46.0
ResetAbsolute reschedules the next fire time to nextTrigger.ResetAbsolute reports whether the timer was still active before the reset.
Source Files¶
Directories¶
| Path | Synopsis |
|---|---|
Package chonktest contains a shared set of tests for the Chonk interface used to store AUM messages in Tailnet Lock, which we can share between different implementations. | Package chonktest contains a shared set of tests for the Chonk interface used to store AUM messages in Tailnet Lock, which we can share between different implementations. |
The deptest package contains a shared implementation of negative dependency tests for other packages, making sure we don't start depending on certain packages. | The deptest package contains a shared implementation of negative dependency tests for other packages, making sure we don't start depending on certain packages. |
Package integration contains Tailscale integration tests. | Package integration contains Tailscale integration tests. |
testcontrol Package testcontrol contains a minimal control plane server for testing purposes. | Package testcontrol contains a minimal control plane server for testing purposes. |
vms Package vms does VM-based integration/functional tests by using qemu and a bank of pre-made VM images. | Package vms does VM-based integration/functional tests by using qemu and a bank of pre-made VM images. |
Package iosdeps is a just a list of the packages we import on iOS, to let us test that our transitive closure of dependencies on iOS doesn't accidentally grow too large, as we've historically been memory constrained there. | Package iosdeps is a just a list of the packages we import on iOS, to let us test that our transitive closure of dependencies on iOS doesn't accidentally grow too large, as we've historically been memory constrained there. |
Package jsdeps is a just a list of the packages we import in the JavaScript/WASM build, to let us test that our transitive closure of dependencies doesn't accidentally grow too large, since binary size is more of a concern. | Package jsdeps is a just a list of the packages we import in the JavaScript/WASM build, to let us test that our transitive closure of dependencies doesn't accidentally grow too large, since binary size is more of a concern. |
The mts ("Multiple Tailscale") command runs multiple tailscaled instances for development, managing their directories and sockets, and lets you easily direct tailscale CLI commands to them. | The mts ("Multiple Tailscale") command runs multiple tailscaled instances for development, managing their directories and sockets, and lets you easily direct tailscale CLI commands to them. |
Package natlab lets us simulate different types of networks all in-memory without running VMs or requiring root, etc. | Package natlab lets us simulate different types of networks all in-memory without running VMs or requiring root, etc. |
vnet Package vnet simulates a virtual Internet containing a set of networks with various NAT behaviors. | Package vnet simulates a virtual Internet containing a set of networks with various NAT behaviors. |
Package nettest contains additional test helpers related to network state that can't go into tstest for circular dependency reasons. | Package nettest contains additional test helpers related to network state that can't go into tstest for circular dependency reasons. |
tkatest has functions for creating a mock control server that responds to TKA endpoints. | tkatest has functions for creating a mock control server that responds to TKA endpoints. |
Package tlstest contains code to help test Tailscale's TLS support without depending on real WebPKI roots or certificates during tests. | Package tlstest contains code to help test Tailscale's TLS support without depending on real WebPKI roots or certificates during tests. |
Package typewalk provides utilities to walk Go types using reflection. | Package typewalk provides utilities to walk Go types using reflection. |