github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/cmd/juju/action/show_test.go (about) 1 // Copyright 2019 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package action_test 5 6 import ( 7 "errors" 8 "strings" 9 10 "github.com/juju/cmd/v3" 11 "github.com/juju/cmd/v3/cmdtesting" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 actionapi "github.com/juju/juju/api/client/action" 16 "github.com/juju/juju/cmd/juju/action" 17 ) 18 19 type ShowSuite struct { 20 BaseActionSuite 21 wrappedCommand cmd.Command 22 command *action.ShowCommand 23 } 24 25 var _ = gc.Suite(&ShowSuite{}) 26 27 func (s *ShowSuite) SetUpTest(c *gc.C) { 28 s.BaseActionSuite.SetUpTest(c) 29 s.wrappedCommand, s.command = action.NewShowCommandForTest(s.store) 30 } 31 32 func (s *ShowSuite) TestInit(c *gc.C) { 33 tests := []struct { 34 should string 35 args []string 36 expectedApp string 37 expectedAction string 38 expectedErr string 39 }{{ 40 should: "fail with missing application name", 41 args: []string{}, 42 expectedErr: "no application specified", 43 }, { 44 should: "fail with missing action name", 45 args: []string{validApplicationId}, 46 expectedErr: "no action specified", 47 }, { 48 should: "fail with invalid application name", 49 args: []string{invalidApplicationId, "doIt"}, 50 expectedErr: "invalid application name \"" + invalidApplicationId + "\"", 51 }, { 52 should: "fail with too many args", 53 args: []string{"one", "two", "things"}, 54 expectedErr: "unrecognized args: \\[\"things\"\\]", 55 }, { 56 should: "init properly with valid application name", 57 args: []string{validApplicationId, "doIt"}, 58 expectedApp: validApplicationId, 59 expectedAction: "doIt", 60 }} 61 62 for i, t := range tests { 63 for _, modelFlag := range s.modelFlags { 64 c.Logf("test %d should %s: juju show-action defined %s", i, 65 t.should, strings.Join(t.args, " ")) 66 s.wrappedCommand, s.command = action.NewShowCommandForTest(s.store) 67 args := append([]string{modelFlag, "admin"}, t.args...) 68 err := cmdtesting.InitCommand(s.wrappedCommand, args) 69 if t.expectedErr == "" { 70 c.Check(err, jc.ErrorIsNil) 71 c.Check(s.command.ApplicationName(), gc.Equals, t.expectedApp) 72 c.Check(s.command.ActionName(), gc.Equals, t.expectedAction) 73 } else { 74 c.Check(err, gc.ErrorMatches, t.expectedErr) 75 } 76 } 77 } 78 } 79 80 func (s *ShowSuite) TestShow(c *gc.C) { 81 simpleOutput := ` 82 Take a snapshot of the database. 83 84 Arguments 85 full: 86 type: boolean 87 description: take a full backup 88 default: true 89 name: 90 type: string 91 description: snapshot name 92 prefix: 93 type: string 94 description: prefix to snapshot name 95 default: "" 96 97 `[1:] 98 99 tests := []struct { 100 should string 101 expectNoResults bool 102 expectMessage string 103 withArgs []string 104 withAPIErr string 105 withCharmActions map[string]actionapi.ActionSpec 106 expectedErr string 107 }{{ 108 should: "pass back API error correctly", 109 withArgs: []string{validApplicationId, "doIt"}, 110 withAPIErr: "an API error", 111 expectedErr: "an API error", 112 }, { 113 should: "work properly when no results found", 114 withArgs: []string{validApplicationId, "snapshot"}, 115 expectNoResults: true, 116 expectedErr: "cmd: error out silently", 117 expectMessage: `unknown action "snapshot"`, 118 }, { 119 should: "error when unknown action specified", 120 withArgs: []string{validApplicationId, "something"}, 121 withCharmActions: someCharmActions, 122 expectMessage: `unknown action "something"`, 123 expectedErr: "cmd: error out silently", 124 }, { 125 should: "get results properly", 126 withArgs: []string{validApplicationId, "snapshot"}, 127 withCharmActions: someCharmActions, 128 }} 129 130 for i, t := range tests { 131 for _, modelFlag := range s.modelFlags { 132 func() { 133 c.Logf("test %d should %s", i, t.should) 134 135 fakeClient := &fakeAPIClient{charmActions: t.withCharmActions} 136 if t.withAPIErr != "" { 137 fakeClient.apiErr = errors.New(t.withAPIErr) 138 } 139 restore := s.patchAPIClient(fakeClient) 140 defer restore() 141 142 args := append([]string{modelFlag, "admin"}, t.withArgs...) 143 s.wrappedCommand, s.command = action.NewShowCommandForTest(s.store) 144 ctx, err := cmdtesting.RunCommand(c, s.wrappedCommand, args...) 145 146 if t.expectedErr != "" || t.withAPIErr != "" { 147 c.Check(err, gc.ErrorMatches, t.expectedErr) 148 if t.expectMessage != "" { 149 msg := cmdtesting.Stderr(ctx) 150 msg = strings.Replace(msg, "\n", "", -1) 151 c.Check(msg, gc.Matches, t.expectMessage) 152 } 153 } else { 154 c.Assert(err, gc.IsNil) 155 c.Check(cmdtesting.Stdout(ctx), gc.Equals, simpleOutput) 156 } 157 158 }() 159 } 160 } 161 }