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

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/state/api/watcher/watcher_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
 
 
4
package watcher_test
 
5
 
 
6
import (
 
7
        stdtesting "testing"
 
8
        "time"
 
9
 
 
10
        . "launchpad.net/gocheck"
 
11
 
 
12
        "launchpad.net/juju-core/juju/testing"
 
13
        "launchpad.net/juju-core/state"
 
14
        "launchpad.net/juju-core/state/api"
 
15
        "launchpad.net/juju-core/state/api/params"
 
16
        "launchpad.net/juju-core/state/api/watcher"
 
17
        "launchpad.net/juju-core/state/apiserver"
 
18
        statetesting "launchpad.net/juju-core/state/testing"
 
19
        coretesting "launchpad.net/juju-core/testing"
 
20
)
 
21
 
 
22
func TestAll(t *stdtesting.T) {
 
23
        coretesting.MgoTestPackage(t)
 
24
}
 
25
 
 
26
// shortWait is a reasonable amount of time to be sure a call is in a blocking
 
27
// state (won't return without other prompting)
 
28
const shortWait = 50 * time.Millisecond
 
29
 
 
30
type watcherSuite struct {
 
31
        testing.JujuConnSuite
 
32
 
 
33
        server   *apiserver.Server
 
34
        stateAPI *api.State
 
35
 
 
36
        // These are raw State objects. Use them for setup and assertions, but
 
37
        // should never be touched by the API calls themselves
 
38
        rawMachine *state.Machine
 
39
        rawCharm   *state.Charm
 
40
        rawService *state.Service
 
41
        rawUnit    *state.Unit
 
42
}
 
43
 
 
44
var _ = Suite(&watcherSuite{})
 
45
 
 
46
func (s *watcherSuite) SetUpTest(c *C) {
 
47
        s.JujuConnSuite.SetUpTest(c)
 
48
 
 
49
        // Create a machine to work with
 
50
        var err error
 
51
        s.rawMachine, err = s.State.AddMachine("series", state.JobHostUnits)
 
52
        c.Assert(err, IsNil)
 
53
        err = s.rawMachine.SetPassword("test-password")
 
54
        c.Assert(err, IsNil)
 
55
 
 
56
        // Start the testing API server.
 
57
        s.server, err = apiserver.NewServer(
 
58
                s.State,
 
59
                "localhost:12345",
 
60
                []byte(coretesting.ServerCert),
 
61
                []byte(coretesting.ServerKey),
 
62
        )
 
63
        c.Assert(err, IsNil)
 
64
 
 
65
        // Login as the machine agent of the created machine.
 
66
        s.stateAPI = s.OpenAPIAs(c, s.rawMachine.Tag(), "test-password")
 
67
        c.Assert(s.stateAPI, NotNil)
 
68
}
 
69
 
 
70
func (s *watcherSuite) TearDownTest(c *C) {
 
71
        if s.stateAPI != nil {
 
72
                err := s.stateAPI.Close()
 
73
                c.Check(err, IsNil)
 
74
        }
 
75
        if s.server != nil {
 
76
                err := s.server.Stop()
 
77
                c.Check(err, IsNil)
 
78
        }
 
79
        s.JujuConnSuite.TearDownTest(c)
 
80
}
 
81
 
 
82
func (s *watcherSuite) TestWatchInitialEventConsumed(c *C) {
 
83
        // Machiner.Watch should send the initial event as part of the Watch
 
84
        // call (for NotifyWatchers there is no state to be transmitted). So a
 
85
        // call to Next() should not have anything to return.
 
86
        var results params.NotifyWatchResults
 
87
        args := params.Entities{Entities: []params.Entity{{Tag: s.rawMachine.Tag()}}}
 
88
        err := s.stateAPI.Call("Machiner", "", "Watch", args, &results)
 
89
        c.Assert(err, IsNil)
 
90
        c.Assert(results.Results, HasLen, 1)
 
91
        result := results.Results[0]
 
92
        c.Assert(result.Error, IsNil)
 
93
 
 
94
        // We expect the Call() to "Next" to block, so run it in a goroutine.
 
95
        done := make(chan error)
 
96
        go func() {
 
97
                ignored := struct{}{}
 
98
                done <- s.stateAPI.Call("NotifyWatcher", result.NotifyWatcherId, "Next", nil, &ignored)
 
99
        }()
 
100
 
 
101
        select {
 
102
        case err := <-done:
 
103
                c.Errorf("Call(Next) did not block immediately after Watch(): err %v", err)
 
104
        case <-time.After(shortWait):
 
105
        }
 
106
}
 
107
 
 
108
func (s *watcherSuite) TestWatchMachine(c *C) {
 
109
        var results params.NotifyWatchResults
 
110
        args := params.Entities{Entities: []params.Entity{{Tag: s.rawMachine.Tag()}}}
 
111
        err := s.stateAPI.Call("Machiner", "", "Watch", args, &results)
 
112
        c.Assert(err, IsNil)
 
113
        c.Assert(results.Results, HasLen, 1)
 
114
        result := results.Results[0]
 
115
        c.Assert(result.Error, IsNil)
 
116
 
 
117
        // params.NotifyWatcher conforms to the state.NotifyWatcher interface
 
118
        w := watcher.NewNotifyWatcher(s.stateAPI, result)
 
119
        wc := statetesting.NewNotifyWatcherC(c, s.State, w)
 
120
        wc.AssertOneChange()
 
121
        statetesting.AssertStop(c, w)
 
122
        wc.AssertClosed()
 
123
}