nktest

package module
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: MIT Imports: 38 Imported by: 0

README

nktest

Package nktest makes it easy to test Nakama game modules.

This package was built to support testing complex Nakama game modules completely from within Go. See also the github.com/ascii8/nakama-go package for a realtime Nakama Go client.

Tests Go Report Card Reference Releases

using

go get github.com/ascii8/nktest

Examples:

Documentation

Overview

Package nktest provides a nakama test runner.

Index

Constants

This section is empty.

Variables

View Source
var DefaultInPrefix = "<- "

DefaultInPrefix is the default in prefix.

View Source
var DefaultOutPrefix = "-> "

DefaultOutPrefix is the default out prefix.

View Source
var DefaultProxyReadSize = 10 * 1024 * 1024

DefaultProxyReadSize is the default websocket proxy read size.

View Source
var DefaultProxyWriteSize = 10 * 1024 * 1024

DefaultProxyWriteSize is the default websocket proxy write size.

View Source
var DefaultTransport = &http.Transport{
	DisableCompression: true,
}

DefaultTransport is the default http transport used by the log transport.

Functions

func GenDockerToken

func GenDockerToken(ctx context.Context, h Handler, id string) (string, error)

GenDockerToken generates a docker auth token for the repo id.

func GetImageTags

func GetImageTags(ctx context.Context, h Handler, id string) ([]string, error)

GetImageTags gets the docker registry tags for a image id.

func GoEnvVar

func GoEnvVar(goPath, name string) (string, error)

GoEnvVar reads the go env variable from `go env <name>`.

func IsSubDir

func IsSubDir(a, b string) error

IsSubDir determines if b is subdir of a.

func LocalAddr added in v0.3.0

func LocalAddr(l net.Listener) string

LocalAddr returns the local address of the listener.

func NewPrefixedWriter

func NewPrefixedWriter(w io.Writer, prefix string) io.Writer

NewPrefixedWriter creates a new prefixed writer.

func ParsePortMapping added in v0.3.4

func ParsePortMapping(s string) (pntypes.PortMapping, error)

ParsePortMapping creates a port mapping from s.

func PodmanBuildMounts

func PodmanBuildMounts(mounts ...string) ([]pspec.Mount, error)

PodmanBuildMounts creates mount specs for a container.

func PodmanContext

func PodmanContext(ctx context.Context) (context.Context, context.Context, error)

PodmanContext creates a podman client. If no client exists in the current context, then a new context is created and merged with the parent context, otherwise ctx is passed through unchanged.

func PodmanCreatePod

func PodmanCreatePod(ctx, conn context.Context, h Handler, ids ...string) (string, error)

PodmanCreatePod creates a pod network.

func PodmanFollowLogs

func PodmanFollowLogs(ctx context.Context, h Handler, id string) error

PodmanFollowLogs follows the logs for a container.

func PodmanGetAddr

func PodmanGetAddr(ctx context.Context, h Handler, id, svc string) (string, string, error)

PodmanGetAddr inspects id and returns the local and remote addresses.

func PodmanPullImages

func PodmanPullImages(ctx, conn context.Context, h Handler, ids ...string) error

PodmanPullImages grabs image ids when not present on the host or when the handler's always pull is true.

func PodmanRun

func PodmanRun(ctx, conn context.Context, h Handler, id string, env map[string]string, mounts []string, entrypoint ...string) (string, error)

PodmanRun runs a container image id.

func PodmanServiceWait

func PodmanServiceWait(ctx context.Context, h Handler, id, svc string, f func(string, string) error) error

PodmanServiceWait waits for a container service to be available.

func PodmanWait

func PodmanWait(ctx context.Context, h Handler, id string) error

PodmanWait waits until a container has stopped.

func QualifiedId

func QualifiedId(id string) string

QualifiedId fully qualifies a container image id.

func ReadCachedFile

func ReadCachedFile(name string, ttl time.Duration) ([]byte, error)

ReadCachedFile reads a cached file from disk, returns error if the file name on disk is past the ttl.

func ShortId

func ShortId(id string) string

ShortId truncates id to 16 characters.

Types

type BuildConfig

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

BuildConfig is a nakama module build config.

type BuildConfigOption

type BuildConfigOption func(*BuildConfig) error

BuildConfigOption is nakama module build config option.

func WithDefaultGoEnv

func WithDefaultGoEnv() BuildConfigOption

WithDefaultGoEnv is a nakama module build config option to copy default host environment variables for Go.

Copies:

GONOPROXY
GONOSUMDB
GOPRIVATE
GOPROXY
GOSUMDB

func WithDefaultGoVolumes

func WithDefaultGoVolumes() BuildConfigOption

WithGoVolumes is a nakama module build config option to mount the host's Go directories (GOCACHE, GOMODCACHE, and GOPATH) to the plugin builder container. Significantly speeds up build times.

