testing

package
v0.0.0-...-732aecd Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2014 License: AGPL-3.0 Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SampleEnvName = "erewhemos"
	EnvDefault    = "default:\n  " + SampleEnvName + "\n"
)
View Source
const DefaultMongoPassword = "conn-from-name-secret"
View Source
const FakeAuthKeys = `` /* 180-byte string literal not displayed */

FakeAuthKeys holds the authorized key used for testing purposes in FakeConfig. It is valid for parsing with the utils/ssh authorized-key utilities.

View Source
const FakeDefaultSeries = "precise"
View Source
const LongWait = 10 * time.Second

LongWait is used when something should have already happened, or happens quickly, but we want to make sure we just haven't missed it. As in, the test suite should proceed without sleeping at all, but just in case. It is long so that we don't have spurious failures without actually slowing down the test suite

View Source
const MultipleEnvConfig = EnvDefault + MultipleEnvConfigNoDefault
View Source
const MultipleEnvConfigNoDefault = `
environments:
    erewhemos:
        type: dummy
        state-server: true
        authorized-keys: i-am-a-key
        admin-secret: ` + DefaultMongoPassword + `
    erewhemos-2:
        type: dummy
        state-server: true
        authorized-keys: i-am-a-key
        admin-secret: ` + DefaultMongoPassword + `
`
View Source
const SampleCertName = "erewhemos"
View Source
const ShortWait = 50 * time.Millisecond

ShortWait is a reasonable amount of time to block waiting for something that shouldn't actually happen. (as in, the test suite will *actually* wait this long before continuing)

View Source
const SingleEnvConfig = EnvDefault + SingleEnvConfigNoDefault
View Source
const SingleEnvConfigNoDefault = `
environments:
    erewhemos:
        type: dummy
        state-server: true
        authorized-keys: i-am-a-key
        admin-secret: ` + DefaultMongoPassword + `
`

Environment names below are explicit as it makes them more readable.

Variables

View Source
var (
	CACert, CAKey = mustNewCA()

	CACertX509, CAKeyRSA = mustParseCertAndKey(CACert, CAKey)

	ServerCert, ServerKey = mustNewServer()
)

CACert and CAKey make up a CA key pair. CACertX509 and CAKeyRSA hold their parsed equivalents. ServerCert and ServerKey hold a CA-signed server cert/key.

View Source
var LongAttempt = &utils.AttemptStrategy{
	Total: LongWait,
	Delay: ShortWait,
}
View Source
var (
	// MgoServer is a shared mongo server used by tests.
	MgoServer = &MgoInstance{ssl: true}
)
View Source
var Server = NewHTTPServer(5 * time.Second)

Functions

func Context

func Context(c *gc.C) *cmd.Context

Context creates a simple command execution context with the current dir set to a newly created directory within the test directory.

func ContextForDir

func ContextForDir(c *gc.C, dir string) *cmd.Context

ContextForDir creates a simple command execution context with the current dir set to the specified directory.

func CustomEnvironConfig

func CustomEnvironConfig(c *gc.C, extra Attrs) *config.Config

CustomEnvironConfig returns an environment configuration with additional specified keys added.

func EnvironConfig

func EnvironConfig(c *gc.C) *config.Config

EnvironConfig returns a default environment configuration suitable for setting in the state.

func FindJujuCoreImports

func FindJujuCoreImports(c *gc.C, packageName string) []string

FindJujuCoreImports returns a sorted list of juju-core packages that are imported by the packageName parameter. The resulting list removes the common prefix "github.com/juju/juju/" 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 InitCommand

func InitCommand(c cmd.Command, args []string) error

InitCommand will create a new flag set, and call the Command's SetFlags and Init methods with the appropriate args.

func MakeSampleJujuHome

func MakeSampleJujuHome(c *gc.C)

MakeSampleJujuHome sets up a sample Juju environment.

func MgoDialInfo

func MgoDialInfo(addrs ...string) *mgo.DialInfo

MgoDialInfo returns a DialInfo suitable for dialling an MgoInstance at any of the given addresses.

func MgoDialInfoTls

func MgoDialInfoTls(useTls bool, addrs ...string) *mgo.DialInfo

MgoDialInfoTls returns a DialInfo suitable for dialling an MgoInstance at any of the given addresses, optionally using TLS.

func MgoTestPackage

func MgoTestPackage(t *stdtesting.T)

MgoTestPackage should be called to register the tests for any package that requires a MongoDB server.

func MgoTestPackageSsl

