From c619a441f6b40283379d5c02e1aa77e57cbed6c6 Mon Sep 17 00:00:00 2001 From: markcowl Date: Tue, 9 May 2017 15:24:45 -0700 Subject: [PATCH 1/4] Fix getting storage account from environment in disconnected scenario --- .../Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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) { From 77df06841604bbf1d77c568822f81cb987b89e48 Mon Sep 17 00:00:00 2001 From: markcowl Date: Tue, 9 May 2017 23:20:19 -0700 Subject: [PATCH 2/4] Adding check-in test for disconnected scenario --- AzurePowershell.Test.targets | 3 +- .../Commands.Storage.Test.csproj | 1 + .../Cmdlet/StorageContextDisconnectedTests.cs | 123 ++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs 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..92afa99dd5b7 --- /dev/null +++ b/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs @@ -0,0 +1,123 @@ +// ---------------------------------------------------------------------------------- +// +// 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; +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.Linq; +using Xunit; +using System; +using System.Collections.Generic; + +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 + { + public IEnumerable Accounts + { + get; + } = new List(); + + public IAzureContext DefaultContext + { + get; set; + } + + public IEnumerable Environments + { + get; + } = new List(); + + public IDictionary ExtendedProperties + { + get; + } = new Dictionary(); + + public IEnumerable Subscriptions + { + get; + } = new List(); + + public void Clear() + { + + } + } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void CanCreateStorageContext() + { + 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); + } + } + } +} From 49074e9efe54defddab91613d8161884d07cf7eb Mon Sep 17 00:00:00 2001 From: markcowl Date: Wed, 10 May 2017 00:02:06 -0700 Subject: [PATCH 3/4] Adding tests for additional offline scenarios --- .../Cmdlet/StorageContextDisconnectedTests.cs | 111 +++++++++++++++++- 1 file changed, 107 insertions(+), 4 deletions(-) diff --git a/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs b/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs index 92afa99dd5b7..0cb5e6939679 100644 --- a/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs +++ b/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs @@ -14,16 +14,14 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; -using Microsoft.WindowsAzure.Commands.Common; 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; -using System; -using System.Collections.Generic; namespace Microsoft.WindowsAzure.Management.Storage.Test.Common.Cmdlet { @@ -85,7 +83,7 @@ public void Clear() [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] - public void CanCreateStorageContext() + public void CanCreateStorageContextNameAndKey() { AzureSessionInitializer.InitializeAzureSession(); var smProvider = AzureSMProfileProvider.Instance; @@ -112,6 +110,36 @@ public void CanCreateStorageContext() var storageContext = output.First() as AzureStorageContext; Assert.NotNull(storageContext); Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); + + cmdlet = new NewAzureStorageContext + { + CommandRuntime = mock, + StorageAccountName = "contosostorage", + Anonymous = true, + }; + + cmdlet.SetParameterSet("AnonymousAccount"); + cmdlet.ExecuteCmdlet(); + output = mock.OutputPipeline; + Assert.NotNull(output); + storageContext = output.First() as AzureStorageContext; + Assert.NotNull(storageContext); + Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); + + cmdlet = new NewAzureStorageContext + { + CommandRuntime = mock, + StorageAccountName = "contosostorage", + SasToken = "AAAAAAAA", + }; + + cmdlet.SetParameterSet("SasToken"); + cmdlet.ExecuteCmdlet(); + output = mock.OutputPipeline; + Assert.NotNull(output); + storageContext = output.First() as AzureStorageContext; + Assert.NotNull(storageContext); + Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); } finally { @@ -119,5 +147,80 @@ public void CanCreateStorageContext() 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); + } + } + } } From 1851ceb230e38506e59ad51d2decd64e04bfc918 Mon Sep 17 00:00:00 2001 From: markcowl Date: Wed, 10 May 2017 00:36:54 -0700 Subject: [PATCH 4/4] Cleaning up tests and adding test for additional environment --- .../Cmdlet/StorageContextDisconnectedTests.cs | 60 ++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs b/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs index 0cb5e6939679..dfc844f4fe19 100644 --- a/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs +++ b/src/Storage/Commands.Storage.Test/Common/Cmdlet/StorageContextDisconnectedTests.cs @@ -50,6 +50,14 @@ 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; @@ -62,8 +70,8 @@ public IAzureContext DefaultContext public IEnumerable Environments { - get; - } = new List(); + get { return _environments; } + } public IDictionary ExtendedProperties { @@ -111,35 +119,46 @@ public void CanCreateStorageContextNameAndKey() Assert.NotNull(storageContext); Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); - cmdlet = new NewAzureStorageContext - { - CommandRuntime = mock, - StorageAccountName = "contosostorage", - Anonymous = true, - }; + } + finally + { + AzureSMProfileProvider.SetInstance(() => smProvider, true); + AzureRmProfileProvider.SetInstance(() => rmProvider, true); + } + } - cmdlet.SetParameterSet("AnonymousAccount"); - cmdlet.ExecuteCmdlet(); - output = mock.OutputPipeline; - Assert.NotNull(output); - storageContext = output.First() as AzureStorageContext; - Assert.NotNull(storageContext); - Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); + [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(); - cmdlet = new NewAzureStorageContext + AzureSMProfileProvider.Instance.Profile = null; + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); + var cmdlet = new NewAzureStorageContext { CommandRuntime = mock, StorageAccountName = "contosostorage", - SasToken = "AAAAAAAA", + StorageAccountKey = "AAAAAAAA", + Environment = EnvironmentName.AzureChinaCloud }; - cmdlet.SetParameterSet("SasToken"); + cmdlet.SetParameterSet("AccountNameAndKeyEnvironment"); cmdlet.ExecuteCmdlet(); - output = mock.OutputPipeline; + var output = mock.OutputPipeline; Assert.NotNull(output); - storageContext = output.First() as AzureStorageContext; + var storageContext = output.First() as AzureStorageContext; Assert.NotNull(storageContext); Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); + Assert.True(storageContext.BlobEndPoint.Contains(".cn")); + } finally { @@ -148,6 +167,7 @@ public void CanCreateStorageContextNameAndKey() } } + [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void CanCreateStorageContextSASToken()