github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/unitswatcher_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 ) 18 19 type unitsWatcherSuite struct{} 20 21 var _ = gc.Suite(&unitsWatcherSuite{}) 22 23 type fakeUnitsWatcher struct { 24 state.UnitsWatcher 25 initial []string 26 fetchError 27 } 28 29 func (f *fakeUnitsWatcher) WatchUnits() state.StringsWatcher { 30 changes := make(chan []string, 1) 31 // Simulate initial event. 32 changes <- f.initial 33 return &fakeStringsWatcher{changes} 34 } 35 36 type fakeStringsWatcher struct { 37 changes chan []string 38 } 39 40 func (*fakeStringsWatcher) Stop() error { 41 return nil 42 } 43 44 func (*fakeStringsWatcher) Kill() {} 45 46 func (*fakeStringsWatcher) Wait() error { 47 return nil 48 } 49 50 func (*fakeStringsWatcher) Err() error { 51 return nil 52 } 53 54 func (w *fakeStringsWatcher) Changes() <-chan []string { 55 return w.changes 56 } 57 58 func (*unitsWatcherSuite) TestWatchUnits(c *gc.C) { 59 st := &fakeState{ 60 entities: map[names.Tag]entityWithError{ 61 u("x/0"): &fakeUnitsWatcher{fetchError: "x0 fails"}, 62 u("x/1"): &fakeUnitsWatcher{initial: []string{"foo", "bar"}}, 63 u("x/2"): &fakeUnitsWatcher{}, 64 }, 65 } 66 getCanWatch := func() (common.AuthFunc, error) { 67 x0 := u("x/0") 68 x1 := u("x/1") 69 return func(tag names.Tag) bool { 70 return tag == x0 || tag == x1 71 }, nil 72 } 73 resources := common.NewResources() 74 w := common.NewUnitsWatcher(st, resources, getCanWatch) 75 entities := params.Entities{[]params.Entity{ 76 {"unit-x-0"}, {"unit-x-1"}, {"unit-x-2"}, {"unit-x-3"}, 77 }} 78 result, err := w.WatchUnits(entities) 79 c.Assert(err, jc.ErrorIsNil) 80 c.Assert(result, jc.DeepEquals, params.StringsWatchResults{ 81 Results: []params.StringsWatchResult{ 82 {Error: ¶ms.Error{Message: "x0 fails"}}, 83 {"1", []string{"foo", "bar"}, nil}, 84 {Error: apiservertesting.ErrUnauthorized}, 85 {Error: apiservertesting.ErrUnauthorized}, 86 }, 87 }) 88 } 89 90 func (*unitsWatcherSuite) TestWatchUnitsError(c *gc.C) { 91 getCanWatch := func() (common.AuthFunc, error) { 92 return nil, fmt.Errorf("pow") 93 } 94 resources := common.NewResources() 95 w := common.NewUnitsWatcher( 96 &fakeState{}, 97 resources, 98 getCanWatch, 99 ) 100 _, err := w.WatchUnits(params.Entities{[]params.Entity{{"x0"}}}) 101 c.Assert(err, gc.ErrorMatches, "pow") 102 } 103 104 func (*unitsWatcherSuite) TestWatchNoArgsNoError(c *gc.C) { 105 getCanWatch := func() (common.AuthFunc, error) { 106 return nil, fmt.Errorf("pow") 107 } 108 resources := common.NewResources() 109 w := common.NewUnitsWatcher( 110 &fakeState{}, 111 resources, 112 getCanWatch, 113 ) 114 result, err := w.WatchUnits(params.Entities{}) 115 c.Assert(err, jc.ErrorIsNil) 116 c.Assert(result.Results, gc.HasLen, 0) 117 }