github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/resource/charmresources_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(&CharmResourcesSuite{}) 21 22 type CharmResourcesSuite struct { 23 testing.IsolationSuite 24 25 stub *testing.Stub 26 client *stubCharmStore 27 } 28 29 func (s *CharmResourcesSuite) 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 *CharmResourcesSuite) TestInfo(c *gc.C) { 37 var command resourcecmd.CharmResourcesCommand 38 info := command.Info() 39 40 c.Check(info, jc.DeepEquals, &jujucmd.Info{ 41 Name: "charm-resources", 42 Args: "<charm>", 43 Purpose: "Display the resources for a charm in the charm store.", 44 Aliases: []string{"list-charm-resources"}, 45 Doc: ` 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 FlagKnownAs: "option", 62 ShowSuperFlags: []string{"show-log", "debug", "logging-config", "verbose", "quiet", "h", "help"}, 63 }) 64 } 65 66 func (s *CharmResourcesSuite) TestOkay(c *gc.C) { 67 resources := newCharmResources(c, 68 "website:.tgz of your website", 69 "music:mp3 of your backing vocals", 70 ) 71 resources[0].Revision = 2 72 s.client.ReturnListResources = [][]charmresource.Resource{resources} 73 74 command := resourcecmd.NewCharmResourcesCommandForTest(s.client) 75 code, stdout, stderr := runCmd(c, command, "cs:a-charm") 76 c.Check(code, gc.Equals, 0) 77 78 c.Check(stdout, gc.Equals, ` 79 Resource Revision 80 music 1 81 website 2 82 83 `[1:]) 84 c.Check(stderr, gc.Equals, "") 85 s.stub.CheckCallNames(c, 86 "ListResources", 87 ) 88 s.stub.CheckCall(c, 0, "ListResources", []charmstore.CharmID{ 89 { 90 URL: charm.MustParseURL("cs:a-charm"), 91 Channel: "stable", 92 }, 93 }) 94 } 95 96 func (s *CharmResourcesSuite) TestNoResources(c *gc.C) { 97 s.client.ReturnListResources = [][]charmresource.Resource{{}} 98 99 command := resourcecmd.NewCharmResourcesCommandForTest(s.client) 100 code, stdout, stderr := runCmd(c, command, "cs:a-charm") 101 c.Check(code, gc.Equals, 0) 102 103 c.Check(stderr, gc.Equals, "No resources to display.\n") 104 c.Check(stdout, gc.Equals, "") 105 s.stub.CheckCallNames(c, "ListResources") 106 } 107 108 func (s *CharmResourcesSuite) TestOutputFormats(c *gc.C) { 109 fp1, err := charmresource.GenerateFingerprint(strings.NewReader("abc")) 110 c.Assert(err, jc.ErrorIsNil) 111 fp2, err := charmresource.GenerateFingerprint(strings.NewReader("xyz")) 112 c.Assert(err, jc.ErrorIsNil) 113 resources := []charmresource.Resource{ 114 charmRes(c, "website", ".tgz", ".tgz of your website", string(fp1.Bytes())), 115 charmRes(c, "music", ".mp3", "mp3 of your backing vocals", string(fp2.Bytes())), 116 } 117 s.client.ReturnListResources = [][]charmresource.Resource{resources} 118 119 formats := map[string]string{ 120 "tabular": ` 121 Resource Revision 122 music 1 123 website 1 124 125 `[1:], 126 "yaml": ` 127 - name: music 128 type: file 129 path: music.mp3 130 description: mp3 of your backing vocals 131 revision: 1 132 fingerprint: b0ea2a0f90267a8bd32848c65d7a61569a136f4e421b56127b6374b10a576d29e09294e620b4dcdee40f602115104bd5 133 size: 48 134 origin: store 135 - name: website 136 type: file 137 path: website.tgz 138 description: .tgz of your website 139 revision: 1 140 fingerprint: 73100f01cf258766906c34a30f9a486f07259c627ea0696d97c4582560447f59a6df4a7cf960708271a30324b1481ef4 141 size: 48 142 origin: store 143 `[1:], 144 "json": strings.Replace(""+ 145 "["+ 146 " {"+ 147 ` "name":"music",`+ 148 ` "type":"file",`+ 149 ` "path":"music.mp3",`+ 150 ` "description":"mp3 of your backing vocals",`+ 151 ` "revision":1,`+ 152 ` "fingerprint":"b0ea2a0f90267a8bd32848c65d7a61569a136f4e421b56127b6374b10a576d29e09294e620b4dcdee40f602115104bd5",`+ 153 ` "size":48,`+ 154 ` "origin":"store"`+ 155 " },{"+ 156 ` "name":"website",`+ 157 ` "type":"file",`+ 158 ` "path":"website.tgz",`+ 159 ` "description":".tgz of your website",`+ 160 ` "revision":1,`+ 161 ` "fingerprint":"73100f01cf258766906c34a30f9a486f07259c627ea0696d97c4582560447f59a6df4a7cf960708271a30324b1481ef4",`+ 162 ` "size":48,`+ 163 ` "origin":"store"`+ 164 " }"+ 165 "]\n", 166 " ", "", -1), 167 } 168 for format, expected := range formats { 169 c.Logf("checking format %q", format) 170 command := resourcecmd.NewCharmResourcesCommandForTest(s.client) 171 args := []string{ 172 "--format", format, 173 "cs:a-charm", 174 } 175 code, stdout, stderr := runCmd(c, command, args...) 176 c.Check(code, gc.Equals, 0) 177 178 c.Check(stdout, gc.Equals, expected) 179 c.Check(stderr, gc.Equals, "") 180 } 181 } 182 183 func (s *CharmResourcesSuite) TestChannelFlag(c *gc.C) { 184 fp1, err := charmresource.GenerateFingerprint(strings.NewReader("abc")) 185 c.Assert(err, jc.ErrorIsNil) 186 fp2, err := charmresource.GenerateFingerprint(strings.NewReader("xyz")) 187 c.Assert(err, jc.ErrorIsNil) 188 resources := []charmresource.Resource{ 189 charmRes(c, "website", ".tgz", ".tgz of your website", string(fp1.Bytes())), 190 charmRes(c, "music", ".mp3", "mp3 of your backing vocals", string(fp2.Bytes())), 191 } 192 s.client.ReturnListResources = [][]charmresource.Resource{resources} 193 command := resourcecmd.NewCharmResourcesCommandForTest(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(resourcecmd.CharmResourcesCommandChannel(command), gc.Equals, "development") 203 }