github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/undertaker/undertaker_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package undertaker_test 5 6 import ( 7 "github.com/juju/testing" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/api/base" 12 basetesting "github.com/juju/juju/api/base/testing" 13 "github.com/juju/juju/api/undertaker" 14 "github.com/juju/juju/apiserver/params" 15 coretesting "github.com/juju/juju/testing" 16 "github.com/juju/juju/watcher" 17 ) 18 19 type UndertakerSuite struct { 20 testing.IsolationSuite 21 } 22 23 var _ = gc.Suite(&UndertakerSuite{}) 24 25 func (s *UndertakerSuite) TestEnvironInfo(c *gc.C) { 26 var called bool 27 client := s.mockClient(c, "ModelInfo", func(response interface{}) { 28 called = true 29 result := response.(*params.UndertakerModelInfoResult) 30 result.Result = params.UndertakerModelInfo{} 31 }) 32 33 result, err := client.ModelInfo() 34 c.Assert(err, jc.ErrorIsNil) 35 c.Assert(called, jc.IsTrue) 36 c.Assert(result, gc.Equals, params.UndertakerModelInfoResult{}) 37 } 38 39 func (s *UndertakerSuite) TestProcessDyingEnviron(c *gc.C) { 40 var called bool 41 client := s.mockClient(c, "ProcessDyingModel", func(response interface{}) { 42 called = true 43 c.Assert(response, gc.IsNil) 44 }) 45 46 c.Assert(client.ProcessDyingModel(), jc.ErrorIsNil) 47 c.Assert(called, jc.IsTrue) 48 } 49 50 func (s *UndertakerSuite) TestRemoveModel(c *gc.C) { 51 var called bool 52 client := s.mockClient(c, "RemoveModel", func(response interface{}) { 53 called = true 54 c.Assert(response, gc.IsNil) 55 }) 56 57 err := client.RemoveModel() 58 c.Assert(err, jc.ErrorIsNil) 59 c.Assert(called, jc.IsTrue) 60 } 61 62 func (s *UndertakerSuite) mockClient(c *gc.C, expectedRequest string, callback func(response interface{})) *undertaker.Client { 63 apiCaller := basetesting.APICallerFunc(func( 64 objType string, 65 version int, 66 id, request string, 67 args, response interface{}, 68 ) error { 69 c.Check(objType, gc.Equals, "Undertaker") 70 c.Check(id, gc.Equals, "") 71 c.Check(request, gc.Equals, expectedRequest) 72 73 a, ok := args.(params.Entities) 74 c.Check(ok, jc.IsTrue) 75 c.Check(a.Entities, gc.DeepEquals, []params.Entity{{Tag: coretesting.ModelTag.String()}}) 76 77 callback(response) 78 return nil 79 }) 80 client, err := undertaker.NewClient(apiCaller, nil) 81 c.Assert(err, jc.ErrorIsNil) 82 return client 83 } 84 85 func (s *UndertakerSuite) TestWatchModelResourcesCreatesWatcher(c *gc.C) { 86 apiCaller := basetesting.APICallerFunc(func( 87 objType string, 88 version int, 89 id, request string, 90 args, response interface{}, 91 ) error { 92 c.Check(objType, gc.Equals, "Undertaker") 93 c.Check(id, gc.Equals, "") 94 c.Check(request, gc.Equals, "WatchModelResources") 95 96 a, ok := args.(params.Entities) 97 c.Check(ok, jc.IsTrue) 98 c.Check(a.Entities, gc.DeepEquals, []params.Entity{{Tag: coretesting.ModelTag.String()}}) 99 100 resp, ok := response.(*params.NotifyWatchResults) 101 c.Assert(ok, jc.IsTrue) 102 resp.Results = []params.NotifyWatchResult{{ 103 NotifyWatcherId: "1001", 104 }} 105 return nil 106 }) 107 108 expectWatcher := &fakeWatcher{} 109 newWatcher := func(apiCaller base.APICaller, result params.NotifyWatchResult) watcher.NotifyWatcher { 110 c.Check(apiCaller, gc.NotNil) // uncomparable 111 c.Check(result, gc.Equals, params.NotifyWatchResult{ 112 NotifyWatcherId: "1001", 113 }) 114 return expectWatcher 115 } 116 117 client, err := undertaker.NewClient(apiCaller, newWatcher) 118 c.Assert(err, jc.ErrorIsNil) 119 w, err := client.WatchModelResources() 120 c.Assert(err, jc.ErrorIsNil) 121 c.Check(w, gc.Equals, expectWatcher) 122 } 123 124 func (s *UndertakerSuite) TestWatchModelResourcesError(c *gc.C) { 125 var called bool 126 client := s.mockClient(c, "WatchModelResources", func(response interface{}) { 127 called = true 128 _, ok := response.(*params.NotifyWatchResults) 129 c.Check(ok, jc.IsTrue) 130 }) 131 132 w, err := client.WatchModelResources() 133 c.Assert(err, gc.ErrorMatches, "expected 1 result, got 0") 134 c.Assert(w, gc.IsNil) 135 c.Assert(called, jc.IsTrue) 136 } 137 138 type fakeWatcher struct { 139 watcher.NotifyWatcher 140 }