gocharm: github.com/juju/gocharm/hook/hooktest Index | Files

package hooktest

import "github.com/juju/gocharm/hook/hooktest"

Package hooktest contains utilities for testing gocharm hooks.


Package Files

hooktest.go service.go serviceeventkind_string.go


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

UUID holds an arbitrary environment UUID for testing purposes.

func NewServiceFunc Uses

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.

type MemState Uses

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 Uses

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

func (MemState) Save Uses

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

type Runner Uses

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 Uses

func (r *Runner) Close() error

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

func (*Runner) Run Uses

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

Run implements hook.Runner.Run.

func (*Runner) RunCommand Uses

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 Uses

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 Uses

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 Uses

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.

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

    // ServiceEventStop happens when a service is stopped.

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

func (ServiceEventKind) String Uses

func (i ServiceEventKind) String() string

Package hooktest imports 7 packages (graph). Updated 2016-07-28. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).