github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/cmd/juju/commands/cmd_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/cmd/envcmd"
    14  	"github.com/juju/juju/cmd/juju/service"
    15  	cmdtesting "github.com/juju/juju/cmd/testing"
    16  	"github.com/juju/juju/juju/osenv"
    17  	"github.com/juju/juju/juju/testing"
    18  	coretesting "github.com/juju/juju/testing"
    19  )
    20  
    21  func badrun(c *gc.C, exit int, args ...string) string {
    22  	args = append([]string{"juju"}, args...)
    23  	return cmdtesting.BadRun(c, exit, args...)
    24  }
    25  
    26  type CmdSuite struct {
    27  	testing.JujuConnSuite
    28  }
    29  
    30  var _ = gc.Suite(&CmdSuite{})
    31  
    32  const envConfig = `
    33  default:
    34      peckham
    35  environments:
    36      peckham:
    37          type: dummy
    38          state-server: false
    39          admin-secret: arble
    40          authorized-keys: i-am-a-key
    41          default-series: raring
    42      walthamstow:
    43          type: dummy
    44          state-server: false
    45          authorized-keys: i-am-a-key
    46      brokenenv:
    47          type: dummy
    48          broken: Bootstrap Destroy
    49          state-server: false
    50          authorized-keys: i-am-a-key
    51          agent-stream: proposed
    52      devenv:
    53          type: dummy
    54          state-server: false
    55          admin-secret: arble
    56          authorized-keys: i-am-a-key
    57          default-series: raring
    58          agent-stream: proposed
    59  `
    60  
    61  func (s *CmdSuite) SetUpTest(c *gc.C) {
    62  	s.JujuConnSuite.SetUpTest(c)
    63  	coretesting.WriteEnvironments(c, envConfig, "peckham", "walthamstow", "brokenenv")
    64  }
    65  
    66  func (s *CmdSuite) TearDownTest(c *gc.C) {
    67  	s.JujuConnSuite.TearDownTest(c)
    68  }
    69  
    70  // testInit checks that a command initialises correctly
    71  // with the given set of arguments.
    72  func testInit(c *gc.C, com cmd.Command, args []string, errPat string) {
    73  	err := coretesting.InitCommand(com, args)
    74  	if errPat != "" {
    75  		c.Assert(err, gc.ErrorMatches, errPat)
    76  	} else {
    77  		c.Assert(err, jc.ErrorIsNil)
    78  	}
    79  }
    80  
    81  type HasConnectionName interface {
    82  	ConnectionName() string
    83  }
    84  
    85  // assertEnvName asserts that the Command is using
    86  // the given environment name.
    87  // Since every command has a different type,
    88  // we use reflection to look at the value of the
    89  // Conn field in the value.
    90  func assertEnvName(c *gc.C, com cmd.Command, name string) {
    91  	i, ok := com.(HasConnectionName)
    92  	c.Assert(ok, jc.IsTrue)
    93  	c.Assert(i.ConnectionName(), gc.Equals, name)
    94  }
    95  
    96  // All members of EnvironmentInitTests are tested for the -environment and -e
    97  // flags, and that extra arguments will cause parsing to fail.
    98  var EnvironmentInitTests = []func() (envcmd.EnvironCommand, []string){
    99  	func() (envcmd.EnvironCommand, []string) { return new(BootstrapCommand), nil },
   100  	func() (envcmd.EnvironCommand, []string) {
   101  		return new(DeployCommand), []string{"charm-name", "service-name"}
   102  	},
   103  	func() (envcmd.EnvironCommand, []string) { return new(StatusCommand), nil },
   104  }
   105  
   106  // TestEnvironmentInit tests that all commands which accept
   107  // the --environment variable initialise their
   108  // environment name correctly.
   109  func (*CmdSuite) TestEnvironmentInit(c *gc.C) {
   110  	for i, cmdFunc := range EnvironmentInitTests {
   111  		c.Logf("test %d", i)
   112  		com, args := cmdFunc()
   113  		testInit(c, envcmd.Wrap(com), args, "")
   114  		assertEnvName(c, com, "peckham")
   115  
   116  		com, args = cmdFunc()
   117  		testInit(c, envcmd.Wrap(com), append(args, "-e", "walthamstow"), "")
   118  		assertEnvName(c, com, "walthamstow")
   119  
   120  		com, args = cmdFunc()
   121  		testInit(c, envcmd.Wrap(com), append(args, "--environment", "walthamstow"), "")
   122  		assertEnvName(c, com, "walthamstow")
   123  
   124  		// JUJU_ENV is the final place the environment can be overriden
   125  		com, args = cmdFunc()
   126  		oldenv := os.Getenv(osenv.JujuEnvEnvKey)
   127  		os.Setenv(osenv.JujuEnvEnvKey, "walthamstow")
   128  		testInit(c, envcmd.Wrap(com), args, "")
   129  		os.Setenv(osenv.JujuEnvEnvKey, oldenv)
   130  		assertEnvName(c, com, "walthamstow")
   131  	}
   132  }
   133  
   134  var deployTests = []struct {
   135  	args []string
   136  	com  *DeployCommand
   137  }{
   138  	{
   139  		[]string{"charm-name"},
   140  		&DeployCommand{},
   141  	}, {
   142  		[]string{"charm-name", "service-name"},
   143  		&DeployCommand{ServiceName: "service-name"},
   144  	}, {
   145  		[]string{"--repository", "/path/to/another-repo", "charm-name"},
   146  		&DeployCommand{RepoPath: "/path/to/another-repo"},
   147  	}, {
   148  		[]string{"--upgrade", "charm-name"},
   149  		&DeployCommand{BumpRevision: true},
   150  	}, {
   151  		[]string{"-u", "charm-name"},
   152  		&DeployCommand{BumpRevision: true},
   153  	}, {
   154  		[]string{"--num-units", "33", "charm-name"},
   155  		&DeployCommand{UnitCommandBase: service.UnitCommandBase{NumUnits: 33}},
   156  	}, {
   157  		[]string{"-n", "104", "charm-name"},
   158  		&DeployCommand{UnitCommandBase: service.UnitCommandBase{NumUnits: 104}},
   159  	},
   160  }
   161  
   162  func initExpectations(com *DeployCommand) {
   163  	if com.CharmName == "" {
   164  		com.CharmName = "charm-name"
   165  	}
   166  	if com.NumUnits == 0 {
   167  		com.NumUnits = 1
   168  	}
   169  	if com.RepoPath == "" {
   170  		com.RepoPath = "/path/to/repo"
   171  	}
   172  	com.SetEnvName("peckham")
   173  }
   174  
   175  func initDeployCommand(args ...string) (*DeployCommand, error) {
   176  	com := &DeployCommand{}
   177  	return com, coretesting.InitCommand(envcmd.Wrap(com), args)
   178  }
   179  
   180  func (*CmdSuite) TestDeployCommandInit(c *gc.C) {
   181  	defer os.Setenv(osenv.JujuRepositoryEnvKey, os.Getenv(osenv.JujuRepositoryEnvKey))
   182  	os.Setenv(osenv.JujuRepositoryEnvKey, "/path/to/repo")
   183  
   184  	for _, t := range deployTests {
   185  		initExpectations(t.com)
   186  		com, err := initDeployCommand(t.args...)
   187  		c.Assert(err, jc.ErrorIsNil)
   188  		c.Assert(com, gc.DeepEquals, t.com)
   189  	}
   190  
   191  	// test relative --config path
   192  	ctx := coretesting.Context(c)
   193  	expected := []byte("test: data")
   194  	path := ctx.AbsPath("testconfig.yaml")
   195  	file, err := os.Create(path)
   196  	c.Assert(err, jc.ErrorIsNil)
   197  	_, err = file.Write(expected)
   198  	c.Assert(err, jc.ErrorIsNil)
   199  	file.Close()
   200  
   201  	com, err := initDeployCommand("--config", "testconfig.yaml", "charm-name")
   202  	c.Assert(err, jc.ErrorIsNil)
   203  	actual, err := com.Config.Read(ctx)
   204  	c.Assert(err, jc.ErrorIsNil)
   205  	c.Assert(expected, gc.DeepEquals, actual)
   206  
   207  	// missing args
   208  	_, err = initDeployCommand()
   209  	c.Assert(err, gc.ErrorMatches, "no charm specified")
   210  
   211  	// bad unit count
   212  	_, err = initDeployCommand("charm-name", "--num-units", "0")
   213  	c.Assert(err, gc.ErrorMatches, "--num-units must be a positive integer")
   214  	_, err = initDeployCommand("charm-name", "-n", "0")
   215  	c.Assert(err, gc.ErrorMatches, "--num-units must be a positive integer")
   216  
   217  	// environment tested elsewhere
   218  }
   219  
   220  func initExposeCommand(args ...string) (*ExposeCommand, error) {
   221  	com := &ExposeCommand{}
   222  	return com, coretesting.InitCommand(com, args)
   223  }
   224  
   225  func (*CmdSuite) TestExposeCommandInit(c *gc.C) {
   226  	// missing args
   227  	_, err := initExposeCommand()
   228  	c.Assert(err, gc.ErrorMatches, "no service name specified")
   229  
   230  	// environment tested elsewhere
   231  }
   232  
   233  func initUnexposeCommand(args ...string) (*UnexposeCommand, error) {
   234  	com := &UnexposeCommand{}
   235  	return com, coretesting.InitCommand(com, args)
   236  }
   237  
   238  func (*CmdSuite) TestUnexposeCommandInit(c *gc.C) {
   239  	// missing args
   240  	_, err := initUnexposeCommand()
   241  	c.Assert(err, gc.ErrorMatches, "no service name specified")
   242  
   243  	// environment tested elsewhere
   244  }
   245  
   246  func initSSHCommand(args ...string) (*SSHCommand, error) {
   247  	com := &SSHCommand{}
   248  	return com, coretesting.InitCommand(com, args)
   249  }
   250  
   251  func (*CmdSuite) TestSSHCommandInit(c *gc.C) {
   252  	// missing args
   253  	_, err := initSSHCommand()
   254  	c.Assert(err, gc.ErrorMatches, "no target name specified")
   255  }
   256  
   257  func initSCPCommand(args ...string) (*SCPCommand, error) {
   258  	com := &SCPCommand{}
   259  	return com, coretesting.InitCommand(com, args)
   260  }
   261  
   262  func (*CmdSuite) TestSCPCommandInit(c *gc.C) {
   263  	// missing args
   264  	_, err := initSCPCommand()
   265  	c.Assert(err, gc.ErrorMatches, "at least two arguments required")
   266  
   267  	// not enough args
   268  	_, err = initSCPCommand("mysql/0:foo")
   269  	c.Assert(err, gc.ErrorMatches, "at least two arguments required")
   270  }
   271  
   272  func initRemoveUnitCommand(args ...string) (*RemoveUnitCommand, error) {
   273  	com := &RemoveUnitCommand{}
   274  	return com, coretesting.InitCommand(com, args)
   275  }
   276  
   277  func (*CmdSuite) TestRemoveUnitCommandInit(c *gc.C) {
   278  	// missing args
   279  	_, err := initRemoveUnitCommand()
   280  	c.Assert(err, gc.ErrorMatches, "no units specified")
   281  	// not a unit
   282  	_, err = initRemoveUnitCommand("seven/nine")
   283  	c.Assert(err, gc.ErrorMatches, `invalid unit name "seven/nine"`)
   284  }