gopherstack

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 22 Imported by: ? 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 gopherstack starts the gopherstack AWS emulator (https://github.com/blackbirdworks/gopherstack) in a Docker container, to run end-to-end tests of the AWS-backed components (S3, Kinesis, SQS).

One emulator is shared by all the tests of a package: the TestMain of the package runs Main, which starts it and removes it when the tests are done, and the tests get it with Connect.

Environment variables:

  • GOPHERSTACK_ENDPOINT reuses an already running instance (ex: http://127.0.0.1:8000) instead of starting a container.
  • GOPHERSTACK_IMAGE overrides Image.

Index

Constants

View Source
const (
	// Image is the pinned gopherstack image (sha256:3683df48d7ed91df17214e3b6169609420d0d3a59b21a26f808b556be4cd8f78).
	Image = "ghcr.io/blackbirdworks/gopherstack:1.10.0"
	// Port is the port gopherstack listens on in the container.
	Port = "8000/tcp"
	// HealthPath is the health check endpoint.
	HealthPath = "/_gopherstack/health"

	// Region is the region used by the tests.
	Region = "us-east-1"
	// AccessKey is the (not validated) access key id used by the tests.
	AccessKey = "test"
	// SecretKey is the (not validated) secret access key used by the tests.
	SecretKey = "test"

	// EnvEndpoint is the env var to reuse a running instance.
	EnvEndpoint = "GOPHERSTACK_ENDPOINT"
	// EnvImage is the env var to override Image.
	EnvImage = "GOPHERSTACK_IMAGE"
)

Variables

This section is empty.

Functions

func Main

func Main(m *testing.M) int

Main is the body of the TestMain of a package that runs end-to-end tests against the emulator. It starts one emulator for the package (unless GOPHERSTACK_ENDPOINT already points to one) when Docker is available, publishes its endpoint in GOPHERSTACK_ENDPOINT for Connect, runs the tests and removes the container. When Docker is not available the tests that call Connect are skipped.

When the tests fail and Main started the container, it prints the last 200 lines of the log of the emulator to stderr, read through the Docker API, before it removes the container. For an external instance (GOPHERSTACK_ENDPOINT) it has no container and prints nothing.

func TestMain(m *testing.M) { os.Exit(gopherstack.Main(m)) }

func UniqueName

func UniqueName(tb testing.TB, prefix string) string

UniqueName returns a name unique to the test and the run (lowercase, DNS valid, <= 63 chars), usable as bucket, queue or stream name.

func Within

func Within(ctx context.Context, perCall time.Duration) (context.Context, context.CancelFunc)

Within returns a context for one call that is made outside a step (a setup call): cut at perCall and never later than the deadline of ctx.

Types

type Calls

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

Calls makes the calls of one step. Each call gets its own context, cut at the per-call bound and never later than the deadline of the whole wait, so a call that is slow does not take the time of the next one, and nothing runs past the deadline. A step cannot share one context across calls by mistake: it has no other way to call.

func (*Calls) Do

func (c *Calls) Do(name string, fn func(ctx context.Context) error) error

Do runs fn with its own call context. An error is recorded in the report under name (the operation) and returned.

func (*Calls) Stop

func (c *Calls) Stop()

Stop ends the polling after the current step, without the job being done: for an error that polling again cannot cure. The caller reports it after Poll returns.

type LogTail

type LogTail struct {
	// ReadErr is the error that ended the read before the end of the log, nil when the log was read to its end.
	ReadErr error
	// Lines are the last lines read, in order. A line longer than the cap is cut and ends with a marker.
	Lines []string
	// MaxLine is the cap of a line, in bytes.
	MaxLine int
	// CutLines counts the lines that were longer than MaxLine.
	CutLines int
	// Want is the number of lines asked for.
	Want int
}

LogTail is the end of a log, with what could not be kept.

func TailLog

func TailLog(r io.Reader, want, maxLine int) *LogTail

TailLog reads r to its end (or its first error) and keeps the last want lines. A line longer than maxLine bytes is cut there and ends with a visible marker, and is counted. It cannot fail on a long line.

func (*LogTail) Partial

func (l *LogTail) Partial() bool

Partial reports that the text is not the plain end of the log: the read failed or a line was cut.

func (*LogTail) String

func (l *LogTail) String() string

String is the text to print. A partial text says so and why, and is not called "the last N lines" when the read failed, because then the lines are the last ones that were read, not the end of the log.

type OpError

type OpError struct {
	// Err is the first error with this text.
	Err error
	// Op is the name of the operation, as the caller gave it.
	Op string
	// Count is the number of times the same operation failed with the same text.
	Count int
}

OpError is an error of a call of the emulator, with the operation that failed and how many times it did.

type PollReport

type PollReport struct {
	// Errors lists the distinct errors (operation and text) in the order they first happened, at most
	// maxDistinctErrors of them.
	Errors []OpError
	// Polls is the number of steps that ran.
	Polls int
	// Incomplete is the number of steps that made no failed call and did not finish the job.
	Incomplete int
	// Dropped counts the errors that were not listed because the list was full.
	Dropped int
	// Done reports that a step finished the job.
	Done bool
}

PollReport is what Poll saw, for the failure message of a test that polls the emulator.

func Poll

func Poll(ctx context.Context, perCall, interval time.Duration, step func(c *Calls) bool) *PollReport

Poll runs step until it returns true or ctx ends. The deadline of the whole wait is the one of ctx. The step makes its calls through the Calls it is given. Between two steps Poll waits interval (or until ctx ends). It makes no read of the clock.

func (*PollReport) String

func (r *PollReport) String() string

String says what happened, for a failure message built after the polling ended.

type Server

type Server struct {

	// Endpoint is the base URL (ex: http://127.0.0.1:49153). An IP host makes the AWS SDK use
	// path-style S3 addressing, so no DNS is needed.
	Endpoint string
	// contains filtered or unexported fields
}

Server is a running gopherstack instance.

func Connect

func Connect(tb testing.TB) *Server

Connect returns the emulator that Main started (or that GOPHERSTACK_ENDPOINT points to). The test is skipped if there is none, because Docker is not available.

func Start

func Start(ctx context.Context) (*Server, error)

Start starts a container (or attaches to GOPHERSTACK_ENDPOINT) and waits until it is healthy.

func StartIsolated

func StartIsolated(ctx context.Context) (*Server, error)

StartIsolated always starts a new container, ignoring GOPHERSTACK_ENDPOINT, so that a test can have a second emulator whose buckets are not those of the shared one (the same bucket name then names two distinct stores). The caller removes it with Server.Terminate.

func (*Server) AwsConfig

func (s *Server) AwsConfig(ctx context.Context) (aws.Config, error)

AwsConfig returns an AWS config with static credentials, the region and the emulator endpoint.

func (*Server) CreateBucket

func (s *Server) CreateBucket(tb testing.TB, prefix string) string

CreateBucket creates a bucket named UniqueName(t, prefix) and returns its name.

func (*Server) GetObject

func (s *Server) GetObject(tb testing.TB, bucket, key string) []byte

GetObject downloads bucket/key.

func (*Server) ListKeys

func (s *Server) ListKeys(tb testing.TB, bucket, prefix string) []string

ListKeys returns all the keys of the bucket under prefix.

func (*Server) MustAwsConfig

func (s *Server) MustAwsConfig(tb testing.TB) aws.Config

MustAwsConfig is AwsConfig failing the test on error.

func (*Server) PutObject

func (s *Server) PutObject(tb testing.TB, bucket, key string, content []byte)

PutObject uploads content at bucket/key.

func (*Server) S3Client

func (s *Server) S3Client(tb testing.TB) *s3.Client

S3Client returns an S3 client on the emulator.

func (*Server) SetAwsEnv

func (s *Server) SetAwsEnv(t *testing.T)

SetAwsEnv points the AWS SDK default config chain (used by the relay proxy, which has no endpoint setting) to the emulator. It uses t.Setenv so the calling test can't be parallel.

func (*Server) Terminate

func (s *Server) Terminate() error

Terminate removes the container (no-op for an external instance).

Jump to

Keyboard shortcuts

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