github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/cmd/juju/action" 14 "github.com/juju/juju/testing" 15 ) 16 17 type StatusSuite struct { 18 BaseActionSuite 19 subcommand *action.StatusCommand 20 } 21 22 var _ = gc.Suite(&StatusSuite{}) 23 24 func (s *StatusSuite) SetUpTest(c *gc.C) { 25 s.BaseActionSuite.SetUpTest(c) 26 s.subcommand = &action.StatusCommand{} 27 } 28 29 func (s *StatusSuite) TestHelp(c *gc.C) { 30 s.checkHelp(c, s.subcommand) 31 } 32 33 func (s *StatusSuite) TestRun(c *gc.C) { 34 prefix := "deadbeef" 35 fakeid := prefix + "-0000-4000-8000-feedfacebeef" 36 fakeid2 := prefix + "-0001-4000-8000-feedfacebeef" 37 faketag := "action-" + fakeid 38 faketag2 := "action-" + fakeid2 39 fakestatus := "bloobered" 40 41 args := []string{prefix} 42 result := []params.ActionResult{{Status: fakestatus}} 43 44 errNotSpecified := "no action identifier specified" 45 errNotFound := `actions for identifier "` + prefix + `" not found` 46 errNotRecognized := `identifier "` + prefix + `" got unrecognized entity tags .*` 47 errMultipleMatches := `identifier "` + prefix + `" matched multiple actions .*` 48 errNoResults := `identifier "` + prefix + `" matched action "` + fakeid + `", but found no results` 49 50 tests := []statusTestCase{ 51 {expectError: errNotSpecified}, 52 {args: args, expectError: errNotFound}, 53 {args: args, expectError: errNotFound, tags: tagsForIdPrefix(prefix)}, 54 {args: args, expectError: errNotRecognized, tags: tagsForIdPrefix(prefix, "bb", "bc")}, 55 {args: args, expectError: errMultipleMatches, tags: tagsForIdPrefix(prefix, faketag, faketag2)}, 56 {args: args, expectError: errNoResults, tags: tagsForIdPrefix(prefix, faketag)}, 57 {args: args, tags: tagsForIdPrefix(prefix, faketag), results: result}, 58 } 59 60 for _, test := range tests { 61 s.runTestCase(c, test) 62 } 63 } 64 65 func (s *StatusSuite) runTestCase(c *gc.C, tc statusTestCase) { 66 fakeClient := &fakeAPIClient{actionTagMatches: tc.tags, actionResults: tc.results} 67 restore := s.patchAPIClient(fakeClient) 68 defer restore() 69 70 s.subcommand = &action.StatusCommand{} 71 ctx, err := testing.RunCommand(c, s.subcommand, tc.args...) 72 if tc.expectError == "" { 73 c.Check(err, jc.ErrorIsNil) 74 } else { 75 c.Check(err, gc.ErrorMatches, tc.expectError) 76 } 77 if len(tc.results) > 0 { 78 expected := "id: .*\nstatus: " + tc.results[0].Status + "\n" 79 c.Check(ctx.Stdout.(*bytes.Buffer).String(), gc.Matches, expected) 80 c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "") 81 } 82 } 83 84 type statusTestCase struct { 85 args []string 86 expectError string 87 tags params.FindTagsResults 88 results []params.ActionResult 89 }