github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/action/client_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 "errors" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/names.v2" 12 13 "github.com/juju/juju/api/action" 14 "github.com/juju/juju/apiserver/params" 15 ) 16 17 type actionSuite struct { 18 baseSuite 19 } 20 21 var _ = gc.Suite(&actionSuite{}) 22 23 func (s *actionSuite) TestClient(c *gc.C) { 24 facade := action.ExposeFacade(s.client) 25 26 c.Check(facade.Name(), gc.Equals, "Action") 27 } 28 29 func (s *actionSuite) TestApplicationCharmActions(c *gc.C) { 30 tests := []struct { 31 description string 32 patchResults []params.ApplicationCharmActionsResult 33 patchErr string 34 expectedErr string 35 expectedResult map[string]params.ActionSpec 36 }{{ 37 description: "result from wrong application", 38 patchResults: []params.ApplicationCharmActionsResult{ 39 { 40 ApplicationTag: names.NewApplicationTag("bar").String(), 41 }, 42 }, 43 expectedErr: `action results received for wrong application "application-bar"`, 44 }, { 45 description: "some other error", 46 patchResults: []params.ApplicationCharmActionsResult{ 47 { 48 ApplicationTag: names.NewApplicationTag("foo").String(), 49 Error: ¶ms.Error{ 50 Message: "something bad", 51 }, 52 }, 53 }, 54 expectedErr: `something bad`, 55 }, { 56 description: "more than one result", 57 patchResults: []params.ApplicationCharmActionsResult{ 58 {}, 59 {}, 60 }, 61 expectedErr: "2 results, expected 1", 62 }, { 63 description: "no results", 64 patchResults: []params.ApplicationCharmActionsResult{}, 65 expectedErr: "0 results, expected 1", 66 }, { 67 description: "error on facade call", 68 patchErr: "something went wrong", 69 expectedErr: "something went wrong", 70 }, { 71 description: "normal result", 72 patchResults: []params.ApplicationCharmActionsResult{ 73 { 74 ApplicationTag: names.NewApplicationTag("foo").String(), 75 Actions: map[string]params.ActionSpec{ 76 "action": { 77 Description: "description", 78 Params: map[string]interface{}{ 79 "foo": "bar", 80 }, 81 }, 82 }, 83 }, 84 }, 85 expectedResult: map[string]params.ActionSpec{ 86 "action": { 87 Description: "description", 88 Params: map[string]interface{}{ 89 "foo": "bar", 90 }, 91 }, 92 }, 93 }} 94 95 for i, t := range tests { 96 // anonymous func to properly trigger defer 97 func() { 98 c.Logf("test %d: %s", i, t.description) 99 cleanup := patchApplicationCharmActions(c, s.client, t.patchResults, t.patchErr) 100 defer cleanup() 101 result, err := s.client.ApplicationCharmActions(params.Entity{Tag: names.NewApplicationTag("foo").String()}) 102 if t.expectedErr != "" { 103 c.Check(err, gc.ErrorMatches, t.expectedErr) 104 } else { 105 c.Check(err, jc.ErrorIsNil) 106 c.Check(result, jc.DeepEquals, t.expectedResult) 107 } 108 }() 109 } 110 } 111 112 // replace sCharmActions" facade call with required results and error 113 // if desired 114 func patchApplicationCharmActions(c *gc.C, apiCli *action.Client, patchResults []params.ApplicationCharmActionsResult, err string) func() { 115 return action.PatchClientFacadeCall(apiCli, 116 func(req string, paramsIn interface{}, resp interface{}) error { 117 c.Assert(req, gc.Equals, "ApplicationsCharmsActions") 118 c.Assert(paramsIn, gc.FitsTypeOf, params.Entities{}) 119 p := paramsIn.(params.Entities) 120 c.Check(p.Entities, gc.HasLen, 1) 121 result := resp.(*params.ApplicationsCharmActionsResults) 122 result.Results = patchResults 123 if err != "" { 124 return errors.New(err) 125 } 126 return nil 127 }, 128 ) 129 }