Skip to content

Fix SQL tests in Profile #148

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

Merged
merged 12 commits into from
Feb 9, 2015
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.6-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public static class WindowsAzureSubscriptionExtensions
{
private static Dictionary<Guid, CloudStorageAccount> storageAccountCache = new Dictionary<Guid,CloudStorageAccount>();

public static CloudStorageAccount GetCloudStorageAccount(this AzureSubscription subscription)
public static CloudStorageAccount GetCloudStorageAccount(this AzureSubscription subscription, AzureProfile profile)
{
if (subscription == null)
{
return null;
}

using (var storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>(subscription, AzureEnvironment.Endpoint.ServiceManagement))
using (var storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>(profile, subscription, AzureEnvironment.Endpoint.ServiceManagement))
{
return StorageUtilities.GenerateCloudStorageAccount(
storageClient, subscription.GetProperty(AzureSubscription.Property.StorageAccount));
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Storage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package id="Hyak.Common" version="1.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.6-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.6-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<HintPath>..\..\packages\Microsoft.Azure.Common.2.0.1\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
Expand Down
11 changes: 4 additions & 7 deletions src/Common/Commands.Common.Test/Common/ProfileCmdltsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public ProfileCmdltsTests() : base()
AzureSession.DataStore = dataStore;
commandRuntimeMock = new MockCommandRuntime();
SetMockData();
AzureSession.Profile = new AzureProfile();
}

[Fact]
Expand Down Expand Up @@ -89,7 +88,7 @@ public void ClearAzureProfileClearsCustomProfile()

cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.Force = new SwitchParameter(true);
cmdlt.SubscriptionDataFile = subscriptionDataFile;
cmdlt.Profile = new AzureProfile(subscriptionDataFile);

// Act
cmdlt.InvokeBeginProcessing();
Expand Down Expand Up @@ -507,16 +506,15 @@ public void SelectAzureSubscriptionByNameUpdatesProfile()
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
cmdlt.SubscriptionName = azureSubscription2.Name;
Assert.NotEqual(azureSubscription2.Id, AzureSession.Profile.CurrentContext.Subscription.Id);

// Act
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

// Verify
Assert.NotNull(AzureSession.Profile.CurrentContext.Subscription);
Assert.Equal(azureSubscription2.Id, AzureSession.Profile.CurrentContext.Subscription.Id);
Assert.NotNull(cmdlt.Profile.Context.Subscription);
Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
}

[Fact]
Expand Down Expand Up @@ -637,7 +635,6 @@ public void SelectAzureSubscriptionByInvalidGuidThrowsException()
cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
string invalidGuid = "foo";
cmdlt.SubscriptionId = invalidGuid;
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);

// Act
cmdlt.InvokeBeginProcessing();
Expand Down
12 changes: 8 additions & 4 deletions src/Common/Commands.Common.Test/Common/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace Microsoft.WindowsAzure.Commands.Test.Utilities.Common
/// </summary>
public abstract class TestBase
{
protected AzureProfile currentProfile;

public TestBase()
{
BaseSetup();
Expand All @@ -43,11 +45,13 @@ public void BaseSetup()
{
AzureSession.DataStore = new MockDataStore();
}
if (AzureSession.Profile.CurrentContext.Subscription == null)
currentProfile = new AzureProfile();

if (currentProfile.Context.Subscription == null)
{
var newGuid = Guid.NewGuid();
AzureSession.Profile.Subscriptions[newGuid] = new AzureSubscription { Id = newGuid, Name = "test", Environment = EnvironmentName.AzureCloud, Account = "test" };
AzureSession.Profile.Accounts["test"] = new AzureAccount
currentProfile.Subscriptions[newGuid] = new AzureSubscription { Id = newGuid, Name = "test", Environment = EnvironmentName.AzureCloud, Account = "test" };
currentProfile.Accounts["test"] = new AzureAccount
{
Id = "test",
Type = AzureAccount.AccountType.User,
Expand All @@ -56,7 +60,7 @@ public void BaseSetup()
{AzureAccount.Property.Subscriptions, newGuid.ToString()}
}
};
AzureSession.Profile.DefaultSubscription = AzureSession.Profile.Subscriptions[newGuid];
currentProfile.DefaultSubscription = currentProfile.Subscriptions[newGuid];
}
AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
}
Expand Down
12 changes: 9 additions & 3 deletions src/Common/Commands.Common.Test/Mocks/MockClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ public TClient CreateClient<TClient>(AzureContext context, AzureEnvironment.Endp
return client;
}

