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 ¶
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 ¶
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!
Types ¶
type FakeRedis ¶
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.
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!
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 ¶
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.