github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/azure/credentials_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azure_test
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/Azure/go-autorest/autorest"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	"github.com/juju/utils"
    14  	"github.com/juju/utils/clock"
    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  	"github.com/juju/juju/provider/azure"
    21  	coretesting "github.com/juju/juju/testing"
    22  )
    23  
    24  type credentialsSuite struct {
    25  	testing.IsolationSuite
    26  	interactiveCreateServicePrincipalCreator
    27  	provider environs.EnvironProvider
    28  }
    29  
    30  var _ = gc.Suite(&credentialsSuite{})
    31  
    32  func (s *credentialsSuite) SetUpTest(c *gc.C) {
    33  	s.IsolationSuite.SetUpTest(c)
    34  	s.interactiveCreateServicePrincipalCreator = interactiveCreateServicePrincipalCreator{}
    35  	s.provider = newProvider(c, azure.ProviderConfig{
    36  		InteractiveCreateServicePrincipal: s.interactiveCreateServicePrincipalCreator.InteractiveCreateServicePrincipal,
    37  	})
    38  }
    39  
    40  func (s *credentialsSuite) TestCredentialSchemas(c *gc.C) {
    41  	envtesting.AssertProviderAuthTypes(c, s.provider,
    42  		"interactive",
    43  		"service-principal-secret",
    44  	)
    45  }
    46  
    47  var sampleCredentialAttributes = map[string]string{
    48  	"application-id":       "application",
    49  	"application-password": "password",
    50  	"subscription-id":      "subscription",
    51  }
    52  
    53  func (s *credentialsSuite) TestServicePrincipalSecretCredentialsValid(c *gc.C) {
    54  	envtesting.AssertProviderCredentialsValid(c, s.provider, "service-principal-secret", map[string]string{
    55  		"application-id":       "application",
    56  		"application-password": "password",
    57  		"subscription-id":      "subscription",
    58  	})
    59  }
    60  
    61  func (s *credentialsSuite) TestServicePrincipalSecretHiddenAttributes(c *gc.C) {
    62  	envtesting.AssertProviderCredentialsAttributesHidden(c, s.provider, "service-principal-secret", "application-password")
    63  }
    64  
    65  func (s *credentialsSuite) TestDetectCredentials(c *gc.C) {
    66  	_, err := s.provider.DetectCredentials()
    67  	c.Assert(err, jc.Satisfies, errors.IsNotFound)
    68  }
    69  
    70  func (s *credentialsSuite) TestFinalizeCredentialInteractive(c *gc.C) {
    71  	in := cloud.NewCredential("interactive", map[string]string{"subscription-id": "subscription"})
    72  	ctx := coretesting.Context(c)
    73  	out, err := s.provider.FinalizeCredential(ctx, environs.FinalizeCredentialParams{
    74  		Credential:            in,
    75  		CloudEndpoint:         "https://arm.invalid",
    76  		CloudIdentityEndpoint: "https://graph.invalid",
    77  	})
    78  	c.Assert(err, jc.ErrorIsNil)
    79  	c.Assert(out, gc.NotNil)
    80  	c.Assert(out.AuthType(), gc.Equals, cloud.AuthType("service-principal-secret"))
    81  	c.Assert(out.Attributes(), jc.DeepEquals, map[string]string{
    82  		"application-id":       "appid",
    83  		"application-password": "service-principal-password",
    84  		"subscription-id":      "subscription",
    85  	})
    86  
    87  	s.interactiveCreateServicePrincipalCreator.CheckCallNames(c, "InteractiveCreateServicePrincipal")
    88  	args := s.interactiveCreateServicePrincipalCreator.Calls()[0].Args
    89  	c.Assert(args[3], gc.Equals, "https://arm.invalid")
    90  	c.Assert(args[4], gc.Equals, "https://graph.invalid")
    91  	c.Assert(args[5], gc.Equals, "subscription")
    92  }
    93  
    94  func (s *credentialsSuite) TestFinalizeCredentialInteractiveError(c *gc.C) {
    95  	in := cloud.NewCredential("interactive", map[string]string{"subscription-id": "subscription"})
    96  	s.interactiveCreateServicePrincipalCreator.SetErrors(errors.New("blargh"))
    97  	ctx := coretesting.Context(c)
    98  	_, err := s.provider.FinalizeCredential(ctx, environs.FinalizeCredentialParams{
    99  		Credential:            in,
   100  		CloudEndpoint:         "https://arm.invalid",
   101  		CloudIdentityEndpoint: "https://graph.invalid",
   102  	})
   103  	c.Assert(err, gc.ErrorMatches, "blargh")
   104  }
   105  
   106  type interactiveCreateServicePrincipalCreator struct {
   107  	testing.Stub
   108  }
   109  
   110  func (c *interactiveCreateServicePrincipalCreator) InteractiveCreateServicePrincipal(
   111  	stderr io.Writer,
   112  	sender autorest.Sender,
   113  	requestInspector autorest.PrepareDecorator,
   114  	resourceManagerEndpoint string,
   115  	graphEndpoint string,
   116  	subscriptionId string,
   117  	clock clock.Clock,
   118  	newUUID func() (utils.UUID, error),
   119  ) (appId, password string, _ error) {
   120  	c.MethodCall(
   121  		c, "InteractiveCreateServicePrincipal",
   122  		stderr, sender, requestInspector, resourceManagerEndpoint,
   123  		graphEndpoint, subscriptionId, clock, newUUID,
   124  	)
   125  	return "appid", "service-principal-password", c.NextErr()
   126  }