github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/action/status_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 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/apiserver/params" 15 "github.com/juju/juju/cmd/juju/action" 16 "github.com/juju/juju/testing" 17 ) 18 19 type StatusSuite struct { 20 BaseActionSuite 21 subcommand cmd.Command 22 } 23 24 var _ = gc.Suite(&StatusSuite{}) 25 26 func (s *StatusSuite) SetUpTest(c *gc.C) { 27 s.BaseActionSuite.SetUpTest(c) 28 s.subcommand, _ = action.NewStatusCommandForTest(s.store) 29 } 30 31 func (s *StatusSuite) 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{{Status: "some-random-status"}} 42 result2 := []params.ActionResult{{Status: "a status"}, {Status: "another status"}} 43 44 errNotFound := "no actions found" 45 errNotFoundForPrefix := `no actions found matching prefix "` + prefix + `"` 46 errFoundTagButNoResults := `identifier "` + prefix + `" matched action\(s\) \[.*\], but found no results` 47 48 nameArgs := []string{"--name", "action_name"} 49 resultMany := params.ActionsByNames{[]params.ActionsByName{{ 50 Name: "1", 51 }, { 52 Name: "2", 53 }}} 54 55 resultOne := params.ActionsByNames{[]params.ActionsByName{{ 56 Name: "action_name", 57 Actions: result1, 58 }}} 59 60 errNames := ¶ms.Error{ 61 Message: "whoops:", 62 } 63 64 resultOneError := params.ActionsByNames{[]params.ActionsByName{{ 65 Error: errNames, 66 }}} 67 68 tests := []statusTestCase{ 69 {expectError: errNotFound}, 70 {args: emptyArgs, expectError: errNotFound}, 71 {args: emptyArgs, tags: tagsForIdPrefix("", faketag, faketag2), results: result2}, 72 {args: emptyPrefixArgs, expectError: errNotFound}, 73 {args: emptyPrefixArgs, tags: tagsForIdPrefix("", faketag, faketag2), results: result2}, 74 {args: prefixArgs, expectError: errNotFoundForPrefix}, 75 {args: prefixArgs, expectError: errNotFoundForPrefix, tags: tagsForIdPrefix(prefix)}, 76 {args: prefixArgs, expectError: errNotFoundForPrefix, tags: tagsForIdPrefix(prefix, "bb", "bc")}, 77 {args: prefixArgs, expectError: errFoundTagButNoResults, tags: tagsForIdPrefix(prefix, faketag, faketag2)}, 78 {args: prefixArgs, expectError: errFoundTagButNoResults, tags: tagsForIdPrefix(prefix, faketag)}, 79 {args: prefixArgs, tags: tagsForIdPrefix(prefix, faketag), results: result1}, 80 {args: prefixArgs, tags: tagsForIdPrefix(prefix, faketag, faketag2), results: result2}, 81 {args: nameArgs, actionsByNames: resultMany, expectError: "expected one result got 2"}, 82 {args: nameArgs, actionsByNames: resultOneError, expectError: errNames.Message}, 83 {args: nameArgs, actionsByNames: params.ActionsByNames{[]params.ActionsByName{{Name: "action_name"}}}, expectError: "no actions were found for name action_name"}, 84 {args: nameArgs, actionsByNames: resultOne, results: result1}, 85 } 86 87 for i, test := range tests { 88 c.Logf("iteration %d, test case %+v", i, test) 89 s.runTestCase(c, test) 90 } 91 } 92 93 func (s *StatusSuite) runTestCase(c *gc.C, tc statusTestCase) { 94 for _, modelFlag := range s.modelFlags { 95 fakeClient := makeFakeClient( 96 0*time.Second, // No API delay 97 5*time.Second, // 5 second test timeout 98 tc.tags, 99 tc.results, 100 tc.actionsByNames, 101 "", // No API error 102 ) 103 104 restore := s.patchAPIClient(fakeClient) 105 defer restore() 106 107 s.subcommand, _ = action.NewStatusCommandForTest(s.store) 108 args := append([]string{modelFlag, "admin"}, tc.args...) 109 ctx, err := testing.RunCommand(c, s.subcommand, args...) 110 if tc.expectError == "" { 111 c.Assert(err, jc.ErrorIsNil) 112 } else { 113 c.Assert(err, gc.ErrorMatches, tc.expectError) 114 } 115 if len(tc.results) > 0 { 116 buf, err := cmd.DefaultFormatters["yaml"](action.ActionResultsToMap(tc.results)) 117 c.Check(err, jc.ErrorIsNil) 118 c.Check(ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, string(buf)+"\n") 119 c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "") 120 } 121 } 122 } 123 124 type statusTestCase struct { 125 args []string 126 expectError string 127 tags params.FindTagsResults 128 results []params.ActionResult 129 actionsByNames params.ActionsByNames 130 }