github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/action/package_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 "io/ioutil" 8 "regexp" 9 "testing" 10 11 "github.com/juju/cmd" 12 jujutesting "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 "gopkg.in/juju/charm.v4" 16 17 "github.com/juju/juju/apiserver/params" 18 "github.com/juju/juju/cmd/envcmd" 19 "github.com/juju/juju/cmd/juju/action" 20 coretesting "github.com/juju/juju/testing" 21 ) 22 23 const ( 24 validActionTagString = "action-f47ac10b-58cc-4372-a567-0e02b2c3d479" 25 invalidActionTagString = "action-f47ac10b-58cc-4372-a567-0e02b2c3d47" 26 validActionId = "f47ac10b-58cc-4372-a567-0e02b2c3d479" 27 invalidActionId = "f47ac10b-58cc-4372-a567-0e02b2c3d47" 28 validUnitId = "mysql/0" 29 invalidUnitId = "something-strange-" 30 validServiceId = "mysql" 31 invalidServiceId = "something-strange-" 32 ) 33 34 func TestPackage(t *testing.T) { 35 gc.TestingT(t) 36 } 37 38 type BaseActionSuite struct { 39 jujutesting.IsolationSuite 40 command cmd.Command 41 } 42 43 var _ = gc.Suite(&FetchSuite{}) 44 45 func (s *BaseActionSuite) SetUpTest(c *gc.C) { 46 s.command = action.NewSuperCommand() 47 } 48 49 func (s *BaseActionSuite) patchAPIClient(client *fakeAPIClient) func() { 50 return jujutesting.PatchValue(action.NewActionAPIClient, 51 func(c *action.ActionCommandBase) (action.APIClient, error) { 52 return client, nil 53 }, 54 ) 55 } 56 57 func (s *BaseActionSuite) checkHelp(c *gc.C, subcmd envcmd.EnvironCommand) { 58 ctx, err := coretesting.RunCommand(c, s.command, subcmd.Info().Name, "--help") 59 c.Assert(err, gc.IsNil) 60 61 expected := "(?sm).*^usage: juju action " + 62 regexp.QuoteMeta(subcmd.Info().Name) + 63 ` \[options\] ` + regexp.QuoteMeta(subcmd.Info().Args) + ".+" 64 c.Check(coretesting.Stdout(ctx), gc.Matches, expected) 65 66 expected = "(?sm).*^purpose: " + regexp.QuoteMeta(subcmd.Info().Purpose) + "$.*" 67 c.Check(coretesting.Stdout(ctx), gc.Matches, expected) 68 69 expected = "(?sm).*^" + regexp.QuoteMeta(subcmd.Info().Doc) + "$.*" 70 c.Check(coretesting.Stdout(ctx), gc.Matches, expected) 71 } 72 73 var someCharmActions = &charm.Actions{ 74 ActionSpecs: map[string]charm.ActionSpec{ 75 "snapshot": { 76 Description: "Take a snapshot of the database.", 77 Params: map[string]interface{}{ 78 "foo": map[string]interface{}{ 79 "bar": "baz", 80 }, 81 "baz": "bar", 82 }, 83 }, 84 "kill": { 85 Description: "Kill the database.", 86 Params: map[string]interface{}{ 87 "bar": map[string]interface{}{ 88 "baz": "foo", 89 }, 90 "foo": "baz", 91 }, 92 }, 93 "no-description": { 94 Params: map[string]interface{}{ 95 "bar": map[string]interface{}{ 96 "baz": "foo", 97 }, 98 "foo": "baz", 99 }, 100 }, 101 "no-params": { 102 Description: "An action with no parameters.", 103 }, 104 }, 105 } 106 107 // tagsForIdPrefix builds a params.FindTagResults for a given id prefix 108 // and 0..n given tags. This is useful for stubbing out the API and 109 // ensuring that the API returns expected tags for a given id prefix. 110 func tagsForIdPrefix(prefix string, tags ...string) params.FindTagsResults { 111 var entities []params.Entity 112 for _, t := range tags { 113 entities = append(entities, params.Entity{Tag: t}) 114 } 115 return params.FindTagsResults{Matches: map[string][]params.Entity{prefix: entities}} 116 } 117 118 // setupValueFile creates a file containing one value for testing. 119 // cf. cmd/juju/set_test.go 120 func setupValueFile(c *gc.C, dir, filename, value string) string { 121 ctx := coretesting.ContextForDir(c, dir) 122 path := ctx.AbsPath(filename) 123 content := []byte(value) 124 err := ioutil.WriteFile(path, content, 0666) 125 c.Assert(err, jc.ErrorIsNil) 126 return path 127 } 128 129 type fakeAPIClient struct { 130 actionResults []params.ActionResult 131 enqueuedActions params.Actions 132 actionsByReceivers []params.ActionsByReceiver 133 actionTagMatches params.FindTagsResults 134 charmActions *charm.Actions 135 apiErr error 136 } 137 138 var _ action.APIClient = (*fakeAPIClient)(nil) 139 140 // EnqueuedActions is a testing method which shows what Actions got enqueued 141 // by our Enqueue stub. 142 func (c *fakeAPIClient) EnqueuedActions() params.Actions { 143 return c.enqueuedActions 144 } 145 146 func (c *fakeAPIClient) Close() error { 147 return nil 148 } 149 150 func (c *fakeAPIClient) Enqueue(args params.Actions) (params.ActionResults, error) { 151 c.enqueuedActions = args 152 return params.ActionResults{Results: c.actionResults}, c.apiErr 153 } 154 155 func (c *fakeAPIClient) ListAll(args params.Entities) (params.ActionsByReceivers, error) { 156 return params.ActionsByReceivers{ 157 Actions: c.actionsByReceivers, 158 }, c.apiErr 159 } 160 161 func (c *fakeAPIClient) ListPending(args params.Entities) (params.ActionsByReceivers, error) { 162 return params.ActionsByReceivers{ 163 Actions: c.actionsByReceivers, 164 }, c.apiErr 165 } 166 167 func (c *fakeAPIClient) ListCompleted(args params.Entities) (params.ActionsByReceivers, error) { 168 return params.ActionsByReceivers{ 169 Actions: c.actionsByReceivers, 170 }, c.apiErr 171 } 172 173 func (c *fakeAPIClient) Cancel(args params.Actions) (params.ActionResults, error) { 174 return params.ActionResults{ 175 Results: c.actionResults, 176 }, c.apiErr 177 } 178 179 func (c *fakeAPIClient) ServiceCharmActions(params.Entity) (*charm.Actions, error) { 180 return c.charmActions, c.apiErr 181 } 182 183 func (c *fakeAPIClient) Actions(args params.Entities) (params.ActionResults, error) { 184 return params.ActionResults{ 185 Results: c.actionResults, 186 }, c.apiErr 187 } 188 189 func (c *fakeAPIClient) FindActionTagsByPrefix(arg params.FindTags) (params.FindTagsResults, error) { 190 return c.actionTagMatches, c.apiErr 191 }