xtest

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package xtest implements common functionality for unit tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertError added in v0.4.0

func AssertError(t *testing.T, err error, expectError bool)

AssertError checks that err is not nil if expectError is true and that is it nil otherwise

func AssertErrorsIs added in v0.4.0

func AssertErrorsIs(t *testing.T, actualErr, expectedErr error)

AssertErrorsIs checks that errors.Is(actualErr, expectedErr) returns true, if expectedErr is not nil.

func AssertReadDoesNotReturnBefore added in v0.3.0

func AssertReadDoesNotReturnBefore(t testing.TB, ch <-chan struct{}, timeout time.Duration)

AssertReadDoesNotReturnBefore will call t.Fatalf if the first read from the channel happens before timeout.

func AssertReadReturnsBefore added in v0.3.0

func AssertReadReturnsBefore(t testing.TB, ch <-chan struct{}, timeout time.Duration)

AssertReadReturnsBefore will call t.Fatalf if the first read from the channel doesn't happen before timeout.

func AssertReadReturnsBetween added in v0.3.0

func AssertReadReturnsBetween(t testing.TB, ch <-chan struct{}, x, y time.Duration)

AssertReadReturnsBetween will call t.Fatalf if the first read from the channel doesn't happen between x and y.

func CopyDir added in v0.6.0

func CopyDir(t testing.TB, from, to string)

CopyDir copies "from" to "to", using the unix cp command.

func ExpandPath

func ExpandPath(file string) string

ExpandPath returns testdata/file.

func FailOnErr

func FailOnErr(t testing.TB, err error, desc ...string)

FailOnErr causes t to exit with a fatal error if err is non-nil.

func LoadChain added in v0.6.0

func LoadChain(t *testing.T, file string) []*x509.Certificate

LoadChain loads a certificate chain from a file. The file must be PEM encoded.

func LoadSigner added in v0.7.0

func LoadSigner(t *testing.T, file string) crypto.Signer

LoadSigner loads a private key from a file. The file must be PEM encoded.

func LoadTRC added in v0.6.0

func LoadTRC(t *testing.T, file string) cppki.SignedTRC

LoadTRC loads a signed TRC from a file.

func MustExtractIA added in v0.6.0

func MustExtractIA(t *testing.T, cert *x509.Certificate) addr.IA

MustExtractIA extracts the IA from the cert's subject and verifies it is non-nil. It is the callers responsibility to make sure that this is a cert that always contains an IA.

func MustMarshalJSONToFile

func MustMarshalJSONToFile(t testing.TB, v interface{}, baseName string)

MustMarshalJSONToFile marshals v and writes the result to file testdata/baseName. If the file exists, it is truncated; if it doesn't exist, it is created. On errors, t.Fatal() is called.

func MustParseAS

func MustParseAS(s string) addr.AS

MustParseAS parses s and returns the corresponding addr.AS object. It panics if s is not valid AS representation.

func MustParseASes added in v0.6.0

func MustParseASes(list string) []addr.AS

MustParseASes parses a list of comma separated AS strings. It panics in case parsing fails.

func MustParseCIDR added in v0.6.0

func MustParseCIDR(t *testing.T, s string) *net.IPNet

MustParseCIDR parses s and returns the corresponding net.IPNet object. It fails the test if s is not a valid CIDR string.

func MustParseCIDRs added in v0.6.0

func MustParseCIDRs(t *testing.T, entries ...string) []*net.IPNet

MustParseCIDRs parses the CIDR entries and returns a list containing the parsed net.IPNet objects.

func MustParseHexString added in v0.2.0

func MustParseHexString(s string) []byte

MustParseHexString parses s and returns the corresponding byte slice. It panics if the decoding fails.

func MustParseIA

func MustParseIA(s string) addr.IA

MustParseIA parses s and returns the corresponding addr.IA object. It panics if s is not a valid ISD-AS representation.

func MustParseIAs added in v0.7.0

func MustParseIAs(list string) []addr.IA

MustParseIAs parses a list of comma separated ISD-AS strings. It panics in case parsing fails.

func MustParseIP added in v0.7.0

func MustParseIP(t *testing.T, addr string) net.IP

MustParseIP parses an IP address and returns the parsed net.IP object.

func MustParseIPPrefixes added in v0.7.0

func MustParseIPPrefixes(t *testing.T, prefixes ...string) []netaddr.IPPrefix

MustParseIPPrefixes parses the CIDR entries and returns a list containing the parsed netaddr.IPPrefix objects.

func MustParseUDPAddr added in v0.6.0

func MustParseUDPAddr(t *testing.T, s string) *net.UDPAddr

MustParseUDPAddr parses s and returns the corresponding net.UDPAddr object. It fails the test if s is not a valid UDP address string.

