testing
v0.0.0-20140813210533-fa988ea027e2
Documentation
pkg.go.dev ↗Constants
DefaultMongoPassword
const ( // The default password to use when connecting to the mongo database. DefaultMongoPassword = "conn-from-name-secret" )
EchoQuotedArgs
const ( // EchoQuotedArgs is a simple bash script that prints out the // basename of the command followed by the args as quoted strings. EchoQuotedArgs = `#!/bin/bash --norc name=` + "`basename $0`" + ` argfile="$name.out" rm -f $argfile printf "%s" $name | tee -a $argfile for arg in "$@"; do printf " \"%s\"" "$arg" | tee -a $argfile done printf "\n" | tee -a $argfile ` )
Variables
HookChannelSize
var HookChannelSize = 10
MgoServer
var (
// MgoServer is a shared mongo server used by tests.
MgoServer = &MgoInstance{}
)
Server
var Server = NewHTTPServer(5 * time.Second)
Functions
AssertEchoArgs
func AssertEchoArgs(c *gc.C, execName string, args ...string)
AssertEchoArgs is used to check the args from an execution of a command that has been patchec using PatchExecutable containing EchoQuotedArgs.
FindImports
func FindImports(packageName, prefix string) ([]string, error)
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.
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.
HomePath
func HomePath(names ...string) string
HomePath joins the specified path snippets and returns an absolute path under Juju home.
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
MgoDialInfo
func MgoDialInfo(certs *Certs, addrs ...string) *mgo.DialInfo
MgoDialInfo returns a DialInfo suitable for dialling an MgoInstance at any of the given addresses, optionally using TLS.
MgoTestPackage
func MgoTestPackage(t *testing.T, certs *Certs)
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.
PatchExecutable
func PatchExecutable(c *gc.C, patcher EnvironmentPatcher, execName, script string)
PatchExecutable creates an executable called 'execName' in a new test directory and that directory is added to the path.
PatchExecutableAsEchoArgs
func PatchExecutableAsEchoArgs(c *gc.C, patcher CleanupPatcher, execName string)
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.
Types
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.
CleanupFunc
type CleanupFunc func(*gc.C)
CleanupPatcher
type CleanupPatcher interface {
PatchEnvironment(name, value string)
AddCleanup(cleanup CleanupFunc)
}
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.
CleanupSuite.AddCleanup
func (s *CleanupSuite) AddCleanup(cleanup CleanupFunc)
AddCleanup pushes the cleanup function onto the stack of functions to be called during TearDownTest.
CleanupSuite.AddSuiteCleanup
func (s *CleanupSuite) AddSuiteCleanup(cleanup CleanupFunc)
AddSuiteCleanup pushes the cleanup function onto the stack of functions to be called during TearDownSuite.
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.
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.
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.
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.
CleanupSuite.SetUpSuite
func (s *CleanupSuite) SetUpSuite(c *gc.C)
CleanupSuite.SetUpTest
func (s *CleanupSuite) SetUpTest(c *gc.C)
CleanupSuite.TearDownSuite
func (s *CleanupSuite) TearDownSuite(c *gc.C)
CleanupSuite.TearDownTest
func (s *CleanupSuite) TearDownTest(c *gc.C)
EnvironmentPatcher
type EnvironmentPatcher interface {
PatchEnvironment(name, value string)
}
EnvironmentPatcher is an interface that requires just one method: PatchEnvironment.
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.
MakeFakeHome
func MakeFakeHome(c *gc.C) *FakeHome
FakeHome.AddFiles
func (h *FakeHome) AddFiles(c *gc.C, files ...TestFile)
FakeHome.FileContents
func (h *FakeHome) FileContents(c *gc.C, path string) string
FileContents returns the test file contents for the given specified path (which may be relative, so we compare with the base filename only).
FakeHome.FileExists
func (h *FakeHome) FileExists(path string) bool
FileExists returns if the given relative file path exists in the fake home.
FakeHomeSuite
type FakeHomeSuite struct {
CleanupSuite
LoggingSuite
Home *FakeHome
}
FakeHomeSuite sets up a fake home directory before running tests.
FakeHomeSuite.SetUpSuite
func (s *FakeHomeSuite) SetUpSuite(c *gc.C)
FakeHomeSuite.SetUpTest
func (s *FakeHomeSuite) SetUpTest(c *gc.C)
FakeHomeSuite.TearDownSuite
func (s *FakeHomeSuite) TearDownSuite(c *gc.C)
FakeHomeSuite.TearDownTest
func (s *FakeHomeSuite) TearDownTest(c *gc.C)
HTTPServer
type HTTPServer struct {
URL string
Timeout time.Duration
// contains filtered or unexported fields
}
NewHTTPServer
func NewHTTPServer(timeout time.Duration) *HTTPServer
HTTPServer.Flush
func (s *HTTPServer) Flush()
Flush discards all pending requests and responses.
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.
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.
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.
HTTPServer.Responses
func (s *HTTPServer) Responses(n int, status int, headers map[string]string, body []byte)
Responses prepares the test server to respond the following n requests using the provided response parameters.
HTTPServer.ServeHTTP
func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request)
HTTPServer.Start
func (s *HTTPServer) Start()
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.
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.
HTTPSuite
type HTTPSuite struct{}
HTTPSuite.SetUpSuite
func (s *HTTPSuite) SetUpSuite(c *gc.C)
HTTPSuite.SetUpTest
func (s *HTTPSuite) SetUpTest(c *gc.C)
HTTPSuite.TearDownSuite
func (s *HTTPSuite) TearDownSuite(c *gc.C)
HTTPSuite.TearDownTest
func (s *HTTPSuite) TearDownTest(c *gc.C)
HTTPSuite.URL
func (s *HTTPSuite) URL(path string) string
IsolationSuite
type IsolationSuite struct {
OsEnvSuite
CleanupSuite
LoggingSuite
}
IsolationSuite isolates the tests from the underlaying system environment, sets up test logging and exposes cleanup facilities.
IsolationSuite.SetUpSuite
func (s *IsolationSuite) SetUpSuite(c *gc.C)
IsolationSuite.SetUpTest
func (s *IsolationSuite) SetUpTest(c *gc.C)
IsolationSuite.TearDownSuite
func (s *IsolationSuite) TearDownSuite(c *gc.C)
IsolationSuite.TearDownTest
func (s *IsolationSuite) TearDownTest(c *gc.C)
LoggingCleanupSuite
type LoggingCleanupSuite struct {
LoggingSuite
CleanupSuite
}
LoggingCleanupSuite is defined for backward compatibility. Do not use this suite in new tests.
LoggingCleanupSuite.SetUpSuite
func (s *LoggingCleanupSuite) SetUpSuite(c *gc.C)
LoggingCleanupSuite.SetUpTest
func (s *LoggingCleanupSuite) SetUpTest(c *gc.C)
LoggingCleanupSuite.TearDownSuite
func (s *LoggingCleanupSuite) TearDownSuite(c *gc.C)
LoggingCleanupSuite.TearDownTest
func (s *LoggingCleanupSuite) TearDownTest(c *gc.C)
LoggingSuite
type LoggingSuite struct{}
LoggingSuite redirects the juju logger to the test logger when embedded in a gocheck suite type.
LoggingSuite.SetUpSuite
func (s *LoggingSuite) SetUpSuite(c *gc.C)
LoggingSuite.SetUpTest
func (s *LoggingSuite) SetUpTest(c *gc.C)
LoggingSuite.TearDownSuite
func (s *LoggingSuite) TearDownSuite(c *gc.C)
LoggingSuite.TearDownTest
func (s *LoggingSuite) TearDownTest(c *gc.C)
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
// WithoutV8 is true if we believe this Mongo doesn't actually have the
// V8 engine
WithoutV8 bool
// contains filtered or unexported fields
}
MgoInstance.Addr
func (m *MgoInstance) Addr() string
Addr returns the address of the MongoDB server.
MgoInstance.Destroy
func (inst *MgoInstance) Destroy()
Destroy kills mongod and cleans up its data directory.
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.
MgoInstance.Dial
func (inst *MgoInstance) Dial() (*mgo.Session, error)
Dial returns a new connection to the MongoDB server.
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.
MgoInstance.DialInfo
func (inst *MgoInstance) DialInfo() *mgo.DialInfo
DialInfo returns information suitable for dialling the receiving MongoDB instance.
MgoInstance.MustDial
func (inst *MgoInstance) MustDial() *mgo.Session
MustDial returns a new connection to the MongoDB server, and panics on errors.
MgoInstance.MustDialDirect
func (inst *MgoInstance) MustDialDirect() *mgo.Session
MustDialDirect works like DialDirect, but panics on errors.
MgoInstance.Port
func (m *MgoInstance) Port() int
Port returns the port of the MongoDB server.
MgoInstance.Reset
func (inst *MgoInstance) Reset()
Reset deletes all content from the MongoDB server and panics if it encounters errors.
MgoInstance.Restart
func (inst *MgoInstance) Restart()
Restart restarts the mongo server, useful for testing what happens when a state server goes down.
MgoInstance.Start
func (inst *MgoInstance) Start(certs *Certs) error
Start starts a MongoDB server in a temporary directory.
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.
MgoSuite.SetUpSuite
func (s *MgoSuite) SetUpSuite(c *gc.C)
MgoSuite.SetUpTest
func (s *MgoSuite) SetUpTest(c *gc.C)
MgoSuite.TearDownSuite
func (s *MgoSuite) TearDownSuite(c *gc.C)
MgoSuite.TearDownTest
func (s *MgoSuite) TearDownTest(c *gc.C)
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.
OsEnvSuite.SetUpSuite
func (s *OsEnvSuite) SetUpSuite(c *gc.C)
OsEnvSuite.SetUpTest
func (s *OsEnvSuite) SetUpTest(c *gc.C)
OsEnvSuite.TearDownSuite
func (s *OsEnvSuite) TearDownSuite(c *gc.C)
OsEnvSuite.TearDownTest
func (s *OsEnvSuite) TearDownTest(c *gc.C)
Response
type Response struct {
Status int
Headers map[string]string
Body []byte
}
ResponseFunc
type ResponseFunc func(path string) Response
ResponseMap
type ResponseMap map[string]Response
ResponseMap maps request paths to responses.
Restorer
type Restorer func()
Restorer holds a function that can be used to restore some previous state.
PatchEnvPathPrepend
func PatchEnvPathPrepend(dir string) Restorer
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.
PatchEnvironment
func PatchEnvironment(name, value string) Restorer
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.
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.
Restorer.Add
func (f Restorer) Add(f1 Restorer) Restorer
Add returns a Restorer that restores first f1 and then f. It is valid to call this on a nil Restorer.
Restorer.Restore
func (r Restorer) Restore()
Restore restores some previous state.
TestFile
type TestFile struct {
Name, Data string
}