github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/resource/cmd/list_charm_resources_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cmd
     5  
     6  import (
     7  	"strings"
     8  
     9  	jujucmd "github.com/juju/cmd"
    10  	"github.com/juju/juju/charmstore"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	"gopkg.in/juju/charm.v6-unstable"
    15  	charmresource "gopkg.in/juju/charm.v6-unstable/resource"
    16  )
    17  
    18  var _ = gc.Suite(&ListCharmSuite{})
    19  
    20  type ListCharmSuite struct {
    21  	testing.IsolationSuite
    22  
    23  	stub   *testing.Stub
    24  	client *stubCharmStore
    25  }
    26  
    27  func (s *ListCharmSuite) SetUpTest(c *gc.C) {
    28  	s.IsolationSuite.SetUpTest(c)
    29  
    30  	s.stub = &testing.Stub{}
    31  	s.client = &stubCharmStore{stub: s.stub}
    32  }
    33  
    34  func (s *ListCharmSuite) TestInfo(c *gc.C) {
    35  	var command ListCharmResourcesCommand
    36  	info := command.Info()
    37  
    38  	c.Check(info, jc.DeepEquals, &jujucmd.Info{
    39  		Name:    "resources",
    40  		Args:    "<charm>",
    41  		Purpose: "display the resources for a charm in the charm store",
    42  		Doc: `
    43  This command will report the resources for a charm in the charm store.
    44  
    45  <charm> can be a charm URL, or an unambiguously condensed form of it,
    46  just like the deploy command. So the following forms will be accepted:
    47  
    48  For cs:trusty/mysql
    49    mysql
    50    trusty/mysql
    51  
    52  For cs:~user/trusty/mysql
    53    cs:~user/mysql
    54  
    55  Where the series is not supplied, the series from your local host is used.
    56  Thus the above examples imply that the local series is trusty.
    57  `,
    58  		Aliases: []string{"list-resources"},
    59  	})
    60  }
    61  
    62  func (s *ListCharmSuite) TestOkay(c *gc.C) {
    63  	resources := newCharmResources(c,
    64  		"website:.tgz of your website",
    65  		"music:mp3 of your backing vocals",
    66  	)
    67  	resources[0].Revision = 2
    68  	s.client.ReturnListResources = [][]charmresource.Resource{resources}
    69  
    70  	command := NewListCharmResourcesCommand()
    71  	command.ResourceLister = s.client
    72  	code, stdout, stderr := runCmd(c, command, "cs:a-charm")
    73  	c.Check(code, gc.Equals, 0)
    74  
    75  	c.Check(stdout, gc.Equals, `
    76  RESOURCE  REVISION
    77  website   2
    78  music     1
    79  
    80  `[1:])
    81  	c.Check(stderr, gc.Equals, "")
    82  	s.stub.CheckCallNames(c,
    83  		"ListResources",
    84  	)
    85  	s.stub.CheckCall(c, 0, "ListResources", []charmstore.CharmID{
    86  		{
    87  			URL:     charm.MustParseURL("cs:a-charm"),
    88  			Channel: "stable",
    89  		},
    90  	})
    91  }
    92  
    93  func (s *ListCharmSuite) TestNoResources(c *gc.C) {
    94  	s.client.ReturnListResources = [][]charmresource.Resource{{}}
    95  
    96  	command := NewListCharmResourcesCommand()
    97  	command.ResourceLister = s.client
    98  	code, stdout, stderr := runCmd(c, command, "cs:a-charm")
    99  	c.Check(code, gc.Equals, 0)
   100  
   101  	c.Check(stderr, gc.Equals, "No resources to display.\n")
   102  	c.Check(stdout, gc.Equals, "")
   103  	s.stub.CheckCallNames(c, "ListResources")
   104  }
   105  
   106  func (s *ListCharmSuite) TestOutputFormats(c *gc.C) {
   107  	fp1, err := charmresource.GenerateFingerprint(strings.NewReader("abc"))
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	fp2, err := charmresource.GenerateFingerprint(strings.NewReader("xyz"))
   110  	c.Assert(err, jc.ErrorIsNil)
   111  	resources := []charmresource.Resource{
   112  		charmRes(c, "website", ".tgz", ".tgz of your website", string(fp1.Bytes())),
   113  		charmRes(c, "music", ".mp3", "mp3 of your backing vocals", string(fp2.Bytes())),
   114  	}
   115  	s.client.ReturnListResources = [][]charmresource.Resource{resources}
   116  
   117  	formats := map[string]string{
   118  		"tabular": `
   119  RESOURCE  REVISION
   120  website   1
   121  music     1
   122  
   123  `[1:],
   124  		"yaml": `
   125  - name: website
   126    type: file
   127    path: website.tgz
   128    description: .tgz of your website
   129    revision: 1
   130    fingerprint: 73100f01cf258766906c34a30f9a486f07259c627ea0696d97c4582560447f59a6df4a7cf960708271a30324b1481ef4
   131    size: 48
   132    origin: store
   133  - name: music
   134    type: file
   135    path: music.mp3
   136    description: mp3 of your backing vocals
   137    revision: 1
   138    fingerprint: b0ea2a0f90267a8bd32848c65d7a61569a136f4e421b56127b6374b10a576d29e09294e620b4dcdee40f602115104bd5
   139    size: 48
   140    origin: store
   141  `[1:],
   142  		"json": strings.Replace(""+
   143  			"["+
   144  			"  {"+
   145  			`    "name":"website",`+
   146  			`    "type":"file",`+
   147  			`    "path":"website.tgz",`+
   148  			`    "description":".tgz of your website",`+
   149  			`    "revision":1,`+
   150  			`    "fingerprint":"73100f01cf258766906c34a30f9a486f07259c627ea0696d97c4582560447f59a6df4a7cf960708271a30324b1481ef4",`+
   151  			`    "size":48,`+
   152  			`    "origin":"store"`+
   153  			"  },{"+
   154  			`    "name":"music",`+
   155  			`    "type":"file",`+
   156  			`    "path":"music.mp3",`+
   157  			`    "description":"mp3 of your backing vocals",`+
   158  			`    "revision":1,`+
   159  			`    "fingerprint":"b0ea2a0f90267a8bd32848c65d7a61569a136f4e421b56127b6374b10a576d29e09294e620b4dcdee40f602115104bd5",`+
   160  			`    "size":48,`+
   161  			`    "origin":"store"`+
   162  			"  }"+
   163  			"]\n",
   164  			"  ", "", -1),
   165  	}
   166  	for format, expected := range formats {
   167  		c.Logf("checking format %q", format)
   168  		command := NewListCharmResourcesCommand()
   169  		command.ResourceLister = s.client
   170  		args := []string{
   171  			"--format", format,
   172  			"cs:a-charm",
   173  		}
   174  		code, stdout, stderr := runCmd(c, command, args...)
   175  		c.Check(code, gc.Equals, 0)
   176  
   177  		c.Check(stdout, gc.Equals, expected)
   178  		c.Check(stderr, gc.Equals, "")
   179  	}
   180  }
   181  
   182  func (s *ListCharmSuite) TestChannelFlag(c *gc.C) {
   183  	fp1, err := charmresource.GenerateFingerprint(strings.NewReader("abc"))
   184  	c.Assert(err, jc.ErrorIsNil)
   185  	fp2, err := charmresource.GenerateFingerprint(strings.NewReader("xyz"))
   186  	c.Assert(err, jc.ErrorIsNil)
   187  	resources := []charmresource.Resource{
   188  		charmRes(c, "website", ".tgz", ".tgz of your website", string(fp1.Bytes())),
   189  		charmRes(c, "music", ".mp3", "mp3 of your backing vocals", string(fp2.Bytes())),
   190  	}
   191  	s.client.ReturnListResources = [][]charmresource.Resource{resources}
   192  	command := NewListCharmResourcesCommand()
   193  	command.ResourceLister = s.client
   194  
   195  	code, _, stderr := runCmd(c, command,
   196  		"--channel", "development",
   197  		"cs:a-charm",
   198  	)
   199  
   200  	c.Check(code, gc.Equals, 0)
   201  	c.Check(stderr, gc.Equals, "")
   202  	c.Check(command.channel, gc.Equals, "development")
   203  }