testing

package
v0.0.0-...-9d6b0cf Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2013 License: AGPL-3.0, AGPL-3.0 Imports: 36 Imported by: 0

Documentation

Index

Constants

View Source
const EnvDefault = "default:\n  " + SampleEnvName + "\n"
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 = `` /* 301-byte string literal not displayed */
View Source
const SampleCertName = "erewhemos"
View Source
const SampleEnvName = "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 = `` /* 157-byte string literal not displayed */

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

Variables

View Source
var (
	CACert, CAKey = mustNewCA()

	CACertX509, CAKeyRSA = mustParseCertAndKey([]byte(CACert), []byte(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 (
	// MgoAddr holds the address of the shared MongoDB server set up by
	// MgoTestPackage.
	MgoAddr string
)
View Source
var Server = NewHTTPServer(5 * time.Second)

Functions

func Context

func Context(c *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 *C, dir string) *cmd.Context

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

func EnvironConfig

func EnvironConfig(c *C) *config.Config

EnvironConfig returns a default environment configuration suitable for testing.

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 "launchpad.net/juju-core/" 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

func HomePath(names ...string) string

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 MatchInstances

func MatchInstances(c *C, result []instance.Instance, expected ...instance.Instance)

MatchInstances uses DeepEquals to check the instances returned. The lists are first put into a map, so the ordering of the result and expected values is not tested, and duplicates are ignored.

func MgoDial

func MgoDial() *mgo.Session

MgoDial returns a new connection to the shared MongoDB server.

func MgoReset

func MgoReset()

MgoReset deletes all content from the shared MongoDB server.

func MgoTestPackage

func MgoTestPackage(t *stdtesting.T)

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

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 PatchEnvironment

func PatchEnvironment(name, value string) func()

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 RunCommand

func RunCommand(c *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 *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

TarGz returns the given files in gzipped tar-archive format.

func TestInit

func TestInit(c *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.

Types

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 FakeHome

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

func MakeEmptyFakeHome

func MakeEmptyFakeHome(c *C) *FakeHome

func MakeEmptyFakeHomeWithoutJuju

func MakeEmptyFakeHomeWithoutJuju(c *C) *FakeHome

func MakeFakeHome

func MakeFakeHome(c *C, envConfig string, certNames ...string) *FakeHome

MakeFakeHome creates a new temporary directory through the test checker, and overrides the HOME environment variable to point to this new temporary directory.

A new ~/.juju/environments.yaml file is created with the content of the `envConfig` parameter, and CAKeys are written for each of the 'certNames' specified.

func MakeFakeHomeNoEnvironments

func MakeFakeHomeNoEnvironments(c *C, certNames ...string) *FakeHome

MakeFakeHomeNoEnvironments creates a new temporary directory through the test checker, and overrides the HOME environment variable to point to this new temporary directory.

No ~/.juju/environments.yaml exists, but CAKeys are written for each of the 'certNames' specified, and the id_rsa.pub file is written to to the .ssh dir.

func MakeFakeHomeWithFiles

func MakeFakeHomeWithFiles(c *C, files []TestFile) *FakeHome

func MakeMultipleEnvHome

func MakeMultipleEnvHome(c *C) *FakeHome

func MakeSampleHome

func MakeSampleHome(c *C) *FakeHome

func (*FakeHome) AddFiles

func (h *FakeHome) AddFiles(c *C, files []TestFile)

func (*FakeHome) FileContents

func (h *FakeHome) FileContents(c *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).

func (*FakeHome) FileExists

func (h *FakeHome) FileExists(path string) bool

FileExists returns if the given relative file path exists in the fake home.

func (*FakeHome) Restore

func (h *FakeHome) Restore()

type GitSuite

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

func (*GitSuite) SetUpTest

func (t *GitSuite) SetUpTest(c *C)

func (*GitSuite) TearDownTest

func (t *GitSuite) TearDownTest(c *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 *C)

func (*HTTPSuite) SetUpTest

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

func (*HTTPSuite) TearDownSuite

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

func (*HTTPSuite) TearDownTest

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

func (*HTTPSuite) URL

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

type LoggingSuite

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

LoggingSuite redirects the juju logger to the test logger when embedded in a gocheck suite type.

func (*LoggingSuite) SetUpSuite

func (t *LoggingSuite) SetUpSuite(c *C)

func (*LoggingSuite) SetUpTest

func (t *LoggingSuite) SetUpTest(c *C)

func (*LoggingSuite) TearDownSuite

func (t *LoggingSuite) TearDownSuite(c *C)

func (*LoggingSuite) TearDownTest

func (t *LoggingSuite) TearDownTest(c *C)

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 *C)

func (*MgoSuite) SetUpTest

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

func (*MgoSuite) TearDownSuite

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

func (*MgoSuite) TearDownTest

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

type MockCharmStore

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

MockCharmStore implements charm.Respository and is used to isolate tests that would otherwise need to hit the real charm store.

func NewMockCharmStore

func NewMockCharmStore() *MockCharmStore

func (*MockCharmStore) Get

func (s *MockCharmStore) Get(charmURL *charm.URL) (charm.Charm, error)

Get implements charm.Repository.Get.

func (*MockCharmStore) Latest

func (s *MockCharmStore) Latest(charmURL *charm.URL) (int, error)

Latest implements charm.Repository.Latest.

func (*MockCharmStore) SetCharm

func (s *MockCharmStore) SetCharm(charmURL *charm.URL, bundle *charm.Bundle) error

SetCharm adds and removes charms in s. The affected charm is identified by charmURL, which must be revisioned. If bundle is nil, the charm will be removed; otherwise, it will be stored. It is an error to store a bundle under a charmURL that does not share its name and revision.

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 Repo

type Repo struct {
	Path string
}

Repo represents a charm repository used for testing.

var Charms *Repo

Charms represents the specific charm repository stored in this package and used by the Juju unit tests. The series name is "series".

func (*Repo) Bundle

func (r *Repo) Bundle(dst, name string) *charm.Bundle

Bundle returns an actual charm.Bundle created from a new charm bundle file created from the charm directory named name, in the directory dst.

func (*Repo) BundlePath

func (r *Repo) BundlePath(dst, name string) string

BundlePath returns the path to a new charm bundle file created from the charm directory named name, in the directory dst.

func (*Repo) ClonedDir

func (r *Repo) ClonedDir(dst, name string) *charm.Dir

ClonedDir returns an actual charm.Dir based on a new copy of the charm directory named name, in the directory dst.

func (*Repo) ClonedDirPath

func (r *Repo) ClonedDirPath(dst, name string) string

ClonedDirPath returns the path to a new copy of the default charm directory named name.

func (*Repo) ClonedURL

func (r *Repo) ClonedURL(dst, series, name string) *charm.URL

ClonedURL makes a copy of the charm directory. It will create a directory with the series name if it does not exist, and then clone the charm named name into that directory. The return value is a URL pointing at the local charm.

func (*Repo) Dir

func (r *Repo) Dir(name string) *charm.Dir

Dir returns the actual charm.Dir named name.

func (*Repo) DirPath

func (r *Repo) DirPath(name string) string

DirPath returns the path to a charm directory with the given name in the default series

func (*Repo) RenamedClonedDirPath

func (r *Repo) RenamedClonedDirPath(dst, name, newName string) string

RenamedClonedDirPath returns the path to a new copy of the default charm directory named name, but renames it to newName.

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.

type TestFile

type TestFile struct {
	Name, Data string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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