testtool

package
v0.0.0-...-7a50bc8 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2018 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TestLogFile string = ""

If a -log or log is provided with an path to a directory then that path is available in this variable. This is a helper for tests that wish to log. An empty string indicates the path was not set. The value is set only to allow callers to make use of in their tests. There are no other side effects.

Functions

func AssertChanNoReceive

func AssertChanNoReceive(
	t *testing.T,
	ch chan struct{},
	name string,
	timeout time.Duration,
)

AssertChanNoreceive ensures that the specified channel never received a message. AssertChanNoReceive requires a name of the function that should not write to the channel in order to provide a useful error message.

func AssertChanReceive

func AssertChanReceive(
	t *testing.T,
	ch chan struct{},
	name string,
	timeout time.Duration,
	numSends int,
)

AssertChanReceive ensures that the given channel received the specified number of messages. The supplied timeout specifies the timeout on each expected read. It is not a cumulative timeout for all reads. AssertChanReceive requires a name of the function that should write to the channel in order to provide a useful error message.

func Fatal

func Fatal(t Logger, args ...interface{})

Fatal fails the test with a simple format for the message.

func Fatalf

func Fatalf(l Logger, f string, args ...interface{})

Fatalf wraps Fatalf in order to provide a functional stack trace on failures rather than just a line number of the failing check. This helps if you have a test that fails in a loop since it will show the path to get there as well as the error directly.

func RandomTestString

func RandomTestString(n int) string

RandomTestString generates a random test string from only upper and lower case letters.

func RootTempDir

func RootTempDir(t *TestTool) string

RootTempDir creates a directory that will exist until the process running the tests exits.

func SendWithCancel

func SendWithCancel(w chan<- struct{}, done <-chan struct{})

SendWithCancel expects a channel to write a signal to, and also a channel that indicates when the signal listener is no longer listening.

func TestEqual

func TestEqual(t Logger, have, want interface{}, msg ...string)

func TestExpectError

func TestExpectError(l Logger, err error, msg ...string)

TestExpectError calls Fatal if err is nil.

func TestExpectNonNil

func TestExpectNonNil(t Logger, i interface{}, msg ...string)

func TestExpectNonZeroLength

func TestExpectNonZeroLength(l Logger, size int)

TestExpectNonZeroLength fails the test if the given value is zero.

func TestExpectPanic

func TestExpectPanic(l Logger, f func(), msg string)

TestExpectPanic verifies that a panic is called with the expected msg.

func TestExpectSuccess

func TestExpectSuccess(l Logger, err error, msg ...string)

TestExpectSuccess fails the test if err is not nil and fails the test and output the reason for the failure as the err argument the same as Fatalf. If err implements the BackTracer interface a backtrace will also be displayed.

func TestExpectZeroLength

func TestExpectZeroLength(l Logger, size int)

TestExpectNonZeroLength fails the test if the given value is not zero.

func TestFalse

func TestFalse(t Logger, ans bool)

func TestHttpGet

func TestHttpGet(t Logger, url string, expectedReturnCode int) (string, int)

Test an HTTP GET to the given URL. If expectedReturnCode is a value other than -1, the test will fail if the response status code doesn't match the exptected code. Method returns the string value of the response body and the status code.

func TestHttpGetTimeout

func TestHttpGetTimeout(t Logger, url string, expectedReturnCode int, duration time.Duration) (string, int)

Test an HTTP GET to the given URL for the amount of time specified in duration. This will retry with multiple requests until one is successful or it has taken longer than the duration. If expectedReturnCode is a value other than -1, the test will fail if the response status code doesn't match the exptected code. Method returns the string value of the response body and the status code.

func TestHttpPost

func TestHttpPost(
	t Logger, url string, contentType string, body string, expectedReturnCode int,
) (string, int)

Test an HTTP POST to the given URL, with the given content type and request body.. If expectedReturnCode is a value other than -1, the test will fail if the response status code doesn't match the exptected code. Method returns the string value of the response body and the status code.

func TestHttpPut

func TestHttpPut(
	t Logger, url string, contentType string, body string, expectedReturnCode int,
) (string, int)

