github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/provider/openstack/credentials_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package openstack_test
     5  
     6  import (
     7  	"io/ioutil"
     8  	"path/filepath"
     9  	"runtime"
    10  
    11  	"github.com/juju/errors"
    12  	"github.com/juju/testing"
    13  	jc "github.com/juju/testing/checkers"
    14  	"github.com/juju/utils"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/cloud"
    18  	"github.com/juju/juju/environs"
    19  	envtesting "github.com/juju/juju/environs/testing"
    20  )
    21  
    22  type credentialsSuite struct {
    23  	testing.IsolationSuite
    24  	provider environs.EnvironProvider
    25  }
    26  
    27  var _ = gc.Suite(&credentialsSuite{})
    28  
    29  func (s *credentialsSuite) SetUpTest(c *gc.C) {
    30  	s.IsolationSuite.SetUpTest(c)
    31  
    32  	var err error
    33  	s.provider, err = environs.Provider("openstack")
    34  	c.Assert(err, jc.ErrorIsNil)
    35  }
    36  
    37  func (s *credentialsSuite) TestCredentialSchemas(c *gc.C) {
    38  	envtesting.AssertProviderAuthTypes(c, s.provider, "access-key", "userpass")
    39  }
    40  
    41  func (s *credentialsSuite) TestAccessKeyCredentialsValid(c *gc.C) {
    42  	envtesting.AssertProviderCredentialsValid(c, s.provider, "access-key", map[string]string{
    43  		"access-key":  "key",
    44  		"secret-key":  "secret",
    45  		"tenant-name": "gary",
    46  	})
    47  }
    48  
    49  func (s *credentialsSuite) TestAccessKeyHiddenAttributes(c *gc.C) {
    50  	envtesting.AssertProviderCredentialsAttributesHidden(c, s.provider, "access-key", "secret-key")
    51  }
    52  
    53  func (s *credentialsSuite) TestUserPassCredentialsValid(c *gc.C) {
    54  	envtesting.AssertProviderCredentialsValid(c, s.provider, "userpass", map[string]string{
    55  		"username":    "bob",
    56  		"password":    "dobbs",
    57  		"tenant-name": "gary",
    58  	})
    59  }
    60  
    61  func (s *credentialsSuite) TestUserPassHiddenAttributes(c *gc.C) {
    62  	envtesting.AssertProviderCredentialsAttributesHidden(c, s.provider, "userpass", "password")
    63  }
    64  
    65  func (s *credentialsSuite) TestDetectCredentialsNotFound(c *gc.C) {
    66  	// No environment variables set, so no credentials should be found.
    67  	_, err := s.provider.DetectCredentials()
    68  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    69  }
    70  
    71  func (s *credentialsSuite) TestDetectCredentialsAccessKeyEnvironmentVariables(c *gc.C) {
    72  	s.PatchEnvironment("USER", "fred")
    73  	s.PatchEnvironment("OS_TENANT_NAME", "gary")
    74  	s.PatchEnvironment("OS_ACCESS_KEY", "key-id")
    75  	s.PatchEnvironment("OS_SECRET_KEY", "secret-access-key")
    76  	s.PatchEnvironment("OS_REGION_NAME", "east")
    77  
    78  	credentials, err := s.provider.DetectCredentials()
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	c.Assert(credentials.DefaultRegion, gc.Equals, "east")
    81  	expected := cloud.NewCredential(
    82  		cloud.AccessKeyAuthType, map[string]string{
    83  			"access-key":  "key-id",
    84  			"secret-key":  "secret-access-key",
    85  			"tenant-name": "gary",
    86  		},
    87  	)
    88  	expected.Label = `openstack region "east" project "gary" user "fred"`
    89  	c.Assert(credentials.AuthCredentials["fred"], jc.DeepEquals, expected)
    90  }
    91  
    92  func (s *credentialsSuite) TestDetectCredentialsUserPassEnvironmentVariables(c *gc.C) {
    93  	s.PatchEnvironment("USER", "fred")
    94  	s.PatchEnvironment("OS_TENANT_NAME", "gary")
    95  	s.PatchEnvironment("OS_USERNAME", "bob")
    96  	s.PatchEnvironment("OS_PASSWORD", "dobbs")
    97  	s.PatchEnvironment("OS_REGION_NAME", "west")
    98  
    99  	credentials, err := s.provider.DetectCredentials()
   100  	c.Assert(err, jc.ErrorIsNil)
   101  	c.Assert(credentials.DefaultRegion, gc.Equals, "west")
   102  	expected := cloud.NewCredential(
   103  		cloud.UserPassAuthType, map[string]string{
   104  			"username":    "bob",
   105  			"password":    "dobbs",
   106  			"tenant-name": "gary",
   107  			"domain-name": "",
   108  		},
   109  	)
   110  	expected.Label = `openstack region "west" project "gary" user "bob"`
   111  	c.Assert(credentials.AuthCredentials["bob"], jc.DeepEquals, expected)
   112  }
   113  
   114  func (s *credentialsSuite) TestDetectCredentialsNovarc(c *gc.C) {
   115  	if runtime.GOOS != "linux" {
   116  		c.Skip("not running linux")
   117  	}
   118  	home := utils.Home()
   119  	dir := c.MkDir()
   120  	err := utils.SetHome(dir)
   121  	c.Assert(err, jc.ErrorIsNil)
   122  	s.AddCleanup(func(*gc.C) {
   123  		err := utils.SetHome(home)
   124  		c.Assert(err, jc.ErrorIsNil)
   125  	})
   126  
   127  	content := `
   128  # Some secrets
   129  export OS_TENANT_NAME=gary
   130  EXPORT OS_USERNAME=bob
   131    export  OS_PASSWORD = dobbs
   132  OS_REGION_NAME=region  
   133  `[1:]
   134  	novarc := filepath.Join(dir, ".novarc")
   135  	err = ioutil.WriteFile(novarc, []byte(content), 0600)
   136  
   137  	credentials, err := s.provider.DetectCredentials()
   138  	c.Assert(err, jc.ErrorIsNil)
   139  	c.Assert(credentials.DefaultRegion, gc.Equals, "region")
   140  	expected := cloud.NewCredential(
   141  		cloud.UserPassAuthType, map[string]string{
   142  			"username":    "bob",
   143  			"password":    "dobbs",
   144  			"tenant-name": "gary",
   145  			"domain-name": "",
   146  		},
   147  	)
   148  	expected.Label = `openstack region "region" project "gary" user "bob"`
   149  	c.Assert(credentials.AuthCredentials["bob"], jc.DeepEquals, expected)
   150  }