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