Skip to content

Commit 0df9a19

Browse files
committed
Merge pull request #148 from markcowl/newprofile
Fix SQL tests in Profile
2 parents a867ebc + cafa38f commit 0df9a19

File tree

240 files changed

+631
-586
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

240 files changed

+631
-586
lines changed

src/Common/Commands.Common.Storage/Commands.Common.Storage.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</Reference>
5959
<Reference Include="Microsoft.Azure.Common.Authentication">
6060
<SpecificVersion>False</SpecificVersion>
61-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
61+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.6-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6464
<SpecificVersion>False</SpecificVersion>

src/Common/Commands.Common.Storage/WindowsAzureSubscriptionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public static class WindowsAzureSubscriptionExtensions
2727
{
2828
private static Dictionary<Guid, CloudStorageAccount> storageAccountCache = new Dictionary<Guid,CloudStorageAccount>();
2929

30-
public static CloudStorageAccount GetCloudStorageAccount(this AzureSubscription subscription)
30+
public static CloudStorageAccount GetCloudStorageAccount(this AzureSubscription subscription, AzureProfile profile)
3131
{
3232
if (subscription == null)
3333
{
3434
return null;
3535
}
3636

37-
using (var storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>(subscription, AzureEnvironment.Endpoint.ServiceManagement))
37+
using (var storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>(profile, subscription, AzureEnvironment.Endpoint.ServiceManagement))
3838
{
3939
return StorageUtilities.GenerateCloudStorageAccount(
4040
storageClient, subscription.GetProperty(AzureSubscription.Property.StorageAccount));

src/Common/Commands.Common.Storage/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package id="Hyak.Common" version="1.0.1" targetFramework="net45" />
44
<package id="Microsoft.Azure.Common" version="2.0.1" targetFramework="net45" />
55
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
6-
<package id="Microsoft.Azure.Common.Authentication" version="1.0.4-preview" targetFramework="net45" />
6+
<package id="Microsoft.Azure.Common.Authentication" version="1.0.6-preview" targetFramework="net45" />
77
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
88
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
99
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />

src/Common/Commands.Common.Test/Commands.Common.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</Reference>
5757
<Reference Include="Microsoft.Azure.Common.Authentication">
5858
<SpecificVersion>False</SpecificVersion>
59-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
59+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.6-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6060
</Reference>
6161
<Reference Include="Microsoft.Azure.Common.NetFramework">
6262
<HintPath>..\..\packages\Microsoft.Azure.Common.2.0.1\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>

src/Common/Commands.Common.Test/Common/ProfileCmdltsTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public ProfileCmdltsTests() : base()
4545
AzureSession.DataStore = dataStore;
4646
commandRuntimeMock = new MockCommandRuntime();
4747
SetMockData();
48-
AzureSession.Profile = new AzureProfile();
4948
}
5049

5150
[Fact]
@@ -89,7 +88,7 @@ public void ClearAzureProfileClearsCustomProfile()
8988

9089
cmdlt.CommandRuntime = commandRuntimeMock;
9190
cmdlt.Force = new SwitchParameter(true);
92-
cmdlt.SubscriptionDataFile = subscriptionDataFile;
91+
cmdlt.Profile = new AzureProfile(subscriptionDataFile);
9392

9493
// Act
9594
cmdlt.InvokeBeginProcessing();
@@ -507,16 +506,15 @@ public void SelectAzureSubscriptionByNameUpdatesProfile()
507506
cmdlt.CommandRuntime = commandRuntimeMock;
508507
cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
509508
cmdlt.SubscriptionName = azureSubscription2.Name;
510-
Assert.NotEqual(azureSubscription2.Id, AzureSession.Profile.CurrentContext.Subscription.Id);
511509

512510
// Act
513511
cmdlt.InvokeBeginProcessing();
514512
cmdlt.ExecuteCmdlet();
515513
cmdlt.InvokeEndProcessing();
516-
514+
517515
// Verify
518-
Assert.NotNull(AzureSession.Profile.CurrentContext.Subscription);
519-
Assert.Equal(azureSubscription2.Id, AzureSession.Profile.CurrentContext.Subscription.Id);
516+
Assert.NotNull(cmdlt.Profile.Context.Subscription);
517+
Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
520518
}
521519

522520
[Fact]
@@ -637,7 +635,6 @@ public void SelectAzureSubscriptionByInvalidGuidThrowsException()
637635
cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
638636
string invalidGuid = "foo";
639637
cmdlt.SubscriptionId = invalidGuid;
640-
Assert.Null(AzureSession.Profile.CurrentContext.Subscription);
641638

642639
// Act
643640
cmdlt.InvokeBeginProcessing();

src/Common/Commands.Common.Test/Common/TestBase.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace Microsoft.WindowsAzure.Commands.Test.Utilities.Common
2828
/// </summary>
2929
public abstract class TestBase
3030
{
31+
protected AzureProfile currentProfile;
32+
3133
public TestBase()
3234
{
3335
BaseSetup();
@@ -43,11 +45,13 @@ public void BaseSetup()
4345
{
4446
AzureSession.DataStore = new MockDataStore();
4547
}
46-
if (AzureSession.Profile.CurrentContext.Subscription == null)
48+
currentProfile = new AzureProfile();
49+
50+
if (currentProfile.Context.Subscription == null)
4751
{
4852
var newGuid = Guid.NewGuid();
49-
AzureSession.Profile.Subscriptions[newGuid] = new AzureSubscription { Id = newGuid, Name = "test", Environment = EnvironmentName.AzureCloud, Account = "test" };
50-
AzureSession.Profile.Accounts["test"] = new AzureAccount
53+
currentProfile.Subscriptions[newGuid] = new AzureSubscription { Id = newGuid, Name = "test", Environment = EnvironmentName.AzureCloud, Account = "test" };
54+
currentProfile.Accounts["test"] = new AzureAccount
5155
{
5256
Id = "test",
5357
Type = AzureAccount.AccountType.User,
@@ -56,7 +60,7 @@ public void BaseSetup()
5660
{AzureAccount.Property.Subscriptions, newGuid.ToString()}
5761
}
5862
};
59-
AzureSession.Profile.DefaultSubscription = AzureSession.Profile.Subscriptions[newGuid];
63+
currentProfile.DefaultSubscription = currentProfile.Subscriptions[newGuid];
6064
}
6165
AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
6266
}

src/Common/Commands.Common.Test/Mocks/MockClientFactory.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,18 @@ public TClient CreateClient<TClient>(AzureContext context, AzureEnvironment.Endp
5353
return client;
5454
}
5555

56-
public TClient CreateClient<TClient>(AzureSubscription subscription, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
56+
public TClient CreateClient<TClient>(AzureProfile profile, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
57+
{
58+
throw new NotImplementedException();
59+
}
60+
61+
public TClient CreateClient<TClient>(AzureProfile profile, AzureSubscription subscription, AzureEnvironment.Endpoint endpoint) where TClient : ServiceClient<TClient>
5762
{
5863
SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription.Id.ToString(), "fake_token");
5964
if (HttpMockServer.GetCurrentMode() != HttpRecorderMode.Playback)
6065
{
61-
ProfileClient profileClient = new ProfileClient(new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
66+
ProfileClient profileClient = new ProfileClient(
67+
profile ?? new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
6268
AzureContext context = new AzureContext(
6369
subscription,
6470
profileClient.GetAccount(subscription.Account),
@@ -68,7 +74,7 @@ public TClient CreateClient<TClient>(AzureSubscription subscription, AzureEnviro
6874
creds = AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(context);
6975
}
7076

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

src/Common/Commands.Common.Test/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package id="Hyak.Common" version="1.0.1" targetFramework="net45" />
44
<package id="Microsoft.Azure.Common" version="2.0.1" targetFramework="net45" />
55
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
6-
<package id="Microsoft.Azure.Common.Authentication" version="1.0.4-preview" targetFramework="net45" />
6+
<package id="Microsoft.Azure.Common.Authentication" version="1.0.6-preview" targetFramework="net45" />
77
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
88
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5513.27084-prerelease" targetFramework="net45" />
99
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common
2525
{
2626
public abstract class AzurePSCmdlet : PSCmdlet
2727
{
28-
private readonly RecordingTracingInterceptor httpTracingInterceptor = new RecordingTracingInterceptor();
28+
private readonly RecordingTracingInterceptor _httpTracingInterceptor = new RecordingTracingInterceptor();
29+
30+
[Parameter(Mandatory = false, HelpMessage = "In-memory profile.")]
31+
public AzureProfile Profile { get; set; }
2932

3033
static AzurePSCmdlet()
3134
{
@@ -37,24 +40,66 @@ static AzurePSCmdlet()
3740
AzureSession.ClientFactory.UserAgents.Add(AzurePowerShell.UserAgentValue);
3841
}
3942

40-
public AzurePSCmdlet()
43+
/// <summary>
44+
/// Cmdlet begin process. Write to logs, setup Http Tracing and initialize profile
45+
/// </summary>
46+
protected override void BeginProcessing()
4147
{
42-
AzureSession.Profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
48+
InitializeProfile();
49+
50+
if (string.IsNullOrEmpty(ParameterSetName))
51+
{
52+
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithoutParameterSetLog, this.GetType().Name));
53+
}
54+
else
55+
{
56+
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithParameterSetLog, this.GetType().Name, ParameterSetName));
57+
}
58+
59+
if (Profile.Context != null && Profile.Context.Account != null && Profile.Context.Account.Id != null)
60+
{
61+
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", Profile.Context.Account.Id));
62+
}
63+
64+
RecordingTracingInterceptor.AddToContext(_httpTracingInterceptor);
4365

44-
DefaultProfileClient = new ProfileClient(AzureSession.Profile);
66+
base.BeginProcessing();
4567
}
4668

47-
public AzureContext CurrentContext
69+
private void InitializeProfile()
4870
{
49-
get { return AzureSession.Profile.CurrentContext; }
71+
// Load profile from disk
72+
var profileFromDisk = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
73+
if (Profile == null ||
74+
Profile.ProfilePath == profileFromDisk.ProfilePath)
75+
{
76+
Profile = profileFromDisk;
77+
}
5078
}
5179

52-
public bool HasCurrentSubscription
80+
/// <summary>
81+
/// End processing. Flush messages in tracing interceptor and save profile.
82+
/// </summary>
83+
protected override void EndProcessing()
5384
{
54-
get { return AzureSession.Profile.CurrentContext.Subscription != null; }
85+
string message = string.Format(Resources.EndProcessingLog, this.GetType().Name);
86+
WriteDebugWithTimestamp(message);
87+
88+
RecordingTracingInterceptor.RemoveFromContext(_httpTracingInterceptor);
89+
FlushMessagesFromTracingInterceptor();
90+
91+
base.EndProcessing();
5592
}
5693

57-
public ProfileClient DefaultProfileClient { get; private set; }
94+
/*public AzureContext Profile.Context
95+
{
96+
get { return Profile.Context; }
97+
}*/
98+
99+
public bool HasCurrentSubscription
100+
{
101+
get { return Profile.Context.Subscription != null; }
102+
}
58103

59104
protected string CurrentPath()
60105
{
@@ -192,53 +237,15 @@ protected override void ProcessRecord()
192237
}
193238
}
194239

195-
/// <summary>
196-
/// Cmdlet begin process
197-
/// </summary>
198-
protected override void BeginProcessing()
199-
{
200-
if (string.IsNullOrEmpty(ParameterSetName))
201-
{
202-
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithoutParameterSetLog, this.GetType().Name));
203-
}
204-
else
205-
{
206-
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithParameterSetLog, this.GetType().Name, ParameterSetName));
207-
}
208-
209-
if (CurrentContext != null && CurrentContext.Account != null && CurrentContext.Account.Id != null)
210-
{
211-
WriteDebugWithTimestamp(string.Format("using account id '{0}'...", CurrentContext.Account.Id));
212-
}
213-
214-
RecordingTracingInterceptor.AddToContext(httpTracingInterceptor);
215-
216-
base.BeginProcessing();
217-
}
218-
219240
private void FlushMessagesFromTracingInterceptor()
220241
{
221242
string message;
222-
while (httpTracingInterceptor.MessageQueue.TryDequeue(out message))
243+
while (_httpTracingInterceptor.MessageQueue.TryDequeue(out message))
223244
{
224245
base.WriteDebug(message);
225246
}
226247
}
227248

