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