github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/payload/api/client/public_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package client_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/payload" 14 "github.com/juju/juju/payload/api" 15 "github.com/juju/juju/payload/api/client" 16 ) 17 18 type publicSuite struct { 19 testing.IsolationSuite 20 21 stub *testing.Stub 22 facade *stubFacade 23 payload api.Payload 24 } 25 26 var _ = gc.Suite(&publicSuite{}) 27 28 func (s *publicSuite) SetUpTest(c *gc.C) { 29 s.IsolationSuite.SetUpTest(c) 30 31 s.stub = &testing.Stub{} 32 s.facade = &stubFacade{stub: s.stub} 33 s.payload = api.Payload{ 34 Class: "spam", 35 Type: "docker", 36 ID: "idspam", 37 Status: payload.StateRunning, 38 Labels: nil, 39 Unit: names.NewUnitTag("a-application/0").String(), 40 Machine: names.NewMachineTag("1").String(), 41 } 42 } 43 44 func (s *publicSuite) TestListOkay(c *gc.C) { 45 s.facade.FacadeCallFn = func(_ string, _, response interface{}) error { 46 typedResponse, ok := response.(*api.EnvListResults) 47 c.Assert(ok, gc.Equals, true) 48 typedResponse.Results = append(typedResponse.Results, s.payload) 49 return nil 50 } 51 52 pclient := client.NewPublicClient(s.facade) 53 54 payloads, err := pclient.ListFull("a-tag", "a-application/0") 55 c.Assert(err, jc.ErrorIsNil) 56 57 expected, _ := api.API2Payload(s.payload) 58 c.Check(payloads, jc.DeepEquals, []payload.FullPayloadInfo{ 59 expected, 60 }) 61 } 62 63 func (s *publicSuite) TestListAPI(c *gc.C) { 64 pclient := client.NewPublicClient(s.facade) 65 66 _, err := pclient.ListFull("a-tag") 67 c.Assert(err, jc.ErrorIsNil) 68 69 s.stub.CheckCalls(c, []testing.StubCall{{ 70 FuncName: "FacadeCall", 71 Args: []interface{}{ 72 "List", 73 &api.EnvListArgs{ 74 Patterns: []string{"a-tag"}, 75 }, 76 &api.EnvListResults{ 77 Results: nil, 78 }, 79 }, 80 }}) 81 } 82 83 type stubFacade struct { 84 stub *testing.Stub 85 FacadeCallFn func(name string, params, response interface{}) error 86 } 87 88 func (s *stubFacade) FacadeCall(request string, params, response interface{}) error { 89 s.stub.AddCall("FacadeCall", request, params, response) 90 if err := s.stub.NextErr(); err != nil { 91 return errors.Trace(err) 92 } 93 94 if s.FacadeCallFn != nil { 95 return s.FacadeCallFn(request, params, response) 96 } 97 return nil 98 } 99 100 func (s *stubFacade) Close() error { 101 s.stub.AddCall("Close") 102 if err := s.stub.NextErr(); err != nil { 103 return errors.Trace(err) 104 } 105 106 return nil 107 }