228-
/// <summary>
229-
/// End processing
230-
/// </summary>
231-
protected override void EndProcessing()
232-
{
233-
string message = string.Format(Resources.EndProcessingLog, this.GetType().Name);
234-
WriteDebugWithTimestamp(message);
235-
236-
RecordingTracingInterceptor.RemoveFromContext(httpTracingInterceptor);
237-
FlushMessagesFromTracingInterceptor();
238-
239-
base.EndProcessing();
240-
}
241-
242249
/// <summary>
243250
/// Asks for confirmation before executing the action.
244251
/// </summary>

src/Common/Commands.Common/CloudBaseCmdlet.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ protected virtual void InitChannelCurrentSubscription(bool force)
8181

8282
protected void DoInitChannelCurrentSubscription(bool force)
8383
{
84-
if (CurrentContext.Subscription == null)
84+
if (Profile.Context.Subscription == null)
8585
{
8686
throw new ArgumentException(Resources.InvalidDefaultSubscription);
8787
}
8888

89-
if (CurrentContext.Account == null)
89+
if (Profile.Context.Account == null)
9090
{
9191
throw new ArgumentException(Resources.AccountNeedsToBeSpecified);
9292
}
@@ -127,19 +127,19 @@ protected virtual T CreateChannel()
127127
return Channel;
128128
}
129129

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

