env

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: GPL-3.0 Imports: 13 Imported by: 1

Documentation

Overview

Package env is what a suite against a live server in a container is handed: the variables its test script exports, the replay proxy the server's internet calls go through, a check the container can reach it, the report on what the proxy saw, and files laid out where the container reads them. Every variable carries the app's prefix: New("APP") reads APP_SERVER, APP_TOKEN and the rest.

Index

Constants

View Source
const DefaultHost = "host.docker.internal"

DefaultHost is the name a container reaches the machine it runs on by when the environment names none.

View Source
const DefaultProxyPort = 18080

DefaultProxyPort is the port the proxy listens on when the environment names none.

Variables

This section is empty.

Functions

func CopyTree

func CopyTree(t *testing.T, root, src, dst string)

CopyTree copies a folder under the tree at root, and everything under it, to a new place.

func FilesUnder

func FilesUnder(t *testing.T, dirs ...string) map[string][]byte

FilesUnder reads every file under the folders, to hold them to later (StillOnDisk); folders with nothing under them fail the test.

func Mkdir

func Mkdir(t *testing.T, root, dir string)

Mkdir makes a directory under root that the server's own user can write in. The umask narrows what MkdirAll gives, so on Linux the server could not delete what the test laid out; chmod is not filtered. Docker Desktop hides this, so it only bites in CI.

func SameTree

func SameTree(t *testing.T, root string, before, after map[string][]byte)

SameTree fails for every path that changed, went or appeared between two reads of a tree (TreeOf), named without root.

func StillOnDisk

func StillOnDisk(t *testing.T, root string, files map[string][]byte, after string)

StillOnDisk checks every file read by FilesUnder is where it was, as it was, after what the test did (after), naming a file without root.

func TreeOf

func TreeOf(t *testing.T, root string, skip ...string) map[string][]byte

TreeOf reads everything under root, folders ending in "/", so a test can hold the disk to what it was; skip leaves those paths out.

func WriteFile

func WriteFile(t *testing.T, path string, data []byte)

WriteFile writes a file the server's own user can write over or remove (see Mkdir).

Types

type Env

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

Env reads one app's test environment.

func New

func New(prefix string) Env

New is the environment under prefix, without its underscore: "APP" for APP_SERVER.

func (Env) CheckReachable added in v0.6.0

func (e Env) CheckReachable(ctx context.Context, what string, port int) (network string, err error)

CheckReachable proves from inside the container that it can reach what this machine serves on port, by Host, and says what it sees of the network. A server that cannot reach the proxy fails every lookup, which reads as dozens of unrelated failures, so this says it once, plainly.

func (Env) Configured

func (e Env) Configured(also ...string) bool

Configured reports whether the container environment is present: a server and a token, and each of the other variables a suite cannot run without.

func (Env) Container

func (e Env) Container() string

Container is the name of the container the test script started (APP_TEST_CONTAINER), "" when the server under test is not one of ours.

func (Env) ContainerAddresses

func (e Env) ContainerAddresses(ctx context.Context) []string

ContainerAddresses are the addresses the server reaches itself on, which go through the proxy because NO_PROXY is set before docker hands them out; nil with no container, or when docker cannot say.

func (Env) DataDir

func (e Env) DataDir(elem ...string) string

DataDir is the host folder the container's data is mounted from (APP_TEST_DATA) with elem joined on, so a test can lay files out for the server. "" when unset, which a test must skip on or it writes into the checkout.

func (Env) Get

func (e Env) Get(name string) string

Get is the value of one of the app's variables, "" when it is not set.

func (Env) Host added in v0.6.0

func (e Env) Host() string

Host is the name the container reaches this machine by (APP_TEST_HOST), DefaultHost when unset; on Linux a script may give the bridge gateway's address instead.

func (Env) ListenProxy added in v0.6.0

func (e Env) ListenProxy(ctx context.Context, opts replayproxy.Options) (*Proxy, error)

ListenProxy brings the proxy up without asking the container anything, for a server that calls out as it starts. The environment sets the mode, the address (every interface on ProxyPort), the authority under APP_TEST_PROXY_CA, and the server's own addresses to ignore; the rest is the suite's. With no CassetteDir the suite keeps no recordings and a miss is told so rather than told to record.

func (Env) Mode

func (e Env) Mode() replayproxy.Mode

Mode is how the environment asks the proxy to run: replaying unless told to record or to verify, and recording when told both.

func (Env) ProxyPort

func (e Env) ProxyPort() (int, error)

ProxyPort is the port the proxy listens on, APP_TEST_PROXY_PORT or DefaultProxyPort, which the container's HTTPS_PROXY already names.

func (Env) Recording

func (e Env) Recording() bool

Recording reports whether this run may call the real services: APP_TEST_RECORD=1 records only what is missing, =all records everything afresh.

func (Env) Server

func (e Env) Server() string

Server is the address of the server under test (APP_SERVER).

func (Env) StartProxy

func (e Env) StartProxy(ctx context.Context, opts replayproxy.Options) (*Proxy, error)

StartProxy brings up the proxy the container's HTTPS_PROXY points at (ListenProxy) and proves the container can reach it. The container must be running; a suite that starts it after the proxy calls ListenProxy, starts it, then CheckReachable.

func (Env) Token

func (e Env) Token() string

Token is the credential for it (APP_TOKEN).

func (Env) Var

func (e Env) Var(name string) string

Var is the full name of one of the app's variables: Var("TEST_DATA") is APP_TEST_DATA.

func (Env) Verifying

func (e Env) Verifying() bool

Verifying reports whether to check the cassettes against the live services without rewriting them (APP_TEST_VERIFY).

type Proxy

type Proxy struct {

	// Misses are the requests with no recording, Drifts the answers that changed shape since recording; both read when the proxy stops
	Misses []string
	Drifts []replayproxy.Drift
	// contains filtered or unexported fields
}

Proxy is the replay proxy a suite runs for its whole run, and what it saw once stopped.

func (*Proxy) Addr

func (p *Proxy) Addr() string

Addr is the address the proxy listens on.

func (*Proxy) Report

func (p *Proxy) Report() string

Report says what a stopped proxy saw that fails a run, "" for nothing: a miss, which means a test ran against a 502, with what to do about it; and a drift, a service answering in a new shape.

func (*Proxy) Serve

func (p *Proxy) Serve(target string, h http.Handler) (stop func())

Serve answers a host, or one path of it, with h rather than a recording until stop is called (replayproxy.Proxy.Serve).

func (*Proxy) Stop

func (p *Proxy) Stop() error

Stop closes the proxy and keeps what it saw; stopping twice is harmless.

func (*Proxy) Transport

func (p *Proxy) Transport() *http.Transport

Transport is for a client in the test's own process to go through the proxy too (replayproxy.Proxy.Transport).

Jump to

Keyboard shortcuts

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