Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getting storage account from environment in disconnected scenario #3938

Merged
merged 4 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AzurePowershell.Test.targets
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
<AsmXUnitTests Include=".\src\ServiceManagement\Common\Commands.ScenarioTest\bin\Debug\Microsoft.WindowsAzure.Commands.ScenarioTest.dll"/>
<AsmXUnitTests Include=".\src\ServiceManagement\RecoveryServices\Commands.RecoveryServices.Test\bin\Debug\Microsoft.Azure.Commands.RecoveryServices.Test.dll"/>
<AsmXUnitTests Include=".\src\ServiceManagement\Network\Commands.Network.Test\bin\Debug\Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.dll"/>
</ItemGroup>
<AsmXUnitTests Include=".\src\Storage\Commands.Storage.Test\bin\Debug\Microsoft.WindowsAzure.Commands.Storage.Test.dll"/>
</ItemGroup>
<ItemGroup Condition=" '$(scope)' == 'all' ">
<XUnitTests Include=".\tools\StaticAnalysis\StaticAnalysis.Test\bin\Debug\StaticAnalysis.Test.dll"/>
<XUnitTests Include=".\src\ResourceManager\ApiManagement\Commands.ApiManagement.Test\bin\Debug\Microsoft.Azure.Commands.ApiManagement.Test.dll"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
<Compile Include="Common\Cmdlet\NewAzureStorageContextTest.cs" />
<Compile Include="Common\Cmdlet\SetAzureStorageServiceLoggingTest.cs" />
<Compile Include="Common\Cmdlet\SetAzureStorageServiceHourMetricsTest.cs" />
<Compile Include="Common\Cmdlet\StorageContextDisconnectedTests.cs" />
<Compile Include="Common\CommunicationExceptionUtilTest.cs" />
<Compile Include="Common\FileNamingGenerator.cs" />
<Compile Include="Common\IStorageManagement.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T>()
{
return default(T);
}
}

class TestSMProfileProvider : AzureSMProfileProvider
{
public override T GetProfile<T>()
{
return default(T);
}

public override void SetTokenCacheForProfile(IAzureContextContainer profile)
{

}
}

class TestContextContainer : IAzureContextContainer
{
List<IAzureEnvironment> _environments = new List<IAzureEnvironment>();
public TestContextContainer()
{
foreach(var environment in AzureEnvironment.PublicEnvironments)
{
_environments.Add(environment.Value);
}
}
public IEnumerable<IAzureAccount> Accounts
{
get;
} = new List<IAzureAccount>();

public IAzureContext DefaultContext
{
get; set;
}

public IEnumerable<IAzureEnvironment> Environments
{
get { return _environments; }
}

public IDictionary<string, string> ExtendedProperties
{
get;
} = new Dictionary<string, string>();

public IEnumerable<IAzureSubscription> Subscriptions
{
get;
} = new List<IAzureSubscription>();

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);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down