github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 s.assertConfigInvalid(c, testing.Attrs{"controller-resource-group": ""}, `"controller-resource-group" config not specified`) 79 } 80 81 func (s *configSuite) TestValidateStorageAccountCantChange(c *gc.C) { 82 cfgOld := makeTestModelConfig(c, testing.Attrs{"storage-account": "abc"}) 83 _, err := s.provider.Validate(cfgOld, cfgOld) 84 c.Assert(err, jc.ErrorIsNil) 85 86 cfgNew := makeTestModelConfig(c) // no storage-account attribute 87 _, err = s.provider.Validate(cfgNew, cfgOld) 88 c.Assert(err, gc.ErrorMatches, `cannot remove immutable "storage-account" config`) 89 90 cfgNew = makeTestModelConfig(c, testing.Attrs{"storage-account": "def"}) 91 _, err = s.provider.Validate(cfgNew, cfgOld) 92 c.Assert(err, gc.ErrorMatches, `cannot change immutable "storage-account" config \(abc -> def\)`) 93 } 94 95 func (s *configSuite) assertConfigValid(c *gc.C, attrs testing.Attrs) { 96 cfg := makeTestModelConfig(c, attrs) 97 _, err := s.provider.Validate(cfg, nil) 98 c.Assert(err, jc.ErrorIsNil) 99 } 100 101 func (s *configSuite) assertConfigInvalid(c *gc.C, attrs testing.Attrs, expect string) { 102 cfg := makeTestModelConfig(c, attrs) 103 _, err := s.provider.Validate(cfg, nil) 104 c.Assert(err, gc.ErrorMatches, expect) 105 } 106 107 func makeTestModelConfig(c *gc.C, extra ...testing.Attrs) *config.Config { 108 attrs := testing.Attrs{ 109 "type": "azure", 110 "application-id": fakeApplicationId, 111 "tenant-id": fakeTenantId, 112 "application-password": "opensezme", 113 "subscription-id": fakeSubscriptionId, 114 "location": "westus", 115 "endpoint": "https://api.azurestack.local", 116 "storage-endpoint": "https://storage.azurestack.local", 117 "controller-resource-group": "arbitrary", 118 "agent-version": "1.2.3", 119 } 120 for _, extra := range extra { 121 attrs = attrs.Merge(extra) 122 } 123 attrs = testing.FakeConfig().Merge(attrs) 124 cfg, err := config.New(config.NoDefaults, attrs) 125 c.Assert(err, jc.ErrorIsNil) 126 return cfg 127 }