github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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  
    12  	"github.com/juju/juju/apiserver/common"
    13  	"github.com/juju/juju/apiserver/params"
    14  	apiservertesting "github.com/juju/juju/apiserver/testing"
    15  	"github.com/juju/juju/state"
    16  	"github.com/juju/names"
    17  )
    18  
    19  type agentEntityWatcherSuite struct{}
    20  
    21  var _ = gc.Suite(&agentEntityWatcherSuite{})
    22  
    23  type fakeAgentEntityWatcher struct {
    24  	state.Entity
    25  	fetchError
    26  }
    27  
    28  func (a *fakeAgentEntityWatcher) Watch() state.NotifyWatcher {
    29  	changes := make(chan struct{}, 1)
    30  	// Simulate initial event.
    31  	changes <- struct{}{}
    32  	return &fakeNotifyWatcher{changes}
    33  }
    34  
    35  type fakeNotifyWatcher struct {
    36  	changes chan struct{}
    37  }
    38  
    39  func (*fakeNotifyWatcher) Stop() error {
    40  	return nil
    41  }
    42  
    43  func (*fakeNotifyWatcher) Kill() {}
    44  
    45  func (*fakeNotifyWatcher) Wait() error {
    46  	return nil
    47  }
    48  
    49  func (*fakeNotifyWatcher) Err() error {
    50  	return nil
    51  }
    52  
    53  func (w *fakeNotifyWatcher) Changes() <-chan struct{} {
    54  	return w.changes
    55  }
    56  
    57  func (*agentEntityWatcherSuite) TestWatch(c *gc.C) {
    58  	st := &fakeState{
    59  		entities: map[names.Tag]entityWithError{
    60  			u("x/0"): &fakeAgentEntityWatcher{fetchError: "x0 fails"},
    61  			u("x/1"): &fakeAgentEntityWatcher{},
    62  			u("x/2"): &fakeAgentEntityWatcher{},
    63  		},
    64  	}
    65  	getCanWatch := func() (common.AuthFunc, error) {
    66  		x0 := u("x/0")
    67  		x1 := u("x/1")
    68  		return func(tag names.Tag) bool {
    69  			return tag == x0 || tag == x1
    70  		}, nil
    71  	}
    72  	resources := common.NewResources()
    73  	a := common.NewAgentEntityWatcher(st, resources, getCanWatch)
    74  	entities := params.Entities{[]params.Entity{
    75  		{"unit-x-0"}, {"unit-x-1"}, {"unit-x-2"}, {"unit-x-3"},
    76  	}}
    77  	result, err := a.Watch(entities)
    78  	c.Assert(err, jc.ErrorIsNil)
    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, jc.ErrorIsNil)
   115  	c.Assert(result.Results, gc.HasLen, 0)
   116  }