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