testutils

package
v0.1.73 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 32 Imported by: 1

Documentation

Overview

Package testutils provides various utilities for use in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArtifactGoogleCreds

func ArtifactGoogleCreds(tb testing.TB) string

ArtifactGoogleCreds returns the google credentials for artifact.

func BackingMongoDBClient

func BackingMongoDBClient(tb testing.TB) *mongo.Client

BackingMongoDBClient returns a backing MongoDB client to use.

func BackingMongoDBClientWithOptions added in v0.1.65

func BackingMongoDBClientWithOptions(tb testing.TB, baseOptions *options.ClientOptions) *mongo.Client

BackingMongoDBClientWithOptions returns a backing MongoDB client to use with the provided options.

func BackingMongoDBURI

func BackingMongoDBURI(tb testing.TB) string

BackingMongoDBURI returns the backing MongoDB URI to use.

func GenerateSelfSignedCertificate added in v0.0.3

func GenerateSelfSignedCertificate(commonName string, altNames ...string) (tls.Certificate, string, string, *x509.CertPool, error)

GenerateSelfSignedCertificate generates a self signed certificate with the given names.

func NewMongoDBNamespace

func NewMongoDBNamespace() (string, string)

NewMongoDBNamespace returns a new random namespace to use.

func ReserveRandomListener added in v0.0.5

func ReserveRandomListener(tb testing.TB) *net.TCPListener

ReserveRandomListener returns a new TCP listener at a random port.

func SkipUnlessArtifactGoogleCreds

func SkipUnlessArtifactGoogleCreds(tb testing.TB)

SkipUnlessArtifactGoogleCreds verifies google credentials are available for artifact.

func SkipUnlessBackingMongoDBURI

func SkipUnlessBackingMongoDBURI(tb testing.TB)

SkipUnlessBackingMongoDBURI verifies there is a backing MongoDB URI to use.

func SkipUnlessInternet

func SkipUnlessInternet(tb testing.TB)

SkipUnlessInternet verifies there is an internet connection.

func Teardown

func Teardown()

Teardown cleans up any temporary resources used by tests.

func TempDir

func TempDir(dir, pattern string) (string, error)

TempDir creates a temporary directory and fails the test if it cannot.

func TempDirT

func TempDirT(tb testing.TB, dir, pattern string) string

TempDirT creates a temporary directory and fails the test if it cannot.

func TempFile added in v0.1.10

func TempFile(tb testing.TB) *os.File

TempFile returns a unique temporary file named "something.txt" or fails the test if it cannot. It automatically closes and removes the file after the test and all its subtests complete.

func TestMain

func TestMain(t *testing.T, mainWithArgs func(ctx context.Context, args []string, logger golog.Logger) error, tcs []MainTestCase)

TestMain tests a main function with a series of test cases in serial.

func WaitForAssertion

func WaitForAssertion(t *testing.T, assertion func(tb testing.TB))

WaitForAssertion waits for a testify.Assertion to succeed or ultimately fail. Note: This should only be used if there's absolutely no way to have the test code be able to be signaled that the assertion is ready to be checked. That is, if waiting with respect time is critical, it's okay to use this.

func WaitForAssertionWithSleep added in v0.1.5

func WaitForAssertionWithSleep(t *testing.T, wait time.Duration, maxAttempts int, assertion func(tb testing.TB))

WaitForAssertionWithSleep is similar to WaitForAssertion but takes a wait time and maximum number of attempts as parameters.

func WaitSuccessfulDial

func WaitSuccessfulDial(address string) error

WaitSuccessfulDial waits for a dial attempt to succeed.

func WatchedFile added in v0.1.73

func WatchedFile(tb testing.TB) (*fsnotify.Watcher, *os.File)

WatchedFile creates a file watcher and a unique temporary file named "something.txt", or fails the test if it cannot. It returns the watcher and the file. It automatically closes the watcher, and closes and removes the file after the test and all its subtests complete.

func WatchedFiles added in v0.1.73

func WatchedFiles(tb testing.TB, n int) (*fsnotify.Watcher, []*os.File)

WatchedFiles creates a file watcher and n unique temporary files all named "something.txt", or fails the test if it cannot. It returns the watcher and a slice of files. It automatically closes the watcher, and closes and removes all files after the test and all its subtests complete.

For safety, this function will not create more than 50 files.

Types

type ContextualMainExecution

type ContextualMainExecution struct {
	Ready       <-chan struct{}
	Done        <-chan error
	Start       func()
	Stop        func()
	QuitSignal  func(t *testing.T)             // reflects syscall.SIGQUIT
	ExpectIters func(t *testing.T, amount int) // expect a certain amount of iters
	WaitIters   func(t *testing.T)             // waits for iters defined by ExpectIters
}

ContextualMainExecution reflects the execution of a main function that can have its lifecycle partially controlled.

func ContextualMain

func ContextualMain(
	main func(ctx context.Context, args []string, logger golog.Logger) error,
	args []string,
	logger golog.Logger,
) ContextualMainExecution

ContextualMain calls a main entry point function with a cancellable context via the returned execution struct. The main function is run in a separate goroutine.

type MainTestCase

type MainTestCase struct {
	Name   string
	Args   []string
	Err    string
	Before func(t *testing.T, logger golog.Logger, exec *ContextualMainExecution)
	During func(ctx context.Context, t *testing.T, exec *ContextualMainExecution)
	After  func(t *testing.T, logs *observer.ObservedLogs)
}

MainTestCase describes how to execute a main function and what to expect from it.

Directories

Path Synopsis
Package testutilsext is purely for test utilities that may access other packages in the codebase that tend to use testutils.
Package testutilsext is purely for test utilities that may access other packages in the codebase that tend to use testutils.

Jump to

Keyboard shortcuts

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