github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/cleaner/cleaner_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cleaner_test 5 6 import ( 7 "errors" 8 "time" 9 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/api/base" 14 apitesting "github.com/juju/juju/api/base/testing" 15 "github.com/juju/juju/api/cleaner" 16 "github.com/juju/juju/apiserver/params" 17 coretesting "github.com/juju/juju/testing" 18 ) 19 20 type CleanerSuite struct { 21 coretesting.BaseSuite 22 } 23 24 var _ = gc.Suite(&CleanerSuite{}) 25 26 type TestCommon struct { 27 apiCaller base.APICaller 28 apiArgs apitesting.CheckArgs 29 called chan struct{} 30 api *cleaner.API 31 } 32 33 // Init returns a new, initialised instance of TestCommon. 34 func Init(c *gc.C, method string, expectArgs, useResults interface{}, err error) (t *TestCommon) { 35 t = &TestCommon{} 36 t.apiArgs = apitesting.CheckArgs{ 37 Facade: "Cleaner", 38 VersionIsZero: true, 39 IdIsEmpty: true, 40 Method: method, 41 Args: expectArgs, 42 Results: useResults, 43 } 44 45 t.called = make(chan struct{}, 100) 46 t.apiCaller = apitesting.NotifyingCheckingAPICaller(c, &t.apiArgs, t.called, err) 47 t.api = cleaner.NewAPI(t.apiCaller) 48 49 c.Check(t.api, gc.NotNil) 50 return 51 } 52 53 // AssertNumReceives checks that the watched channel receives "expected" messages 54 // within a LongWait, but returns as soon as possible. 55 func AssertNumReceives(c *gc.C, watched chan struct{}, expected uint32) { 56 var receives uint32 57 58 for receives < expected { 59 select { 60 case <-watched: 61 receives++ 62 case <-time.After(coretesting.LongWait): 63 c.Errorf("timeout while waiting for a call") 64 } 65 } 66 67 time.Sleep(coretesting.ShortWait) 68 c.Assert(receives, gc.Equals, expected) 69 } 70 71 func (s *CleanerSuite) TestNewAPI(c *gc.C) { 72 Init(c, "", nil, nil, nil) 73 } 74 75 func (s *CleanerSuite) TestWatchCleanups(c *gc.C) { 76 t := Init(c, "", nil, nil, nil) 77 t.apiArgs.Facade = "" // Multiple facades are called, so we can't check this. 78 m, err := t.api.WatchCleanups() 79 AssertNumReceives(c, t.called, 2) 80 c.Assert(err, jc.ErrorIsNil) 81 c.Assert(m, gc.NotNil) 82 } 83 84 func (s *CleanerSuite) TestCleanup(c *gc.C) { 85 t := Init(c, "Cleanup", nil, nil, nil) 86 err := t.api.Cleanup() 87 AssertNumReceives(c, t.called, 1) 88 c.Assert(err, jc.ErrorIsNil) 89 } 90 91 func (s *CleanerSuite) TestWatchCleanupsFailFacadeCall(c *gc.C) { 92 t := Init(c, "WatchCleanups", nil, nil, errors.New("client error!")) 93 m, err := t.api.WatchCleanups() 94 c.Assert(err, gc.ErrorMatches, "client error!") 95 AssertNumReceives(c, t.called, 1) 96 c.Assert(m, gc.IsNil) 97 } 98 99 func (s *CleanerSuite) TestWatchCleanupsFailFacadeResult(c *gc.C) { 100 e := params.Error{ 101 Message: "Server Error", 102 } 103 p := params.NotifyWatchResult{ 104 Error: &e, 105 } 106 t := Init(c, "WatchCleanups", nil, p, nil) 107 m, err := t.api.WatchCleanups() 108 AssertNumReceives(c, t.called, 1) 109 c.Assert(err, gc.ErrorMatches, e.Message) 110 c.Assert(m, gc.IsNil) 111 }