testenv

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 29 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testenv runs an in-process nats-server in operator mode for integration tests. It mirrors production: an operator, a system account, and account MASH with JetStream, whose users are issued by MASH's signing key and carry mash's real permission template.

Index

Constants

View Source
const GitTimeout = 60 * time.Second

GitTimeout bounds every git subprocess a test runs through RunGit: a hang fails that one test with the output so far, instead of waiting for the whole package's test timeout.

View Source
const Owner = "o"

Owner is the SAP owner used in tests.

Variables

This section is empty.

Functions

func HermeticGit

func HermeticGit() (cleanup func(), err error)

HermeticGit makes the git a test package runs independent of the machine it runs on: no global or system config (credential helpers, url.insteadOf, hooksPath, signing), no prompts, no askpass, no ssh command, and a throwaway HOME so ~/.gitconfig, ~/.ssh and keychains are never read. Call it from TestMain before m.Run and run the returned cleanup after. Go's own caches are pinned first, since they default to paths under HOME.

func OtherCA added in v0.3.1

func OtherCA(t *testing.T) string

OtherCA writes the PEM of a CA that signed nothing the server holds, to test a CA the server does not chain to.

func RunGit

func RunGit(args ...string) ([]byte, error)

RunGit runs git with args and returns its combined output. It gives up after GitTimeout.

Types

type AckDropProxy

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

AckDropProxy is a TCP proxy that loses a publisher's acknowledgement: once the client has published to a given subject, nothing more flows from the server to that client connection. The server still stores the message, so the client sees a timeout for a send that did land, the case a retry must not turn into a second copy.

func NewAckDropProxy

func NewAckDropProxy(t *testing.T, natsURL, subject string) *AckDropProxy

NewAckDropProxy listens on a loopback port and forwards to the server at natsURL. It stops when the test ends.

func (*AckDropProxy) Dropped

func (p *AckDropProxy) Dropped() int

Dropped is the number of connections whose server traffic was cut after a publish to the subject.

func (*AckDropProxy) URL

func (p *AckDropProxy) URL() string

URL is the address a client connects to in place of the server's.

type Env

type Env struct {
	URL         string
	Owner       string
	Account     string // MASH account public key
	AdminCreds  string // path of the admin's creds file
	AccountSeed string // path of the MASH account identity seed file (0600), as ctlkeys.ImportAccount takes

	CAFile, ClientCert, ClientKey string // PEM paths, set when the server runs with TLS (the client pair only if it requires one)
	// contains filtered or unexported fields
}

Env is one running test server and its keys.

func Launch

func Launch(dir string) (e *Env, stop func(), err error)

Launch is Start without a test: the release check drives real mash binaries against the same server. dir holds the server's store and creds files and is the caller's to remove; stop shuts the server down.

func Start

func Start(t *testing.T) *Env

Start launches the server; it is shut down when the test ends.

func StartWith

func StartWith(t *testing.T, o Options) *Env

StartWith is Start with options.

func (*Env) Conn

func (e *Env) Conn(t *testing.T, creds string) *nats.Conn

Conn connects with a creds file; it is closed when the test ends.

func (*Env) ConnAs

func (e *Env) ConnAs(t *testing.T, creds, name string) *nats.Conn

ConnAs connects as endpoint name, using its private inbox prefix.

func (*Env) Connections

func (e *Env) Connections() int

Connections is the number of client connections the server holds now.

func (*Env) Mint

func (e *Env) Mint(t *testing.T, a sap.Address) (string, string)

Mint issues an endpoint user with mash's permission template and writes its creds file. It returns the creds path and the user public key.

func (*Env) MintEndpoint

func (e *Env) MintEndpoint(a sap.Address) (creds, userPub string, err error)

MintEndpoint is Mint without a test.

func (*Env) MintRaw

func (e *Env) MintRaw(t *testing.T, name string, p jwt.Permissions) (string, string)

MintRaw issues a user with arbitrary permissions (for negative tests).

func (*Env) MintUser

func (e *Env) MintUser(pub, sub []string) (creds []byte, userPub string, err error)

