1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
11
"gopkg.in/juju/names.v2"
13
"github.com/juju/juju/apiserver/common"
14
"github.com/juju/juju/apiserver/params"
15
apiservertesting "github.com/juju/juju/apiserver/testing"
16
"github.com/juju/juju/state"
17
statetesting "github.com/juju/juju/state/testing"
20
type agentEntityWatcherSuite struct{}
22
var _ = gc.Suite(&agentEntityWatcherSuite{})
24
type fakeAgentEntityWatcher struct {
29
func (a *fakeAgentEntityWatcher) Watch() state.NotifyWatcher {
30
return apiservertesting.NewFakeNotifyWatcher()
33
func (*agentEntityWatcherSuite) TestWatch(c *gc.C) {
35
entities: map[names.Tag]entityWithError{
36
u("x/0"): &fakeAgentEntityWatcher{fetchError: "x0 fails"},
37
u("x/1"): &fakeAgentEntityWatcher{},
38
u("x/2"): &fakeAgentEntityWatcher{},
41
getCanWatch := func() (common.AuthFunc, error) {
44
return func(tag names.Tag) bool {
45
return tag == x0 || tag == x1
48
resources := common.NewResources()
49
a := common.NewAgentEntityWatcher(st, resources, getCanWatch)
50
entities := params.Entities{[]params.Entity{
51
{"unit-x-0"}, {"unit-x-1"}, {"unit-x-2"}, {"unit-x-3"},
53
result, err := a.Watch(entities)
54
c.Assert(err, jc.ErrorIsNil)
55
c.Assert(result, gc.DeepEquals, params.NotifyWatchResults{
56
Results: []params.NotifyWatchResult{
57
{Error: ¶ms.Error{Message: "x0 fails"}},
59
{Error: apiservertesting.ErrUnauthorized},
60
{Error: apiservertesting.ErrUnauthorized},
65
func (*agentEntityWatcherSuite) TestWatchError(c *gc.C) {
66
getCanWatch := func() (common.AuthFunc, error) {
67
return nil, fmt.Errorf("pow")
69
resources := common.NewResources()
70
a := common.NewAgentEntityWatcher(
75
_, err := a.Watch(params.Entities{[]params.Entity{{"x0"}}})
76
c.Assert(err, gc.ErrorMatches, "pow")
79
func (*agentEntityWatcherSuite) TestWatchNoArgsNoError(c *gc.C) {
80
getCanWatch := func() (common.AuthFunc, error) {
81
return nil, fmt.Errorf("pow")
83
resources := common.NewResources()
84
a := common.NewAgentEntityWatcher(
89
result, err := a.Watch(params.Entities{})
90
c.Assert(err, jc.ErrorIsNil)
91
c.Assert(result.Results, gc.HasLen, 0)
94
type multiNotifyWatcherSuite struct{}
96
var _ = gc.Suite(&multiNotifyWatcherSuite{})
98
func (*multiNotifyWatcherSuite) TestMultiNotifyWatcher(c *gc.C) {
99
w0 := apiservertesting.NewFakeNotifyWatcher()
100
w1 := apiservertesting.NewFakeNotifyWatcher()
102
mw := common.NewMultiNotifyWatcher(w0, w1)
103
defer statetesting.AssertStop(c, mw)
105
wc := statetesting.NewNotifyWatcherC(c, nopSyncStarter{}, mw)
118
func (*multiNotifyWatcherSuite) TestMultiNotifyWatcherStop(c *gc.C) {
119
w0 := apiservertesting.NewFakeNotifyWatcher()
120
w1 := apiservertesting.NewFakeNotifyWatcher()
122
mw := common.NewMultiNotifyWatcher(w0, w1)
123
wc := statetesting.NewNotifyWatcherC(c, nopSyncStarter{}, mw)
125
statetesting.AssertCanStopWhenSending(c, mw)
129
type nopSyncStarter struct{}
131
func (nopSyncStarter) StartSync() {}