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