func WithEnv

func WithEnv(env map[string]string) BuildConfigOption

WithEnv is a nakama module build config option to set additional env variables used during builds.

func WithGoEnv

func WithGoEnv(env ...string) BuildConfigOption

WithGoEnv is a nakama module build config option to copy the host Go environment variables.

func WithGoEnvVolumes

func WithGoEnvVolumes(volumes ...EnvVolumeInfo) BuildConfigOption

WithGoVolumes is a nakama module build config option to mount the host's Go directories (ie, the Go environment's GOCACHE, GOMODCACHE, and GOPATH locations) to the plugin builder container. Significantly speeds up build times.

Note: use WithDefaultGoVolumes (see below).

func WithMounts

func WithMounts(mounts ...string) BuildConfigOption

WithMounts is a nakama module build config option to set additional mounts used during builds.

func WithOut

func WithOut(out string) BuildConfigOption

WithOut is a nakama module build config option to set the out name. When not specified, the name will be derived from the directory name of the module.

type EnvVolumeInfo

type EnvVolumeInfo struct {
	Key    string
	Target string
	Sub    string
}

EnvVolumeInfo holds information about an environment variable derived volume.

func NewEnvVolume

func NewEnvVolume(key, target, sub string) EnvVolumeInfo

NewEnvVolume creates a new environment volume.

type Handler

type Handler interface {
	Name() string
	HttpClient() *http.Client
	DockerAuthName() string
	DockerAuthScope(string) string
	DockerRegistryURL() string
	DockerTokenURL() string
	AlwaysPull() bool
	PodId() string
	HostPortMap(string, string, uint16, uint16) uint16
	Stdout(string) io.Writer
	Stderr(string) io.Writer
	Logf(string, ...interface{})
	Errf(string, ...interface{})
	Backoff(context.Context, func() error) error
	ContainerRemoveDelay() time.Duration
	NetworkRemoveDelay() time.Duration
}

Handler is a handler.

type Logger

type Logger interface {
	io.Writer
	Logf(string, ...interface{})
	Errf(string, ...interface{})
	Stdout(string) io.Writer
	Stderr(string) io.Writer
	Transport(http.RoundTripper) http.RoundTripper
}

Logger is the interface for a logger/writer.

func NewLogger

func NewLogger(f func(string, ...interface{})) Logger

NewLogger creates a new logger/writer.

func TruncatedLogger

func TruncatedLogger() Logger

TruncatedLogger creates a truncating logger/writer.

type Option

type Option func(*Runner)

Option is a nakama test runner option.

func WithAlwaysPull

func WithAlwaysPull(alwaysPull bool) Option

WithAlwaysPull is a nakama test runner option to set the always pull flag. When true, causes container images to be pulled regardless of if they are available on the host or not.

func WithBackoffMaxElapsedTime

func WithBackoffMaxElapsedTime(backoffMaxElapsedTime time.Duration) Option

WithBackoffMaxElapsedTime is a nakama test runner option to set the backoff max elapsed time for waiting for services (ie, postgres, nakama) to start/become available.

func WithBackoffMaxInterval

func WithBackoffMaxInterval(backoffMaxInterval time.Duration) Option

WithBackoffMaxInterval is a nakama test runner option to set the backoff max interval for waiting for services (ie, postres, nakama) to start/become available.

func WithBuildConfig

func WithBuildConfig(modulePath string, opts ...BuildConfigOption) Option

WithBuildConfig is a nakama test runner option to add a module path, and extra options to the build config.

func WithConfigFilename

func WithConfigFilename(configFilename string) Option

WithConfigFilename is a nakama test runner option to set the config filename.

func WithConfigTemplate

func WithConfigTemplate(configTemplate string) Option

WithConfigTemplate is a nakama test runner option to set the config template.

func WithContainerRemoveDelay

func WithContainerRemoveDelay(containerRemoveDelay time.Duration) Option

WithContainerRemoveDelay is a nakama test runner option to set the container remove delay.

func WithDir

func WithDir(dir string) Option

WithDir is a nakama test runner option to set the project root dir.

func WithDockerAuthName

func WithDockerAuthName(dockerAuthName string) Option

WithDockerAuthName is a nakama test runner option to set the docker token auth name used when generating auth tokens for the docker registry.

func WithDockerAuthScope

func WithDockerAuthScope(dockerAuthScope string) Option

WithDockerAuthScope is a nakama test runner option to set a docker token auth scope mask. Must include "%s" to interpolate the image id.

func WithDockerRegistryURL

func WithDockerRegistryURL(dockerRegistryURL string) Option

WithDockerRegistryURL is a nakama test runner option to set the docker registry url used for retrieving images.

func WithDockerTokenURL

