diff --git a/AzurePowershell.Test.targets b/AzurePowershell.Test.targets index 3747a6ce6514..7019cff82326 100644 --- a/AzurePowershell.Test.targets +++ b/AzurePowershell.Test.targets @@ -65,7 +65,8 @@ - + + diff --git a/src/Storage/Commands.Storage.Test/Commands.Storage.Test.csproj b/src/Storage/Commands.Storage.Test/Commands.Storage.Test.csproj index cf64ac0556bf..3ad55c053d3a 100644 --- a/src/Storage/Commands.Storage.Test/Commands.Storage.Test.csproj +++ b/src/Storage/Commands.Storage.Test/Commands.Storage.Test.csproj @@ -203,6 +203,7 @@ + diff --git a/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs b/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs new file mode 100644 index 000000000000..dfc844f4fe19 --- /dev/null +++ b/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs @@ -0,0 +1,246 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using Microsoft.Azure.Commands.Common.Authentication; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; +using Microsoft.WindowsAzure.Commands.ScenarioTest; +using Microsoft.WindowsAzure.Commands.Storage; +using Microsoft.WindowsAzure.Commands.Storage.Common.Cmdlet; +using Microsoft.WindowsAzure.Commands.Utilities.Common; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace Microsoft.WindowsAzure.Management.Storage.Test.Common.Cmdlet +{ + public class StorageContextDisconnectedTests + { + class TestProfileProvider : AzureRmProfileProvider + { + public override T GetProfile() + { + return default(T); + } + } + + class TestSMProfileProvider : AzureSMProfileProvider + { + public override T GetProfile() + { + return default(T); + } + + public override void SetTokenCacheForProfile(IAzureContextContainer profile) + { + + } + } + + class TestContextContainer : IAzureContextContainer + { + List _environments = new List(); + public TestContextContainer() + { + foreach(var environment in AzureEnvironment.PublicEnvironments) + { + _environments.Add(environment.Value); + } + } + public IEnumerable Accounts + { + get; + } = new List(); + + public IAzureContext DefaultContext + { + get; set; + } + + public IEnumerable Environments + { + get { return _environments; } + } + + public IDictionary ExtendedProperties + { + get; + } = new Dictionary(); + + public IEnumerable Subscriptions + { + get; + } = new List(); + + public void Clear() + { + + } + } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void CanCreateStorageContextNameAndKey() + { + AzureSessionInitializer.InitializeAzureSession(); + var smProvider = AzureSMProfileProvider.Instance; + var rmProvider = AzureRmProfileProvider.Instance; + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); + AzureSMProfileProvider.SetInstance(()=> new TestSMProfileProvider(), true); + try + { + var mock = new MockCommandRuntime(); + + AzureSMProfileProvider.Instance.Profile = null; + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); + var cmdlet = new NewAzureStorageContext + { + CommandRuntime = mock, + StorageAccountName = "contosostorage", + StorageAccountKey = "AAAAAAAA", + }; + + cmdlet.SetParameterSet("AccountNameAndKey"); + cmdlet.ExecuteCmdlet(); + var output = mock.OutputPipeline; + Assert.NotNull(output); + var storageContext = output.First() as AzureStorageContext; + Assert.NotNull(storageContext); + Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); + + } + finally + { + AzureSMProfileProvider.SetInstance(() => smProvider, true); + AzureRmProfileProvider.SetInstance(() => rmProvider, true); + } + } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void CanCreateStorageContextInChinaCloud() + { + AzureSessionInitializer.InitializeAzureSession(); + var smProvider = AzureSMProfileProvider.Instance; + var rmProvider = AzureRmProfileProvider.Instance; + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); + AzureSMProfileProvider.SetInstance(() => new TestSMProfileProvider(), true); + try + { + var mock = new MockCommandRuntime(); + + AzureSMProfileProvider.Instance.Profile = null; + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); + var cmdlet = new NewAzureStorageContext + { + CommandRuntime = mock, + StorageAccountName = "contosostorage", + StorageAccountKey = "AAAAAAAA", + Environment = EnvironmentName.AzureChinaCloud + }; + + cmdlet.SetParameterSet("AccountNameAndKeyEnvironment"); + cmdlet.ExecuteCmdlet(); + var output = mock.OutputPipeline; + Assert.NotNull(output); + var storageContext = output.First() as AzureStorageContext; + Assert.NotNull(storageContext); + Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); + Assert.True(storageContext.BlobEndPoint.Contains(".cn")); + + } + finally + { + AzureSMProfileProvider.SetInstance(() => smProvider, true); + AzureRmProfileProvider.SetInstance(() => rmProvider, true); + } + } + + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void CanCreateStorageContextSASToken() + { + AzureSessionInitializer.InitializeAzureSession(); + var smProvider = AzureSMProfileProvider.Instance; + var rmProvider = AzureRmProfileProvider.Instance; + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); + AzureSMProfileProvider.SetInstance(() => new TestSMProfileProvider(), true); + try + { + var mock = new MockCommandRuntime(); + + AzureSMProfileProvider.Instance.Profile = null; + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); + var cmdlet = new NewAzureStorageContext + { + CommandRuntime = mock, + StorageAccountName = "contosostorage", + SasToken = "AAAAAAAA", + }; + + cmdlet.SetParameterSet("SasToken"); + cmdlet.ExecuteCmdlet(); + var output = mock.OutputPipeline; + Assert.NotNull(output); + var storageContext = output.First() as AzureStorageContext; + Assert.NotNull(storageContext); + Assert.Equal("[SasToken]", storageContext.StorageAccountName); + } + finally + { + AzureSMProfileProvider.SetInstance(() => smProvider, true); + AzureRmProfileProvider.SetInstance(() => rmProvider, true); + } + } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void CanCreateStorageContextAnonymous() + { + AzureSessionInitializer.InitializeAzureSession(); + var smProvider = AzureSMProfileProvider.Instance; + var rmProvider = AzureRmProfileProvider.Instance; + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); + AzureSMProfileProvider.SetInstance(() => new TestSMProfileProvider(), true); + try + { + var mock = new MockCommandRuntime(); + + AzureSMProfileProvider.Instance.Profile = null; + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); + var cmdlet = new NewAzureStorageContext + { + CommandRuntime = mock, + StorageAccountName = "contosostorage", + Anonymous = true, + }; + + cmdlet.SetParameterSet("AnonymousAccount"); + cmdlet.ExecuteCmdlet(); + var output = mock.OutputPipeline; + Assert.NotNull(output); + var storageContext = output.First() as AzureStorageContext; + Assert.NotNull(storageContext); + Assert.Equal("[Anonymous]", storageContext.StorageAccountName); + } + finally + { + AzureSMProfileProvider.SetInstance(() => smProvider, true); + AzureRmProfileProvider.SetInstance(() => rmProvider, true); + } + } + + } +} diff --git a/src/Storage/Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs b/src/Storage/Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs index ace513e68ac3..55ddddba2b35 100644 --- a/src/Storage/Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs +++ b/src/Storage/Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs @@ -341,9 +341,9 @@ internal CloudStorageAccount GetStorageAccountWithAzureEnvironment(StorageCreden var profile = SMProfile??RMProfile; if (null != profile) { - if (DefaultContext != null && DefaultContext.Environment != null && string.IsNullOrEmpty(azureEnvironmentName)) + if (profile.DefaultContext != null && profile.DefaultContext.Environment != null && string.IsNullOrEmpty(azureEnvironmentName)) { - azureEnvironment = DefaultContext.Environment; + azureEnvironment = profile.DefaultContext.Environment; if (null == azureEnvironment) {