github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/cloud/defaultregion_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloud_test
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/cmd"
    14  	jujucloud "github.com/juju/juju/cloud"
    15  	"github.com/juju/juju/cmd/juju/cloud"
    16  	"github.com/juju/juju/juju/osenv"
    17  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type defaultRegionSuite struct {
    22  	testing.BaseSuite
    23  }
    24  
    25  var _ = gc.Suite(&defaultRegionSuite{})
    26  
    27  func (s *defaultRegionSuite) SetUpTest(c *gc.C) {
    28  	origHome := osenv.SetJujuXDGDataHome(c.MkDir())
    29  	s.AddCleanup(func(*gc.C) { osenv.SetJujuXDGDataHome(origHome) })
    30  }
    31  
    32  func (s *defaultRegionSuite) TestBadArgs(c *gc.C) {
    33  	cmd := cloud.NewSetDefaultRegionCommand()
    34  	_, err := testing.RunCommand(c, cmd)
    35  	c.Assert(err, gc.ErrorMatches, "Usage: juju set-default-region <cloud-name> <region>")
    36  	_, err = testing.RunCommand(c, cmd, "cloud", "region", "extra")
    37  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["extra"\]`)
    38  }
    39  
    40  func (s *defaultRegionSuite) TestBadRegion(c *gc.C) {
    41  	cmd := cloud.NewSetDefaultRegionCommand()
    42  	_, err := testing.RunCommand(c, cmd, "aws", "foo")
    43  	c.Assert(err, gc.ErrorMatches, `region "foo" for cloud aws not valid, valid regions are .*`)
    44  }
    45  
    46  func (s *defaultRegionSuite) TestBadCloudName(c *gc.C) {
    47  	cmd := cloud.NewSetDefaultRegionCommand()
    48  	_, err := testing.RunCommand(c, cmd, "somecloud", "us-west-1")
    49  	c.Assert(err, gc.ErrorMatches, `cloud somecloud not valid`)
    50  }
    51  
    52  func (s *defaultRegionSuite) assertSetDefaultRegion(c *gc.C, cmd cmd.Command, store *jujuclienttesting.MemStore, cloud, errStr string) {
    53  	ctx, err := testing.RunCommand(c, cmd, cloud, "us-west-1")
    54  	output := testing.Stderr(ctx)
    55  	output = strings.Replace(output, "\n", "", -1)
    56  	if errStr != "" {
    57  		c.Assert(err, gc.ErrorMatches, errStr)
    58  		c.Assert(output, gc.Equals, "")
    59  		c.Assert(store.Credentials[cloud].DefaultRegion, gc.Equals, "")
    60  		return
    61  	}
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	c.Assert(output, gc.Equals, fmt.Sprintf(`Default region in %s set to "us-west-1".`, cloud))
    64  	c.Assert(store.Credentials[cloud].DefaultRegion, gc.Equals, "us-west-1")
    65  }
    66  
    67  func (s *defaultRegionSuite) TestSetDefaultRegion(c *gc.C) {
    68  	store := jujuclienttesting.NewMemStore()
    69  	cmd := cloud.NewSetDefaultRegionCommandForTest(store)
    70  	s.assertSetDefaultRegion(c, cmd, store, "aws", "")
    71  }
    72  
    73  func (s *defaultRegionSuite) TestSetDefaultRegionBuiltIn(c *gc.C) {
    74  	store := jujuclienttesting.NewMemStore()
    75  	cmd := cloud.NewSetDefaultRegionCommandForTest(store)
    76  	// maas has no regions
    77  	s.assertSetDefaultRegion(c, cmd, store, "maas", `cloud maas has no regions`)
    78  }
    79  
    80  func (s *defaultRegionSuite) TestOverwriteDefaultRegion(c *gc.C) {
    81  	store := jujuclienttesting.NewMemStore()
    82  	store.Credentials["aws"] = jujucloud.CloudCredential{DefaultRegion: "us-east-1"}
    83  	cmd := cloud.NewSetDefaultRegionCommandForTest(store)
    84  	s.assertSetDefaultRegion(c, cmd, store, "aws", "")
    85  }
    86  
    87  func (s *defaultRegionSuite) TestCaseInsensitiveRegionSpecification(c *gc.C) {
    88  	store := jujuclienttesting.NewMemStore()
    89  	store.Credentials["aws"] = jujucloud.CloudCredential{DefaultRegion: "us-east-1"}
    90  
    91  	cmd := cloud.NewSetDefaultRegionCommandForTest(store)
    92  	_, err := testing.RunCommand(c, cmd, "aws", "us-WEST-1")
    93  	c.Assert(err, jc.ErrorIsNil)
    94  	c.Assert(store.Credentials["aws"].DefaultRegion, gc.Equals, "us-west-1")
    95  }