Test an HTTP PUT to the given URL, with the given content type and request body.. If expectedReturnCode is a value other than -1, the test will fail if the response status code doesn't match the exptected code. Method returns the string value of the response body and the status code.

func TestMatch

func TestMatch(t Logger, have string, r *regexp.Regexp)

func TestNotEqual

func TestNotEqual(t Logger, have, want interface{}, msg ...string)

func TestNotMatch

func TestNotMatch(t Logger, have string, r *regexp.Regexp)

func TestRequiresRoot

func TestRequiresRoot(l Logger)

TestRequiresRoot is called to require that your test is run as root. NOTICE: this does not cause the test to FAIL. This seems like the most sane thing to do based on the shortcomings of Go's test utilities.

As an added feature this function will append all skipped test names into the file name specified in the environment variable:

$SKIPPED_ROOT_TESTS_FILE

func TestTrue

func TestTrue(t Logger, ans bool)

func Timeout

func Timeout(l Logger, timeout, sleep time.Duration, f func() bool)

Timeout runs the given function until 'timeout' has passed, sleeping 'sleep' duration in between runs. If the function returns true this exits, otherwise after timeout this will fail the test.

Types

type Backtracer

type Backtracer interface {
	Backtrace() []string
}

Backtracer is an interface that provies additional information to be displayed using the TestExpectSuccess() functions. For an example see BackError in the apcera/cfg package.

type Logger

type Logger interface {
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Failed() bool
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Skip(args ...interface{})
	Skipf(format string, args ...interface{})
	Log(args ...interface{})
	Logf(format string, args ...interface{})
}

Logger is a common interface that can be used to allow testing.B and testing.T objects to be passed to the same function.

type TestData

type TestData struct {
	File       string
	Package    string
	TestName   string
	PackageDir string
	Line       int
}

func GetTestData

func GetTestData(tb testing.TB) *TestData

GetTestData goes through the call stack of the current goroutine and creates a ordered set of strings for the files, function names, and line numbers then adds this data to the error message.

type TestTool

type TestTool struct {
	testing.TB

	// Stores output from the logging system so it can be written only if
	// the test actually fails.
	LogBuffer *unittest.LogBuffer

	// This is a list of functions that will be run on test completion. Having
	// this allows us to clean up temporary directories or files after the
	// test is done which is a huge win.
	Finalizers []func()

	// Parameters contains test-specific caches of data.
	Parameters map[string]interface{}

	RandomTestString string
	PackageHash      string

	*TestData
}

TestTool type allows for parallel tests.

func StartTest

func StartTest(tb testing.TB) *TestTool

StartTest should be called at the start of a test to setup all the various state bits that are needed.

func (*TestTool) AddTestFinalizer

func (tt *TestTool) AddTestFinalizer(f func())

AddTestFinalizer adds a function to be called once the test finishes.

func (*TestTool) FinishTest

func (tt *TestTool) FinishTest()

FinishTest is called as a defer to a test in order to clean up after a test run. All tests in this module should call this function as a defer right after calling StartTest()

func (*TestTool) TempDir

func (tt *TestTool) TempDir() string

TempDir makes a temporary directory.

func (*TestTool) TempDirMode

func (tt *TestTool) TempDirMode(mode os.FileMode) string

TempDirMode makes a temporary directory with the given mode.

func (*TestTool) TempFile

func (tt *TestTool) TempFile() string

TempFile allocate a temporary file and ensures that it gets cleaned up when the test is completed.

func (*TestTool) TempFileMode

func (tt *TestTool) TempFileMode(mode os.FileMode) string

TempFileMode writes a temp file with the given mode.

func (*TestTool) WriteTempFile

func (tt *TestTool) WriteTempFile(contents string) string

WriteTempFile writes contents to a temporary file, sets up a Finalizer to remove the file once the test is complete, and then returns the newly created filename to the caller.

func (*TestTool) WriteTempFileMode

func (tt *TestTool) WriteTempFileMode(contents string, mode os.FileMode) string

WriteTempFileMode is like WriteTempFile but sets the mode.

Jump to

Keyboard shortcuts

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