github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/hostkeyreporter/facade_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package hostkeyreporter_test 5 6 import ( 7 "errors" 8 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/names.v2" 13 14 basetesting "github.com/juju/juju/api/base/testing" 15 "github.com/juju/juju/api/hostkeyreporter" 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) TestReportKeys(c *gc.C) { 26 stub := new(testing.Stub) 27 apiCaller := basetesting.APICallerFunc(func( 28 objType string, version int, 29 id, request string, 30 args, response interface{}, 31 ) error { 32 c.Check(objType, gc.Equals, "HostKeyReporter") 33 c.Check(version, gc.Equals, 0) 34 c.Check(id, gc.Equals, "") 35 stub.AddCall(request, args) 36 *response.(*params.ErrorResults) = params.ErrorResults{ 37 Results: []params.ErrorResult{{ 38 (*params.Error)(nil), 39 }}, 40 } 41 return nil 42 }) 43 facade := hostkeyreporter.NewFacade(apiCaller) 44 45 err := facade.ReportKeys("42", []string{"rsa", "dsa"}) 46 c.Assert(err, jc.ErrorIsNil) 47 48 stub.CheckCalls(c, []testing.StubCall{{ 49 "ReportKeys", []interface{}{params.SSHHostKeySet{ 50 EntityKeys: []params.SSHHostKeys{{ 51 Tag: names.NewMachineTag("42").String(), 52 PublicKeys: []string{"rsa", "dsa"}, 53 }}, 54 }}, 55 }}) 56 } 57 58 func (s *facadeSuite) TestCallError(c *gc.C) { 59 apiCaller := basetesting.APICallerFunc(func( 60 objType string, version int, 61 id, request string, 62 args, response interface{}, 63 ) error { 64 return errors.New("blam") 65 }) 66 facade := hostkeyreporter.NewFacade(apiCaller) 67 68 err := facade.ReportKeys("42", []string{"rsa", "dsa"}) 69 c.Assert(err, gc.ErrorMatches, "blam") 70 } 71 72 func (s *facadeSuite) TestInnerError(c *gc.C) { 73 apiCaller := basetesting.APICallerFunc(func( 74 objType string, version int, 75 id, request string, 76 args, response interface{}, 77 ) error { 78 *response.(*params.ErrorResults) = params.ErrorResults{ 79 Results: []params.ErrorResult{{ 80 ¶ms.Error{Message: "blam"}, 81 }}, 82 } 83 return nil 84 }) 85 facade := hostkeyreporter.NewFacade(apiCaller) 86 87 err := facade.ReportKeys("42", []string{"rsa", "dsa"}) 88 c.Assert(err, gc.ErrorMatches, "blam") 89 }