133133
return ChannelHelper.CreateServiceManagementChannel<T>(
134134
ServiceBinding,
135-
CurrentContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement),
135+
Profile.Context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement),
136136
AzureSession.DataStore.GetCertificate(certificateThumbprint),
137137
new HttpRestMessageInspector(WriteDebug));
138138
}
139139

140140
protected void RetryCall(Action<string> call)
141141
{
142-
RetryCall(CurrentContext.Subscription.Id, call);
142+
RetryCall(Profile.Context.Subscription.Id, call);
143143
}
144144

145145
protected void RetryCall(Guid subsId, Action<string> call)
@@ -172,7 +172,7 @@ protected void RetryCall(Guid subsId, Action<string> call)
172172

173173
protected TResult RetryCall<TResult>(Func<string, TResult> call)
174174
{
175-
return RetryCall(CurrentContext.Subscription.Id, call);
175+
return RetryCall(Profile.Context.Subscription.Id, call);
176176
}
177177

178178
protected TResult RetryCall<TResult>(Guid subsId, Func<string, TResult> call)

src/Common/Commands.Common/CmdletExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ public static void InvokeEndProcessing(this PSCmdlet cmdlt)
123123
MethodInfo dynMethod = (typeof(PSCmdlet)).GetMethod("EndProcessing", BindingFlags.NonPublic | BindingFlags.Instance);
124124
dynMethod.Invoke(cmdlt, null);
125125
}
126+
public static void ExecuteWithProcessing(this AzurePSCmdlet cmdlt)
127+
{
128+
cmdlt.InvokeBeginProcessing();
129+
cmdlt.ExecuteCmdlet();
130+
cmdlt.InvokeEndProcessing();
131+
132+
}
126133

127134
#endregion
128135
}

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</Reference>
6161
<Reference Include="Microsoft.Azure.Common.Authentication">
6262
<SpecificVersion>False</SpecificVersion>
63-
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.4-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
63+
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.6-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
6464
</Reference>
6565
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6666
<SpecificVersion>False</SpecificVersion>

0 commit comments

Comments
 (0)