func WithDockerTokenURL(dockerTokenURL string) Option

WithDockerTokenURL is a nakama test runner option to set the docker token url, used for generating auth tokens when pulling images.

func WithHostPortMap added in v0.3.4

func WithHostPortMap() Option

WithHostPortMap is a nakama test runner option to add host port mapping for the postgres and nakama services (5432/tcp, 7349/tcp, 7350/tcp, 7351/tcp).

func WithHttpClient

func WithHttpClient(httpClient *http.Client) Option

WithHttpClient is a nakama test runner option to set a http client used. Used for generating auth tokens for image repositories.

func WithNakamaImageId

func WithNakamaImageId(nakamaImageId string) Option

WithNakamaImageId is a nakama test runner option to set the nakama image id.

func WithNakamaVersion

func WithNakamaVersion(nakamaVersion string) Option

WithNakamaVersion is a nakama test runner option to set the nakama image tag.

func WithName

func WithName(name string) Option

WithName is a nakama test runner option to set the name.

func WithNetworkRemoveDelay

func WithNetworkRemoveDelay(networkRemoveDelay time.Duration) Option

WithNetworkRemoveDelay is a nakama test runner option to set the container network remove delay.

func WithPluginbuilderImageId

func WithPluginbuilderImageId(pluginbuilderImageId string) Option

WithPluginbuilderImageId is a nakama test runner option to set the pluginbuilder image id.

func WithPortMap added in v0.3.4

func WithPortMap(id, svc string, port uint16) Option

WithPortMap is a nakama test runner option to add a host port mapping for a service.

func WithPostgresImageId

func WithPostgresImageId(postgresImageId string) Option

WithPostgresImageId is a nakama test runner option to set the postgres image id.

func WithPostgresVersion

func WithPostgresVersion(postgresVersion string) Option

WithPostgresVersion is a nakama test runner option to set the postgres image tag.

func WithStderr

func WithStderr(stderr io.Writer) Option

WithStderr is a nakama test runner option to set the stderr.

func WithStdout

func WithStdout(stdout io.Writer) Option

WithStdout is a nakama test runner option to set the stdout.

func WithVersionCacheTTL

func WithVersionCacheTTL(versionCacheTTL time.Duration) Option

WithVersionCacheTTL is a nakama test runner option to set the version cache TTL.

func WithVolumeDir

func WithVolumeDir(volumeDir string) Option

WithVolumeDir is a nakama test runner option to set the volume dir, where nakama and postgres data/configs are written. Default is <project root>/.cache. Must be a sub dir of the project root.

type PrefixedWriter

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

PrefixedWriter is a prefixed writer.

func (*PrefixedWriter) Write

func (w *PrefixedWriter) Write(buf []byte) (int, error)

Write satisfies the io.Writer interface.

type Proxy

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

Proxy is a http and websocket logging proxy.

func NewProxy

func NewProxy(opts ...ProxyOption) *Proxy

NewProxy creates a new http and websocket logging proxy.

func (*Proxy) Addr

func (p *Proxy) Addr() string

Addr returns the listening address.

func (Proxy) DialError added in v0.3.0

func (p Proxy) DialError(logger io.Writer, w http.ResponseWriter, req *http.Request, res *http.Response, err error)

func (Proxy) InternalError added in v0.3.0

func (p Proxy) InternalError(res http.ResponseWriter, s string, v ...interface{})

InternalError handles internal errors.

func (*Proxy) Run

func (p *Proxy) Run(ctx context.Context, urlstr string) (string, error)

Run proxies requests to the url until the context is closed.

type ProxyOption

type ProxyOption func(*Proxy)

ProxyOption is a proxy option.

func WithAddr

func WithAddr(addr string) ProxyOption

WithAddr is a proxy option to set the listen address.

func WithDialer

func WithDialer(dialer websocket.Dialer) ProxyOption

WithDialer is a proxy option to set the websocket dialer.

func WithLogf

func WithLogf(f func(string, ...interface{})) ProxyOption

WithLogf is a proxy option to set a wrapped logger. Useful with *testing.T.

func WithLogger

func WithLogger(logger Logger) ProxyOption

WithLogger is a proxy option to set logger.

func WithUpgrader

func WithUpgrader(upgrader websocket.Upgrader) ProxyOption

WithUpgrader is a proxy option to set the websocket upgrader.

func WithWsPath

func WithWsPath(wsPath string) ProxyOption

WithWsPath is a proxy option to set the websocket remote path.

type Runner

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

Runner is a nakama test runner.

func New

func New(opts ...Option) *Runner

New creates a new nakama test runner.

func (*Runner) AlwaysPull

func (t *Runner) AlwaysPull() bool

AlwaysPull returns whether or not to always pull an image.

func (*Runner) Backoff

func (t *Runner) Backoff(ctx context.Context, f func() error) error

