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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package modelcmd_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"io/ioutil"
    11  	"path/filepath"
    12  
    13  	"github.com/juju/juju/cloud"
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  	"github.com/juju/juju/environs"
    16  	"github.com/juju/juju/jujuclient/jujuclienttesting"
    17  	_ "github.com/juju/juju/provider/dummy"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  func init() {
    22  	dummyProvider, err := environs.Provider("dummy")
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  	environs.RegisterProvider("fake", mockProvider{dummyProvider})
    27  }
    28  
    29  type mockProvider struct {
    30  	environs.EnvironProvider
    31  }
    32  
    33  func (mockProvider) CredentialSchemas() map[cloud.AuthType]cloud.CredentialSchema {
    34  	schema := cloud.CredentialSchema{
    35  		{
    36  			"username", cloud.CredentialAttr{},
    37  		}, {
    38  			"password", cloud.CredentialAttr{},
    39  		}, {
    40  			"key", cloud.CredentialAttr{FileAttr: "key-file"},
    41  		},
    42  	}
    43  	return map[cloud.AuthType]cloud.CredentialSchema{
    44  		cloud.UserPassAuthType: schema,
    45  	}
    46  }
    47  
    48  type credentialsSuite struct {
    49  	testing.FakeJujuXDGDataHomeSuite
    50  }
    51  
    52  var _ = gc.Suite(&credentialsSuite{})
    53  
    54  func (s *credentialsSuite) assertGetCredentials(c *gc.C, region string) {
    55  	dir := c.MkDir()
    56  	keyFile := filepath.Join(dir, "keyfile")
    57  	err := ioutil.WriteFile(keyFile, []byte("value"), 0600)
    58  	c.Assert(err, jc.ErrorIsNil)
    59  
    60  	store := jujuclienttesting.NewMemStore()
    61  	store.Credentials["cloud"] = cloud.CloudCredential{
    62  		DefaultRegion: "default-region",
    63  		AuthCredentials: map[string]cloud.Credential{
    64  			"secrets": cloud.NewCredential(cloud.UserPassAuthType, map[string]string{
    65  				"username": "user",
    66  				"password": "sekret",
    67  				"key-file": keyFile,
    68  			}),
    69  		},
    70  	}
    71  
    72  	credential, credentialName, regionName, err := modelcmd.GetCredentials(
    73  		store, region, "secrets", "cloud", "fake",
    74  	)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	expectedRegion := region
    77  	if expectedRegion == "" {
    78  		expectedRegion = "default-region"
    79  	}
    80  	c.Assert(regionName, gc.Equals, expectedRegion)
    81  	c.Assert(credentialName, gc.Equals, "secrets")
    82  	c.Assert(credential.Attributes(), jc.DeepEquals, map[string]string{
    83  		"key":      "value",
    84  		"username": "user",
    85  		"password": "sekret",
    86  	})
    87  }
    88  
    89  func (s *credentialsSuite) TestGetCredentialsDefaultRegion(c *gc.C) {
    90  	s.assertGetCredentials(c, "")
    91  }
    92  
    93  func (s *credentialsSuite) TestGetCredentials(c *gc.C) {
    94  	s.assertGetCredentials(c, "region")
    95  }