github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/apiserver/unitassigner/unitassigner_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package unitassigner
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/apiserver/common"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/state"
    13  )
    14  
    15  var _ = gc.Suite(testsuite{})
    16  
    17  type testsuite struct{}
    18  
    19  func (testsuite) TestAssignUnits(c *gc.C) {
    20  	f := &fakeState{}
    21  	f.results = []state.UnitAssignmentResult{{Unit: "foo/0"}}
    22  	api := API{st: f, res: common.NewResources()}
    23  	args := params.Entities{Entities: []params.Entity{{Tag: "unit-foo-0"}, {Tag: "unit-bar-1"}}}
    24  	res, err := api.AssignUnits(args)
    25  	c.Assert(f.ids, gc.DeepEquals, []string{"foo/0", "bar/1"})
    26  	c.Assert(err, jc.ErrorIsNil)
    27  	c.Assert(res.Results, gc.HasLen, 2)
    28  	c.Assert(res.Results, gc.HasLen, 2)
    29  	c.Assert(res.Results[0].Error, gc.IsNil)
    30  	c.Assert(res.Results[1].Error, gc.ErrorMatches, `unit "unit-bar-1" not found`)
    31  }
    32  
    33  func (testsuite) TestWatchUnitAssignment(c *gc.C) {
    34  	f := &fakeState{}
    35  	api := API{st: f, res: common.NewResources()}
    36  	f.ids = []string{"boo", "far"}
    37  	res, err := api.WatchUnitAssignments()
    38  	c.Assert(f.watchCalled, jc.IsTrue)
    39  	c.Assert(err, jc.ErrorIsNil)
    40  	c.Assert(res.Changes, gc.DeepEquals, f.ids)
    41  }
    42  
    43  func (testsuite) TestSetStatus(c *gc.C) {
    44  	f := &fakeStatusSetter{
    45  		res: params.ErrorResults{
    46  			Results: []params.ErrorResult{
    47  				{Error: &params.Error{Message: "boo"}}}}}
    48  	api := API{statusSetter: f}
    49  	args := params.SetStatus{
    50  		Entities: []params.EntityStatusArgs{{Tag: "foo/0"}},
    51  	}
    52  	res, err := api.SetAgentStatus(args)
    53  	c.Assert(args, jc.DeepEquals, f.args)
    54  	c.Assert(res, jc.DeepEquals, f.res)
    55  	c.Assert(err, gc.Equals, f.err)
    56  }
    57  
    58  type fakeState struct {
    59  	watchCalled bool
    60  	ids         []string
    61  	results     []state.UnitAssignmentResult
    62  	err         error
    63  }
    64  
    65  func (f *fakeState) WatchForUnitAssignment() state.StringsWatcher {
    66  	f.watchCalled = true
    67  	return fakeWatcher{f.ids}
    68  }
    69  
    70  func (f *fakeState) AssignStagedUnits(ids []string) ([]state.UnitAssignmentResult, error) {
    71  	f.ids = ids
    72  	return f.results, f.err
    73  }
    74  
    75  type fakeWatcher struct {
    76  	changes []string
    77  }
    78  
    79  func (f fakeWatcher) Changes() <-chan []string {
    80  	changes := make(chan []string, 1)
    81  	changes <- f.changes
    82  	return changes
    83  }
    84  func (fakeWatcher) Kill() {}
    85  
    86  func (fakeWatcher) Wait() error { return nil }
    87  
    88  func (fakeWatcher) Stop() error { return nil }
    89  
    90  func (fakeWatcher) Err() error { return nil }
    91  
    92  type fakeStatusSetter struct {
    93  	args params.SetStatus
    94  	res  params.ErrorResults
    95  	err  error
    96  }
    97  
    98  func (f *fakeStatusSetter) SetStatus(args params.SetStatus) (params.ErrorResults, error) {
    99  	f.args = args
   100  	return f.res, f.err
   101  }