launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/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  	"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  )
    15  
    16  type agentEntityWatcherSuite struct{}
    17  
    18  var _ = gc.Suite(&agentEntityWatcherSuite{})
    19  
    20  type fakeAgentEntityWatcher struct {
    21  	state.Entity
    22  	fetchError
    23  }
    24  
    25  func (a *fakeAgentEntityWatcher) Watch() state.NotifyWatcher {
    26  	changes := make(chan struct{}, 1)
    27  	// Simulate initial event.
    28  	changes <- struct{}{}
    29  	return &fakeNotifyWatcher{changes}
    30  }
    31  
    32  type fakeNotifyWatcher struct {
    33  	changes chan struct{}
    34  }
    35  
    36  func (*fakeNotifyWatcher) Stop() error {
    37  	return nil
    38  }
    39  
    40  func (*fakeNotifyWatcher) Kill() {}
    41  
    42  func (*fakeNotifyWatcher) Wait() error {
    43  	return nil
    44  }
    45  
    46  func (*fakeNotifyWatcher) Err() error {
    47  	return nil
    48  }
    49  
    50  func (w *fakeNotifyWatcher) Changes() <-chan struct{} {
    51  	return w.changes
    52  }
    53  
    54  func (*agentEntityWatcherSuite) TestWatch(c *gc.C) {
    55  	st := &fakeState{
    56  		entities: map[string]entityWithError{
    57  			"x0": &fakeAgentEntityWatcher{fetchError: "x0 fails"},
    58  			"x1": &fakeAgentEntityWatcher{},
    59  			"x2": &fakeAgentEntityWatcher{},
    60  		},
    61  	}
    62  	getCanWatch := func() (common.AuthFunc, error) {
    63  		return func(tag string) bool {
    64  			switch tag {
    65  			case "x0", "x1":
    66  				return true
    67  			}
    68  			return false
    69  		}, nil
    70  	}
    71  	resources := common.NewResources()
    72  	a := common.NewAgentEntityWatcher(st, resources, getCanWatch)
    73  	entities := params.Entities{[]params.Entity{
    74  		{"x0"}, {"x1"}, {"x2"}, {"x3"},
    75  	}}
    76  	result, err := a.Watch(entities)
    77  	c.Assert(err, gc.IsNil)
    78  	c.Assert(result, gc.DeepEquals, params.NotifyWatchResults{
    79  		Results: []params.NotifyWatchResult{
    80  			{Error: &params.Error{Message: "x0 fails"}},
    81  			{"1", nil},
    82  			{Error: apiservertesting.ErrUnauthorized},
    83  			{Error: apiservertesting.ErrUnauthorized},
    84  		},
    85  	})
    86  }
    87  
    88  func (*agentEntityWatcherSuite) TestWatchError(c *gc.C) {
    89  	getCanWatch := func() (common.AuthFunc, error) {
    90  		return nil, errors.Newf("pow")
    91  	}
    92  	resources := common.NewResources()
    93  	a := common.NewAgentEntityWatcher(
    94  		&fakeState{},
    95  		resources,
    96  		getCanWatch,
    97  	)
    98  	_, err := a.Watch(params.Entities{[]params.Entity{{"x0"}}})
    99  	c.Assert(err, gc.ErrorMatches, "pow")
   100  }
   101  
   102  func (*agentEntityWatcherSuite) TestWatchNoArgsNoError(c *gc.C) {
   103  	getCanWatch := func() (common.AuthFunc, error) {
   104  		return nil, errors.Newf("pow")
   105  	}
   106  	resources := common.NewResources()
   107  	a := common.NewAgentEntityWatcher(
   108  		&fakeState{},
   109  		resources,
   110  		getCanWatch,
   111  	)
   112  	result, err := a.Watch(params.Entities{})
   113  	c.Assert(err, gc.IsNil)
   114  	c.Assert(result.Results, gc.HasLen, 0)
   115  }