1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
. "launchpad.net/gocheck"
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"
22
func TestAll(t *stdtesting.T) {
23
coretesting.MgoTestPackage(t)
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
30
type watcherSuite struct {
33
server *apiserver.Server
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
40
rawService *state.Service
44
var _ = Suite(&watcherSuite{})
46
func (s *watcherSuite) SetUpTest(c *C) {
47
s.JujuConnSuite.SetUpTest(c)
49
// Create a machine to work with
51
s.rawMachine, err = s.State.AddMachine("series", state.JobHostUnits)
53
err = s.rawMachine.SetPassword("test-password")
56
// Start the testing API server.
57
s.server, err = apiserver.NewServer(
60
[]byte(coretesting.ServerCert),
61
[]byte(coretesting.ServerKey),
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)
70
func (s *watcherSuite) TearDownTest(c *C) {
71
if s.stateAPI != nil {
72
err := s.stateAPI.Close()
76
err := s.server.Stop()
79
s.JujuConnSuite.TearDownTest(c)
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)
90
c.Assert(results.Results, HasLen, 1)
91
result := results.Results[0]
92
c.Assert(result.Error, IsNil)
94
// We expect the Call() to "Next" to block, so run it in a goroutine.
95
done := make(chan error)
98
done <- s.stateAPI.Call("NotifyWatcher", result.NotifyWatcherId, "Next", nil, &ignored)
103
c.Errorf("Call(Next) did not block immediately after Watch(): err %v", err)
104
case <-time.After(shortWait):
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)
113
c.Assert(results.Results, HasLen, 1)
114
result := results.Results[0]
115
c.Assert(result.Error, IsNil)
117
// params.NotifyWatcher conforms to the state.NotifyWatcher interface
118
w := watcher.NewNotifyWatcher(s.stateAPI, result)
119
wc := statetesting.NewNotifyWatcherC(c, s.State, w)
121
statetesting.AssertStop(c, w)