github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/jujuclient/jujuclienttesting"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type defaultRegionSuite struct {
    21  	testing.FakeJujuXDGDataHomeSuite
    22  }
    23  
    24  var _ = gc.Suite(&defaultRegionSuite{})
    25  
    26  func (s *defaultRegionSuite) TestBadArgs(c *gc.C) {
    27  	cmd := cloud.NewSetDefaultRegionCommand()
    28  	_, err := testing.RunCommand(c, cmd)
    29  	c.Assert(err, gc.ErrorMatches, "Usage: juju set-default-region <cloud-name> <region>")
    30  	_, err = testing.RunCommand(c, cmd, "cloud", "region", "extra")
    31  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["extra"\]`)
    32  }
    33  
    34  func (s *defaultRegionSuite) TestBadRegion(c *gc.C) {
    35  	cmd := cloud.NewSetDefaultRegionCommand()
    36  	_, err := testing.RunCommand(c, cmd, "aws", "foo")
    37  	c.Assert(err, gc.ErrorMatches, `region "foo" for cloud aws not valid, valid regions are .*`)
    38  }
    39  
    40  func (s *defaultRegionSuite) TestBadCloudName(c *gc.C) {
    41  	cmd := cloud.NewSetDefaultRegionCommand()
    42  	_, err := testing.RunCommand(c, cmd, "somecloud", "us-west-1")
    43  	c.Assert(err, gc.ErrorMatches, `cloud somecloud not valid`)
    44  }
    45  
    46  func (s *defaultRegionSuite) assertSetDefaultRegion(c *gc.C, cmd cmd.Command, store *jujuclienttesting.MemStore, cloud, errStr string) {
    47  	s.assertSetCustomDefaultRegion(c, cmd, store, cloud, "us-west-1", errStr)
    48  }
    49  
    50  func (s *defaultRegionSuite) assertSetCustomDefaultRegion(c *gc.C, cmd cmd.Command, store *jujuclienttesting.MemStore, cloud, desiredDefault, errStr string) {
    51  	ctx, err := testing.RunCommand(c, cmd, cloud, desiredDefault)
    52  	output := testing.Stderr(ctx)
    53  	output = strings.Replace(output, "\n", "", -1)
    54  	if errStr != "" {
    55  		c.Assert(err, gc.ErrorMatches, errStr)
    56  		c.Assert(output, gc.Equals, "")
    57  		c.Assert(store.Credentials[cloud].DefaultRegion, gc.Equals, "")
    58  		return
    59  	}
    60  	c.Assert(err, jc.ErrorIsNil)
    61  	c.Assert(output, gc.Equals, fmt.Sprintf(`Default region in %s set to %q.`, cloud, desiredDefault))
    62  	c.Assert(store.Credentials[cloud].DefaultRegion, gc.Equals, desiredDefault)
    63  }
    64  
    65  func (s *defaultRegionSuite) TestSetDefaultRegion(c *gc.C) {
    66  	store := jujuclienttesting.NewMemStore()
    67  	cmd := cloud.NewSetDefaultRegionCommandForTest(store)
    68  	s.assertSetDefaultRegion(c, cmd, store, "aws", "")
    69  }
    70  
    71  func (s *defaultRegionSuite) TestSetDefaultRegionBuiltIn(c *gc.C) {
    72  	store := jujuclienttesting.NewMemStore()
    73  	cmd := cloud.NewSetDefaultRegionCommandForTest(store)
    74  	// Cloud 'localhost' is of type lxd.
    75  	s.assertSetCustomDefaultRegion(c, cmd, store, "localhost", "localhost", "")
    76  }
    77  
    78  func (s *defaultRegionSuite) TestOverwriteDefaultRegion(c *gc.C) {
    79  	store := jujuclienttesting.NewMemStore()
    80  	store.Credentials["aws"] = jujucloud.CloudCredential{DefaultRegion: "us-east-1"}
    81  	cmd := cloud.NewSetDefaultRegionCommandForTest(store)
    82  	s.assertSetDefaultRegion(c, cmd, store, "aws", "")
    83  }
    84  
    85  func (s *defaultRegionSuite) TestCaseInsensitiveRegionSpecification(c *gc.C) {
    86  	store := jujuclienttesting.NewMemStore()
    87  	store.Credentials["aws"] = jujucloud.CloudCredential{DefaultRegion: "us-east-1"}
    88  
    89  	cmd := cloud.NewSetDefaultRegionCommandForTest(store)
    90  	_, err := testing.RunCommand(c, cmd, "aws", "us-WEST-1")
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	c.Assert(store.Credentials["aws"].DefaultRegion, gc.Equals, "us-west-1")
    93  }