hooktest

package
v0.0.0-...-c60bc85 Latest Latest
Warning

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

Go to latest
Published: May 19, 2017 License: LGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package hooktest contains utilities for testing gocharm hooks.

Index

Constants

View Source
const UUID = "373b309b-4a86-4f13-88e2-c213d97075b8"

UUID holds an arbitrary environment UUID for testing purposes.

Variables

This section is empty.

Functions

func NewServiceFunc

func NewServiceFunc(r *Runner, notify chan ServiceEvent) func(service.OSServiceParams) service.OSService

NewServiceFunc returns a function that can be used to create new services and can be assigned to service.NewService to mock the OS-level service creator. When the service is started, the given Runner will be used to run it (via hook.Main).

If the notify channel is not nil, it will be used to send events about services created with the function. It should be buffered with a size of at least 2.

Types

type MemState

type MemState map[string][]byte

MemState implements hook.PersistentState in memory. Each element of the map holds the value key stored in the state.

func (MemState) Load

func (s MemState) Load(name string) ([]byte, error)

func (MemState) Save

func (s MemState) Save(name string, data []byte) error

type Runner

type Runner struct {
	RegisterHooks func(r *hook.Registry)

	// The following fields hold information that will
	// be available through the hook context.
	Relations   map[hook.RelationId]map[hook.UnitId]map[string]string
	RelationIds map[string][]hook.RelationId
	Config      map[string]interface{}

	PublicAddress  string
	PrivateAddress string

	// HookStateDir holds the directory in which state
	// other than hook state will be stored (for instance,
	// this is used by the service package to store service
	// logs). If this is empty, RunHook will panic.
	HookStateDir string

	// State holds the persistent state.
	// If it is nil, it will be set to a hooktest.MemState
	// instance.
	State hook.PersistentState

	// RunFunc is called when a hook tool runs.
	// It may be nil, in which case it will be assumed
	// that the hook tool runs successfully with no output.
	RunFunc func(string, ...string) ([]byte, error)
	Record  [][]string

	// Logger should be set to a logger. The Logf method
	// will be called when the charm generates log messages.
	Logger interface {
		Logf(string, ...interface{})
	}

	// Close records whether the Close method has been called.
	Closed bool
}

Runner is implemention of hook.Runner suitable for use in tests. It calls the given RunFunc function whenever the Run method and records all the calls in the Record field, with the exception of the calls mentioned below.

Any calls to juju-log are logged using Logger, but otherwise ignored. Calls to config-get from the Config field and not invoked through RunFunc. Likewise, calls to unit-get will be satisfied from the PublicAddress and PrivateAddress fields.

func (*Runner) Close

func (r *Runner) Close() error

Run implements hook.Runner.Close. It panics if called more than once.

func (*Runner) Run

func (r *Runner) Run(cmd string, args ...string) ([]byte, error)

Run implements hook.Runner.Run.

func (*Runner) RunCommand

func (runner *Runner) RunCommand(cmdName string, args []string) (hook.Command, error)

RunCommand runs the given command in the context of the Runner. The cmdName should be a name returned by hook.Context.CommandName.

func (*Runner) RunHook

func (runner *Runner) RunHook(hookName string, relId hook.RelationId, relUnit hook.UnitId) error

RunHook runs a hook in the context of the Runner. If it's a relation hook, then relId should hold the current relation id and relUnit should hold the unit that the relation hook is running for.

Any hook tools that have been run will be stored in r.Record.

type ServiceEvent

type ServiceEvent struct {
	// Kind holds the kind of the service event.
	Kind ServiceEventKind

	// Params holds the parameters used to create the service,
	Params service.OSServiceParams

	// Error holds an error returned from the service's
	// command. It is valid only for ServiceEventError
	// events.
	Error error
}

ServiceEvent represents an event on a service created by the function returned from NewServiceFunc.

type ServiceEventKind

type ServiceEventKind int

ServiceEventKind represents the kind of a ServiceEvent.

const (

	// ServiceEventInstall happens when a service is installed.
	ServiceEventInstall ServiceEventKind

	// ServiceEventStart happens when a service is started.
	ServiceEventStart

	// ServiceEventError happens when a service's command
	// terminates with an error.
	ServiceEventError

	// ServiceEventStop happens when a service is stopped.
	ServiceEventStop

	// ServiceEventRemove happens when a service is removed.
	// The service will always be stopped first.
	ServiceEventRemove
)

func (ServiceEventKind) String

func (i ServiceEventKind) String() string

Jump to

Keyboard shortcuts

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