github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/application/cmd_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package application
     5  
     6  import (
     7  	"path/filepath"
     8  
     9  	"github.com/juju/cmd/cmdtesting"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    15  	coretesting "github.com/juju/juju/testing"
    16  )
    17  
    18  type CmdSuite struct {
    19  	coretesting.FakeJujuXDGDataHomeSuite
    20  }
    21  
    22  var _ = gc.Suite(&CmdSuite{})
    23  
    24  func (s *CmdSuite) SetUpTest(c *gc.C) {
    25  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    26  }
    27  
    28  var deployTests = []struct {
    29  	about                 string
    30  	args                  []string
    31  	expectCharmOrBundle   string
    32  	expectApplicationName string
    33  	expectNumUnits        int
    34  	expectError           string
    35  	expectConfigFile      string
    36  }{{
    37  	about:               "simple init",
    38  	args:                []string{"charm-name"},
    39  	expectCharmOrBundle: "charm-name",
    40  	expectNumUnits:      1,
    41  }, {
    42  	about:                 "charm and application name specified",
    43  	args:                  []string{"charm-name", "application-name"},
    44  	expectCharmOrBundle:   "charm-name",
    45  	expectApplicationName: "application-name",
    46  	expectNumUnits:        1,
    47  }, {
    48  	about:               "--num-units long form",
    49  	args:                []string{"--num-units", "33", "charm-name"},
    50  	expectNumUnits:      33,
    51  	expectCharmOrBundle: "charm-name",
    52  }, {
    53  	about:               "--num-units short form",
    54  	args:                []string{"-n", "104", "charm-name"},
    55  	expectNumUnits:      104,
    56  	expectCharmOrBundle: "charm-name",
    57  }, {
    58  	about:               "config specified",
    59  	args:                []string{"--config", "testconfig.yaml", "charm-name"},
    60  	expectCharmOrBundle: "charm-name",
    61  	expectNumUnits:      1,
    62  	expectConfigFile:    "testconfig.yaml",
    63  }, {
    64  	about:       "missing args",
    65  	expectError: "no charm or bundle specified",
    66  }, {
    67  	about:       "bad unit count",
    68  	args:        []string{"charm-name", "--num-units", "0"},
    69  	expectError: "--num-units must be a positive integer",
    70  }, {
    71  	about:       "bad unit count (short form)",
    72  	args:        []string{"charm-name", "-n", "0"},
    73  	expectError: "--num-units must be a positive integer",
    74  }}
    75  
    76  func (s *CmdSuite) TestDeployCommandInit(c *gc.C) {
    77  	for i, t := range deployTests {
    78  		c.Logf("\ntest %d: %s", i, t.about)
    79  		wrappedDeployCmd := NewDeployCommandForTest(nil, nil)
    80  		wrappedDeployCmd.SetClientStore(jujuclienttesting.MinimalStore())
    81  		err := cmdtesting.InitCommand(wrappedDeployCmd, t.args)
    82  		if t.expectError != "" {
    83  			c.Assert(err, gc.ErrorMatches, t.expectError)
    84  			continue
    85  		}
    86  		c.Assert(err, jc.ErrorIsNil)
    87  		deployCmd := modelcmd.InnerCommand(wrappedDeployCmd).(*DeployCommand)
    88  		c.Assert(deployCmd.ApplicationName, gc.Equals, t.expectApplicationName)
    89  		c.Assert(deployCmd.CharmOrBundle, gc.Equals, t.expectCharmOrBundle)
    90  		c.Assert(deployCmd.NumUnits, gc.Equals, t.expectNumUnits)
    91  		if t.expectConfigFile != "" {
    92  			ctx := cmdtesting.Context(c)
    93  			absFiles, err := deployCmd.ConfigOptions.AbsoluteFileNames(ctx)
    94  			c.Check(err, jc.ErrorIsNil)
    95  			c.Check(absFiles, gc.HasLen, 1)
    96  			c.Assert(absFiles[0], gc.Equals, filepath.Join(ctx.Dir, t.expectConfigFile))
    97  		}
    98  	}
    99  }
   100  
   101  func (*CmdSuite) TestExposeCommandInitWithMissingArgs(c *gc.C) {
   102  	cmd := NewExposeCommand()
   103  	cmd.SetClientStore(jujuclienttesting.MinimalStore())
   104  	err := cmdtesting.InitCommand(cmd, nil)
   105  	c.Assert(err, gc.ErrorMatches, "no application name specified")
   106  
   107  	// environment tested elsewhere
   108  }
   109  
   110  func (*CmdSuite) TestUnexposeCommandInitWithMissingArgs(c *gc.C) {
   111  	cmd := NewUnexposeCommand()
   112  	cmd.SetClientStore(jujuclienttesting.MinimalStore())
   113  	err := cmdtesting.InitCommand(cmd, nil)
   114  	c.Assert(err, gc.ErrorMatches, "no application name specified")
   115  }
   116  
   117  func (*CmdSuite) TestRemoveUnitCommandInitMissingArgs(c *gc.C) {
   118  	cmd := NewRemoveUnitCommand()
   119  	cmd.SetClientStore(jujuclienttesting.MinimalStore())
   120  	err := cmdtesting.InitCommand(cmd, nil)
   121  	c.Assert(err, gc.ErrorMatches, "no units specified")
   122  }
   123  
   124  func (*CmdSuite) TestRemoveUnitCommandInitInvalidUnit(c *gc.C) {
   125  	cmd := NewRemoveUnitCommand()
   126  	cmd.SetClientStore(jujuclienttesting.MinimalStore())
   127  	err := cmdtesting.InitCommand(cmd, []string{"seven/nine"})
   128  	c.Assert(err, gc.ErrorMatches, `invalid unit name "seven/nine"`)
   129  }