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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package server_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  
    12  	"github.com/juju/juju/apiserver/common"
    13  	"github.com/juju/juju/apiserver/params"
    14  	"github.com/juju/juju/resource"
    15  	"github.com/juju/juju/resource/api"
    16  	"github.com/juju/juju/resource/api/private"
    17  	"github.com/juju/juju/resource/api/private/server"
    18  	"github.com/juju/juju/resource/resourcetesting"
    19  )
    20  
    21  var _ = gc.Suite(&UnitFacadeSuite{})
    22  
    23  type UnitFacadeSuite struct {
    24  	testing.IsolationSuite
    25  
    26  	stub *testing.Stub
    27  }
    28  
    29  func (s *UnitFacadeSuite) SetUpTest(c *gc.C) {
    30  	s.IsolationSuite.SetUpTest(c)
    31  
    32  	s.stub = &testing.Stub{}
    33  }
    34  
    35  func (s *UnitFacadeSuite) TestNewUnitFacade(c *gc.C) {
    36  	expected := &stubUnitDataStore{Stub: s.stub}
    37  
    38  	uf := server.NewUnitFacade(expected)
    39  
    40  	s.stub.CheckNoCalls(c)
    41  	c.Check(uf.DataStore, gc.Equals, expected)
    42  }
    43  
    44  func (s *UnitFacadeSuite) TestGetResourceInfoOkay(c *gc.C) {
    45  	opened1 := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
    46  	res1 := opened1.Resource
    47  	opened2 := resourcetesting.NewResource(c, s.stub, "eggs", "a-application", "other data")
    48  	res2 := opened2.Resource
    49  	store := &stubUnitDataStore{Stub: s.stub}
    50  	store.ReturnListResources = resource.ServiceResources{
    51  		Resources: []resource.Resource{res1, res2},
    52  	}
    53  	uf := server.UnitFacade{DataStore: store}
    54  
    55  	results, err := uf.GetResourceInfo(private.ListResourcesArgs{
    56  		ResourceNames: []string{"spam", "eggs"},
    57  	})
    58  	c.Assert(err, jc.ErrorIsNil)
    59  
    60  	s.stub.CheckCallNames(c, "ListResources")
    61  	c.Check(results, jc.DeepEquals, private.ResourcesResult{
    62  		Resources: []private.ResourceResult{{
    63  			Resource: api.Resource2API(res1),
    64  		}, {
    65  			Resource: api.Resource2API(res2),
    66  		}},
    67  	})
    68  }
    69  
    70  func (s *UnitFacadeSuite) TestGetResourceInfoEmpty(c *gc.C) {
    71  	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
    72  	store := &stubUnitDataStore{Stub: s.stub}
    73  	store.ReturnListResources = resource.ServiceResources{
    74  		Resources: []resource.Resource{opened.Resource},
    75  	}
    76  	uf := server.UnitFacade{DataStore: store}
    77  
    78  	results, err := uf.GetResourceInfo(private.ListResourcesArgs{
    79  		ResourceNames: []string{},
    80  	})
    81  	c.Assert(err, jc.ErrorIsNil)
    82  
    83  	s.stub.CheckCallNames(c, "ListResources")
    84  	c.Check(results, jc.DeepEquals, private.ResourcesResult{
    85  		Resources: []private.ResourceResult{},
    86  	})
    87  }
    88  
    89  func (s *UnitFacadeSuite) TestGetResourceInfoNotFound(c *gc.C) {
    90  	opened := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
    91  	store := &stubUnitDataStore{Stub: s.stub}
    92  	store.ReturnListResources = resource.ServiceResources{
    93  		Resources: []resource.Resource{opened.Resource},
    94  	}
    95  	uf := server.UnitFacade{DataStore: store}
    96  
    97  	results, err := uf.GetResourceInfo(private.ListResourcesArgs{
    98  		ResourceNames: []string{"eggs"},
    99  	})
   100  	c.Assert(err, jc.ErrorIsNil)
   101  
   102  	s.stub.CheckCallNames(c, "ListResources")
   103  	c.Check(results, jc.DeepEquals, private.ResourcesResult{
   104  		Resources: []private.ResourceResult{{
   105  			ErrorResult: params.ErrorResult{
   106  				Error: common.ServerError(errors.NotFoundf(`resource "eggs"`)),
   107  			},
   108  		}},
   109  	})
   110  }