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

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/testing"
    14  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    15  )
    16  
    17  type ActionFailSuite struct {
    18  	ContextSuite
    19  }
    20  
    21  type actionFailContext struct {
    22  	jujuc.Context
    23  	actionFailed  bool
    24  	actionMessage string
    25  }
    26  
    27  func (ctx *actionFailContext) SetActionMessage(message string) error {
    28  	ctx.actionMessage = message
    29  	return nil
    30  }
    31  
    32  func (ctx *actionFailContext) SetActionFailed() error {
    33  	ctx.actionFailed = true
    34  	return nil
    35  }
    36  
    37  type nonActionFailContext struct {
    38  	jujuc.Context
    39  }
    40  
    41  func (ctx *nonActionFailContext) SetActionMessage(message string) error {
    42  	return fmt.Errorf("not running an action")
    43  }
    44  
    45  func (ctx *nonActionFailContext) SetActionFailed() error {
    46  	return fmt.Errorf("not running an action")
    47  }
    48  
    49  var _ = gc.Suite(&ActionFailSuite{})
    50  
    51  func (s *ActionFailSuite) TestActionFail(c *gc.C) {
    52  	var actionFailTests = []struct {
    53  		summary string
    54  		command []string
    55  		message string
    56  		failed  bool
    57  		errMsg  string
    58  		code    int
    59  	}{{
    60  		summary: "no parameters sets a default message",
    61  		command: []string{},
    62  		message: "action failed without reason given, check action for errors",
    63  		failed:  true,
    64  	}, {
    65  		summary: "a message sent is set as the failure reason",
    66  		command: []string{"a failure message"},
    67  		message: "a failure message",
    68  		failed:  true,
    69  	}, {
    70  		summary: "extra arguments are an error, leaving the action not failed",
    71  		command: []string{"a failure message", "something else"},
    72  		errMsg:  "error: unrecognized args: [\"something else\"]\n",
    73  		code:    2,
    74  	}}
    75  
    76  	for i, t := range actionFailTests {
    77  		c.Logf("test %d: %s", i, t.summary)
    78  		hctx := &actionFailContext{}
    79  		com, err := jujuc.NewCommand(hctx, cmdString("action-fail"))
    80  		c.Assert(err, jc.ErrorIsNil)
    81  		ctx := testing.Context(c)
    82  		code := cmd.Main(com, ctx, t.command)
    83  		c.Check(code, gc.Equals, t.code)
    84  		c.Check(bufferString(ctx.Stderr), gc.Equals, t.errMsg)
    85  		c.Check(hctx.actionMessage, gc.Equals, t.message)
    86  		c.Check(hctx.actionFailed, gc.Equals, t.failed)
    87  	}
    88  }
    89  
    90  func (s *ActionFailSuite) TestNonActionSetActionFailedFails(c *gc.C) {
    91  	hctx := &nonActionFailContext{}
    92  	com, err := jujuc.NewCommand(hctx, cmdString("action-fail"))
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	ctx := testing.Context(c)
    95  	code := cmd.Main(com, ctx, []string{"oops"})
    96  	c.Check(code, gc.Equals, 1)
    97  	c.Check(bufferString(ctx.Stderr), gc.Equals, "error: not running an action\n")
    98  	c.Check(bufferString(ctx.Stdout), gc.Equals, "")
    99  }
   100  
   101  func (s *ActionFailSuite) TestHelp(c *gc.C) {
   102  	hctx, _ := s.NewHookContext()
   103  	com, err := jujuc.NewCommand(hctx, cmdString("action-fail"))
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	ctx := testing.Context(c)
   106  	code := cmd.Main(com, ctx, []string{"--help"})
   107  	c.Assert(code, gc.Equals, 0)
   108  	c.Assert(bufferString(ctx.Stdout), gc.Equals, `Usage: action-fail ["<failure message>"]
   109  
   110  Summary:
   111  set action fail status with message
   112  
   113  Details:
   114  action-fail sets the action's fail state with a given error message.  Using
   115  action-fail without a failure message will set a default message indicating a
   116  problem with the action.
   117  `)
   118  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   119  }