public TClient CreateClient<TClient>(AzureSubscription subscription, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
public TClient CreateClient<TClient>(AzureProfile profile, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
{
throw new NotImplementedException();
}

public TClient CreateClient<TClient>(AzureProfile profile, AzureSubscription subscription, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
{
SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription.Id.ToString(), "fake_token");
if (HttpMockServer.GetCurrentMode() != HttpRecorderMode.Playback)
{
ProfileClient profileClient = new ProfileClient(new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
ProfileClient profileClient = new ProfileClient(
profile ?? new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
AzureContext context = new AzureContext(
subscription,
profileClient.GetAccount(subscription.Account),
Expand All @@ -68,7 +74,7 @@ public TClient CreateClient<TClient>(AzureSubscription subscription, AzureEnviro
creds = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(context);
}

Uri endpointUri = (new ProfileClient(new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)))).Profile.Environments[subscription.Environment].GetEndpointAsUri(endpoint);
Uri endpointUri = (new ProfileClient(profile ?? new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)))).Profile.Environments[subscription.Environment].GetEndpointAsUri(endpoint);
return CreateCustomClient<TClient>(creds, endpointUri);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package id="Hyak.Common" version="1.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.4-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.6-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5486.28526-prerelease" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
103 changes: 55 additions & 48 deletions src/Common/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common
{
public abstract class AzurePSCmdlet : PSCmdlet
{
private readonly RecordingTracingInterceptor httpTracingInterceptor = new RecordingTracingInterceptor();
private readonly RecordingTracingInterceptor _httpTracingInterceptor = new RecordingTracingInterceptor();

[Parameter(Mandatory = false, HelpMessage = "In-memory profile.")]
public AzureProfile Profile { get; set; }

static AzurePSCmdlet()
{
Expand All @@ -37,24 +40,66 @@ static AzurePSCmdlet()
AzureSession.ClientFactory.UserAgents.Add(AzurePowerShell.UserAgentValue);
}

public AzurePSCmdlet()
/// <summary>
/// Cmdlet begin process. Write to logs, setup Http Tracing and initialize profile
/// </summary>
protected override void BeginProcessing()
{
AzureSession.Profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
InitializeProfile();

if (string.IsNullOrEmpty(ParameterSetName))
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithoutParameterSetLog, this.GetType().Name));
}
else
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithParameterSetLog, this.GetType().Name, ParameterSetName));
}

if (Profile.Context != null && Profile.Context.Account != null && Profile.Context.Account.Id != null)
{
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", Profile.Context.Account.Id));
}

RecordingTracingInterceptor.AddToContext(_httpTracingInterceptor);

DefaultProfileClient = new ProfileClient(AzureSession.Profile);
base.BeginProcessing();
}

public AzureContext CurrentContext
private void InitializeProfile()
{
get { return AzureSession.Profile.CurrentContext; }
// Load profile from disk
var profileFromDisk = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
if (Profile == null ||
Profile.ProfilePath == profileFromDisk.ProfilePath)
{
Profile = profileFromDisk;
}
}

public bool HasCurrentSubscription
/// <summary>
/// End processing. Flush messages in tracing interceptor and save profile.
/// </summary>
protected override void EndProcessing()
{
get { return AzureSession.Profile.CurrentContext.Subscription != null; }
string message = string.Format(Resources.EndProcessingLog, this.GetType().Name);
WriteDebugWithTimestamp(message);

RecordingTracingInterceptor.RemoveFromContext(_httpTracingInterceptor);
FlushMessagesFromTracingInterceptor();

base.EndProcessing();
}