func MgoTestPackageSsl(t *stdtesting.T, ssl bool)

func NewFlagSet

func NewFlagSet() *gnuflag.FlagSet

NewFlagSet creates a new flag set using the standard options, particularly the option to stop the gnuflag methods from writing to StdErr or StdOut.

func RunCommand

func RunCommand(c *gc.C, com cmd.Command, args ...string) (*cmd.Context, error)

RunCommand runs a command with the specified args. The returned error may come from either the parsing of the args, the command initialisation, or the actual running of the command. Access to the resulting output streams is provided through the returned context instance.

func RunCommandInDir

func RunCommandInDir(c *gc.C, com cmd.Command, args []string, dir string) (*cmd.Context, error)

RunCommandInDir works like RunCommand, but runs with a context that uses dir.

func Stderr

func Stderr(ctx *cmd.Context) string

Stderr takes a command Context that we assume has been created in this package, and gets the content of the Stderr buffer as a string.

func Stdout

func Stdout(ctx *cmd.Context) string

Stdout takes a command Context that we assume has been created in this package, and gets the content of the Stdout buffer as a string.

func TarGz

func TarGz(files ...*TarFile) ([]byte, string)

TarGz returns the given files in gzipped tar-archive format, along with the sha256 checksum.

func TestInit

func TestInit(c *gc.C, com cmd.Command, args []string, errPat string)

TestInit checks that a command initialises correctly with the given set of arguments.

func TestLockingFunction

func TestLockingFunction(lock *sync.Mutex, function func())

TestLockingFunction verifies that a function obeys a given lock.

Use this as a building block in your own tests for proper locking. Parameters are a lock that you expect your function to block on, and the function that you want to test for proper locking on that lock.

This helper attempts to verify that the function both obtains and releases the lock. It will panic if the function fails to do either. TODO: Support generic sync.Locker instead of just Mutex. TODO: This could be a gocheck checker. TODO(rog): make this work reliably even for functions that take longer than a few µs to execute.

func WriteEnvironments

func WriteEnvironments(c *gc.C, envConfig string, certNames ...string)

WriteEnvironments creates an environments file with envConfig and certs from certNames.

Types

type Attrs

type Attrs map[string]interface{}

Attrs is a convenience type for messing around with configuration attributes.

func FakeConfig

func FakeConfig() Attrs

FakeConfig() returns an environment configuration for a fake provider with all required attributes set.

func (Attrs) Delete

func (a Attrs) Delete(attrNames ...string) Attrs

func (Attrs) Merge

func (a Attrs) Merge(with Attrs) Attrs

type BaseSuite

BaseSuite provides required functionality for all test suites when embedded in a gocheck suite type: - logger redirect - no outgoing network access - protection of user's home directory - scrubbing of env vars TODO (frankban) 2014-06-09: switch to using IsolationSuite.

func (*BaseSuite) SetUpSuite

func (s *BaseSuite) SetUpSuite(c *gc.C)

func (*BaseSuite) SetUpTest

func (s *BaseSuite) SetUpTest(c *gc.C)

func (*BaseSuite) TearDownSuite

func (s *BaseSuite) TearDownSuite(c *gc.C)

func (*BaseSuite) TearDownTest

func (s *BaseSuite) TearDownTest(c *gc.C)

type ContentAsserterC

type ContentAsserterC struct {
	// C is a gocheck C structure for doing assertions
	C *gc.C
	// Chan is the channel we want to receive on
	Chan interface{}
	// Precond will be called before waiting on the channel, can be nil
	Precond func()
}

ContentAsserterC is like NotifyAsserterC in that it checks the behavior of a channel. The difference is that we expect actual content on the channel, so callers need to put that into and out of an 'interface{}'

func (*ContentAsserterC) AssertClosed

func (a *ContentAsserterC) AssertClosed()

AssertClosed ensures that we get a closed event on the channel

func (*ContentAsserterC) AssertNoReceive

func (a *ContentAsserterC) AssertNoReceive()

Assert that we fail to receive on the channel after a short wait.

func (*ContentAsserterC) AssertOneReceive

func (a *ContentAsserterC) AssertOneReceive() interface{}

AssertOneReceive checks that we have exactly one message, and no more

func (*ContentAsserterC) AssertReceive

func (a *ContentAsserterC) AssertReceive() interface{}

AssertReceive will ensure that we get an event on the channel and the channel is not closed. It will return the content received

type FakeJujuHomeSuite

