github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/action/list_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package action_test 5 6 import ( 7 "bytes" 8 "errors" 9 "fmt" 10 "strings" 11 12 "github.com/juju/cmd" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 "gopkg.in/juju/names.v2" 16 17 "github.com/juju/juju/apiserver/params" 18 "github.com/juju/juju/cmd/juju/action" 19 "github.com/juju/juju/state" 20 "github.com/juju/juju/testing" 21 ) 22 23 type ListSuite struct { 24 BaseActionSuite 25 svc *state.Application 26 wrappedCommand cmd.Command 27 command *action.ListCommand 28 } 29 30 var _ = gc.Suite(&ListSuite{}) 31 32 func (s *ListSuite) SetUpTest(c *gc.C) { 33 s.BaseActionSuite.SetUpTest(c) 34 s.wrappedCommand, s.command = action.NewListCommandForTest(s.store) 35 } 36 37 func (s *ListSuite) TestInit(c *gc.C) { 38 tests := []struct { 39 should string 40 args []string 41 expectedSvc names.ApplicationTag 42 expectedOutputSchema bool 43 expectedErr string 44 }{{ 45 should: "fail with missing application name", 46 args: []string{}, 47 expectedErr: "no application name specified", 48 }, { 49 should: "fail with invalid application name", 50 args: []string{invalidServiceId}, 51 expectedErr: "invalid application name \"" + invalidServiceId + "\"", 52 }, { 53 should: "fail with too many args", 54 args: []string{"two", "things"}, 55 expectedErr: "unrecognized args: \\[\"things\"\\]", 56 }, { 57 should: "init properly with valid application name", 58 args: []string{validServiceId}, 59 expectedSvc: names.NewApplicationTag(validServiceId), 60 }, { 61 should: "schema with tabular output", 62 args: []string{"--format=tabular", "--schema", validServiceId}, 63 expectedErr: "full schema not compatible with tabular output", 64 }, { 65 should: "init properly with valid application name and --schema", 66 args: []string{"--format=yaml", "--schema", validServiceId}, 67 expectedOutputSchema: true, 68 expectedSvc: names.NewApplicationTag(validServiceId), 69 }} 70 71 for i, t := range tests { 72 for _, modelFlag := range s.modelFlags { 73 c.Logf("test %d should %s: juju actions defined %s", i, 74 t.should, strings.Join(t.args, " ")) 75 s.wrappedCommand, s.command = action.NewListCommandForTest(s.store) 76 args := append([]string{modelFlag, "admin"}, t.args...) 77 err := testing.InitCommand(s.wrappedCommand, args) 78 if t.expectedErr == "" { 79 c.Check(err, jc.ErrorIsNil) 80 c.Check(s.command.ApplicationTag(), gc.Equals, t.expectedSvc) 81 c.Check(s.command.FullSchema(), gc.Equals, t.expectedOutputSchema) 82 } else { 83 c.Check(err, gc.ErrorMatches, t.expectedErr) 84 } 85 } 86 } 87 } 88 89 func (s *ListSuite) TestRun(c *gc.C) { 90 simpleOutput := ` 91 Action Description 92 kill Kill the database. 93 no-description No description 94 no-params An action with no parameters. 95 snapshot Take a snapshot of the database. 96 97 `[1:] 98 99 tests := []struct { 100 should string 101 expectFullSchema bool 102 expectNoResults bool 103 expectMessage string 104 withArgs []string 105 withAPIErr string 106 withCharmActions map[string]params.ActionSpec 107 expectedErr string 108 }{{ 109 should: "pass back API error correctly", 110 withArgs: []string{validServiceId}, 111 withAPIErr: "an API error", 112 expectedErr: "an API error", 113 }, { 114 should: "get short results properly", 115 withArgs: []string{validServiceId}, 116 withCharmActions: someCharmActions, 117 }, { 118 should: "get full schema results properly", 119 withArgs: []string{"--format=yaml", "--schema", validServiceId}, 120 expectFullSchema: true, 121 withCharmActions: someCharmActions, 122 }, { 123 should: "work properly when no results found", 124 withArgs: []string{validServiceId}, 125 expectNoResults: true, 126 expectMessage: fmt.Sprintf("No actions defined for %s.\n", validServiceId), 127 }} 128 129 for i, t := range tests { 130 for _, modelFlag := range s.modelFlags { 131 func() { 132 c.Logf("test %d should %s", i, t.should) 133 134 fakeClient := &fakeAPIClient{charmActions: t.withCharmActions} 135 if t.withAPIErr != "" { 136 fakeClient.apiErr = errors.New(t.withAPIErr) 137 } 138 restore := s.patchAPIClient(fakeClient) 139 defer restore() 140 141 args := append([]string{modelFlag, "admin"}, t.withArgs...) 142 s.wrappedCommand, s.command = action.NewListCommandForTest(s.store) 143 ctx, err := testing.RunCommand(c, s.wrappedCommand, args...) 144 145 if t.expectedErr != "" || t.withAPIErr != "" { 146 c.Check(err, gc.ErrorMatches, t.expectedErr) 147 } else { 148 c.Assert(err, gc.IsNil) 149 result := ctx.Stdout.(*bytes.Buffer).Bytes() 150 if t.expectFullSchema { 151 checkFullSchema(c, t.withCharmActions, result) 152 } else if t.expectNoResults { 153 c.Check(testing.Stderr(ctx), gc.Matches, t.expectMessage) 154 } else { 155 c.Check(testing.Stdout(ctx), gc.Equals, simpleOutput) 156 } 157 } 158 159 }() 160 } 161 } 162 } 163 164 func checkFullSchema(c *gc.C, expected map[string]params.ActionSpec, actual []byte) { 165 expectedOutput := make(map[string]interface{}) 166 for k, v := range expected { 167 expectedOutput[k] = v.Params 168 } 169 c.Check(string(actual), jc.YAMLEquals, expectedOutput) 170 }