github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/agent/facade_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package agent_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/names.v2" 12 13 "github.com/juju/juju/api/agent" 14 "github.com/juju/juju/api/base" 15 apitesting "github.com/juju/juju/api/base/testing" 16 "github.com/juju/juju/apiserver/params" 17 ) 18 19 type FacadeSuite struct { 20 testing.IsolationSuite 21 } 22 23 var _ = gc.Suite(&FacadeSuite{}) 24 25 func (s *FacadeSuite) TestLifeCallError(c *gc.C) { 26 apiCaller := apiCaller(c, func(request string, arg, _ interface{}) error { 27 c.Check(request, gc.Equals, "GetEntities") 28 c.Check(arg, jc.DeepEquals, params.Entities{ 29 Entities: []params.Entity{{ 30 Tag: "application-omg", 31 }}, 32 }) 33 return errors.New("splat") 34 }) 35 facade, err := agent.NewConnFacade(apiCaller) 36 c.Assert(err, jc.ErrorIsNil) 37 38 life, err := facade.Life(names.NewApplicationTag("omg")) 39 c.Check(err, gc.ErrorMatches, "splat") 40 c.Check(life, gc.Equals, agent.Life("")) 41 } 42 43 func (s *FacadeSuite) TestLifeNoResult(c *gc.C) { 44 result := params.AgentGetEntitiesResults{} 45 facade, err := agent.NewConnFacade(lifeChecker(c, result)) 46 c.Assert(err, jc.ErrorIsNil) 47 48 life, err := facade.Life(names.NewApplicationTag("omg")) 49 c.Check(err, gc.ErrorMatches, "expected 1 result, got 0") 50 c.Check(life, gc.Equals, agent.Life("")) 51 } 52 53 func (s *FacadeSuite) TestLifeOversizedResult(c *gc.C) { 54 result := params.AgentGetEntitiesResults{ 55 Entities: []params.AgentGetEntitiesResult{{}, {}}, 56 } 57 facade, err := agent.NewConnFacade(lifeChecker(c, result)) 58 c.Assert(err, jc.ErrorIsNil) 59 60 life, err := facade.Life(names.NewApplicationTag("omg")) 61 c.Check(err, gc.ErrorMatches, "expected 1 result, got 2") 62 c.Check(life, gc.Equals, agent.Life("")) 63 } 64 65 func (s *FacadeSuite) TestLifeRandomError(c *gc.C) { 66 result := params.AgentGetEntitiesResult{ 67 Error: ¶ms.Error{Message: "squish"}, 68 } 69 life, err := testLifeAPIResult(c, result) 70 c.Check(err, gc.ErrorMatches, "squish") 71 c.Check(life, gc.Equals, agent.Life("")) 72 } 73 74 func (s *FacadeSuite) TestLifeErrNotFound(c *gc.C) { 75 result := params.AgentGetEntitiesResult{ 76 Error: ¶ms.Error{Code: params.CodeNotFound}, 77 } 78 life, err := testLifeAPIResult(c, result) 79 c.Check(err, gc.Equals, agent.ErrDenied) 80 c.Check(life, gc.Equals, agent.Life("")) 81 } 82 83 func (s *FacadeSuite) TestLifeErrUnauthorized(c *gc.C) { 84 result := params.AgentGetEntitiesResult{ 85 Error: ¶ms.Error{Code: params.CodeUnauthorized}, 86 } 87 life, err := testLifeAPIResult(c, result) 88 c.Check(err, gc.Equals, agent.ErrDenied) 89 c.Check(life, gc.Equals, agent.Life("")) 90 } 91 92 func (s *FacadeSuite) TestLifeUnknown(c *gc.C) { 93 result := params.AgentGetEntitiesResult{ 94 Life: "revenant", 95 } 96 life, err := testLifeAPIResult(c, result) 97 c.Check(err, gc.ErrorMatches, `unknown life value "revenant"`) 98 c.Check(life, gc.Equals, agent.Life("")) 99 } 100 101 func (s *FacadeSuite) TestLifeAlive(c *gc.C) { 102 result := params.AgentGetEntitiesResult{ 103 Life: "alive", 104 } 105 life, err := testLifeAPIResult(c, result) 106 c.Check(err, jc.ErrorIsNil) 107 c.Check(life, gc.Equals, agent.Alive) 108 } 109 110 func (s *FacadeSuite) TestLifeDying(c *gc.C) { 111 result := params.AgentGetEntitiesResult{ 112 Life: "dying", 113 } 114 life, err := testLifeAPIResult(c, result) 115 c.Check(err, jc.ErrorIsNil) 116 c.Check(life, gc.Equals, agent.Dying) 117 } 118 119 func (s *FacadeSuite) TestLifeDead(c *gc.C) { 120 result := params.AgentGetEntitiesResult{ 121 Life: "dead", 122 } 123 life, err := testLifeAPIResult(c, result) 124 c.Check(err, jc.ErrorIsNil) 125 c.Check(life, gc.Equals, agent.Dead) 126 } 127 128 func (s *FacadeSuite) TestSetPasswordCallError(c *gc.C) { 129 apiCaller := apiCaller(c, func(request string, arg, _ interface{}) error { 130 c.Check(request, gc.Equals, "SetPasswords") 131 c.Check(arg, jc.DeepEquals, params.EntityPasswords{ 132 Changes: []params.EntityPassword{{ 133 Tag: "application-omg", 134 Password: "seekr1t", 135 }}, 136 }) 137 return errors.New("splat") 138 }) 139 facade, err := agent.NewConnFacade(apiCaller) 140 c.Assert(err, jc.ErrorIsNil) 141 142 err = facade.SetPassword(names.NewApplicationTag("omg"), "seekr1t") 143 c.Check(err, gc.ErrorMatches, "splat") 144 } 145 146 func (s *FacadeSuite) TestSetPasswordNoResult(c *gc.C) { 147 result := params.ErrorResults{} 148 facade, err := agent.NewConnFacade(passwordChecker(c, result)) 149 c.Assert(err, jc.ErrorIsNil) 150 151 err = facade.SetPassword(names.NewApplicationTag("omg"), "blah") 152 c.Check(err, gc.ErrorMatches, "expected 1 result, got 0") 153 } 154 155 func (s *FacadeSuite) TestSetPasswordOversizedResult(c *gc.C) { 156 result := params.ErrorResults{ 157 Results: []params.ErrorResult{{}, {}}, 158 } 159 facade, err := agent.NewConnFacade(passwordChecker(c, result)) 160 c.Assert(err, jc.ErrorIsNil) 161 162 err = facade.SetPassword(names.NewApplicationTag("omg"), "blah") 163 c.Check(err, gc.ErrorMatches, "expected 1 result, got 2") 164 } 165 166 func (s *FacadeSuite) TestSetPasswordRandomError(c *gc.C) { 167 result := params.ErrorResult{ 168 Error: ¶ms.Error{Message: "squish"}, 169 } 170 err := testPasswordAPIResult(c, result) 171 c.Check(err, gc.ErrorMatches, "squish") 172 } 173 174 func (s *FacadeSuite) TestSetPasswordErrDead(c *gc.C) { 175 result := params.ErrorResult{ 176 Error: ¶ms.Error{Code: params.CodeDead}, 177 } 178 err := testPasswordAPIResult(c, result) 179 c.Check(err, gc.Equals, agent.ErrDenied) 180 } 181 182 func (s *FacadeSuite) TestSetPasswordErrNotFound(c *gc.C) { 183 result := params.ErrorResult{ 184 Error: ¶ms.Error{Code: params.CodeNotFound}, 185 } 186 err := testPasswordAPIResult(c, result) 187 c.Check(err, gc.Equals, agent.ErrDenied) 188 } 189 190 func (s *FacadeSuite) TestSetPasswordErrUnauthorized(c *gc.C) { 191 result := params.ErrorResult{ 192 Error: ¶ms.Error{Code: params.CodeUnauthorized}, 193 } 194 err := testPasswordAPIResult(c, result) 195 c.Check(err, gc.Equals, agent.ErrDenied) 196 } 197 198 func (s *FacadeSuite) TestSetPasswordSuccess(c *gc.C) { 199 result := params.ErrorResult{} 200 err := testPasswordAPIResult(c, result) 201 c.Check(err, jc.ErrorIsNil) 202 } 203 204 func testLifeAPIResult(c *gc.C, result params.AgentGetEntitiesResult) (agent.Life, error) { 205 facade, err := agent.NewConnFacade(lifeChecker(c, params.AgentGetEntitiesResults{ 206 Entities: []params.AgentGetEntitiesResult{result}, 207 })) 208 c.Assert(err, jc.ErrorIsNil) 209 210 return facade.Life(names.NewApplicationTag("omg")) 211 } 212 213 func lifeChecker(c *gc.C, result params.AgentGetEntitiesResults) base.APICaller { 214 return apiCaller(c, func(_ string, _, out interface{}) error { 215 typed, ok := out.(*params.AgentGetEntitiesResults) 216 c.Assert(ok, jc.IsTrue) 217 *typed = result 218 return nil 219 }) 220 } 221 222 func testPasswordAPIResult(c *gc.C, result params.ErrorResult) error { 223 facade, err := agent.NewConnFacade(passwordChecker(c, params.ErrorResults{ 224 Results: []params.ErrorResult{result}, 225 })) 226 c.Assert(err, jc.ErrorIsNil) 227 228 return facade.SetPassword(names.NewApplicationTag("omg"), "blah") 229 } 230 231 func passwordChecker(c *gc.C, result params.ErrorResults) base.APICaller { 232 return apiCaller(c, func(_ string, _, out interface{}) error { 233 typed, ok := out.(*params.ErrorResults) 234 c.Assert(ok, jc.IsTrue) 235 *typed = result 236 return nil 237 }) 238 } 239 240 func apiCaller(c *gc.C, check func(request string, arg, result interface{}) error) base.APICaller { 241 return apitesting.APICallerFunc(func(facade string, version int, id, request string, arg, result interface{}) error { 242 c.Check(facade, gc.Equals, "Agent") 243 c.Check(version, gc.Equals, 0) // because of BestFacadeVersion test infrastructure 244 c.Check(id, gc.Equals, "") 245 return check(request, arg, result) 246 }) 247 }