github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/common/watch_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common_test 5 6 import ( 7 "fmt" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/names.v2" 12 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" 18 ) 19 20 type agentEntityWatcherSuite struct{} 21 22 var _ = gc.Suite(&agentEntityWatcherSuite{}) 23 24 type fakeAgentEntityWatcher struct { 25 state.Entity 26 fetchError 27 } 28 29 func (a *fakeAgentEntityWatcher) Watch() state.NotifyWatcher { 30 return apiservertesting.NewFakeNotifyWatcher() 31 } 32 33 func (*agentEntityWatcherSuite) TestWatch(c *gc.C) { 34 st := &fakeState{ 35 entities: map[names.Tag]entityWithError{ 36 u("x/0"): &fakeAgentEntityWatcher{fetchError: "x0 fails"}, 37 u("x/1"): &fakeAgentEntityWatcher{}, 38 u("x/2"): &fakeAgentEntityWatcher{}, 39 }, 40 } 41 getCanWatch := func() (common.AuthFunc, error) { 42 x0 := u("x/0") 43 x1 := u("x/1") 44 return func(tag names.Tag) bool { 45 return tag == x0 || tag == x1 46 }, nil 47 } 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"}, 52 }} 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"}}, 58 {"1", nil}, 59 {Error: apiservertesting.ErrUnauthorized}, 60 {Error: apiservertesting.ErrUnauthorized}, 61 }, 62 }) 63 } 64 65 func (*agentEntityWatcherSuite) TestWatchError(c *gc.C) { 66 getCanWatch := func() (common.AuthFunc, error) { 67 return nil, fmt.Errorf("pow") 68 } 69 resources := common.NewResources() 70 a := common.NewAgentEntityWatcher( 71 &fakeState{}, 72 resources, 73 getCanWatch, 74 ) 75 _, err := a.Watch(params.Entities{[]params.Entity{{"x0"}}}) 76 c.Assert(err, gc.ErrorMatches, "pow") 77 } 78 79 func (*agentEntityWatcherSuite) TestWatchNoArgsNoError(c *gc.C) { 80 getCanWatch := func() (common.AuthFunc, error) { 81 return nil, fmt.Errorf("pow") 82 } 83 resources := common.NewResources() 84 a := common.NewAgentEntityWatcher( 85 &fakeState{}, 86 resources, 87 getCanWatch, 88 ) 89 result, err := a.Watch(params.Entities{}) 90 c.Assert(err, jc.ErrorIsNil) 91 c.Assert(result.Results, gc.HasLen, 0) 92 } 93 94 type multiNotifyWatcherSuite struct{} 95 96 var _ = gc.Suite(&multiNotifyWatcherSuite{}) 97 98 func (*multiNotifyWatcherSuite) TestMultiNotifyWatcher(c *gc.C) { 99 w0 := apiservertesting.NewFakeNotifyWatcher() 100 w1 := apiservertesting.NewFakeNotifyWatcher() 101 102 mw := common.NewMultiNotifyWatcher(w0, w1) 103 defer statetesting.AssertStop(c, mw) 104 105 wc := statetesting.NewNotifyWatcherC(c, nopSyncStarter{}, mw) 106 wc.AssertOneChange() 107 108 w0.C <- struct{}{} 109 wc.AssertOneChange() 110 w1.C <- struct{}{} 111 wc.AssertOneChange() 112 113 w0.C <- struct{}{} 114 w1.C <- struct{}{} 115 wc.AssertOneChange() 116 } 117 118 func (*multiNotifyWatcherSuite) TestMultiNotifyWatcherStop(c *gc.C) { 119 w0 := apiservertesting.NewFakeNotifyWatcher() 120 w1 := apiservertesting.NewFakeNotifyWatcher() 121 122 mw := common.NewMultiNotifyWatcher(w0, w1) 123 wc := statetesting.NewNotifyWatcherC(c, nopSyncStarter{}, mw) 124 wc.AssertOneChange() 125 statetesting.AssertCanStopWhenSending(c, mw) 126 wc.AssertClosed() 127 } 128 129 type nopSyncStarter struct{} 130 131 func (nopSyncStarter) StartSync() {}