github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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/cmd/cmdtesting"
    12  	"github.com/juju/errors"
    13  	"github.com/juju/testing"
    14  	jc "github.com/juju/testing/checkers"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/payload"
    18  	"github.com/juju/juju/payload/status"
    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  		FlagKnownAs:    "option",
    71  		ShowSuperFlags: []string{"show-log", "debug", "logging-config", "verbose", "quiet", "h", "help"},
    72  	})
    73  }
    74  
    75  func (s *listSuite) TestOkay(c *gc.C) {
    76  	p1 := status.NewPayload("spam", "a-application", 1, 0)
    77  	p1.Labels = []string{"a-tag"}
    78  	p2 := status.NewPayload("eggs", "another-application", 2, 1)
    79  	s.client.payloads = append(s.client.payloads, p1, p2)
    80  
    81  	command := status.NewListCommand(s.newAPIClient)
    82  	code, stdout, stderr := runList(c, command)
    83  	c.Assert(code, gc.Equals, 0)
    84  
    85  	c.Check(stdout, gc.Equals, `
    86  [Unit Payloads]
    87  Unit                   Machine  Payload class  Status   Type    Id      Tags   
    88  a-application/0        1        spam           running  docker  idspam  a-tag  
    89  another-application/1  2        eggs           running  docker  ideggs         
    90  
    91  `[1:])
    92  	c.Check(stderr, gc.Equals, "")
    93  }
    94  
    95  func (s *listSuite) TestNoPayloads(c *gc.C) {
    96  	command := status.NewListCommand(s.newAPIClient)
    97  	code, stdout, stderr := runList(c, command)
    98  	c.Assert(code, gc.Equals, 0)
    99  
   100  	c.Check(stderr, gc.Equals, "No payloads to display.\n")
   101  	c.Check(stdout, gc.Equals, "")
   102  }
   103  
   104  func (s *listSuite) TestPatternsOkay(c *gc.C) {
   105  	p1 := status.NewPayload("spam", "a-application", 1, 0)
   106  	p1.Labels = []string{"a-tag"}
   107  	p2 := status.NewPayload("eggs", "another-application", 2, 1)
   108  	p2.Labels = []string{"a-tag"}
   109  	s.client.payloads = append(s.client.payloads, p1, p2)
   110  
   111  	command := status.NewListCommand(s.newAPIClient)
   112  	args := []string{
   113  		"a-tag",
   114  		"other",
   115  		"some-application/1",
   116  	}
   117  	code, stdout, stderr := runList(c, command, args...)
   118  	c.Assert(code, gc.Equals, 0)
   119  
   120  	c.Check(stdout, gc.Equals, `
   121  [Unit Payloads]
   122  Unit                   Machine  Payload class  Status   Type    Id      Tags   
   123  a-application/0        1        spam           running  docker  idspam  a-tag  
   124  another-application/1  2        eggs           running  docker  ideggs  a-tag  
   125  
   126  `[1:])
   127  	c.Check(stderr, gc.Equals, "")
   128  	s.stub.CheckCalls(c, []testing.StubCall{{
   129  		FuncName: "newAPIClient",
   130  		Args: []interface{}{
   131  			command,
   132  		},
   133  	}, {
   134  		FuncName: "List",
   135  		Args: []interface{}{
   136  			[]string{
   137  				"a-tag",
   138  				"other",
   139  				"some-application/1",
   140  			},
   141  		},
   142  	}, {
   143  		FuncName: "Close",
   144  	}})
   145  }
   146  
   147  func (s *listSuite) TestOutputFormats(c *gc.C) {
   148  	p1 := status.NewPayload("spam", "a-application", 1, 0)
   149  	p1.Labels = []string{"a-tag"}
   150  	p2 := status.NewPayload("eggs", "another-application", 2, 1)
   151  	s.client.payloads = append(s.client.payloads,
   152  		p1,
   153  		p2,
   154  	)
   155  
   156  	formats := map[string]string{
   157  		"tabular": `
   158  [Unit Payloads]
   159  Unit                   Machine  Payload class  Status   Type    Id      Tags   
   160  a-application/0        1        spam           running  docker  idspam  a-tag  
   161  another-application/1  2        eggs           running  docker  ideggs         
   162  
   163  `[1:],
   164  		"yaml": `
   165  - unit: a-application/0
   166    machine: "1"
   167    id: idspam
   168    type: docker
   169    payload-class: spam
   170    tags:
   171    - a-tag
   172    status: running
   173  - unit: another-application/1
   174    machine: "2"
   175    id: ideggs
   176    type: docker
   177    payload-class: eggs
   178    status: running
   179  `[1:],
   180  		"json": strings.Replace(""+
   181  			"["+
   182  			" {"+
   183  			`  "unit":"a-application/0",`+
   184  			`  "machine":"1",`+
   185  			`  "id":"idspam",`+
   186  			`  "type":"docker",`+
   187  			`  "payload-class":"spam",`+
   188  			`  "tags":["a-tag"],`+
   189  			`  "status":"running"`+
   190  			" },{"+
   191  			`  "unit":"another-application/1",`+
   192  			`  "machine":"2",`+
   193  			`  "id":"ideggs",`+
   194  			`  "type":"docker",`+
   195  			`  "payload-class":"eggs",`+
   196  			`  "status":"running"`+
   197  			" }"+
   198  			"]\n",
   199  			" ", "", -1),
   200  	}
   201  	for format, expected := range formats {
   202  		command := status.NewListCommand(s.newAPIClient)
   203  		args := []string{
   204  			"--format", format,
   205  		}
   206  		code, stdout, stderr := runList(c, command, args...)
   207  		c.Assert(code, gc.Equals, 0)
   208  
   209  		c.Check(stdout, gc.Equals, expected)
   210  		c.Check(stderr, gc.Equals, "")
   211  	}
   212  }
   213  
   214  func runList(c *gc.C, command *status.ListCommand, args ...string) (int, string, string) {
   215  	ctx := cmdtesting.Context(c)
   216  	code := cmd.Main(command, ctx, args)
   217  	stdout := ctx.Stdout.(*bytes.Buffer).Bytes()
   218  	stderr := ctx.Stderr.(*bytes.Buffer).Bytes()
   219  	return code, string(stdout), string(stderr)
   220  }
   221  
   222  type stubClient struct {
   223  	stub     *testing.Stub
   224  	payloads []payload.FullPayloadInfo
   225  }
   226  
   227  func (s *stubClient) ListFull(patterns ...string) ([]payload.FullPayloadInfo, error) {
   228  	s.stub.AddCall("List", patterns)
   229  	if err := s.stub.NextErr(); err != nil {
   230  		return nil, errors.Trace(err)
   231  	}
   232  
   233  	return s.payloads, nil
   234  }
   235  
   236  func (s *stubClient) Close() error {
   237  	s.stub.AddCall("Close")
   238  	if err := s.stub.NextErr(); err != nil {
   239  		return errors.Trace(err)
   240  	}
   241  
   242  	return nil
   243  }