public ProfileClient DefaultProfileClient { get; private set; }
/*public AzureContext Profile.Context
{
get { return Profile.Context; }
}*/

public bool HasCurrentSubscription
{
get { return Profile.Context.Subscription != null; }
}

protected string CurrentPath()
{
Expand Down Expand Up @@ -192,53 +237,15 @@ protected override void ProcessRecord()
}
}

/// <summary>
/// Cmdlet begin process
/// </summary>
protected override void BeginProcessing()
{
if (string.IsNullOrEmpty(ParameterSetName))
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithoutParameterSetLog, this.GetType().Name));
}
else
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithParameterSetLog, this.GetType().Name, ParameterSetName));
}

if (CurrentContext != null && CurrentContext.Account != null && CurrentContext.Account.Id != null)
{
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", CurrentContext.Account.Id));
}

RecordingTracingInterceptor.AddToContext(httpTracingInterceptor);

base.BeginProcessing();
}

private void FlushMessagesFromTracingInterceptor()
{
string message;
while (httpTracingInterceptor.MessageQueue.TryDequeue(out message))
while (_httpTracingInterceptor.MessageQueue.TryDequeue(out message))
{
base.WriteDebug(message);
}
}

/// <summary>
/// End processing
/// </summary>
protected override void EndProcessing()
{
string message = string.Format(Resources.EndProcessingLog, this.GetType().Name);
WriteDebugWithTimestamp(message);

RecordingTracingInterceptor.RemoveFromContext(httpTracingInterceptor);
FlushMessagesFromTracingInterceptor();

base.EndProcessing();
}

/// <summary>
/// Asks for confirmation before executing the action.
/// </summary>
Expand Down
14 changes: 7 additions & 7 deletions src/Common/Commands.Common/CloudBaseCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ protected virtual void InitChannelCurrentSubscription(bool force)

protected void DoInitChannelCurrentSubscription(bool force)
{
if (CurrentContext.Subscription == null)
if (Profile.Context.Subscription == null)
{
throw new ArgumentException(Resources.InvalidDefaultSubscription);
}

if (CurrentContext.Account == null)
if (Profile.Context.Account == null)
{
throw new ArgumentException(Resources.AccountNeedsToBeSpecified);
}
Expand Down Expand Up @@ -127,19 +127,19 @@ protected virtual T CreateChannel()
return Channel;
}

string certificateThumbprint = CurrentContext.Account.Id;
Debug.Assert(DefaultProfileClient.Profile.Accounts[certificateThumbprint].Type == AzureAccount.AccountType.Certificate);
string certificateThumbprint = Profile.Context.Account.Id;
Debug.Assert(Profile.Accounts[certificateThumbprint].Type == AzureAccount.AccountType.Certificate);

return ChannelHelper.CreateServiceManagementChannel<T>(
ServiceBinding,
CurrentContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement),
Profile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement),
AzureSession.DataStore.GetCertificate(certificateThumbprint),
new HttpRestMessageInspector(WriteDebug));
}

protected void RetryCall(Action<string> call)
{
RetryCall(CurrentContext.Subscription.Id, call);
RetryCall(Profile.Context.Subscription.Id, call);
}

protected void RetryCall(Guid subsId, Action<string> call)
Expand Down Expand Up @@ -172,7 +172,7 @@ protected void RetryCall(Guid subsId, Action<string> call)

protected TResult RetryCall<TResult>(Func<string, TResult> call)
{
return RetryCall(CurrentContext.Subscription.Id, call);
return RetryCall(Profile.Context.Subscription.Id, call);
}

protected TResult RetryCall<TResult>(Guid subsId, Func<string, TResult> call)
Expand Down
7 changes: 7 additions & 0 deletions src/Common/Commands.Common/CmdletExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ public static void InvokeEndProcessing(this PSCmdlet cmdlt)
MethodInfo dynMethod = (typeof(PSCmdlet)).GetMethod("EndProcessing", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod.Invoke(cmdlt, null);
}
public static void ExecuteWithProcessing(this AzurePSCmdlet cmdlt)
{
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

}

#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.6-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
Loading