github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/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 "sync" 8 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/names.v2" 13 14 "github.com/juju/juju/api/base" 15 "github.com/juju/juju/apiserver/params" 16 ) 17 18 var _ = gc.Suite(testsuite{}) 19 20 type testsuite struct{} 21 22 func (testsuite) TestAssignUnits(c *gc.C) { 23 f := &fakeAssignCaller{c: c, response: params.ErrorResults{ 24 Results: []params.ErrorResult{ 25 {}, 26 {}, 27 }}} 28 api := New(f) 29 ids := []names.UnitTag{names.NewUnitTag("mysql/0"), names.NewUnitTag("mysql/1")} 30 errs, err := api.AssignUnits(ids) 31 c.Assert(f.request, gc.Equals, "AssignUnits") 32 c.Assert(f.params, gc.DeepEquals, 33 params.Entities{[]params.Entity{ 34 {Tag: "unit-mysql-0"}, 35 {Tag: "unit-mysql-1"}, 36 }}, 37 ) 38 c.Assert(err, jc.ErrorIsNil) 39 c.Assert(errs, gc.DeepEquals, []error{nil, nil}) 40 } 41 42 func (testsuite) TestAssignUnitsNotFound(c *gc.C) { 43 f := &fakeAssignCaller{c: c, response: params.ErrorResults{ 44 Results: []params.ErrorResult{ 45 {Error: ¶ms.Error{Code: params.CodeNotFound}}, 46 }}} 47 api := New(f) 48 ids := []names.UnitTag{names.NewUnitTag("mysql/0")} 49 errs, err := api.AssignUnits(ids) 50 f.Lock() 51 c.Assert(f.request, gc.Equals, "AssignUnits") 52 c.Assert(f.params, gc.DeepEquals, 53 params.Entities{[]params.Entity{ 54 {Tag: "unit-mysql-0"}, 55 }}, 56 ) 57 c.Assert(err, jc.ErrorIsNil) 58 c.Assert(errs, gc.HasLen, 1) 59 c.Assert(errs[0], jc.Satisfies, errors.IsNotFound) 60 } 61 62 func (testsuite) TestWatchUnitAssignment(c *gc.C) { 63 f := &fakeWatchCaller{ 64 c: c, 65 response: params.StringsWatchResult{}, 66 } 67 api := New(f) 68 w, err := api.WatchUnitAssignments() 69 f.Lock() 70 c.Assert(f.request, gc.Equals, "WatchUnitAssignments") 71 c.Assert(f.params, gc.IsNil) 72 c.Assert(err, jc.ErrorIsNil) 73 c.Assert(w, gc.NotNil) 74 } 75 76 type fakeAssignCaller struct { 77 base.APICaller 78 sync.Mutex 79 request string 80 params interface{} 81 response params.ErrorResults 82 err error 83 c *gc.C 84 } 85 86 func (f *fakeAssignCaller) APICall(objType string, version int, id, request string, param, response interface{}) error { 87 f.Lock() 88 defer f.Unlock() 89 f.request = request 90 f.params = param 91 res, ok := response.(*params.ErrorResults) 92 if !ok { 93 f.c.Errorf("Expected *params.ErrorResults as response, but was %#v", response) 94 } else { 95 *res = f.response 96 } 97 return f.err 98 99 } 100 101 func (*fakeAssignCaller) BestFacadeVersion(facade string) int { 102 return 1 103 } 104 105 type fakeWatchCaller struct { 106 base.APICaller 107 sync.Mutex 108 request string 109 params interface{} 110 response params.StringsWatchResult 111 err error 112 c *gc.C 113 } 114 115 func (f *fakeWatchCaller) APICall(objType string, version int, id, request string, param, response interface{}) error { 116 f.Lock() 117 defer f.Unlock() 118 119 // We only care for the first request as that is all the tests 120 // assert on. The watcher (StringsWatcher) is continuously 121 // running and this function gets called repeatedly 122 // overwriting f.request leading to intermittent failures. 123 // Fixes: https://bugs.launchpad.net/juju/+bug/1606302 124 125 if f.request == "" { 126 f.request = request 127 f.params = param 128 _, ok := response.(*params.StringsWatchResult) 129 if !ok { 130 f.c.Errorf("Expected *params.StringsWatchResult as response, but was %#v", response) 131 } 132 } 133 return f.err 134 } 135 136 func (*fakeWatchCaller) BestFacadeVersion(facade string) int { 137 return 1 138 }