MintUser issues a user whose publish and subscribe allow lists are exactly pub and sub, and returns its .creds bytes and public key. Nothing is written to disk. It is what a fake Synadia hands out for a user it was asked to create with those lists.

func (*Env) MintUserMod

func (e *Env) MintUserMod(p jwt.Permissions, limits *jwt.NatsLimits, mod func(*jwt.UserClaims)) (creds []byte, userPub string, err error)

MintUserMod is MintUserWith that lets mod change the claims (connection types, source CIDRs, times) before signing.

func (*Env) MintUserWith

func (e *Env) MintUserWith(p jwt.Permissions, limits *jwt.NatsLimits) (creds []byte, userPub string, err error)

MintUserWith is MintUser for a full permission set (deny lists, a response permission) and, if limits is not nil, the user's limits (otherwise unlimited, as a user created with -1 limits has).

func (*Env) Restart

func (e *Env) Restart(t *testing.T)

Restart stops the server and starts it again on the same port and store.

func (*Env) Shutdown

func (e *Env) Shutdown()

Shutdown stops the server without restarting it.

type Options

type Options struct {
	// MaxPayload is the server's max_payload in bytes; 0 keeps nats-server's default (1 MiB).
	MaxPayload int32
	// RequireMaxBytes makes the MASH account refuse a stream or bucket config with no MaxBytes, as Synadia's
	// accounts do (err_code 10113).
	RequireMaxBytes bool
	// TLS serves tls://127.0.0.1 with a certificate from a throwaway CA, whose PEM is Env.CAFile.
	TLS bool
	// RequireClientCert (with TLS) makes the server verify client certificates; Env.ClientCert and Env.ClientKey
	// are one the CA signed.
	RequireClientCert bool
}

Options shape the server for tests of a hosted service's limits. The zero value is the default server.

type RTTProxy

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

RTTProxy is a TCP proxy between a NATS client and the test server that counts the client's dependent round trips.

A wave is a group of client requests that could all have been sent without waiting for each other: a client request (CONNECT, PUB, HPUB) that follows a server response (INFO, PONG, MSG, HMSG) delivered since the previous wave began starts a new one. Requests pipelined in one go are one wave however many there are, and one that waits for an answer is the next. The count is the number of times the client waited on the network, so it does not depend on how fast the machine is, only on the order of the protocol.

Each direction is delayed by the one-way latency so a server response reaches the proxy's log at the moment the client would see it. That makes the order of the log the order of causality as long as the client's burst of pipelined sends is shorter than one round trip.

Two kinds of client traffic are not requests and never start a wave: heartbeats, and replies to a request someone else made (a PUB to an _INBOX subject). A reply is the client answering, not waiting, and nothing it sends next depends on it. An endpoint's own $SRV.INFO probe, for one, is answered by its own service, and that reply lands beside the dependent chain at whatever moment the loopback MSG arrives, which under load can split a wave in two.

func NewRTTProxy

func NewRTTProxy(t *testing.T, natsURL string, oneWay time.Duration) *RTTProxy

NewRTTProxy listens on a loopback port and forwards to the server at natsURL, adding oneWay latency to each direction. It stops when the test ends.

func (*RTTProxy) Describe

func (p *RTTProxy) Describe() string

Describe lists the waves and the subjects each sent, for a failure message.

func (*RTTProxy) RTT

func (p *RTTProxy) RTT() float64

RTT is Waves plus the connection's TCP handshake and INFO: the round trips a latency of oneWay*2 would cost.

func (*RTTProxy) Reset

func (p *RTTProxy) Reset()

Reset forgets the waves counted so far.

func (*RTTProxy) URL

func (p *RTTProxy) URL() string

URL is the address a client connects to in place of the server's.

func (*RTTProxy) Waves

func (p *RTTProxy) Waves() int

Waves is the number of dependent waves since Reset, the CONNECT handshake's among them.

func (*RTTProxy) WavesThrough

func (p *RTTProxy) WavesThrough(prefix string) (int, bool)

WavesThrough is the number of waves up to and including the first that sent a request whose subject starts with prefix, and whether one has been sent. It marks a point in a long-running process's startup as the end of the count.

Jump to

Keyboard shortcuts

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