testutil

package module
v0.0.0-...-d29e221 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2020 License: MIT Imports: 12 Imported by: 0

README

testutil Build Status

Yet Another Big Bucket of Go Testing Utility Functions

See docs for more details.

Documentation

Overview

testutil contains utility functions for integration-style tests. The S3 and SQS utilities require the fakes3 and fake_sqs gems, respectively, to be installed and available in the $PATH.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureStdout

func CaptureStdout(printFunction func() error) (string, error)

CaptureStdOut takes a function that prints to os.Stdout and returns the output as a string. If any error occurs when the given function is called, an empty string and the error is returned to the caller of CaptureStdOut.

Example
printFn := func() error {
	fmt.Println("This goes to stdout")
	return nil
}
output, _ := CaptureStdout(printFn)
fmt.Println(output == "This goes to stdout\n")
Output:

true

func ListenRedisChan

func ListenRedisChan(pool *redis.Pool, c string) chan struct{}

ListenRedisChan subscribes to redis channel c and signals the returned channel when it receives messages.

func ShouldCrash

func ShouldCrash(testName string, try func(), fail func())

ShouldCrash checks that the code under test, contained in the try function, exits the program with a non-zero exit code (for example with a log.Fatal()). If the try function does not exit the program with a non-zero exit code, the fail function is called. testName is the top-level test name from which ShouldCrash is called.

In order for ShouldCrash to work properly, it should only be called once per test; otherwise subsequent conditions will not be tested.

This uses a technique from https://talks.golang.org/2014/testing.slide#23

Example
// With crashing code under test
try := func() {
	log.Fatal("Crash!")
}
fail := func() {
	// This should be a t.Fatal() or similar in a real test
	fmt.Println("failed!")
}

ShouldCrash("ExampleShouldCrash", try, fail)
fmt.Println("succeeded!")
Output:

succeeded!
Example (Second)
// With non-crashing code under test
try := func() {
	log.Printf("No crash!")
}
fail := func() {
	// This should be a t.Fatal() or similar in a real test
	fmt.Println("failed!")
}

ShouldCrash("ExampleShouldCrash_second", try, fail)
Output:

failed!

func WaitFor

func WaitFor(try func() bool, fail func(), timeout time.Duration)

WaitFor runs the try function repeatedly until it returns true. If the try function does not return true within the timeout period, fail is called. WaitFor is mostly useful for checking conditions asynchronously in tests; it probably shouldn't be used for production code.

Types

type FakeRedis

type FakeRedis struct {
	Pool *redis.Pool
}

FakeRedis holds a redis pool for for testing. It requires a local redis server to be installed and running. All tests will be run on DB 9, which will be flushed before and after usage, so do not run against a server where you need values from DB 9!

Example
r := NewFakeRedis()
defer r.Close()

// Get connection
conn := r.Pool.Get()

pong, err := redis.String(conn.Do("PING"))
if err != nil {
	// handle error
}

fmt.Println(pong)
Output:

PONG

func NewFakeRedis

func NewFakeRedis() *FakeRedis

NewFakeRedis creates sets up a redis DB for testing and returns a pointer to a FakeRedis object.

func (*FakeRedis) Close

func (r *FakeRedis) Close()

Close cleans up after a redis test.

type FakeS3

type FakeS3 struct {
	// Client is a pointer to an S3 client set up for a fakes3
	// instance.
	Client *s3.S3

	// Session is an AWS Session that uses the fake config.
	Session *session.Session
}

FakeS3 holds a client for a fakes3 server. It requires the fakes3 gem be installed with the executable in $PATH.

Example
s := NewFakeS3("mybucket")
defer s.Close()

bucket := "mybucket"
key := "mykey"

// Put object
s.Client.PutObject(&s3.PutObjectInput{
	Bucket: &bucket,
	Key:    &key,
	Body:   bytes.NewReader([]byte("Hello!")),
})

// Get object
out, err := s.Client.GetObject(&s3.GetObjectInput{
	Bucket: &bucket,
	Key:    &key,
})
if err != nil {
	// handle error
}

hello := make([]byte, 6)
_, err = out.Body.Read(hello)
if err != nil {
	// handle error
}
out.Body.Close()

fmt.Println(string(hello))
Output:

Hello!

func NewFakeS3

func NewFakeS3(bucketName string) *FakeS3

NewFakeS3 starts a fakes3 process and creates a bucket with name bucketName. It returns a pointer to a FakeS3.

func (*FakeS3) Close

func (s *FakeS3) Close()

Close cleans up after and kills a fakes3 instance.

type FakeSQS

type FakeSQS struct {
	// Client is an SQS client configured to point to a fake_sqs
	// queue.
	Client *sqs.SQS

	// Session is an AWS Session that uses the fake config.
	Session *session.Session

	// URL is the URL for a fake SQS queue.
	URL string
}

FakeSQS holds an SQS client and queue for a fake_sqs instance. It requires the fake_sqs gem to be installed with the executable in the $PATH.

Example
s := NewFakeSQS("fake-queue")
defer s.Close()

// Send message
s.Client.SendMessage(&sqs.SendMessageInput{
	MessageBody: aws.String("Hello!"),
	QueueUrl:    &s.URL,
})

// Receive message
out, err := s.Client.ReceiveMessage(&sqs.ReceiveMessageInput{
	QueueUrl:        &s.URL,
	WaitTimeSeconds: aws.Int64(3),
})
if err != nil {
	// handle error
}

fmt.Printf(*out.Messages[0].Body)
Output:

Hello!

func NewFakeSQS

func NewFakeSQS(queueName string) *FakeSQS

NewFakeSQS starts a fake_sqs process and creates a queue with name queueName. It returns a FakeSQS object with an SQS client and a URL for the newly-created queue.

func (*FakeSQS) Close

func (s *FakeSQS) Close()

Close cleans up after a fake_sqs process.

Jump to

Keyboard shortcuts

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