func MustParseUDPAddrs added in v0.7.0

func MustParseUDPAddrs(t *testing.T, entries ...string) []*net.UDPAddr

MustParseUDPAddrs parses the UPD address entries and returns a list containing the parsed net.UDPAddr objects.

func MustReadFromFile

func MustReadFromFile(t testing.TB, baseName string) []byte

MustReadFromFile reads testdata/baseName and returns the raw content. On errors, t.Fatal() is called.

func MustTempDir

func MustTempDir(dir, prefix string) (string, func())

MustTempDir creates a new temporary directory under dir with the specified prefix. If the function encounters an error it panics. The second return value is a clean-up function that can be called to recursively delete the entire directory.

func MustTempFileName

func MustTempFileName(dir, prefix string) string

MustTempFileName is a wrapper around TempFileName. The function panics if an error occurs. It is intended for tests where error handling is not necessary, and for chaining functions.

func MustWriteToFile

func MustWriteToFile(t testing.TB, b []byte, baseName string)

MustWriteToFile writes b to file testdata/baseName. If the file exists, it is truncated; if it doesn't exist, it is created. On errors, t.Fatal() is called.

func SanitizedName added in v0.7.0

func SanitizedName(t testing.TB) string

SanitizedName sanitizes the test name such that it can be used as a file name.

func TempDir added in v0.7.0

func TempDir(t testing.TB) (string, func())

func TempFileName

func TempFileName(dir, prefix string) (string, error)

TempFileName creates a temporary file in dir with the specified prefix, and then closes and deletes the file and returns its name. It is useful for testing packages that care about a unique path without being able to overwrite it (e.g., UNIX domain socket addresses or databases).

func UpdateGoldenFiles added in v0.7.0

func UpdateGoldenFiles() *bool

Update registers the '-update' flag for the test.

This flag should be checked by golden file tests to see whether the golden files should be updated or not. The golden files should be deterministic. Use UpdateNonDeterminsticGoldenFiles instead, if they are not deterministic.

To update all golden files, run the following command:

go test ./... -update

To update a specific package, run the following command:

go test ./path/to/package -update

The flag should be registered as a package global variable:

var update = xtest.UpdateGoldenFiles()

func UpdateNonDeterminsticGoldenFiles added in v0.7.0

func UpdateNonDeterminsticGoldenFiles() *bool

UpdateNonDeterminsticGoldenFiles registers the '-update-non-deterministic' flag for the test.

This flag should be checked by golden file tests to see whether the non-deterministic golden files should be updated or not.

To update all golden files, run the following command:

go test ./... -update-non-deterministic

To update a specific package, run the following command:

go test ./path/to/package -update-non-deterministic

The flag should be registered as a package global variable:

var updateNonDeterministic = xtest.UpdateNonDeterminsticGoldenFiles()

Types

type Callback added in v0.4.0

type Callback interface {
	Call()
}

Callback defines an interfaces that provides a callback function that is mockable. A mock implementation implementing this interface can be found in sub-package mock_xtest.

type GRPCService added in v0.6.0

type GRPCService struct {
	// contains filtered or unexported fields
}

func NewGRPCService added in v0.6.0

func NewGRPCService() *GRPCService

func (*GRPCService) Dial added in v0.6.0

func (s *GRPCService) Dial(ctx context.Context, addr net.Addr) (*grpc.ClientConn, error)

func (*GRPCService) Server added in v0.6.0

func (s *GRPCService) Server() *grpc.Server

func (*GRPCService) Start added in v0.6.0

func (s *GRPCService) Start(t *testing.T)

type PanickingReporter added in v0.3.0

type PanickingReporter struct {
	*testing.T
}

PanickingReporter is a wrapper around the *testing.T implementation of gomock.TestReporter which panics and logs on FatalF instead of calling runtime.Goexit(). This avoids deadlocks when a child goroutine fails a mocking constraint when using gomock.

For more information, see https://github.com/golang/mock/issues/139.

func (*PanickingReporter) Fatalf added in v0.3.0

func (reporter *PanickingReporter) Fatalf(format string, args ...interface{})

type Waiter added in v0.4.0

type Waiter struct {
	sync.WaitGroup
}

Waiter wraps the waitgroup and allows waiting with timeouts.

func (*Waiter) WaitWithTimeout added in v0.4.0

func (w *Waiter) WaitWithTimeout(timeout time.Duration) bool

WaitWithTimeout returns immediately after the waitgroup is done, or the timeout has passed. The return value indicates whether the call timed out.

Directories

Path Synopsis
Code generated by graphupdater tool, DO NOT EDIT.
Code generated by graphupdater tool, DO NOT EDIT.
Package mock_xtest is a generated GoMock package.
Package mock_xtest is a generated GoMock package.

Jump to

Keyboard shortcuts

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