Documentation ¶
Index ¶
- Constants
- Variables
- func AssertEchoArgs(c *gc.C, execName string, args ...string)
- func FindImports(packageName, prefix string) ([]string, error)
- func FindTCPPort() int
- func HomePath(names ...string) string
- func HookCommandOutput(outputFunc *func(cmd *exec.Cmd) ([]byte, error), output []byte, err error) (<-chan *exec.Cmd, func())
- func MgoDialInfo(certs *Certs, addrs ...string) *mgo.DialInfo
- func MgoTestPackage(t *testing.T, certs *Certs)
- func PatchExecutable(c *gc.C, patcher CleanupPatcher, execName, script string, exitCodes ...int)
- func PatchExecutableAsEchoArgs(c *gc.C, patcher CleanupPatcher, execName string, exitCodes ...int)
- func PatchExecutableThrowError(c *gc.C, patcher CleanupPatcher, execName string, exitCode int)
- type Certs
- type CleanupFunc
- type CleanupPatcher
- type CleanupSuite
- func (s *CleanupSuite) AddCleanup(cleanup CleanupFunc)
- func (s *CleanupSuite) AddSuiteCleanup(cleanup CleanupFunc)
- func (s *CleanupSuite) HookCommandOutput(outputFunc *func(cmd *exec.Cmd) ([]byte, error), output []byte, err error) <-chan *exec.Cmd
- func (s *CleanupSuite) PatchEnvPathPrepend(dir string)
- func (s *CleanupSuite) PatchEnvironment(name, value string)
- func (s *CleanupSuite) PatchValue(dest, value interface{})
- func (s *CleanupSuite) SetUpSuite(c *gc.C)
- func (s *CleanupSuite) SetUpTest(c *gc.C)
- func (s *CleanupSuite) TearDownSuite(c *gc.C)
- func (s *CleanupSuite) TearDownTest(c *gc.C)
- type EnvironmentPatcher
- type FakeHome
- type FakeHomeSuite
- type HTTPServer
- func (s *HTTPServer) Flush()
- func (s *HTTPServer) Response(status int, headers map[string]string, body []byte)
- func (s *HTTPServer) ResponseFunc(n int, f ResponseFunc)
- func (s *HTTPServer) ResponseMap(n int, m ResponseMap)
- func (s *HTTPServer) Responses(n int, status int, headers map[string]string, body []byte)
- func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (s *HTTPServer) Start()
- func (s *HTTPServer) WaitRequest() *http.Request
- func (s *HTTPServer) WaitRequests(n int) []*http.Request
- type HTTPSuite
- type IsolatedMgoSuite
- type IsolationSuite
- type LoggingCleanupSuite
- type LoggingSuite
- type MgoInstance
- func (m *MgoInstance) Addr() string
- func (inst *MgoInstance) Destroy()
- func (inst *MgoInstance) DestroyWithLog()
- func (inst *MgoInstance) Dial() (*mgo.Session, error)
- func (inst *MgoInstance) DialDirect() (*mgo.Session, error)
- func (inst *MgoInstance) DialInfo() *mgo.DialInfo
- func (inst *MgoInstance) MustDial() *mgo.Session
- func (inst *MgoInstance) MustDialDirect() *mgo.Session
- func (m *MgoInstance) Port() int
- func (inst *MgoInstance) Reset()
- func (inst *MgoInstance) Restart()
- func (inst *MgoInstance) Start(certs *Certs) error
- type MgoSuite
- type OsEnvSuite
- type Response
- type ResponseFunc
- type ResponseMap
- type Restorer
- type Stub
- func (f *Stub) AddCall(funcName string, args ...interface{})
- func (f *Stub) Calls() []StubCall
- func (f *Stub) CheckCall(c *gc.C, index int, funcName string, args ...interface{})
- func (f *Stub) CheckCallNames(c *gc.C, expected ...string) bool
- func (f *Stub) CheckCalls(c *gc.C, expected []StubCall)
- func (f *Stub) CheckErrors(c *gc.C, expected ...error) bool
- func (f *Stub) CheckReceivers(c *gc.C, expected ...interface{}) bool
- func (f *Stub) MethodCall(receiver interface{}, funcName string, args ...interface{})
- func (f *Stub) NextErr() error
- func (f *Stub) ResetCalls()
- func (f *Stub) SetErrors(errors ...error)
- type StubCall
- type TestFile
Constants ¶
const ( // EchoQuotedArgs is a simple bash script that prints out the // basename of the command followed by the args as quoted strings. // If a ; separated list of exit codes is provided in $name.exitcodes // then it will return them in turn over multiple calls. If // $name.exitcodes does not exist, or the list runs out, return 0. EchoQuotedArgsUnix = `#!/bin/bash --norc name=` + "`basename $0`" + ` argfile="$name.out" exitcodesfile="$name.exitcodes" printf "%s" $name | tee -a $argfile for arg in "$@"; do printf " \"%s\"" "$arg" | tee -a $argfile done printf "\n" | tee -a $argfile if [ -f $exitcodesfile ] then exitcodes=$(cat $exitcodesfile) arr=(${exitcodes/;/ }) echo ${arr[1]} | tee $exitcodesfile exit ${arr[0]} fi ` EchoQuotedArgsWindows = `` /* 491-byte string literal not displayed */ )
const (
// The default password to use when connecting to the mongo database.
DefaultMongoPassword = "conn-from-name-secret"
)
Variables ¶
var HookChannelSize = 10
var ( // MgoServer is a shared mongo server used by tests. MgoServer = &MgoInstance{} )
var Server = NewHTTPServer(5 * time.Second)
Functions ¶
func AssertEchoArgs ¶
AssertEchoArgs is used to check the args from an execution of a command that has been patchec using PatchExecutable containing EchoQuotedArgs.
func FindImports ¶
FindImports returns a sorted list of packages imported by the package with the given name that have the given prefix. The resulting list removes the common prefix, leaving just the short names.
func FindTCPPort ¶
func FindTCPPort() int
FindTCPPort finds an unused TCP port and returns it. Use of this function has an inherent race condition - another process may claim the port before we try to use it. We hope that the probability is small enough during testing to be negligible.
func HomePath ¶
HomePath joins the specified path snippets and returns an absolute path under Juju home.
func HookCommandOutput ¶
func HookCommandOutput( outputFunc *func(cmd *exec.Cmd) ([]byte, error), output []byte, err error) (<-chan *exec.Cmd, func())
HookCommandOutput intercepts CommandOutput to a function that passes the actual command and it's output back via a channel, and returns the error passed into this function. It also returns a cleanup function so you can restore the original function
func MgoDialInfo ¶
MgoDialInfo returns a DialInfo suitable for dialling an MgoInstance at any of the given addresses, optionally using TLS.
func MgoTestPackage ¶
MgoTestPackage should be called to register the tests for any package that requires a MongoDB server. If certs is non-nil, a secure SSL connection will be used from client to server.
func PatchExecutable ¶
func PatchExecutable(c *gc.C, patcher CleanupPatcher, execName, script string, exitCodes ...int)
PatchExecutable creates an executable called 'execName' in a new test directory and that directory is added to the path.
func PatchExecutableAsEchoArgs ¶
func PatchExecutableAsEchoArgs(c *gc.C, patcher CleanupPatcher, execName string, exitCodes ...int)
PatchExecutableAsEchoArgs creates an executable called 'execName' in a new test directory and that directory is added to the path. The content of the script is 'EchoQuotedArgs', and the args file is removed using a cleanup function.
func PatchExecutableThrowError ¶
func PatchExecutableThrowError(c *gc.C, patcher CleanupPatcher, execName string, exitCode int)
PatchExecutableThrowError is needed to test cases in which we expect exit codes from executables called from the system path
Types ¶
type Certs ¶
type Certs struct { // CACert holds the CA certificate. This must certify the private key that // was used to sign the server certificate. CACert *x509.Certificate // ServerCert holds the certificate that certifies the server's // private key. ServerCert *x509.Certificate // ServerKey holds the server's private key. ServerKey *rsa.PrivateKey }
Certs holds the certificates and keys required to make a secure SSL connection.
type CleanupFunc ¶
type CleanupPatcher ¶
type CleanupPatcher interface { PatchEnvironment(name, value string) AddCleanup(cleanup CleanupFunc) }
type CleanupSuite ¶
type CleanupSuite struct {
// contains filtered or unexported fields
}
CleanupSuite adds the ability to add cleanup functions that are called during either test tear down or suite tear down depending on the method called.
func (*CleanupSuite) AddCleanup ¶
func (s *CleanupSuite) AddCleanup(cleanup CleanupFunc)
AddCleanup pushes the cleanup function onto the stack of functions to be called during TearDownTest.
func (*CleanupSuite) AddSuiteCleanup ¶
func (s *CleanupSuite) AddSuiteCleanup(cleanup CleanupFunc)
AddSuiteCleanup pushes the cleanup function onto the stack of functions to be called during TearDownSuite.
func (*CleanupSuite) HookCommandOutput ¶
func (s *CleanupSuite) HookCommandOutput( outputFunc *func(cmd *exec.Cmd) ([]byte, error), output []byte, err error, ) <-chan *exec.Cmd
HookCommandOutput calls the package function of the same name to mock out the result of a particular comand execution, and will call the restore function on test teardown.
func (*CleanupSuite) PatchEnvPathPrepend ¶
func (s *CleanupSuite) PatchEnvPathPrepend(dir string)
PatchEnvPathPrepend prepends the given path to the environment $PATH and restores the original path on test teardown.
func (*CleanupSuite) PatchEnvironment ¶
func (s *CleanupSuite) PatchEnvironment(name, value string)
PatchEnvironment sets the environment variable 'name' the the value passed in. The old value is saved and returned to the original value at test tear down time using a cleanup function.
func (*CleanupSuite) PatchValue ¶
func (s *CleanupSuite) PatchValue(dest, value interface{})
PatchValue sets the 'dest' variable the the value passed in. The old value is saved and returned to the original value at test tear down time using a cleanup function. The value must be assignable to the element type of the destination.
func (*CleanupSuite) SetUpSuite ¶
func (s *CleanupSuite) SetUpSuite(c *gc.C)
func (*CleanupSuite) SetUpTest ¶
func (s *CleanupSuite) SetUpTest(c *gc.C)
func (*CleanupSuite) TearDownSuite ¶
func (s *CleanupSuite) TearDownSuite(c *gc.C)
func (*CleanupSuite) TearDownTest ¶
func (s *CleanupSuite) TearDownTest(c *gc.C)
type EnvironmentPatcher ¶
type EnvironmentPatcher interface {
PatchEnvironment(name, value string)
}
EnvironmentPatcher is an interface that requires just one method: PatchEnvironment.
type FakeHome ¶
type FakeHome struct {
// contains filtered or unexported fields
}
FakeHome stores information about the user's home environment so it can be cast aside for tests and restored afterwards.
func MakeFakeHome ¶
func (*FakeHome) FileContents ¶
FileContents returns the test file contents for the given specified path (which may be relative, so we compare with the base filename only).
func (*FakeHome) FileExists ¶
FileExists returns if the given relative file path exists in the fake home.
type FakeHomeSuite ¶
type FakeHomeSuite struct { CleanupSuite LoggingSuite Home *FakeHome }
FakeHomeSuite sets up a fake home directory before running tests.
func (*FakeHomeSuite) SetUpSuite ¶
func (s *FakeHomeSuite) SetUpSuite(c *gc.C)
func (*FakeHomeSuite) SetUpTest ¶
func (s *FakeHomeSuite) SetUpTest(c *gc.C)
func (*FakeHomeSuite) TearDownSuite ¶
func (s *FakeHomeSuite) TearDownSuite(c *gc.C)
func (*FakeHomeSuite) TearDownTest ¶
func (s *FakeHomeSuite) TearDownTest(c *gc.C)
type HTTPServer ¶
type HTTPServer struct { URL string Timeout time.Duration // contains filtered or unexported fields }
func NewHTTPServer ¶
func NewHTTPServer(timeout time.Duration) *HTTPServer
func (*HTTPServer) Flush ¶
func (s *HTTPServer) Flush()
Flush discards all pending requests and responses.
func (*HTTPServer) Response ¶
func (s *HTTPServer) Response(status int, headers map[string]string, body []byte)
Response prepares the test server to respond the following request using the provided response parameters.
func (*HTTPServer) ResponseFunc ¶
func (s *HTTPServer) ResponseFunc(n int, f ResponseFunc)
ResponseFunc prepares the test server to respond the following n requests using f to build each response.
func (*HTTPServer) ResponseMap ¶
func (s *HTTPServer) ResponseMap(n int, m ResponseMap)
ResponseMap prepares the test server to respond the following n requests using the m to obtain the responses.
func (*HTTPServer) Responses ¶
Responses prepares the test server to respond the following n requests using the provided response parameters.
func (*HTTPServer) ServeHTTP ¶
func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request)
func (*HTTPServer) Start ¶
func (s *HTTPServer) Start()
func (*HTTPServer) WaitRequest ¶
func (s *HTTPServer) WaitRequest() *http.Request
WaitRequest returns the next request made to the http server from the queue. If no requests were previously made, it waits until the timeout value for one to be made.
func (*HTTPServer) WaitRequests ¶
func (s *HTTPServer) WaitRequests(n int) []*http.Request
WaitRequests returns the next n requests made to the http server from the queue. If not enough requests were previously made, it waits until the timeout value for them to be made.
type HTTPSuite ¶
type HTTPSuite struct{}
func (*HTTPSuite) SetUpSuite ¶
func (*HTTPSuite) TearDownSuite ¶
func (*HTTPSuite) TearDownTest ¶
type IsolatedMgoSuite ¶
type IsolatedMgoSuite struct { IsolationSuite MgoSuite }
IsolatedMgoSuite is a convenience type that combines the functionality IsolationSuite and MgoSuite.
func (*IsolatedMgoSuite) SetUpSuite ¶
func (s *IsolatedMgoSuite) SetUpSuite(c *gc.C)
func (*IsolatedMgoSuite) SetUpTest ¶
func (s *IsolatedMgoSuite) SetUpTest(c *gc.C)
func (*IsolatedMgoSuite) TearDownSuite ¶
func (s *IsolatedMgoSuite) TearDownSuite(c *gc.C)
func (*IsolatedMgoSuite) TearDownTest ¶
func (s *IsolatedMgoSuite) TearDownTest(c *gc.C)
type IsolationSuite ¶
type IsolationSuite struct { OsEnvSuite CleanupSuite LoggingSuite }
IsolationSuite isolates the tests from the underlaying system environment, sets up test logging and exposes cleanup facilities.
func (*IsolationSuite) SetUpSuite ¶
func (s *IsolationSuite) SetUpSuite(c *gc.C)
func (*IsolationSuite) SetUpTest ¶
func (s *IsolationSuite) SetUpTest(c *gc.C)
func (*IsolationSuite) TearDownSuite ¶
func (s *IsolationSuite) TearDownSuite(c *gc.C)
func (*IsolationSuite) TearDownTest ¶
func (s *IsolationSuite) TearDownTest(c *gc.C)
type LoggingCleanupSuite ¶
type LoggingCleanupSuite struct { LoggingSuite CleanupSuite }
LoggingCleanupSuite is defined for backward compatibility. Do not use this suite in new tests.
func (*LoggingCleanupSuite) SetUpSuite ¶
func (s *LoggingCleanupSuite) SetUpSuite(c *gc.C)
func (*LoggingCleanupSuite) SetUpTest ¶
func (s *LoggingCleanupSuite) SetUpTest(c *gc.C)
func (*LoggingCleanupSuite) TearDownSuite ¶
func (s *LoggingCleanupSuite) TearDownSuite(c *gc.C)
func (*LoggingCleanupSuite) TearDownTest ¶
func (s *LoggingCleanupSuite) TearDownTest(c *gc.C)
type LoggingSuite ¶
type LoggingSuite struct{}
LoggingSuite redirects the juju logger to the test logger when embedded in a gocheck suite type.
func (*LoggingSuite) SetUpSuite ¶
func (s *LoggingSuite) SetUpSuite(c *gc.C)
func (*LoggingSuite) SetUpTest ¶
func (s *LoggingSuite) SetUpTest(c *gc.C)
func (*LoggingSuite) TearDownSuite ¶
func (s *LoggingSuite) TearDownSuite(c *gc.C)
func (*LoggingSuite) TearDownTest ¶
func (s *LoggingSuite) TearDownTest(c *gc.C)
type MgoInstance ¶
type MgoInstance struct { // Params is a list of additional parameters that will be passed to // the mongod application Params []string // EnableJournal enables journaling. EnableJournal bool // EnableAuth enables authentication/authorization. EnableAuth bool // WithoutV8 is true if we believe this Mongo doesn't actually have the // V8 engine WithoutV8 bool // contains filtered or unexported fields }
func (*MgoInstance) Addr ¶
func (m *MgoInstance) Addr() string
Addr returns the address of the MongoDB server.
func (*MgoInstance) Destroy ¶
func (inst *MgoInstance) Destroy()
Destroy kills mongod and cleans up its data directory.
func (*MgoInstance) DestroyWithLog ¶
func (inst *MgoInstance) DestroyWithLog()
DestroyWithLog causes mongod to exit, cleans up its data directory, and captures the last N lines of mongod's log output.
func (*MgoInstance) Dial ¶
func (inst *MgoInstance) Dial() (*mgo.Session, error)
Dial returns a new connection to the MongoDB server.
func (*MgoInstance) DialDirect ¶
func (inst *MgoInstance) DialDirect() (*mgo.Session, error)
DialDirect returns a new direct connection to the shared MongoDB server. This must be used if you're connecting to a replicaset that hasn't been initiated yet.
func (*MgoInstance) DialInfo ¶
func (inst *MgoInstance) DialInfo() *mgo.DialInfo
DialInfo returns information suitable for dialling the receiving MongoDB instance.
func (*MgoInstance) MustDial ¶
func (inst *MgoInstance) MustDial() *mgo.Session
MustDial returns a new connection to the MongoDB server, and panics on errors.
func (*MgoInstance) MustDialDirect ¶
func (inst *MgoInstance) MustDialDirect() *mgo.Session
MustDialDirect works like DialDirect, but panics on errors.
func (*MgoInstance) Port ¶
func (m *MgoInstance) Port() int
Port returns the port of the MongoDB server.
func (*MgoInstance) Reset ¶
func (inst *MgoInstance) Reset()
Reset deletes all content from the MongoDB server and panics if it encounters errors.
func (*MgoInstance) Restart ¶
func (inst *MgoInstance) Restart()
Restart restarts the mongo server, useful for testing what happens when a state server goes down.
func (*MgoInstance) Start ¶
func (inst *MgoInstance) Start(certs *Certs) error
Start starts a MongoDB server in a temporary directory.
type MgoSuite ¶
type MgoSuite struct {
Session *mgo.Session
}
MgoSuite is a suite that deletes all content from the shared MongoDB server at the end of every test and supplies a connection to the shared MongoDB server.
func (*MgoSuite) SetUpSuite ¶
func (*MgoSuite) TearDownSuite ¶
func (*MgoSuite) TearDownTest ¶
type OsEnvSuite ¶
type OsEnvSuite struct {
// contains filtered or unexported fields
}
OsEnvSuite isolates the tests from the underlaying system environment. Environment variables are reset in SetUpTest and restored in TearDownTest.
func (*OsEnvSuite) SetUpSuite ¶
func (s *OsEnvSuite) SetUpSuite(c *gc.C)
func (*OsEnvSuite) SetUpTest ¶
func (s *OsEnvSuite) SetUpTest(c *gc.C)
func (*OsEnvSuite) TearDownSuite ¶
func (s *OsEnvSuite) TearDownSuite(c *gc.C)
func (*OsEnvSuite) TearDownTest ¶
func (s *OsEnvSuite) TearDownTest(c *gc.C)
type ResponseFunc ¶
type ResponseMap ¶
ResponseMap maps request paths to responses.
type Restorer ¶
type Restorer func()
Restorer holds a function that can be used to restore some previous state.
func PatchEnvPathPrepend ¶
PatchEnvPathPrepend provides a simple way to prepend path to the start of the PATH environment variable. Returns a function that restores the environment to what it was before.
func PatchEnvironment ¶
PatchEnvironment provides a test a simple way to override a single environment variable. A function is returned that will return the environment to what it was before.
func PatchValue ¶
func PatchValue(dest, value interface{}) Restorer
PatchValue sets the value pointed to by the given destination to the given value, and returns a function to restore it to its original value. The value must be assignable to the element type of the destination.
type Stub ¶
type Stub struct {
// contains filtered or unexported fields
}
Stub is used in testing to stand in for some other value, to record all calls to stubbed methods/functions, and to allow users to set the values that are returned from those calls. Stub is intended to be embedded in another struct that will define the methods to track:
type stubConn struct { *testing.Stub Response []byte } func newStubConn() *stubConn { return &stubConn{ Stub: &testing.Stub{}, } } // Send implements Connection. func (fc *stubConn) Send(request string) []byte { fc.MethodCall(fc, "Send", request) return fc.Response, fc.NextErr() }
As demonstrated in the example, embed a pointer to testing.Stub. This allows a single testing.Stub to be shared between multiple stubs.
Error return values are set through Stub.Errors. Set it to the errors you want returned (or use the convenience method `SetErrors`). The `NextErr` method returns the errors from Stub.Errors in sequence, falling back to `DefaultError` when the sequence is exhausted. Thus each stubbed method should call `NextErr` to get its error return value.
To validate calls made to the stub in a test call the CheckCalls (or CheckCall) method:
s.stub.CheckCalls(c, []StubCall{{ FuncName: "Send", Args: []interface{}{ expected, }, }}) s.stub.CheckCall(c, 0, "Send", expected)
Not only is Stub useful for building a interface implementation to use in testing (e.g. a network API client), it is also useful in regular function patching situations:
type myStub struct { *testing.Stub } func (f *myStub) SomeFunc(arg interface{}) error { f.AddCall("SomeFunc", arg) return f.NextErr() } s.PatchValue(&somefunc, s.myStub.SomeFunc)
This allows for easily monitoring the args passed to the patched func, as well as controlling the return value from the func in a clean manner (by simply setting the correct field on the stub).
func (*Stub) AddCall ¶
AddCall records a stubbed function call for later inspection using the CheckCalls method. A nil receiver is recorded. Thus for methods use MethodCall. All stubbed functions should call AddCall.
func (*Stub) Calls ¶
Calls returns the list of calls that have been registered on the stub (i.e. made on the stub's methods), in the order that they were made.
func (*Stub) CheckCall ¶
CheckCall checks the recorded call at the given index against the provided values. If the index is out of bounds then the check fails. The receiver is not checked. If it is significant for a test then it can be checked separately:
c.Check(mystub.Receivers[index], gc.Equals, expected)
func (*Stub) CheckCallNames ¶
CheckCallNames verifies that the in-order list of called method names matches the expected calls.
func (*Stub) CheckCalls ¶
CheckCalls verifies that the history of calls on the stub's methods matches the expected calls. The receivers are not checked. If they are significant then check Stub.Receivers separately.
func (*Stub) CheckErrors ¶
CheckErrors verifies that the list of errors is matches the expected list.
func (*Stub) CheckReceivers ¶
CheckReceivers verifies that the list of errors is matches the expected list.
func (*Stub) MethodCall ¶
MethodCall records a stubbed method call for later inspection using the CheckCalls method. The receiver is added to Stub.Receivers.
func (*Stub) NextErr ¶
NextErr returns the error that should be returned on the nth call to any method on the stub. It should be called for the error return in all stubbed methods.
func (*Stub) ResetCalls ¶
func (f *Stub) ResetCalls()
ResetCalls erases the calls recorded by this Stub.
type StubCall ¶
type StubCall struct { // Funcname is the name of the function that was called. FuncName string // Args is the set of arguments passed to the function. They are // in the same order as the function's parameters Args []interface{} }
StubCall records the name of a called function and the passed args.