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