launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/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  	"launchpad.net/errgo/errors"
     8  	gc "launchpad.net/gocheck"
     9  
    10  	"launchpad.net/juju-core/state"
    11  	"launchpad.net/juju-core/state/api/params"
    12  	"launchpad.net/juju-core/state/apiserver/common"
    13  	apiservertesting "launchpad.net/juju-core/state/apiserver/testing"
    14  	jc "launchpad.net/juju-core/testing/checkers"
    15  )
    16  
    17  type unitsWatcherSuite struct{}
    18  
    19  var _ = gc.Suite(&unitsWatcherSuite{})
    20  
    21  type fakeUnitsWatcher struct {
    22  	state.UnitsWatcher
    23  	initial []string
    24  	fetchError
    25  }
    26  
    27  func (f *fakeUnitsWatcher) WatchUnits() state.StringsWatcher {
    28  	changes := make(chan []string, 1)
    29  	// Simulate initial event.
    30  	changes <- f.initial
    31  	return &fakeStringsWatcher{changes}
    32  }
    33  
    34  type fakeStringsWatcher struct {
    35  	changes chan []string
    36  }
    37  
    38  func (*fakeStringsWatcher) Stop() error {
    39  	return nil
    40  }
    41  
    42  func (*fakeStringsWatcher) Kill() {}
    43  
    44  func (*fakeStringsWatcher) Wait() error {
    45  	return nil
    46  }
    47  
    48  func (*fakeStringsWatcher) Err() error {
    49  	return nil
    50  }
    51  
    52  func (w *fakeStringsWatcher) Changes() <-chan []string {
    53  	return w.changes
    54  }
    55  
    56  func (*unitsWatcherSuite) TestWatchUnits(c *gc.C) {
    57  	st := &fakeState{
    58  		entities: map[string]entityWithError{
    59  			"x0": &fakeUnitsWatcher{fetchError: "x0 fails"},
    60  			"x1": &fakeUnitsWatcher{initial: []string{"foo", "bar"}},
    61  			"x2": &fakeUnitsWatcher{},
    62  		},
    63  	}
    64  	getCanWatch := func() (common.AuthFunc, error) {
    65  		return func(tag string) bool {
    66  			switch tag {
    67  			case "x0", "x1":
    68  				return true
    69  			}
    70  			return false
    71  		}, nil
    72  	}
    73  	resources := common.NewResources()
    74  	w := common.NewUnitsWatcher(st, resources, getCanWatch)
    75  	entities := params.Entities{[]params.Entity{
    76  		{"x0"}, {"x1"}, {"x2"}, {"x3"},
    77  	}}
    78  	result, err := w.WatchUnits(entities)
    79  	c.Assert(err, gc.IsNil)
    80  	c.Assert(result, jc.DeepEquals, params.StringsWatchResults{
    81  		Results: []params.StringsWatchResult{
    82  			{Error: &params.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, errors.Newf("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, errors.Newf("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, gc.IsNil)
   116  	c.Assert(result.Results, gc.HasLen, 0)
   117  }