github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/resource/service_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resource_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	charmresource "gopkg.in/juju/charm.v6-unstable/resource"
    11  
    12  	"github.com/juju/juju/resource"
    13  	"github.com/juju/juju/resource/resourcetesting"
    14  )
    15  
    16  type ServiceResourcesSuite struct {
    17  	testing.IsolationSuite
    18  }
    19  
    20  var _ = gc.Suite(&ServiceResourcesSuite{})
    21  
    22  func (s *ServiceResourcesSuite) TestUpdatesUploaded(c *gc.C) {
    23  	csRes := newStoreResource(c, "spam", "a-application", 2)
    24  	res := csRes // a copy
    25  	res.Origin = charmresource.OriginUpload
    26  	sr := resource.ServiceResources{
    27  		Resources: []resource.Resource{
    28  			res,
    29  		},
    30  		CharmStoreResources: []charmresource.Resource{
    31  			csRes.Resource,
    32  		},
    33  	}
    34  
    35  	updates, err := sr.Updates()
    36  	c.Assert(err, jc.ErrorIsNil)
    37  
    38  	c.Check(updates, gc.HasLen, 0)
    39  }
    40  
    41  func (s *ServiceResourcesSuite) TestUpdatesDifferent(c *gc.C) {
    42  	spam := newStoreResource(c, "spam", "a-application", 2)
    43  	eggs := newStoreResource(c, "eggs", "a-application", 3)
    44  	expected := eggs.Resource
    45  	expected.Revision += 1
    46  	sr := resource.ServiceResources{
    47  		Resources: []resource.Resource{
    48  			spam,
    49  			eggs,
    50  		},
    51  		CharmStoreResources: []charmresource.Resource{
    52  			spam.Resource,
    53  			expected,
    54  		},
    55  	}
    56  
    57  	updates, err := sr.Updates()
    58  	c.Assert(err, jc.ErrorIsNil)
    59  
    60  	c.Check(updates, jc.DeepEquals, []charmresource.Resource{expected})
    61  }
    62  
    63  func (s *ServiceResourcesSuite) TestUpdatesBadOrdering(c *gc.C) {
    64  	spam := newStoreResource(c, "spam", "a-application", 2)
    65  	eggs := newStoreResource(c, "eggs", "a-application", 3)
    66  	expected := eggs.Resource
    67  	expected.Revision += 1
    68  	sr := resource.ServiceResources{
    69  		Resources: []resource.Resource{
    70  			spam,
    71  			eggs,
    72  		},
    73  		CharmStoreResources: []charmresource.Resource{
    74  			expected,
    75  			spam.Resource,
    76  		},
    77  	}
    78  
    79  	updates, err := sr.Updates()
    80  	c.Assert(err, jc.ErrorIsNil)
    81  
    82  	c.Check(updates, jc.DeepEquals, []charmresource.Resource{expected})
    83  }
    84  
    85  func (s *ServiceResourcesSuite) TestUpdatesNone(c *gc.C) {
    86  	spam := newStoreResource(c, "spam", "a-application", 2)
    87  	eggs := newStoreResource(c, "eggs", "a-application", 3)
    88  	sr := resource.ServiceResources{
    89  		Resources: []resource.Resource{
    90  			spam,
    91  			eggs,
    92  		},
    93  		CharmStoreResources: []charmresource.Resource{
    94  			spam.Resource,
    95  			eggs.Resource,
    96  		},
    97  	}
    98  
    99  	updates, err := sr.Updates()
   100  	c.Assert(err, jc.ErrorIsNil)
   101  
   102  	c.Check(updates, gc.HasLen, 0)
   103  }
   104  
   105  func newStoreResource(c *gc.C, name, applicationID string, revision int) resource.Resource {
   106  	content := name
   107  	opened := resourcetesting.NewResource(c, nil, name, applicationID, content)
   108  	res := opened.Resource
   109  	res.Origin = charmresource.OriginStore
   110  	res.Revision = revision
   111  	err := res.Validate()
   112  	c.Assert(err, jc.ErrorIsNil)
   113  	return res
   114  }