github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/resource/api/private/client/client_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package client_test
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  
    10  	"github.com/juju/errors"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	"github.com/juju/testing/filetesting"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/resource"
    17  	"github.com/juju/juju/resource/api"
    18  	"github.com/juju/juju/resource/api/private"
    19  	"github.com/juju/juju/resource/api/private/client"
    20  	"github.com/juju/juju/resource/resourcetesting"
    21  )
    22  
    23  var _ = gc.Suite(&UnitFacadeClientSuite{})
    24  
    25  type UnitFacadeClientSuite struct {
    26  	testing.IsolationSuite
    27  
    28  	stub *testing.Stub
    29  	api  *stubAPI
    30  }
    31  
    32  func (s *UnitFacadeClientSuite) SetUpTest(c *gc.C) {
    33  	s.IsolationSuite.SetUpTest(c)
    34  
    35  	s.stub = &testing.Stub{}
    36  	s.api = &stubAPI{Stub: s.stub}
    37  }
    38  
    39  func (s *UnitFacadeClientSuite) TestNewUnitFacadeClient(c *gc.C) {
    40  	caller := &stubAPI{Stub: s.stub}
    41  	doer := &stubAPI{Stub: s.stub}
    42  
    43  	cl := client.NewUnitFacadeClient(caller, doer)
    44  
    45  	s.stub.CheckNoCalls(c)
    46  	c.Check(cl.FacadeCaller, gc.Equals, caller)
    47  	c.Check(cl.HTTPClient, gc.Equals, doer)
    48  }
    49  
    50  func (s *UnitFacadeClientSuite) TestGetResource(c *gc.C) {
    51  	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
    52  	s.api.setResource(opened.Resource, opened)
    53  	cl := client.NewUnitFacadeClient(s.api, s.api)
    54  
    55  	info, content, err := cl.GetResource("spam")
    56  	c.Assert(err, jc.ErrorIsNil)
    57  
    58  	s.stub.CheckCallNames(c, "Do", "FacadeCall")
    59  	c.Check(info, jc.DeepEquals, opened.Resource)
    60  	c.Check(content, jc.DeepEquals, opened)
    61  }
    62  
    63  func (s *UnitFacadeClientSuite) TestUnitDoer(c *gc.C) {
    64  	req, err := http.NewRequest("GET", "/resources/eggs", nil)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	body := filetesting.NewStubFile(s.stub, nil)
    67  	var resp *http.Response
    68  	doer := client.NewUnitHTTPClient(s.api, "spam/1")
    69  
    70  	err = doer.Do(req, body, &resp)
    71  	c.Assert(err, jc.ErrorIsNil)
    72  
    73  	s.stub.CheckCallNames(c, "Do")
    74  	//s.stub.CheckCall(c, 0, "Do", expected, body, resp)
    75  	c.Check(req.URL.Path, gc.Equals, "/units/spam/1/resources/eggs")
    76  }
    77  
    78  type stubAPI struct {
    79  	*testing.Stub
    80  
    81  	ReturnFacadeCall private.ResourcesResult
    82  	ReturnUnit       string
    83  	ReturnDo         *http.Response
    84  }
    85  
    86  func (s *stubAPI) setResource(info resource.Resource, reader io.ReadCloser) {
    87  	s.ReturnFacadeCall = private.ResourcesResult{
    88  		Resources: []private.ResourceResult{{
    89  			Resource: api.Resource2API(info),
    90  		}},
    91  	}
    92  	s.ReturnDo = &http.Response{
    93  		Body: reader,
    94  	}
    95  }
    96  
    97  func (s *stubAPI) FacadeCall(request string, params, response interface{}) error {
    98  	s.AddCall("FacadeCall", params, response)
    99  	if err := s.NextErr(); err != nil {
   100  		return errors.Trace(err)
   101  	}
   102  
   103  	resp := response.(*private.ResourcesResult)
   104  	*resp = s.ReturnFacadeCall
   105  	return nil
   106  }
   107  
   108  func (s *stubAPI) Unit() string {
   109  	s.AddCall("Unit")
   110  	s.NextErr() // Pop one off.
   111  
   112  	return s.ReturnUnit
   113  }
   114  
   115  func (s *stubAPI) Do(req *http.Request, body io.ReadSeeker, response interface{}) error {
   116  	s.AddCall("Do", req, body, response)
   117  	if err := s.NextErr(); err != nil {
   118  		return errors.Trace(err)
   119  	}
   120  
   121  	resp := response.(**http.Response)
   122  	*resp = s.ReturnDo
   123  	return nil
   124  }