github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/action/defined_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/names"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	"gopkg.in/juju/charm.v6-unstable"
    15  	"gopkg.in/yaml.v1"
    16  
    17  	"github.com/juju/juju/cmd/juju/action"
    18  	"github.com/juju/juju/state"
    19  	"github.com/juju/juju/testing"
    20  )
    21  
    22  type DefinedSuite struct {
    23  	BaseActionSuite
    24  	svc        *state.Service
    25  	subcommand *action.DefinedCommand
    26  }
    27  
    28  var _ = gc.Suite(&DefinedSuite{})
    29  
    30  func (s *DefinedSuite) SetUpTest(c *gc.C) {
    31  	s.BaseActionSuite.SetUpTest(c)
    32  }
    33  
    34  func (s *DefinedSuite) TestHelp(c *gc.C) {
    35  	s.checkHelp(c, s.subcommand)
    36  }
    37  
    38  func (s *DefinedSuite) TestInit(c *gc.C) {
    39  	tests := []struct {
    40  		should               string
    41  		args                 []string
    42  		expectedSvc          names.ServiceTag
    43  		expectedOutputSchema bool
    44  		expectedErr          string
    45  	}{{
    46  		should:      "fail with missing service name",
    47  		args:        []string{},
    48  		expectedErr: "no service name specified",
    49  	}, {
    50  		should:      "fail with invalid service name",
    51  		args:        []string{invalidServiceId},
    52  		expectedErr: "invalid service name \"" + invalidServiceId + "\"",
    53  	}, {
    54  		should:      "fail with too many args",
    55  		args:        []string{"two", "things"},
    56  		expectedErr: "unrecognized args: \\[\"things\"\\]",
    57  	}, {
    58  		should:      "init properly with valid service name",
    59  		args:        []string{validServiceId},
    60  		expectedSvc: names.NewServiceTag(validServiceId),
    61  	}, {
    62  		should:               "init properly with valid service name and --schema",
    63  		args:                 []string{"--schema", validServiceId},
    64  		expectedOutputSchema: true,
    65  		expectedSvc:          names.NewServiceTag(validServiceId),
    66  	}}
    67  
    68  	for i, t := range tests {
    69  		c.Logf("test %d should %s: juju actions defined %s", i,
    70  			t.should, strings.Join(t.args, " "))
    71  		s.subcommand = &action.DefinedCommand{}
    72  		err := testing.InitCommand(s.subcommand, t.args)
    73  		if t.expectedErr == "" {
    74  			c.Check(s.subcommand.ServiceTag(), gc.Equals, t.expectedSvc)
    75  			c.Check(s.subcommand.FullSchema(), gc.Equals, t.expectedOutputSchema)
    76  		} else {
    77  			c.Check(err, gc.ErrorMatches, t.expectedErr)
    78  		}
    79  	}
    80  }
    81  
    82  func (s *DefinedSuite) TestRun(c *gc.C) {
    83  	tests := []struct {
    84  		should           string
    85  		expectFullSchema bool
    86  		expectNoResults  bool
    87  		expectMessage    string
    88  		withArgs         []string
    89  		withAPIErr       string
    90  		withCharmActions *charm.Actions
    91  		expectedErr      string
    92  	}{{
    93  		should:      "pass back API error correctly",
    94  		withArgs:    []string{validServiceId},
    95  		withAPIErr:  "an API error",
    96  		expectedErr: "an API error",
    97  	}, {
    98  		should:           "get short results properly",
    99  		withArgs:         []string{validServiceId},
   100  		withCharmActions: someCharmActions,
   101  	}, {
   102  		should:           "get full schema results properly",
   103  		withArgs:         []string{"--schema", validServiceId},
   104  		expectFullSchema: true,
   105  		withCharmActions: someCharmActions,
   106  	}, {
   107  		should:           "work properly when no results found",
   108  		withArgs:         []string{validServiceId},
   109  		expectNoResults:  true,
   110  		expectMessage:    "No actions defined for " + validServiceId,
   111  		withCharmActions: &charm.Actions{ActionSpecs: map[string]charm.ActionSpec{}},
   112  	}}
   113  
   114  	for i, t := range tests {
   115  		func() {
   116  			c.Logf("test %d should %s", i, t.should)
   117  
   118  			fakeClient := &fakeAPIClient{charmActions: t.withCharmActions}
   119  			if t.withAPIErr != "" {
   120  				fakeClient.apiErr = errors.New(t.withAPIErr)
   121  			}
   122  			restore := s.patchAPIClient(fakeClient)
   123  			defer restore()
   124  
   125  			s.subcommand = &action.DefinedCommand{}
   126  			ctx, err := testing.RunCommand(c, s.subcommand, t.withArgs...)
   127  
   128  			if t.expectedErr != "" || t.withAPIErr != "" {
   129  				c.Check(err, gc.ErrorMatches, t.expectedErr)
   130  			} else {
   131  				c.Assert(err, gc.IsNil)
   132  				result := ctx.Stdout.(*bytes.Buffer).Bytes()
   133  				if t.expectFullSchema {
   134  					checkFullSchema(c, t.withCharmActions, result)
   135  				} else if t.expectNoResults {
   136  					c.Check(string(result), gc.Matches, t.expectMessage+"(?sm).*")
   137  				} else {
   138  					checkSimpleSchema(c, t.withCharmActions, result)
   139  				}
   140  			}
   141  		}()
   142  	}
   143  }
   144  
   145  func checkFullSchema(c *gc.C, expected *charm.Actions, actual []byte) {
   146  	expectedOutput := make(map[string]interface{})
   147  	for k, v := range expected.ActionSpecs {
   148  		expectedOutput[k] = v.Params
   149  	}
   150  	c.Check(string(actual), jc.YAMLEquals, expectedOutput)
   151  }
   152  
   153  func checkSimpleSchema(c *gc.C, expected *charm.Actions, actualOutput []byte) {
   154  	specs := expected.ActionSpecs
   155  	expectedSpecs := make(map[string]string)
   156  	for name, spec := range specs {
   157  		expectedSpecs[name] = spec.Description
   158  		if expectedSpecs[name] == "" {
   159  			expectedSpecs[name] = "No description"
   160  		}
   161  	}
   162  	actual := make(map[string]string)
   163  	err := yaml.Unmarshal(actualOutput, &actual)
   164  	c.Assert(err, gc.IsNil)
   165  	c.Check(actual, jc.DeepEquals, expectedSpecs)
   166  }