github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/envcmd/systemcommand_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package envcmd_test
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/cmd/cmdtesting"
    11  	gitjujutesting "github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/cmd/envcmd"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type SystemCommandSuite struct {
    20  	testing.FakeJujuHomeSuite
    21  }
    22  
    23  var _ = gc.Suite(&SystemCommandSuite{})
    24  
    25  func (s *SystemCommandSuite) TestSystemCommandInitMultipleConfigs(c *gc.C) {
    26  	// The environments.yaml file is ignored for system commands.
    27  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    28  	_, err := initTestSystemCommand(c)
    29  	c.Assert(err, gc.ErrorMatches, "no system specified")
    30  }
    31  
    32  func (s *SystemCommandSuite) TestSystemCommandInitNoEnvFile(c *gc.C) {
    33  	// Since we ignore the environments.yaml file, we don't care if it isn't
    34  	// there.
    35  	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
    36  	err := os.Remove(envPath)
    37  	_, err = initTestSystemCommand(c)
    38  	c.Assert(err, gc.ErrorMatches, "no system specified")
    39  }
    40  
    41  func (s *SystemCommandSuite) TestSystemCommandInitSystemFile(c *gc.C) {
    42  	// If there is a current-system file, use that.
    43  	err := envcmd.WriteCurrentSystem("fubar")
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	testEnsureSystemName(c, "fubar")
    46  }
    47  func (s *SystemCommandSuite) TestSystemCommandInitEnvFile(c *gc.C) {
    48  	// If there is a current-environment file, use that.
    49  	err := envcmd.WriteCurrentEnvironment("fubar")
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	testEnsureSystemName(c, "fubar")
    52  }
    53  
    54  func (s *SystemCommandSuite) TestSystemCommandInitExplicit(c *gc.C) {
    55  	// Take system name from command line arg, and it trumps the current-
    56  	// system file.
    57  	err := envcmd.WriteCurrentSystem("fubar")
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	testEnsureSystemName(c, "explicit", "-s", "explicit")
    60  	testEnsureSystemName(c, "explicit", "--system", "explicit")
    61  }
    62  
    63  type testSystemCommand struct {
    64  	envcmd.SysCommandBase
    65  }
    66  
    67  func (c *testSystemCommand) Info() *cmd.Info {
    68  	panic("should not be called")
    69  }
    70  
    71  func (c *testSystemCommand) Run(ctx *cmd.Context) error {
    72  	panic("should not be called")
    73  }
    74  
    75  func initTestSystemCommand(c *gc.C, args ...string) (*testSystemCommand, error) {
    76  	cmd := new(testSystemCommand)
    77  	wrapped := envcmd.WrapSystem(cmd)
    78  	return cmd, cmdtesting.InitCommand(wrapped, args)
    79  }
    80  
    81  func testEnsureSystemName(c *gc.C, expect string, args ...string) {
    82  	cmd, err := initTestSystemCommand(c, args...)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	c.Assert(cmd.SystemName(), gc.Equals, expect)
    85  }