~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/state/export_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012, 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
1
4
package state
2
5
 
3
6
import (
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"
11
15
        "net/url"
12
16
        "path/filepath"
13
17
)
14
18
 
15
 
// TestingEnvironConfig returns a default environment configuration.
16
 
func TestingEnvironConfig(c *C) *config.Config {
17
 
        cfg, err := config.New(map[string]interface{}{
18
 
                "type":            "test",
19
 
                "name":            "test-name",
20
 
                "default-series":  "test-series",
21
 
                "authorized-keys": "test-keys",
22
 
                "agent-version":   "9.9.9.9",
23
 
                "ca-cert":         testing.CACert,
24
 
                "ca-private-key":  "",
 
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
 
22
 
 
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()
 
27
 
 
28
func (c TransactionChecker) Check() {
 
29
        c()
 
30
}
 
31
 
 
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-
 
39
// unsafe.
 
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])
 
45
        }
 
46
        original := <-st.transactionHooks
 
47
        st.transactionHooks <- converted
 
48
        c.Assert(original, HasLen, 0)
 
49
        return func() {
 
50
                remaining := <-st.transactionHooks
 
51
                st.transactionHooks <- nil
 
52
                c.Assert(remaining, HasLen, 0)
 
53
        }
 
54
}
 
55
 
 
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}
 
64
        }
 
65
        return SetTransactionHooks(c, st, transactionHooks...)
 
66
}
 
67
 
 
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{
 
74
                Before: block,
 
75
        }, TransactionHook{
 
76
                After: check,
25
77
        })
26
 
        c.Assert(err, IsNil)
27
 
        return cfg
28
78
}
29
79
 
30
80
// TestingInitialize initializes the state and returns it. If state was not
32
82
// configuration will be used.
33
83
func TestingInitialize(c *C, cfg *config.Config) *State {
34
84
        if cfg == nil {
35
 
                cfg = TestingEnvironConfig(c)
 
85
                cfg = testing.EnvironConfig(c)
36
86
        }
37
87
        st, err := Initialize(TestingStateInfo(), cfg, TestingDialOpts())
38
88
        c.Assert(err, IsNil)
90
140
        return sch
91
141
}
92
142
 
 
143
func MachineIdLessThan(id1, id2 string) bool {
 
144
        return machineIdLessThan(id1, id2)
 
145
}
 
146
 
 
147
// SCHEMACHANGE
 
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)
 
151
}
 
152
 
93
153
func init() {
94
154
        logSize = logSizeTests
95
155
}
 
156
 
 
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) {
 
160
        var doc minUnitsDoc
 
161
        if err := st.minUnits.FindId(serviceName).One(&doc); err != nil {
 
162
                return 0, err
 
163
        }
 
164
        return doc.Revno, nil
 
165
}