github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/bundle_resources_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service_test
     5  
     6  import (
     7  	"sort"
     8  	"strings"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	charmresource "gopkg.in/juju/charm.v6-unstable/resource"
    13  
    14  	"github.com/juju/juju/cmd/juju/service"
    15  	"github.com/juju/juju/component/all"
    16  	"github.com/juju/juju/resource"
    17  	"github.com/juju/juju/testcharms"
    18  )
    19  
    20  func init() {
    21  	if err := all.RegisterForServer(); err != nil {
    22  		panic(err)
    23  	}
    24  }
    25  
    26  type ResourcesBundleSuite struct {
    27  	service.BundleDeployCharmStoreSuite
    28  }
    29  
    30  var _ = gc.Suite(&ResourcesBundleSuite{})
    31  
    32  func (s *ResourcesBundleSuite) TestDeployBundleResources(c *gc.C) {
    33  	testcharms.UploadCharm(c, s.Client(), "trusty/starsay-42", "starsay")
    34  	bundleMeta := `
    35          services:
    36              starsay:
    37                  charm: cs:starsay
    38                  num_units: 1
    39                  resources:
    40                      store-resource: 0
    41                      install-resource: 0
    42                      upload-resource: 0
    43      `
    44  	output, err := s.DeployBundleYAML(c, bundleMeta)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  
    47  	lines := strings.Split(output, "\n")
    48  	expectedLines := strings.Split(strings.TrimSpace(`
    49  added charm cs:trusty/starsay-42
    50  service starsay deployed (charm cs:trusty/starsay-42 with the charm series "trusty")
    51  added resource install-resource
    52  added resource store-resource
    53  added resource upload-resource
    54  added starsay/0 unit to new machine
    55  deployment of bundle "local:bundle/example-0" completed
    56      `), "\n")
    57  	c.Check(lines, gc.HasLen, len(expectedLines))
    58  	c.Check(lines[0], gc.Equals, expectedLines[0])
    59  	c.Check(lines[1], gc.Equals, expectedLines[1])
    60  	// The "added resource" lines are checked after we sort since
    61  	// the ordering of those lines is unknown.
    62  	c.Check(lines[5], gc.Equals, expectedLines[5])
    63  	c.Check(lines[6], gc.Equals, expectedLines[6])
    64  	sort.Strings(lines)
    65  	sort.Strings(expectedLines)
    66  	c.Check(lines, jc.DeepEquals, expectedLines)
    67  	s.checkResources(c, "starsay", []resource.Resource{{
    68  		Resource: charmresource.Resource{
    69  			Meta: charmresource.Meta{
    70  				Name:        "install-resource",
    71  				Type:        charmresource.TypeFile,
    72  				Path:        "gotta-have-it.txt",
    73  				Description: "get things started",
    74  			},
    75  			Origin:      charmresource.OriginStore,
    76  			Revision:    0,
    77  			Fingerprint: resourceHash("install-resource content"),
    78  			Size:        int64(len("install-resource content")),
    79  		},
    80  		ID:        "starsay/install-resource",
    81  		ServiceID: "starsay",
    82  	}, {
    83  		Resource: charmresource.Resource{
    84  			Meta: charmresource.Meta{
    85  				Name:        "store-resource",
    86  				Type:        charmresource.TypeFile,
    87  				Path:        "filename.tgz",
    88  				Description: "One line that is useful when operators need to push it.",
    89  			},
    90  			Origin:      charmresource.OriginStore,
    91  			Fingerprint: resourceHash("store-resource content"),
    92  			Size:        int64(len("store-resource content")),
    93  			Revision:    0,
    94  		},
    95  		ID:        "starsay/store-resource",
    96  		ServiceID: "starsay",
    97  	}, {
    98  		Resource: charmresource.Resource{
    99  			Meta: charmresource.Meta{
   100  				Name:        "upload-resource",
   101  				Type:        charmresource.TypeFile,
   102  				Path:        "somename.xml",
   103  				Description: "Who uses xml anymore?",
   104  			},
   105  			Origin:      charmresource.OriginStore,
   106  			Fingerprint: resourceHash("upload-resource content"),
   107  			Size:        int64(len("upload-resource content")),
   108  			Revision:    0,
   109  		},
   110  		ID:        "starsay/upload-resource",
   111  		ServiceID: "starsay",
   112  	}})
   113  }
   114  
   115  func (s *ResourcesBundleSuite) checkResources(c *gc.C, serviceName string, expected []resource.Resource) {
   116  	_, err := s.State.Service("starsay")
   117  	c.Check(err, jc.ErrorIsNil)
   118  	st, err := s.State.Resources()
   119  	c.Assert(err, jc.ErrorIsNil)
   120  	svcResources, err := st.ListResources("starsay")
   121  	c.Assert(err, jc.ErrorIsNil)
   122  	resources := svcResources.Resources
   123  	resource.Sort(resources)
   124  	c.Assert(resources, jc.DeepEquals, expected)
   125  }