github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 "strings" 10 11 "github.com/juju/cmd" 12 "github.com/juju/names" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 "gopkg.in/juju/charm.v6-unstable" 16 "gopkg.in/yaml.v2" 17 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.Service 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.ServiceTag 42 expectedOutputSchema bool 43 expectedErr string 44 }{{ 45 should: "fail with missing service name", 46 args: []string{}, 47 expectedErr: "no service name specified", 48 }, { 49 should: "fail with invalid service name", 50 args: []string{invalidServiceId}, 51 expectedErr: "invalid service 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 service name", 58 args: []string{validServiceId}, 59 expectedSvc: names.NewServiceTag(validServiceId), 60 }, { 61 should: "init properly with valid service name and --schema", 62 args: []string{"--schema", validServiceId}, 63 expectedOutputSchema: true, 64 expectedSvc: names.NewServiceTag(validServiceId), 65 }} 66 67 for i, t := range tests { 68 for _, modelFlag := range s.modelFlags { 69 c.Logf("test %d should %s: juju actions defined %s", i, 70 t.should, strings.Join(t.args, " ")) 71 s.wrappedCommand, s.command = action.NewListCommandForTest(s.store) 72 args := append([]string{modelFlag, "admin"}, t.args...) 73 err := testing.InitCommand(s.wrappedCommand, args) 74 if t.expectedErr == "" { 75 c.Check(s.command.ServiceTag(), gc.Equals, t.expectedSvc) 76 c.Check(s.command.FullSchema(), gc.Equals, t.expectedOutputSchema) 77 } else { 78 c.Check(err, gc.ErrorMatches, t.expectedErr) 79 } 80 } 81 } 82 } 83 84 func (s *ListSuite) TestRun(c *gc.C) { 85 tests := []struct { 86 should string 87 expectFullSchema bool 88 expectNoResults bool 89 expectMessage string 90 withArgs []string 91 withAPIErr string 92 withCharmActions *charm.Actions 93 expectedErr string 94 }{{ 95 should: "pass back API error correctly", 96 withArgs: []string{validServiceId}, 97 withAPIErr: "an API error", 98 expectedErr: "an API error", 99 }, { 100 should: "get short results properly", 101 withArgs: []string{validServiceId}, 102 withCharmActions: someCharmActions, 103 }, { 104 should: "get full schema results properly", 105 withArgs: []string{"--schema", validServiceId}, 106 expectFullSchema: true, 107 withCharmActions: someCharmActions, 108 }, { 109 should: "work properly when no results found", 110 withArgs: []string{validServiceId}, 111 expectNoResults: true, 112 expectMessage: "No actions defined for " + validServiceId, 113 withCharmActions: &charm.Actions{ActionSpecs: map[string]charm.ActionSpec{}}, 114 }} 115 116 for i, t := range tests { 117 for _, modelFlag := range s.modelFlags { 118 func() { 119 c.Logf("test %d should %s", i, t.should) 120 121 fakeClient := &fakeAPIClient{charmActions: t.withCharmActions} 122 if t.withAPIErr != "" { 123 fakeClient.apiErr = errors.New(t.withAPIErr) 124 } 125 restore := s.patchAPIClient(fakeClient) 126 defer restore() 127 128 args := append([]string{modelFlag, "admin"}, t.withArgs...) 129 s.wrappedCommand, s.command = action.NewListCommandForTest(s.store) 130 ctx, err := testing.RunCommand(c, s.wrappedCommand, args...) 131 132 if t.expectedErr != "" || t.withAPIErr != "" { 133 c.Check(err, gc.ErrorMatches, t.expectedErr) 134 } else { 135 c.Assert(err, gc.IsNil) 136 result := ctx.Stdout.(*bytes.Buffer).Bytes() 137 if t.expectFullSchema { 138 checkFullSchema(c, t.withCharmActions, result) 139 } else if t.expectNoResults { 140 c.Check(string(result), gc.Matches, t.expectMessage+"(?sm).*") 141 } else { 142 checkSimpleSchema(c, t.withCharmActions, result) 143 } 144 } 145 146 }() 147 } 148 } 149 } 150 151 func checkFullSchema(c *gc.C, expected *charm.Actions, actual []byte) { 152 expectedOutput := make(map[string]interface{}) 153 for k, v := range expected.ActionSpecs { 154 expectedOutput[k] = v.Params 155 } 156 c.Check(string(actual), jc.YAMLEquals, expectedOutput) 157 } 158 159 func checkSimpleSchema(c *gc.C, expected *charm.Actions, actualOutput []byte) { 160 specs := expected.ActionSpecs 161 expectedSpecs := make(map[string]string) 162 for name, spec := range specs { 163 expectedSpecs[name] = spec.Description 164 if expectedSpecs[name] == "" { 165 expectedSpecs[name] = "No description" 166 } 167 } 168 actual := make(map[string]string) 169 err := yaml.Unmarshal(actualOutput, &actual) 170 c.Assert(err, gc.IsNil) 171 c.Check(actual, jc.DeepEquals, expectedSpecs) 172 }