Backoff executes f until backoff conditions are met or until f returns nil.

func (*Runner) BuildModule

func (t *Runner) BuildModule(ctx, conn context.Context, bc *BuildConfig) error

BuildModule builds a nakama plugin module.

func (*Runner) BuildModules

func (t *Runner) BuildModules(ctx, conn context.Context) error

BuildModules builds the nakama modules.

func (*Runner) ContainerRemoveDelay

func (t *Runner) ContainerRemoveDelay() time.Duration

ContainerRemoveDelay returns the container remove delay.

func (*Runner) DockerAuthName

func (t *Runner) DockerAuthName() string

DockerAuthName returns the docker token auth name.

func (*Runner) DockerAuthScope

func (t *Runner) DockerAuthScope(id string) string

DockerAuthScope returns the docker token auth scope for a image id.

func (*Runner) DockerRegistryURL

func (t *Runner) DockerRegistryURL() string

DockerRegistryURL returns the docker registry url.

func (*Runner) DockerTokenURL

func (t *Runner) DockerTokenURL() string

DockerTokenURL returns the docker token url.

func (*Runner) Errf

func (t *Runner) Errf(s string, v ...interface{})

Errf logs messages to stdout.

func (*Runner) GetNakamaVersion

func (t *Runner) GetNakamaVersion(ctx context.Context) error

GetNakamaVersion loads and caches the nakama version.

func (*Runner) GrpcLocal

func (t *Runner) GrpcLocal() string

GrpcLocal returns the grpc local address.

func (*Runner) GrpcRemote

func (t *Runner) GrpcRemote() string

GrpcRemote returns the grpc remote address.

func (*Runner) HostPortMap added in v0.3.4

func (t *Runner) HostPortMap(id, svc string, containerPort, hostPort uint16) uint16

HostPortMap returns the host port for the provided container id and service.

func (*Runner) HttpClient

func (t *Runner) HttpClient() *http.Client

HttpClient returns the http client for the test runner.

func (*Runner) HttpLocal

func (t *Runner) HttpLocal() string

HttpLocal returns the http local address.

func (*Runner) HttpRemote

func (t *Runner) HttpRemote() string

HttpRemote returns the http remote address.

func (*Runner) Init

func (t *Runner) Init() error

Init initializes the environment for running nakama-pluginbuilder and nakama images.

func (*Runner) Logf

func (t *Runner) Logf(s string, v ...interface{})

Logf logs messages to stdout.

func (*Runner) Name

func (t *Runner) Name() string

Name returns the name.

func (*Runner) NetworkRemoveDelay

func (t *Runner) NetworkRemoveDelay() time.Duration

NetworkRemoveDelay returns the network remove delay.

func (*Runner) PodId

func (t *Runner) PodId() string

PodId returns the pod id.

func (*Runner) PostgresLocal

func (t *Runner) PostgresLocal() string

PostgresLocal returns the postgres local address.

func (*Runner) PostgresRemote

func (t *Runner) PostgresRemote() string

PostgresRemote returns the postgres remote address.

func (*Runner) Run

func (t *Runner) Run(ctx context.Context) error

Run handles building the nakama plugin and starting the postgres and nakama server containers.

func (*Runner) RunNakama

func (t *Runner) RunNakama(ctx, conn context.Context) error

RunNakama runs the nakama server.

func (*Runner) RunPostgres

func (t *Runner) RunPostgres(ctx, conn context.Context) error

RunPostgres runs the postgres server.

func (*Runner) RunProxy

func (t *Runner) RunProxy(ctx context.Context, opts ...ProxyOption) (string, error)

RunProxy creates and runs a http proxy until the context is closed.

func (*Runner) ServerKey added in v0.3.0

func (t *Runner) ServerKey() string

ServerKey returns the server key.

func (*Runner) Stderr

func (t *Runner) Stderr(prefix string) io.Writer

Stderr returns a prefixed writer for errors.

func (*Runner) Stdout

func (t *Runner) Stdout(prefix string) io.Writer

Stdout returns a prefixed writer for output.

func (*Runner) Transport

func (t *Runner) Transport(transport http.RoundTripper) http.RoundTripper

Transport satisfies the logger interface.

func (*Runner) Write

func (t *Runner) Write(buf []byte) (int, error)

Write satisfies the io.Writer interface.

type Transport

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

Transport is a logging transport.

func NewTransport

func NewTransport(req, res io.Writer, transport http.RoundTripper) *Transport

NewTransport creates a logging transport.

func (*Transport) DisableReqBody

func (t *Transport) DisableReqBody()

DisableReqBody disables logging the request body.

func (*Transport) DisableResBody

func (t *Transport) DisableResBody()

DisableResBody disables logging the response body.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip satisfies the http.RoundTripper interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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