Documentation
¶
Overview ¶
Package testutils provides various utilities for use in tests.
Index ¶
- func ArtifactGoogleCreds(tb testing.TB) string
- func BackingMongoDBClient(tb testing.TB) *mongo.Client
- func BackingMongoDBClientWithOptions(tb testing.TB, baseOptions *options.ClientOptions) *mongo.Client
- func BackingMongoDBURI(tb testing.TB) string
- func GenerateSelfSignedCertificate(commonName string, altNames ...string) (tls.Certificate, string, string, *x509.CertPool, error)
- func NewMongoDBNamespace() (string, string)
- func ReserveRandomListener(tb testing.TB) *net.TCPListener
- func SkipUnlessArtifactGoogleCreds(tb testing.TB)
- func SkipUnlessBackingMongoDBURI(tb testing.TB)
- func SkipUnlessInternet(tb testing.TB)
- func Teardown()
- func TempDir(dir, pattern string) (string, error)
- func TempDirT(tb testing.TB, dir, pattern string) string
- func TempFile(tb testing.TB) *os.File
- func TestMain(t *testing.T, ...)
- func WaitForAssertion(t *testing.T, assertion func(tb testing.TB))
- func WaitForAssertionWithSleep(t *testing.T, wait time.Duration, maxAttempts int, ...)
- func WaitSuccessfulDial(address string) error
- func WatchedFile(tb testing.TB) (*fsnotify.Watcher, *os.File)
- func WatchedFiles(tb testing.TB, n int) (*fsnotify.Watcher, []*os.File)
- type ContextualMainExecution
- type MainTestCase
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArtifactGoogleCreds ¶
ArtifactGoogleCreds returns the google credentials for artifact.
func BackingMongoDBClient ¶
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 ¶
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 ¶
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 ¶
SkipUnlessArtifactGoogleCreds verifies google credentials are available for artifact.
func SkipUnlessBackingMongoDBURI ¶
SkipUnlessBackingMongoDBURI verifies there is a backing MongoDB URI to use.
func SkipUnlessInternet ¶
SkipUnlessInternet verifies there is an internet connection.
func TempFile ¶ added in v0.1.10
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 utils.ZapCompatibleLogger) error, tcs []MainTestCase, )
TestMain tests a main function with a series of test cases in serial.
func WaitForAssertion ¶
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 ¶
WaitSuccessfulDial waits for a dial attempt to succeed.
func WatchedFile ¶ added in v0.1.73
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
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 utils.ZapCompatibleLogger) error, args []string, logger utils.ZapCompatibleLogger, ) 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 utils.ZapCompatibleLogger, 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.