github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/switch_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"os"
     8  
     9  	gitjujutesting "github.com/juju/testing"
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"github.com/juju/juju/cmd/envcmd"
    13  	_ "github.com/juju/juju/juju"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  type SwitchSimpleSuite struct {
    18  	testing.FakeJujuHomeSuite
    19  }
    20  
    21  var _ = gc.Suite(&SwitchSimpleSuite{})
    22  
    23  func (*SwitchSimpleSuite) TestNoEnvironment(c *gc.C) {
    24  	envPath := gitjujutesting.HomePath(".juju", "environments.yaml")
    25  	err := os.Remove(envPath)
    26  	c.Assert(err, gc.IsNil)
    27  	_, err = testing.RunCommand(c, &SwitchCommand{})
    28  	c.Assert(err, gc.ErrorMatches, "couldn't read the environment")
    29  }
    30  
    31  func (*SwitchSimpleSuite) TestNoDefault(c *gc.C) {
    32  	testing.WriteEnvironments(c, testing.MultipleEnvConfigNoDefault)
    33  	_, err := testing.RunCommand(c, &SwitchCommand{})
    34  	c.Assert(err, gc.ErrorMatches, "no currently specified environment")
    35  }
    36  
    37  func (*SwitchSimpleSuite) TestShowsDefault(c *gc.C) {
    38  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    39  	context, err := testing.RunCommand(c, &SwitchCommand{})
    40  	c.Assert(err, gc.IsNil)
    41  	c.Assert(testing.Stdout(context), gc.Equals, "erewhemos\n")
    42  }
    43  
    44  func (s *SwitchSimpleSuite) TestCurrentEnvironmentHasPrecidence(c *gc.C) {
    45  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    46  	s.FakeHomeSuite.Home.AddFiles(c, gitjujutesting.TestFile{".juju/current-environment", "fubar"})
    47  	context, err := testing.RunCommand(c, &SwitchCommand{})
    48  	c.Assert(err, gc.IsNil)
    49  	c.Assert(testing.Stdout(context), gc.Equals, "fubar\n")
    50  }
    51  
    52  func (*SwitchSimpleSuite) TestShowsJujuEnv(c *gc.C) {
    53  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    54  	os.Setenv("JUJU_ENV", "using-env")
    55  	context, err := testing.RunCommand(c, &SwitchCommand{})
    56  	c.Assert(err, gc.IsNil)
    57  	c.Assert(testing.Stdout(context), gc.Equals, "using-env\n")
    58  }
    59  
    60  func (s *SwitchSimpleSuite) TestJujuEnvOverCurrentEnvironment(c *gc.C) {
    61  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    62  	s.FakeHomeSuite.Home.AddFiles(c, gitjujutesting.TestFile{".juju/current-environment", "fubar"})
    63  	os.Setenv("JUJU_ENV", "using-env")
    64  	context, err := testing.RunCommand(c, &SwitchCommand{})
    65  	c.Assert(err, gc.IsNil)
    66  	c.Assert(testing.Stdout(context), gc.Equals, "using-env\n")
    67  }
    68  
    69  func (*SwitchSimpleSuite) TestSettingWritesFile(c *gc.C) {
    70  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    71  	context, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2")
    72  	c.Assert(err, gc.IsNil)
    73  	c.Assert(testing.Stdout(context), gc.Equals, "erewhemos -> erewhemos-2\n")
    74  	c.Assert(envcmd.ReadCurrentEnvironment(), gc.Equals, "erewhemos-2")
    75  }
    76  
    77  func (*SwitchSimpleSuite) TestSettingToUnknown(c *gc.C) {
    78  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    79  	_, err := testing.RunCommand(c, &SwitchCommand{}, "unknown")
    80  	c.Assert(err, gc.ErrorMatches, `"unknown" is not a name of an existing defined environment`)
    81  }
    82  
    83  func (*SwitchSimpleSuite) TestSettingWhenJujuEnvSet(c *gc.C) {
    84  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    85  	os.Setenv("JUJU_ENV", "using-env")
    86  	_, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2")
    87  	c.Assert(err, gc.ErrorMatches, `cannot switch when JUJU_ENV is overriding the environment \(set to "using-env"\)`)
    88  }
    89  
    90  const expectedEnvironments = `erewhemos
    91  erewhemos-2
    92  `
    93  
    94  func (*SwitchSimpleSuite) TestListEnvironments(c *gc.C) {
    95  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
    96  	context, err := testing.RunCommand(c, &SwitchCommand{}, "--list")
    97  	c.Assert(err, gc.IsNil)
    98  	c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments)
    99  }
   100  
   101  func (*SwitchSimpleSuite) TestListEnvironmentsOSJujuEnvSet(c *gc.C) {
   102  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
   103  	os.Setenv("JUJU_ENV", "using-env")
   104  	context, err := testing.RunCommand(c, &SwitchCommand{}, "--list")
   105  	c.Assert(err, gc.IsNil)
   106  	c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments)
   107  }
   108  
   109  func (*SwitchSimpleSuite) TestListEnvironmentsAndChange(c *gc.C) {
   110  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
   111  	_, err := testing.RunCommand(c, &SwitchCommand{}, "--list", "erewhemos-2")
   112  	c.Assert(err, gc.ErrorMatches, "cannot switch and list at the same time")
   113  }
   114  
   115  func (*SwitchSimpleSuite) TestTooManyParams(c *gc.C) {
   116  	testing.WriteEnvironments(c, testing.MultipleEnvConfig)
   117  	_, err := testing.RunCommand(c, &SwitchCommand{}, "foo", "bar")
   118  	c.Assert(err, gc.ErrorMatches, `unrecognized args: ."bar".`)
   119  }