github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/resource/list_charm_resources_test.go (about)

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