testutil

package
v0.0.0-...-ff5f600 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2016 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Overview

Package testutil implements utilities for unit and integration tests.

Index

Constants

View Source
const RaceEnabled = false
View Source
const (
	SeedEnv = "V23_RNG_SEED"
)

Variables

This section is empty.

Functions

func CallAndRecover

func CallAndRecover(f func()) (result interface{})

CallAndRecover calls the function f and returns the result of recover(). This minimizes the scope of the deferred recover, to ensure f is actually the function that paniced.

func FileTreeEqual

func FileTreeEqual(aRoot, bRoot string, opts FileTreeOpts) (bool, error)

FileTreeEqual returns true if the filesystem trees rooted at aRoot and bRoot are the same. Use opts to control how matching is performed.

func FormatLogLine

func FormatLogLine(depth int, format string, args ...interface{}) string

FormatLogLine will prepend the file and line number of the caller at the specificied depth (as per runtime.Caller) to the supplied format and args and return a formatted string. It is useful when implementing functions that factor out error handling and reporting in tests.

func GlobName

func GlobName(ctx *context.T, name, pattern string) ([]string, []naming.GlobError, error)

GlobName calls __Glob on the given object with the given pattern and returns a sorted list of matching object names, or an error.

func InitRandGenerator

func InitRandGenerator(logger loggingFunc)

InitRandGenerator creates an instance of Random in the public variable Rand and prints out the seed use when creating the number number generator using the supplied logging function.

func LeafDispatcher

func LeafDispatcher(obj interface{}, auth security.Authorizer) rpc.Dispatcher

LeafDispatcher returns a dispatcher for a single object obj, using ReflectInvokerOrDie to invoke methods. Lookup only succeeds on the empty suffix. The provided auth is returned for successful lookups.

func NewPrincipal

func NewPrincipal(blessingNames ...string) security.Principal

NewPrincipal creates a new security.Principal.

It is a convenience wrapper over utility functions available in the v.io/x/ref/lib/security package.

If the set of blessingNames provided is non-empty, it creates self-signed blessings for each of those names and marks all of them as the default and shareable with all peers on the principal's blessing store.

Errors are truly rare events and since this is a utility intended only for unittests, NewPrincipal will panic on any errors.

func RandomBytes

func RandomBytes(size int) []byte

RandomBytes generates the given number of random bytes using the public variable Rand.

func RandomInt

func RandomInt() int

RandomInt returns a non-negative pseudo-random int using the public variable Rand.

func RandomInt63

func RandomInt63() int64

RandomInt63 returns a non-negative 63-bit pseudo-random integer as an int64 using the public variable Rand.

func RandomIntn

func RandomIntn(n int) int

RandomIntn returns a non-negative pseudo-random int in the range [0, n) using the public variable Rand.

func RetryFor

func RetryFor(timeout time.Duration, fn func() error) error

RetryFor calls fn repeatedly, with a brief delay after each invocation, until either (1) fn returns something other than TryAgain or (2) the specified timeout is reached. RetryFor returns nil, TimeoutError, or the non-TryAgain error returned by fn.

func TryAgain

func TryAgain(err error) error

TryAgain should be returned by the function provided to RetryFor to indicate that the function should be retried. If the function returns a non-TryAgain error, RetryFor will exit.

func WaitForProxyEndpoints

func WaitForProxyEndpoints(s rpc.Server, proxyName string) rpc.ServerStatus

WaitForProxyEndpoints blocks until the server's proxied endpoints appear in status.Endpoints, and returns the resulting server status.

func WaitForServerPublished

func WaitForServerPublished(s rpc.Server) rpc.ServerStatus

WaitForServerPublished blocks until all published mounts/unmounts have reached their desired state, and returns the resulting server status.

Types

type FileTreeOpts

type FileTreeOpts struct {
	// Debug is written to with additional information describing trees that are
	// not equal.
	Debug io.Writer

	// The pathname of regular files must match File{A,B} if they are provided,
	// otherwise the file is filtered out.  Similarly the pathname of directories
	// must match Dir{A,B} if they are provided.  {File,Dir}A matches pathnames
	// found under aRoot, while {File,Dir}B matches under bRoot.
	FileA, DirA *regexp.Regexp
	FileB, DirB *regexp.Regexp
}

FileTreeOpts contains options controlling how FileTreeEqual is performed.

type IDProvider

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

IDProvider is a convenience type to act as an "identity provider", i.e., it provides other principals with a blessing whose root certificate is signed by the IDProvider.

Typical usage:

p1, p2 := NewPrincipal(), NewPrincipal()
idp := NewIDProvider("xyz")
idp.Bless(p1, "alpha")
idp.Bless(p2, "beta")

Now, p1 and p2 will present "xyz/alpha" and "xyz/beta" as their blessing names and when communicating with each other, p1 and p2 will recognize these names as they both trust the root certificate (that of the IDProvider)

func IDProviderFromPrincipal

func IDProviderFromPrincipal(p security.Principal) *IDProvider

IDProviderFromPrincipal creates and IDProvider for the given principal. It will bless other principals with extensions of its default blessing.

func NewIDProvider

func NewIDProvider(name string) *IDProvider

NewIDProvider creates an IDProvider that will bless other principals with extensions of 'name'.

NewIDProvider panics on any errors.

func (*IDProvider) Bless

func (idp *IDProvider) Bless(who security.Principal, extension string, caveats ...security.Caveat) error

Bless sets up the provided principal to use blessings from idp as its default. It is shorthand for:

b, _ := idp.NewBlessings(who, extension, caveats...)
who.BlessingStore().SetDefault(b)
who.BlessingStore().Set(b, security.AllPrincipals)
security.AddToRoots(who, b)

func (*IDProvider) NewBlessings

func (idp *IDProvider) NewBlessings(p security.Principal, extension string, caveats ...security.Caveat) (security.Blessings, error)

NewBlessings returns Blessings that extend the identity provider's blessing with 'extension' and binds it to 'p.PublicKey'.

Unlike Bless, it does not modify p's BlessingStore or set of recognized root certificates.

func (*IDProvider) PublicKey

func (idp *IDProvider) PublicKey() security.PublicKey

PublicKey returns the public key of the identity provider.

type Random

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

Random is a concurrent-access friendly source of randomness.

var (
	Rand *Random
)

An instance of Random initialized by the InitRandomGenerator function.

func NewRandGenerator

func NewRandGenerator(logger loggingFunc) *Random

NewRandGenerator creates a new pseudo-random number generator; the seed may be supplied by V23_RNG_SEED to allow for reproducing a previous sequence, and is printed using the supplied logging function.

func (*Random) RandomBytes

func (rand *Random) RandomBytes(size int) []byte

RandomBytes generates the given number of random bytes.

func (*Random) RandomInt

func (r *Random) RandomInt() int

RandomInt returns a non-negative pseudo-random int.

func (*Random) RandomInt63

func (r *Random) RandomInt63() int64

RandomInt63 returns a non-negative 63-bit pseudo-random integer as an int64.

func (*Random) RandomIntn

func (r *Random) RandomIntn(n int) int

RandomIntn returns a non-negative pseudo-random int in the range [0, n).

type TimeoutError

type TimeoutError struct {
	// Timeout is the user-specified timeout.
	Timeout time.Duration
	// LastErr is the most recent TryAgain error returned by the user-provided
	// function.
	LastErr error
}

TimeoutError is returned by RetryFor when the specified timeout is reached.

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

Jump to

Keyboard shortcuts

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