github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/status-get_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"encoding/json"
     8  	"io/ioutil"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/cmd"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	goyaml "gopkg.in/yaml.v2"
    15  
    16  	"github.com/juju/juju/testing"
    17  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    18  )
    19  
    20  type statusGetSuite struct {
    21  	ContextSuite
    22  }
    23  
    24  var _ = gc.Suite(&statusGetSuite{})
    25  
    26  func (s *statusGetSuite) SetUpTest(c *gc.C) {
    27  	s.ContextSuite.SetUpTest(c)
    28  }
    29  
    30  var (
    31  	statusAttributes = map[string]interface{}{
    32  		"status":      "error",
    33  		"message":     "doing work",
    34  		"status-data": map[string]interface{}{"foo": "bar"},
    35  	}
    36  )
    37  
    38  var statusGetTests = []struct {
    39  	args   []string
    40  	format int
    41  	out    interface{}
    42  }{
    43  	{[]string{"--format", "json", "--include-data"}, formatJson, statusAttributes},
    44  	{[]string{"--format", "yaml"}, formatYaml, map[string]interface{}{"status": "error"}},
    45  }
    46  
    47  func setFakeStatus(ctx *Context) {
    48  	ctx.SetUnitStatus(jujuc.StatusInfo{
    49  		Status: statusAttributes["status"].(string),
    50  		Info:   statusAttributes["message"].(string),
    51  		Data:   statusAttributes["status-data"].(map[string]interface{}),
    52  	})
    53  }
    54  
    55  func setFakeServiceStatus(ctx *Context) {
    56  	ctx.info.Status.SetApplicationStatus(
    57  		jujuc.StatusInfo{
    58  			Status: "active",
    59  			Info:   "this is a service status",
    60  			Data:   nil,
    61  		},
    62  		[]jujuc.StatusInfo{{
    63  			Status: "active",
    64  			Info:   "this is a unit status",
    65  			Data:   nil,
    66  		}},
    67  	)
    68  }
    69  
    70  func (s *statusGetSuite) TestOutputFormatJustStatus(c *gc.C) {
    71  	for i, t := range statusGetTests {
    72  		c.Logf("test %d: %#v", i, t.args)
    73  		hctx := s.GetStatusHookContext(c)
    74  		setFakeStatus(hctx)
    75  		com, err := jujuc.NewCommand(hctx, cmdString("status-get"))
    76  		c.Assert(err, jc.ErrorIsNil)
    77  		ctx := testing.Context(c)
    78  		code := cmd.Main(com, ctx, t.args)
    79  		c.Assert(code, gc.Equals, 0)
    80  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    81  
    82  		var out interface{}
    83  		var outMap map[string]interface{}
    84  		switch t.format {
    85  		case formatYaml:
    86  			c.Check(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil)
    87  			out = outMap
    88  		case formatJson:
    89  			c.Check(json.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil)
    90  			out = outMap
    91  		default:
    92  			out = string(bufferBytes(ctx.Stdout))
    93  		}
    94  		c.Check(out, gc.DeepEquals, t.out)
    95  	}
    96  }
    97  
    98  func (s *statusGetSuite) TestHelp(c *gc.C) {
    99  	hctx := s.GetStatusHookContext(c)
   100  	com, err := jujuc.NewCommand(hctx, cmdString("status-get"))
   101  	c.Assert(err, jc.ErrorIsNil)
   102  	ctx := testing.Context(c)
   103  	code := cmd.Main(com, ctx, []string{"--help"})
   104  	c.Assert(code, gc.Equals, 0)
   105  	expectedHelp := "" +
   106  		"Usage: status-get [options] [--include-data] [--application]\n" +
   107  		"\n" +
   108  		"Summary:\n" +
   109  		"print status information\n" +
   110  		"\n" +
   111  		"Options:\n" +
   112  		"--application  (= false)\n" +
   113  		"    print status for all units of this application if this unit is the leader\n" +
   114  		"--format  (= smart)\n" +
   115  		"    Specify output format (json|smart|yaml)\n" +
   116  		"--include-data  (= false)\n" +
   117  		"    print all status data\n" +
   118  		"-o, --output (= \"\")\n" +
   119  		"    Specify an output file\n" +
   120  		"\n" +
   121  		"Details:\n" +
   122  		"By default, only the status value is printed.\n" +
   123  		"If the --include-data flag is passed, the associated data are printed also.\n"
   124  
   125  	c.Assert(bufferString(ctx.Stdout), gc.Equals, expectedHelp)
   126  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   127  }
   128  
   129  func (s *statusGetSuite) TestOutputPath(c *gc.C) {
   130  	hctx := s.GetStatusHookContext(c)
   131  	setFakeStatus(hctx)
   132  	com, err := jujuc.NewCommand(hctx, cmdString("status-get"))
   133  	c.Assert(err, jc.ErrorIsNil)
   134  	ctx := testing.Context(c)
   135  	code := cmd.Main(com, ctx, []string{"--format", "json", "--output", "some-file", "--include-data"})
   136  	c.Assert(code, gc.Equals, 0)
   137  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   138  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
   139  	content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
   140  	c.Assert(err, jc.ErrorIsNil)
   141  
   142  	var out map[string]interface{}
   143  	c.Assert(json.Unmarshal(content, &out), gc.IsNil)
   144  	c.Assert(out, gc.DeepEquals, statusAttributes)
   145  }
   146  
   147  func (s *statusGetSuite) TestServiceStatus(c *gc.C) {
   148  	expected := map[string]interface{}{
   149  		"application-status": map[interface{}]interface{}{
   150  			"status-data": map[interface{}]interface{}{},
   151  			"units": map[interface{}]interface{}{
   152  				"": map[interface{}]interface{}{
   153  					"message":     "this is a unit status",
   154  					"status":      "active",
   155  					"status-data": map[interface{}]interface{}{},
   156  				},
   157  			},
   158  			"message": "this is a service status",
   159  			"status":  "active"},
   160  	}
   161  	hctx := s.GetStatusHookContext(c)
   162  	setFakeServiceStatus(hctx)
   163  	com, err := jujuc.NewCommand(hctx, cmdString("status-get"))
   164  	c.Assert(err, jc.ErrorIsNil)
   165  	ctx := testing.Context(c)
   166  	code := cmd.Main(com, ctx, []string{"--format", "json", "--include-data", "--application"})
   167  	c.Assert(code, gc.Equals, 0)
   168  
   169  	var out map[string]interface{}
   170  	c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &out), gc.IsNil)
   171  	c.Assert(out, gc.DeepEquals, expected)
   172  
   173  }