7
10
. "launchpad.net/gocheck"
8
11
"launchpad.net/juju-core/charm"
9
12
"launchpad.net/juju-core/environs/config"
13
"launchpad.net/juju-core/instance"
10
14
"launchpad.net/juju-core/testing"
15
// TestingEnvironConfig returns a default environment configuration.
16
func TestingEnvironConfig(c *C) *config.Config {
17
cfg, err := config.New(map[string]interface{}{
20
"default-series": "test-series",
21
"authorized-keys": "test-keys",
22
"agent-version": "9.9.9.9",
23
"ca-cert": testing.CACert,
19
// transactionHook holds Before and After func()s that will be called
20
// respectively before and after a particular state transaction is executed.
21
type TransactionHook transactionHook
23
// TransactionChecker values are returned from the various Set*Hooks calls,
24
// and should be run after the code under test has been executed to check
25
// that the expected number of transactions were run.
26
type TransactionChecker func()
28
func (c TransactionChecker) Check() {
32
// SetTransactionHooks queues up hooks to be applied to the next transactions,
33
// and returns a function that asserts all hooks have been run (and removes any
34
// that have not). Each hook function can freely execute its own transactions
35
// without causing other hooks to be triggered.
36
// It returns a function that asserts that all hooks have been run, and removes
37
// any that have not. It is an error to set transaction hooks when any are
38
// already queued; and setting transaction hooks renders the *State goroutine-
40
func SetTransactionHooks(c *C, st *State, transactionHooks ...TransactionHook) TransactionChecker {
41
converted := make([]transactionHook, len(transactionHooks))
42
for i, hook := range transactionHooks {
43
converted[i] = transactionHook(hook)
44
c.Logf("%d: %#v", i, converted[i])
46
original := <-st.transactionHooks
47
st.transactionHooks <- converted
48
c.Assert(original, HasLen, 0)
50
remaining := <-st.transactionHooks
51
st.transactionHooks <- nil
52
c.Assert(remaining, HasLen, 0)
56
// SetBeforeHooks uses SetTransactionHooks to queue N functions to be run
57
// immediately before the next N transactions. Nil values are accepted, and
58
// useful, in that they can be used to ensure that a transaction is run at
59
// the expected time, without having to make any changes or assert any state.
60
func SetBeforeHooks(c *C, st *State, fs ...func()) TransactionChecker {
61
transactionHooks := make([]TransactionHook, len(fs))
62
for i, f := range fs {
63
transactionHooks[i] = TransactionHook{Before: f}
65
return SetTransactionHooks(c, st, transactionHooks...)
68
// SetRetryHooks uses SetTransactionHooks to inject a block function designed
69
// to disrupt a transaction built against recent state, and a check function
70
// designed to verify that the replacement transaction against the new state
71
// has been applied as expected.
72
func SetRetryHooks(c *C, st *State, block, check func()) TransactionChecker {
73
return SetTransactionHooks(c, st, TransactionHook{
30
80
// TestingInitialize initializes the state and returns it. If state was not
143
func MachineIdLessThan(id1, id2 string) bool {
144
return machineIdLessThan(id1, id2)
148
// This method is used to reset a deprecated machine attriute.
149
func SetMachineInstanceId(m *Machine, instanceId string) {
150
m.doc.InstanceId = instance.Id(instanceId)
94
154
logSize = logSizeTests
157
// MinUnitsRevno returns the Revno of the minUnits document
158
// associated with the given service name.
159
func MinUnitsRevno(st *State, serviceName string) (int, error) {
161
if err := st.minUnits.FindId(serviceName).One(&doc); err != nil {
164
return doc.Revno, nil