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
- func HermeticGit() (cleanup func(), err error)
- func OtherCA(t *testing.T) string
- func RunGit(args ...string) ([]byte, error)
- type AckDropProxy
- type Env
- func (e *Env) Conn(t *testing.T, creds string) *nats.Conn
- func (e *Env) ConnAs(t *testing.T, creds, name string) *nats.Conn
- func (e *Env) Connections() int
- func (e *Env) Mint(t *testing.T, name string) (string, string)
- func (e *Env) MintEndpoint(name string) (creds, userPub string, err error)
- func (e *Env) MintRaw(t *testing.T, name string, p jwt.Permissions) (string, string)
- func (e *Env) MintUser(pub, sub []string) (creds []byte, userPub string, err error)
- func (e *Env) MintUserMod(p jwt.Permissions, limits *jwt.NatsLimits, mod func(*jwt.UserClaims)) (creds []byte, userPub string, err error)
- func (e *Env) MintUserWith(p jwt.Permissions, limits *jwt.NatsLimits) (creds []byte, userPub string, err error)
- func (e *Env) Restart(t *testing.T)
- func (e *Env) Shutdown()
- type Options
- type RTTProxy
Constants ¶
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.
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.
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
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 ¶
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 (*Env) Connections ¶
Connections is the number of client connections the server holds now.
func (*Env) Mint ¶
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 ¶
MintEndpoint is Mint without a test.
func (*Env) MintUser ¶
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).
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 ¶
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 ¶
Describe lists the waves and the subjects each sent, for a failure message.
func (*RTTProxy) RTT ¶
RTT is Waves plus the connection's TCP handshake and INFO: the round trips a latency of oneWay*2 would cost.
func (*RTTProxy) Waves ¶
Waves is the number of dependent waves since Reset, the CONNECT handshake's among them.
func (*RTTProxy) WavesThrough ¶
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.