type FakeJujuHomeSuite struct {
	JujuOSEnvSuite
	gitjujutesting.FakeHomeSuite
	// contains filtered or unexported fields
}

FakeJujuHomeSuite isolates the user's home directory and sets up a Juju home with a sample environment and certificate.

func (*FakeJujuHomeSuite) SetUpSuite

func (s *FakeJujuHomeSuite) SetUpSuite(c *gc.C)

func (*FakeJujuHomeSuite) SetUpTest

func (s *FakeJujuHomeSuite) SetUpTest(c *gc.C)

func (*FakeJujuHomeSuite) TearDownSuite

func (s *FakeJujuHomeSuite) TearDownSuite(c *gc.C)

func (*FakeJujuHomeSuite) TearDownTest

func (s *FakeJujuHomeSuite) TearDownTest(c *gc.C)

type GitSuite

type GitSuite struct {
	BaseSuite
}

func (*GitSuite) SetUpTest

func (t *GitSuite) SetUpTest(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

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.

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 (s *HTTPSuite) SetUpSuite(c *gc.C)

func (*HTTPSuite) SetUpTest

func (s *HTTPSuite) SetUpTest(c *gc.C)

func (*HTTPSuite) TearDownSuite

func (s *HTTPSuite) TearDownSuite(c *gc.C)

func (*HTTPSuite) TearDownTest

func (s *HTTPSuite) TearDownTest(c *gc.C)

func (*HTTPSuite) URL

func (s *HTTPSuite) URL(path string) string

type JujuOSEnvSuite

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

JujuOSEnvSuite isolates the tests from Juju environment variables. This is intended to be only used by existing suites, usually embedded in BaseSuite and in FakeJujuHomeSuite. Eventually the tests relying on JujuOSEnvSuite will be converted to use the IsolationSuite in github.com/juju/testing, and this suite will be removed. Do not use JujuOSEnvSuite when writing new tests.

func (*JujuOSEnvSuite) SetUpSuite

func (s *JujuOSEnvSuite) SetUpSuite(c *gc.C)

func (*JujuOSEnvSuite) SetUpTest

func (s *JujuOSEnvSuite) SetUpTest(c *gc.C)

func (*JujuOSEnvSuite) TearDownSuite

func (s *JujuOSEnvSuite) TearDownSuite(c *gc.C)

func (*JujuOSEnvSuite) TearDownTest

func (s *JujuOSEnvSuite) 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

	// 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(ssl bool) 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 (s *MgoSuite) SetUpSuite(c *gc.C)

func (*MgoSuite) SetUpTest

func (s *MgoSuite) SetUpTest(c *gc.C)

func (*MgoSuite) TearDownSuite

func (s *MgoSuite) TearDownSuite(c *gc.C)

func (*MgoSuite) TearDownTest

func (s *MgoSuite) TearDownTest(c *gc.C)

type NotifyAsserterC

type NotifyAsserterC struct {
	// C is a gocheck C structure for doing assertions
	C *gc.C
	// Chan is the channel we want to receive on
	Chan <-chan struct{}
	// Precond will be called before waiting on the channel, can be nil
	Precond func()
}

NotifyAsserterC gives helper functions for making assertions about how a channel operates (whether we get a receive event or not, whether it is closed, etc.)

func (*NotifyAsserterC) AssertClosed

func (a *NotifyAsserterC) AssertClosed()

AssertClosed ensures that we get a closed event on the channel

func (*NotifyAsserterC) AssertNoReceive

func (a *NotifyAsserterC) AssertNoReceive()

Assert that we fail to receive on the channel after a short wait.

func (*NotifyAsserterC) AssertOneReceive

func (a *NotifyAsserterC) AssertOneReceive()

AssertOneReceive checks that we have exactly one message, and no more

func (*NotifyAsserterC) AssertReceive

func (a *NotifyAsserterC) AssertReceive()

AssertReceive will ensure that we get an event on the channel and the channel is not closed.

type Response

type Response struct {
	Status  int
	Headers map[string]string
	Body    []byte
}

type ResponseFunc

type ResponseFunc func(path string) Response

type ResponseMap

type ResponseMap map[string]Response

ResponseMap maps request paths to responses.

type TarFile

type TarFile struct {
	Header   tar.Header
	Contents string
}

TarFile represents a file to be archived.

func NewTarFile

func NewTarFile(name string, mode os.FileMode, contents string) *TarFile

NewTarFile returns a new TarFile instance with the given file mode and contents.

Jump to

Keyboard shortcuts

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