github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/action/cancel_test.go (about)

     1  // Copyright 2014-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package action_test
     5  
     6  import (
     7  	"bytes"
     8  	"time"
     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/apiserver/params"
    16  	"github.com/juju/juju/cmd/juju/action"
    17  )
    18  
    19  type CancelSuite struct {
    20  	BaseActionSuite
    21  	subcommand cmd.Command
    22  }
    23  
    24  var _ = gc.Suite(&CancelSuite{})
    25  
    26  func (s *CancelSuite) SetUpTest(c *gc.C) {
    27  	s.BaseActionSuite.SetUpTest(c)
    28  	s.subcommand, _ = action.NewCancelCommandForTest(s.store)
    29  }
    30  
    31  func (s *CancelSuite) TestRun(c *gc.C) {
    32  	prefix := "deadbeef"
    33  	fakeid := prefix + "-0000-4000-8000-feedfacebeef"
    34  	fakeid2 := prefix + "-0001-4000-8000-feedfacebeef"
    35  	faketag := "action-" + fakeid
    36  	faketag2 := "action-" + fakeid2
    37  
    38  	emptyArgs := []string{}
    39  	emptyPrefixArgs := []string{}
    40  	prefixArgs := []string{prefix}
    41  	result1 := []params.ActionResult{{Action: &params.Action{}, Status: "some-random-status"}}
    42  	result2 := []params.ActionResult{{Action: &params.Action{}, Status: "a status"}, {Action: &params.Action{}, Status: "another status"}}
    43  
    44  	errNotFound := "no actions specified"
    45  	errNotFoundForPrefix := `no actions found matching prefix ` + prefix + `, no actions have been canceled`
    46  	errFoundTagButNotCanceled := `identifier\(s\) \["` + prefix + `"\] matched action\(s\) \[.*\], but no actions were canceled`
    47  
    48  	tests := []cancelTestCase{
    49  		{expectError: errNotFound},
    50  		{args: emptyArgs, expectError: errNotFound},
    51  		{args: emptyArgs, tags: tagsForIdPrefix("", faketag, faketag2), expectError: errNotFound},
    52  		{args: emptyPrefixArgs, expectError: errNotFound},
    53  		{args: emptyPrefixArgs, tags: tagsForIdPrefix("", faketag, faketag2), expectError: errNotFound},
    54  		{args: prefixArgs, expectError: errNotFoundForPrefix},
    55  		{args: prefixArgs, expectError: errNotFoundForPrefix, tags: tagsForIdPrefix(prefix)},
    56  		{args: prefixArgs, expectError: errNotFoundForPrefix, tags: tagsForIdPrefix(prefix, "bb", "bc")},
    57  		{args: prefixArgs, expectError: errFoundTagButNotCanceled, tags: tagsForIdPrefix(prefix, faketag, faketag2)},
    58  		{args: prefixArgs, expectError: errFoundTagButNotCanceled, tags: tagsForIdPrefix(prefix, faketag)},
    59  		{args: prefixArgs, tags: tagsForIdPrefix(prefix, faketag), results: result1},
    60  		{args: prefixArgs, tags: tagsForIdPrefix(prefix, faketag, faketag2), results: result2},
    61  	}
    62  
    63  	for i, test := range tests {
    64  		c.Logf("iteration %d, test case %+v", i, test)
    65  		s.runTestCase(c, test)
    66  	}
    67  }
    68  
    69  func (s *CancelSuite) runTestCase(c *gc.C, tc cancelTestCase) {
    70  	for _, modelFlag := range s.modelFlags {
    71  		fakeClient := makeFakeClient(
    72  			0*time.Second, // No API delay
    73  			5*time.Second, // 5 second test timeout
    74  			tc.tags,
    75  			tc.results,
    76  			tc.actionsByNames,
    77  			"", // No API error
    78  		)
    79  
    80  		restore := s.patchAPIClient(fakeClient)
    81  		defer restore()
    82  
    83  		s.subcommand, _ = action.NewCancelCommandForTest(s.store)
    84  		args := append([]string{modelFlag, "admin"}, tc.args...)
    85  		ctx, err := cmdtesting.RunCommand(c, s.subcommand, args...)
    86  		if tc.expectError == "" {
    87  			c.Assert(err, jc.ErrorIsNil)
    88  		} else {
    89  			c.Assert(err, gc.ErrorMatches, tc.expectError)
    90  		}
    91  		if len(tc.results) > 0 {
    92  			out := &bytes.Buffer{}
    93  			err := cmd.FormatYaml(out, action.ActionResultsToMap(tc.results))
    94  			c.Check(err, jc.ErrorIsNil)
    95  			c.Check(ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, out.String())
    96  			c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "")
    97  		}
    98  	}
    99  }
   100  
   101  type cancelTestCase struct {
   102  	args           []string
   103  	expectError    string
   104  	tags           params.FindTagsResults
   105  	results        []params.ActionResult
   106  	actionsByNames params.ActionsByNames
   107  }