github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/goal-state_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"io/ioutil"
     8  	"path/filepath"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/cmd/cmdtesting"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    16  )
    17  
    18  type GoalStateSuite struct {
    19  	ContextSuite
    20  }
    21  
    22  var _ = gc.Suite(&GoalStateSuite{})
    23  
    24  func (s *GoalStateSuite) TestHelp(c *gc.C) {
    25  	hctx := s.GetHookContext(c, -1, "")
    26  	com, err := jujuc.NewCommand(hctx, cmdString("goal-state"))
    27  	c.Assert(err, jc.ErrorIsNil)
    28  	ctx := cmdtesting.Context(c)
    29  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"})
    30  	c.Assert(code, gc.Equals, 0)
    31  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `Usage: goal-state [options]
    32  
    33  Summary:
    34  print the status of the charm's peers and related units
    35  
    36  Options:
    37  --format  (= yaml)
    38      Specify output format (json|yaml)
    39  -o, --output (= "")
    40      Specify an output file
    41  
    42  Details:
    43  'goal-state' command will list the charm units and relations, specifying their status and their relations to other units in different charms.
    44  `)
    45  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    46  }
    47  
    48  var (
    49  	goalStateTestResultYaml = `units:
    50    mysql/0:
    51      status: active
    52      since: 2200-11-05 15:29:12Z
    53  relations:
    54    db:
    55      mysql/0:
    56        status: active
    57        since: 2200-11-05 15:29:12Z
    58    server:
    59      wordpress/0:
    60        status: active
    61        since: 2200-11-05 15:29:12Z
    62  `
    63  	goalStateTestResultJson = `{"units":{"mysql/0":{"status":"active","since":"2200-11-05 15:29:12Z"}},"relations":{"db":{"mysql/0":{"status":"active","since":"2200-11-05 15:29:12Z"}},"server":{"wordpress/0":{"status":"active","since":"2200-11-05 15:29:12Z"}}}}
    64  `
    65  
    66  	goalStateAllTests = []struct {
    67  		args []string
    68  		out  string
    69  	}{
    70  		{nil, goalStateTestResultYaml},
    71  		{[]string{"--format", "yaml"}, goalStateTestResultYaml},
    72  		{[]string{"--format", "json"}, goalStateTestResultJson},
    73  	}
    74  
    75  	goalStateAllOutPutFileTests = []struct {
    76  		args []string
    77  		out  string
    78  	}{
    79  		{[]string{"--output", "some-file"}, goalStateTestResultYaml},
    80  		{[]string{"--format", "yaml", "--output", "some-file"}, goalStateTestResultYaml},
    81  		{[]string{"--format", "json", "--output", "some-file"}, goalStateTestResultJson},
    82  	}
    83  )
    84  
    85  func (s *GoalStateSuite) TestOutputFormatAll(c *gc.C) {
    86  	for i, t := range goalStateAllTests {
    87  		c.Logf("test %d: %#v", i, t.args)
    88  
    89  		ctx, code := s.getGoalStateCommand(c, t.args)
    90  
    91  		c.Assert(code, gc.Equals, 0)
    92  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    93  		c.Assert(bufferString(ctx.Stdout), gc.Equals, t.out)
    94  	}
    95  }
    96  
    97  func (s *GoalStateSuite) TestOutputPath(c *gc.C) {
    98  
    99  	for i, t := range goalStateAllOutPutFileTests {
   100  		c.Logf("test %d: %#v", i, t.args)
   101  
   102  		ctx, code := s.getGoalStateCommand(c, t.args)
   103  
   104  		c.Assert(code, gc.Equals, 0)
   105  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   106  		c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
   107  
   108  		content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file"))
   109  		c.Assert(err, jc.ErrorIsNil)
   110  		c.Assert(string(content), gc.Equals, t.out)
   111  	}
   112  }
   113  
   114  func (s *GoalStateSuite) getGoalStateCommand(c *gc.C, args []string) (*cmd.Context, int) {
   115  	hctx := s.GetHookContext(c, -1, "")
   116  	com, err := jujuc.NewCommand(hctx, cmdString("goal-state"))
   117  	c.Assert(err, jc.ErrorIsNil)
   118  	ctx := cmdtesting.Context(c)
   119  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, args)
   120  	return ctx, code
   121  }
   122  
   123  func (s *GoalStateSuite) TestUnknownArg(c *gc.C) {
   124  	hctx := s.GetHookContext(c, -1, "")
   125  	com, err := jujuc.NewCommand(hctx, cmdString("goal-state"))
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	cmdtesting.TestInit(c, jujuc.NewJujucCommandWrappedForTest(com), []string{}, "")
   128  }