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