github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/payload/status/list_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package status_test 5 6 import ( 7 "bytes" 8 "strings" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/payload" 17 "github.com/juju/juju/payload/status" 18 coretesting "github.com/juju/juju/testing" 19 ) 20 21 var _ = gc.Suite(&listSuite{}) 22 23 type listSuite struct { 24 testing.IsolationSuite 25 26 stub *testing.Stub 27 client *stubClient 28 } 29 30 func (s *listSuite) SetUpTest(c *gc.C) { 31 s.IsolationSuite.SetUpTest(c) 32 33 s.stub = &testing.Stub{} 34 s.client = &stubClient{stub: s.stub} 35 } 36 37 func (s *listSuite) newAPIClient(c *status.ListCommand) (status.ListAPI, error) { 38 s.stub.AddCall("newAPIClient", c) 39 if err := s.stub.NextErr(); err != nil { 40 return nil, errors.Trace(err) 41 } 42 43 return s.client, nil 44 } 45 46 func (s *listSuite) TestInfo(c *gc.C) { 47 var command status.ListCommand 48 info := command.Info() 49 50 c.Check(info, jc.DeepEquals, &cmd.Info{ 51 Name: "payloads", 52 Args: "[pattern ...]", 53 Purpose: "display status information about known payloads", 54 Doc: ` 55 This command will report on the runtime state of defined payloads. 56 57 When one or more pattern is given, Juju will limit the results to only 58 those payloads which match *any* of the provided patterns. Each pattern 59 will be checked against the following info in Juju: 60 61 - unit name 62 - machine id 63 - payload type 64 - payload class 65 - payload id 66 - payload tag 67 - payload status 68 `, 69 Aliases: []string{"list-payloads"}, 70 }) 71 } 72 73 func (s *listSuite) TestOkay(c *gc.C) { 74 p1 := status.NewPayload("spam", "a-application", 1, 0) 75 p1.Labels = []string{"a-tag"} 76 p2 := status.NewPayload("eggs", "another-application", 2, 1) 77 s.client.payloads = append(s.client.payloads, p1, p2) 78 79 command := status.NewListCommand(s.newAPIClient) 80 code, stdout, stderr := runList(c, command) 81 c.Assert(code, gc.Equals, 0) 82 83 c.Check(stdout, gc.Equals, ` 84 [Unit Payloads] 85 UNIT MACHINE PAYLOAD-CLASS STATUS TYPE ID TAGS 86 a-application/0 1 spam running docker idspam a-tag 87 another-application/1 2 eggs running docker ideggs 88 89 `[1:]) 90 c.Check(stderr, gc.Equals, "") 91 } 92 93 func (s *listSuite) TestNoPayloads(c *gc.C) { 94 command := status.NewListCommand(s.newAPIClient) 95 code, stdout, stderr := runList(c, command) 96 c.Assert(code, gc.Equals, 0) 97 98 c.Check(stderr, gc.Equals, "No payloads to display.\n") 99 c.Check(stdout, gc.Equals, "") 100 } 101 102 func (s *listSuite) TestPatternsOkay(c *gc.C) { 103 p1 := status.NewPayload("spam", "a-application", 1, 0) 104 p1.Labels = []string{"a-tag"} 105 p2 := status.NewPayload("eggs", "another-application", 2, 1) 106 p2.Labels = []string{"a-tag"} 107 s.client.payloads = append(s.client.payloads, p1, p2) 108 109 command := status.NewListCommand(s.newAPIClient) 110 args := []string{ 111 "a-tag", 112 "other", 113 "some-application/1", 114 } 115 code, stdout, stderr := runList(c, command, args...) 116 c.Assert(code, gc.Equals, 0) 117 118 c.Check(stdout, gc.Equals, ` 119 [Unit Payloads] 120 UNIT MACHINE PAYLOAD-CLASS STATUS TYPE ID TAGS 121 a-application/0 1 spam running docker idspam a-tag 122 another-application/1 2 eggs running docker ideggs a-tag 123 124 `[1:]) 125 c.Check(stderr, gc.Equals, "") 126 s.stub.CheckCalls(c, []testing.StubCall{{ 127 FuncName: "newAPIClient", 128 Args: []interface{}{ 129 command, 130 }, 131 }, { 132 FuncName: "List", 133 Args: []interface{}{ 134 []string{ 135 "a-tag", 136 "other", 137 "some-application/1", 138 }, 139 }, 140 }, { 141 FuncName: "Close", 142 }}) 143 } 144 145 func (s *listSuite) TestOutputFormats(c *gc.C) { 146 p1 := status.NewPayload("spam", "a-application", 1, 0) 147 p1.Labels = []string{"a-tag"} 148 p2 := status.NewPayload("eggs", "another-application", 2, 1) 149 s.client.payloads = append(s.client.payloads, 150 p1, 151 p2, 152 ) 153 154 formats := map[string]string{ 155 "tabular": ` 156 [Unit Payloads] 157 UNIT MACHINE PAYLOAD-CLASS STATUS TYPE ID TAGS 158 a-application/0 1 spam running docker idspam a-tag 159 another-application/1 2 eggs running docker ideggs 160 161 `[1:], 162 "yaml": ` 163 - unit: a-application/0 164 machine: "1" 165 id: idspam 166 type: docker 167 payload-class: spam 168 tags: 169 - a-tag 170 status: running 171 - unit: another-application/1 172 machine: "2" 173 id: ideggs 174 type: docker 175 payload-class: eggs 176 status: running 177 `[1:], 178 "json": strings.Replace(""+ 179 "["+ 180 " {"+ 181 ` "unit":"a-application/0",`+ 182 ` "machine":"1",`+ 183 ` "id":"idspam",`+ 184 ` "type":"docker",`+ 185 ` "payload-class":"spam",`+ 186 ` "tags":["a-tag"],`+ 187 ` "status":"running"`+ 188 " },{"+ 189 ` "unit":"another-application/1",`+ 190 ` "machine":"2",`+ 191 ` "id":"ideggs",`+ 192 ` "type":"docker",`+ 193 ` "payload-class":"eggs",`+ 194 ` "status":"running"`+ 195 " }"+ 196 "]\n", 197 " ", "", -1), 198 } 199 for format, expected := range formats { 200 command := status.NewListCommand(s.newAPIClient) 201 args := []string{ 202 "--format", format, 203 } 204 code, stdout, stderr := runList(c, command, args...) 205 c.Assert(code, gc.Equals, 0) 206 207 c.Check(stdout, gc.Equals, expected) 208 c.Check(stderr, gc.Equals, "") 209 } 210 } 211 212 func runList(c *gc.C, command *status.ListCommand, args ...string) (int, string, string) { 213 ctx := coretesting.Context(c) 214 code := cmd.Main(command, ctx, args) 215 stdout := ctx.Stdout.(*bytes.Buffer).Bytes() 216 stderr := ctx.Stderr.(*bytes.Buffer).Bytes() 217 return code, string(stdout), string(stderr) 218 } 219 220 type stubClient struct { 221 stub *testing.Stub 222 payloads []payload.FullPayloadInfo 223 } 224 225 func (s *stubClient) ListFull(patterns ...string) ([]payload.FullPayloadInfo, error) { 226 s.stub.AddCall("List", patterns) 227 if err := s.stub.NextErr(); err != nil { 228 return nil, errors.Trace(err) 229 } 230 231 return s.payloads, nil 232 } 233 234 func (s *stubClient) Close() error { 235 s.stub.AddCall("Close") 236 if err := s.stub.NextErr(); err != nil { 237 return errors.Trace(err) 238 } 239 240 return nil 241 }