github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/cloud/defaultcredential_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  	jujucloud "github.com/juju/juju/cloud"
    14  	"github.com/juju/juju/cmd/juju/cloud"
    15  	"github.com/juju/juju/juju/osenv"
    16  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  type defaultCredentialSuite struct {
    21  	testing.BaseSuite
    22  }
    23  
    24  var _ = gc.Suite(&defaultCredentialSuite{})
    25  
    26  func (s *defaultCredentialSuite) SetUpTest(c *gc.C) {
    27  	origHome := osenv.SetJujuXDGDataHome(c.MkDir())
    28  	s.AddCleanup(func(*gc.C) { osenv.SetJujuXDGDataHome(origHome) })
    29  }
    30  
    31  func (s *defaultCredentialSuite) TestBadArgs(c *gc.C) {
    32  	cmd := cloud.NewSetDefaultCredentialCommand()
    33  	_, err := testing.RunCommand(c, cmd)
    34  	c.Assert(err, gc.ErrorMatches, "Usage: juju set-default-credential <cloud-name> <credential-name>")
    35  	_, err = testing.RunCommand(c, cmd, "cloud", "credential", "extra")
    36  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["extra"\]`)
    37  }
    38  
    39  func (s *defaultCredentialSuite) TestBadCredential(c *gc.C) {
    40  	cmd := cloud.NewSetDefaultCredentialCommand()
    41  	_, err := testing.RunCommand(c, cmd, "aws", "foo")
    42  	c.Assert(err, gc.ErrorMatches, `credential "foo" for cloud aws not valid`)
    43  }
    44  
    45  func (s *defaultCredentialSuite) TestBadCloudName(c *gc.C) {
    46  	cmd := cloud.NewSetDefaultCredentialCommand()
    47  	_, err := testing.RunCommand(c, cmd, "somecloud", "us-west-1")
    48  	c.Assert(err, gc.ErrorMatches, `cloud somecloud not valid`)
    49  }
    50  
    51  func (s *defaultCredentialSuite) assertSetDefaultCredential(c *gc.C, cloudName string) {
    52  	store := jujuclienttesting.NewMemStore()
    53  	store.Credentials[cloudName] = jujucloud.CloudCredential{
    54  		AuthCredentials: map[string]jujucloud.Credential{
    55  			"my-sekrets": {},
    56  		},
    57  	}
    58  	cmd := cloud.NewSetDefaultCredentialCommandForTest(store)
    59  	ctx, err := testing.RunCommand(c, cmd, cloudName, "my-sekrets")
    60  	c.Assert(err, jc.ErrorIsNil)
    61  	output := testing.Stderr(ctx)
    62  	output = strings.Replace(output, "\n", "", -1)
    63  	c.Assert(output, gc.Equals, fmt.Sprintf(`Default credential for %s set to "my-sekrets".`, cloudName))
    64  	c.Assert(store.Credentials[cloudName].DefaultCredential, gc.Equals, "my-sekrets")
    65  }
    66  
    67  func (s *defaultCredentialSuite) TestSetDefaultCredential(c *gc.C) {
    68  	s.assertSetDefaultCredential(c, "aws")
    69  }
    70  
    71  func (s *defaultCredentialSuite) TestSetDefaultCredentialBuiltIn(c *gc.C) {
    72  	s.assertSetDefaultCredential(c, "maas")
    73  }