github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/provider/azure/config_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azure_test
     5  
     6  import (
     7  	"github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/mocks"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/environs"
    12  	"github.com/juju/juju/environs/config"
    13  	"github.com/juju/juju/provider/azure"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  const (
    18  	fakeApplicationId     = "00000000-0000-0000-0000-000000000000"
    19  	fakeTenantId          = "11111111-1111-1111-1111-111111111111"
    20  	fakeSubscriptionId    = "22222222-2222-2222-2222-222222222222"
    21  	fakeStorageAccount    = "mrblobby"
    22  	fakeStorageAccountKey = "quay"
    23  )
    24  
    25  type configSuite struct {
    26  	testing.BaseSuite
    27  
    28  	provider environs.EnvironProvider
    29  }
    30  
    31  var _ = gc.Suite(&configSuite{})
    32  
    33  func (s *configSuite) SetUpTest(c *gc.C) {
    34  	s.BaseSuite.SetUpTest(c)
    35  	s.provider, _ = newProviders(c, azure.ProviderConfig{
    36  		Sender: mocks.NewSender(),
    37  	})
    38  }
    39  
    40  func (s *configSuite) TestValidateNew(c *gc.C) {
    41  	s.assertConfigValid(c, nil)
    42  }
    43  
    44  func (s *configSuite) TestValidateInvalidStorageAccountType(c *gc.C) {
    45  	s.assertConfigInvalid(
    46  		c, testing.Attrs{"storage-account-type": "savings"},
    47  		`invalid storage account type "savings", expected one of: \["Standard_LRS" "Standard_GRS" "Standard_RAGRS" "Standard_ZRS" "Premium_LRS"\]`,
    48  	)
    49  }
    50  
    51  func (s *configSuite) TestValidateInvalidFirewallMode(c *gc.C) {
    52  	s.assertConfigInvalid(
    53  		c, testing.Attrs{"firewall-mode": "global"},
    54  		"global firewall mode is not supported",
    55  	)
    56  }
    57  
    58  func (s *configSuite) TestValidateLocation(c *gc.C) {
    59  	s.assertConfigInvalid(c, testing.Attrs{"location": ""}, `"location" config not specified`)
    60  	// We don't validate locations, because new locations may be added.
    61  	// Azure will complain if the location is invalid anyway.
    62  	s.assertConfigValid(c, testing.Attrs{"location": "eurasia"})
    63  }
    64  
    65  func (s *configSuite) TestValidateModelNameLength(c *gc.C) {
    66  	s.assertConfigInvalid(
    67  		c, testing.Attrs{"name": "someextremelyoverlylongishmodelname"},
    68  		`resource group name "juju-someextremelyoverlylongishmodelname-model-deadbeef-0bad-400d-8000-4b1d0d06f00d" is too long
    69  
    70  Please choose a model name of no more than 32 characters.`)
    71  }
    72  
    73  func (s *configSuite) TestValidateInvalidCredentials(c *gc.C) {
    74  	s.assertConfigInvalid(c, testing.Attrs{"application-id": ""}, `"application-id" config not specified`)
    75  	s.assertConfigInvalid(c, testing.Attrs{"application-password": ""}, `"application-password" config not specified`)
    76  	s.assertConfigInvalid(c, testing.Attrs{"tenant-id": ""}, `"tenant-id" config not specified`)
    77  	s.assertConfigInvalid(c, testing.Attrs{"subscription-id": ""}, `"subscription-id" config not specified`)
    78  }
    79  
    80  func (s *configSuite) TestValidateStorageAccountCantChange(c *gc.C) {
    81  	cfgOld := makeTestModelConfig(c, testing.Attrs{"storage-account": "abc"})
    82  	_, err := s.provider.Validate(cfgOld, cfgOld)
    83  	c.Assert(err, jc.ErrorIsNil)
    84  
    85  	cfgNew := makeTestModelConfig(c) // no storage-account attribute
    86  	_, err = s.provider.Validate(cfgNew, cfgOld)
    87  	c.Assert(err, gc.ErrorMatches, `cannot remove immutable "storage-account" config`)
    88  
    89  	cfgNew = makeTestModelConfig(c, testing.Attrs{"storage-account": "def"})
    90  	_, err = s.provider.Validate(cfgNew, cfgOld)
    91  	c.Assert(err, gc.ErrorMatches, `cannot change immutable "storage-account" config \(abc -> def\)`)
    92  }
    93  
    94  func (s *configSuite) assertConfigValid(c *gc.C, attrs testing.Attrs) {
    95  	cfg := makeTestModelConfig(c, attrs)
    96  	_, err := s.provider.Validate(cfg, nil)
    97  	c.Assert(err, jc.ErrorIsNil)
    98  }
    99  
   100  func (s *configSuite) assertConfigInvalid(c *gc.C, attrs testing.Attrs, expect string) {
   101  	cfg := makeTestModelConfig(c, attrs)
   102  	_, err := s.provider.Validate(cfg, nil)
   103  	c.Assert(err, gc.ErrorMatches, expect)
   104  }
   105  
   106  func makeTestModelConfig(c *gc.C, extra ...testing.Attrs) *config.Config {
   107  	attrs := testing.Attrs{
   108  		"type":                 "azure",
   109  		"application-id":       fakeApplicationId,
   110  		"tenant-id":            fakeTenantId,
   111  		"application-password": "opensezme",
   112  		"subscription-id":      fakeSubscriptionId,
   113  		"location":             "westus",
   114  		"endpoint":             "https://api.azurestack.local",
   115  		"storage-endpoint":     "https://storage.azurestack.local",
   116  		"agent-version":        "1.2.3",
   117  	}
   118  	for _, extra := range extra {
   119  		attrs = attrs.Merge(extra)
   120  	}
   121  	attrs = testing.FakeConfig().Merge(attrs)
   122  	cfg, err := config.New(config.NoDefaults, attrs)
   123  	c.Assert(err, jc.ErrorIsNil)
   124  	return cfg
   125  }