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