github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"fmt"
    10  	"strings"
    11  
    12  	"github.com/juju/cmd"
    13  	"github.com/juju/cmd/cmdtesting"
    14  	jc "github.com/juju/testing/checkers"
    15  	gc "gopkg.in/check.v1"
    16  	"gopkg.in/juju/names.v2"
    17  
    18  	"github.com/juju/juju/apiserver/params"
    19  	"github.com/juju/juju/cmd/juju/action"
    20  	"github.com/juju/juju/state"
    21  )
    22  
    23  type ListSuite struct {
    24  	BaseActionSuite
    25  	svc            *state.Application
    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.ApplicationTag
    42  		expectedOutputSchema bool
    43  		expectedErr          string
    44  	}{{
    45  		should:      "fail with missing application name",
    46  		args:        []string{},
    47  		expectedErr: "no application name specified",
    48  	}, {
    49  		should:      "fail with invalid application name",
    50  		args:        []string{invalidApplicationId},
    51  		expectedErr: "invalid application name \"" + invalidApplicationId + "\"",
    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 application name",
    58  		args:        []string{validApplicationId},
    59  		expectedSvc: names.NewApplicationTag(validApplicationId),
    60  	}, {
    61  		should:      "schema with tabular output",
    62  		args:        []string{"--format=tabular", "--schema", validApplicationId},
    63  		expectedErr: "full schema not compatible with tabular output",
    64  	}, {
    65  		should:               "init properly with valid application name and --schema",
    66  		args:                 []string{"--format=yaml", "--schema", validApplicationId},
    67  		expectedOutputSchema: true,
    68  		expectedSvc:          names.NewApplicationTag(validApplicationId),
    69  	}, {
    70  		should:               "default to yaml output when --schema option is specified",
    71  		args:                 []string{"--schema", validApplicationId},
    72  		expectedOutputSchema: true,
    73  		expectedSvc:          names.NewApplicationTag(validApplicationId),
    74  	}}
    75  
    76  	for i, t := range tests {
    77  		for _, modelFlag := range s.modelFlags {
    78  			c.Logf("test %d should %s: juju actions defined %s", i,
    79  				t.should, strings.Join(t.args, " "))
    80  			s.wrappedCommand, s.command = action.NewListCommandForTest(s.store)
    81  			args := append([]string{modelFlag, "admin"}, t.args...)
    82  			err := cmdtesting.InitCommand(s.wrappedCommand, args)
    83  			if t.expectedErr == "" {
    84  				c.Check(err, jc.ErrorIsNil)
    85  				c.Check(s.command.ApplicationTag(), gc.Equals, t.expectedSvc)
    86  				c.Check(s.command.FullSchema(), gc.Equals, t.expectedOutputSchema)
    87  			} else {
    88  				c.Check(err, gc.ErrorMatches, t.expectedErr)
    89  			}
    90  		}
    91  	}
    92  }
    93  
    94  func (s *ListSuite) TestRun(c *gc.C) {
    95  	simpleOutput := `
    96  Action          Description
    97  kill            Kill the database.
    98  no-description  No description
    99  no-params       An action with no parameters.
   100  snapshot        Take a snapshot of the database.
   101  `[1:]
   102  
   103  	tests := []struct {
   104  		should           string
   105  		expectFullSchema bool
   106  		expectNoResults  bool
   107  		expectMessage    string
   108  		withArgs         []string
   109  		withAPIErr       string
   110  		withCharmActions map[string]params.ActionSpec
   111  		expectedErr      string
   112  	}{{
   113  		should:      "pass back API error correctly",
   114  		withArgs:    []string{validApplicationId},
   115  		withAPIErr:  "an API error",
   116  		expectedErr: "an API error",
   117  	}, {
   118  		should:           "get short results properly",
   119  		withArgs:         []string{validApplicationId},
   120  		withCharmActions: someCharmActions,
   121  	}, {
   122  		should:           "get full schema results properly",
   123  		withArgs:         []string{"--format=yaml", "--schema", validApplicationId},
   124  		expectFullSchema: true,
   125  		withCharmActions: someCharmActions,
   126  	}, {
   127  		should:          "work properly when no results found",
   128  		withArgs:        []string{validApplicationId},
   129  		expectNoResults: true,
   130  		expectMessage:   fmt.Sprintf("No actions defined for %s.\n", validApplicationId),
   131  	}, {
   132  		should:           "get tabular default output when --schema is NOT specified",
   133  		withArgs:         []string{"--format=default", validApplicationId},
   134  		withCharmActions: someCharmActions,
   135  	}, {
   136  		should:           "get full schema default output (YAML) when --schema is specified",
   137  		withArgs:         []string{"--format=default", "--schema", validApplicationId},
   138  		expectFullSchema: true,
   139  		withCharmActions: someCharmActions,
   140  	}}
   141  
   142  	for i, t := range tests {
   143  		for _, modelFlag := range s.modelFlags {
   144  			func() {
   145  				c.Logf("test %d should %s", i, t.should)
   146  
   147  				fakeClient := &fakeAPIClient{charmActions: t.withCharmActions}
   148  				if t.withAPIErr != "" {
   149  					fakeClient.apiErr = errors.New(t.withAPIErr)
   150  				}
   151  				restore := s.patchAPIClient(fakeClient)
   152  				defer restore()
   153  
   154  				args := append([]string{modelFlag, "admin"}, t.withArgs...)
   155  				s.wrappedCommand, s.command = action.NewListCommandForTest(s.store)
   156  				ctx, err := cmdtesting.RunCommand(c, s.wrappedCommand, args...)
   157  
   158  				if t.expectedErr != "" || t.withAPIErr != "" {
   159  					c.Check(err, gc.ErrorMatches, t.expectedErr)
   160  				} else {
   161  					c.Assert(err, gc.IsNil)
   162  					result := ctx.Stdout.(*bytes.Buffer).Bytes()
   163  					if t.expectFullSchema {
   164  						checkFullSchema(c, t.withCharmActions, result)
   165  					} else if t.expectNoResults {
   166  						c.Check(cmdtesting.Stderr(ctx), gc.Matches, t.expectMessage)
   167  					} else {
   168  						c.Check(cmdtesting.Stdout(ctx), gc.Equals, simpleOutput)
   169  					}
   170  				}
   171  
   172  			}()
   173  		}
   174  	}
   175  }
   176  
   177  func checkFullSchema(c *gc.C, expected map[string]params.ActionSpec, actual []byte) {
   178  	expectedOutput := make(map[string]interface{})
   179  	for k, v := range expected {
   180  		expectedOutput[k] = v.Params
   181  	}
   182  	c.Check(string(actual), jc.YAMLEquals, expectedOutput)
   183  }