Skip to content

Commit fe4d010

Browse files
author
Aman Orazaev
committed
Add checkin test for export logfile cmdlet
1 parent 3474231 commit fe4d010

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

src/ResourceManager/AnalysisServices/Commands.AnalysisServices.Dataplane/Commands/Export-AzureAsInstanceLog.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,11 @@ public override void ExecuteCmdlet()
151151
UriBuilder resolvedUriBuilder = new UriBuilder(logfileBaseUri);
152152
resolvedUriBuilder.Host = ClusterResolve(logfileBaseUri, accessToken, serverName);
153153

154-
var logfileEndpoint =
155-
string.Format(
156-
(string)
157-
context.Environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.LogfileEndpointFormat],
154+
var logfileEndpoint = string.Format(
155+
(string) context.Environment.Endpoints[AsAzureEnvironment.AsRolloutEndpoints.LogfileEndpointFormat],
158156
serverName);
159157

160-
this.AsAzureHttpClient = new AsAzureHttpClient(() => new HttpClient());
158+
this.AsAzureHttpClient.resetHttpClient();
161159
using (HttpResponseMessage message = AsAzureHttpClient.CallGetAsync(
162160
resolvedUriBuilder.Uri,
163161
logfileEndpoint,

src/ResourceManager/AnalysisServices/Commands.AnalysisServices.Dataplane/Models/AsAzureHttpClient.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ public interface IAsAzureHttpClient
3131
Task<HttpResponseMessage> CallPostAsync(Uri baseURI, string requestURL, string accessToken, HttpContent content = null);
3232

3333
Task<HttpResponseMessage> CallGetAsync(Uri baseURI, string requestURL, string accessToken);
34+
35+
void resetHttpClient();
3436
}
3537

3638
public class AsAzureHttpClient : IAsAzureHttpClient
3739
{
3840
public HttpClient HttpClient { get; set; }
3941

42+
private Func<HttpClient> HttpClientProvider { get; set; }
43+
4044
public AsAzureHttpClient(Func<HttpClient> httpClientProvider)
4145
{
42-
HttpClient = httpClientProvider();
46+
this.HttpClientProvider = httpClientProvider;
47+
this.HttpClient = this.HttpClientProvider();
4348
}
4449

4550
public async Task<HttpResponseMessage> CallGetAsync(Uri baseURI, string requestURL, string accessToken)
@@ -52,6 +57,11 @@ public async Task<HttpResponseMessage> CallPostAsync(Uri baseURI, string request
5257
return await CallAsync(HttpMethod.Post, baseURI, requestURL, accessToken, content);
5358
}
5459

60+
public void resetHttpClient()
61+
{
62+
this.HttpClient = this.HttpClientProvider();
63+
}
64+
5565
private async Task<HttpResponseMessage> CallAsync(HttpMethod method, Uri baseURI, string requestURL, string accessToken, HttpContent content = null)
5666
{
5767
using (HttpClient)

src/ResourceManager/AnalysisServices/Commands.AnalysisServices.Test/InMemoryTests/DataPlaneCommandTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,79 @@ public void RestartAzureASInstance_NotLoggedInThrows()
269269
}
270270
}
271271

272+
[Fact]
273+
[Trait(Category.AcceptanceType, Category.CheckIn)]
274+
public void ExportAzureASInstanceLogTest()
275+
{
276+
Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
277+
// Setup
278+
// Clear the the current profile
279+
AsAzureClientSession.Instance.Profile.Environments.Clear();
280+
var mockAuthenticationProvider = new Mock<IAsAzureAuthenticationProvider>();
281+
mockAuthenticationProvider.Setup(
282+
authProvider => authProvider.GetAadAuthenticatedToken(
283+
It.IsAny<AsAzureContext>(),
284+
It.IsAny<SecureString>(),
285+
It.IsAny<PromptBehavior>(),
286+
It.IsAny<string>(),
287+
It.IsAny<string>(),
288+
It.IsAny<Uri>())).Returns(testToken);
289+
AsAzureClientSession.Instance.SetAsAzureAuthenticationProvider(mockAuthenticationProvider.Object);
290+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
291+
292+
// Set up AsAzureHttpClient mock
293+
var mockAsAzureHttpClient = new Mock<IAsAzureHttpClient>();
294+
mockAsAzureHttpClient
295+
.Setup(obj => obj.CallPostAsync(
296+
It.IsAny<Uri>(),
297+
It.Is<string>(s => s.Contains("clusterResolve")),
298+
It.IsAny<string>(),
299+
It.IsAny<HttpContent>()))
300+
.Returns(Task<HttpResponseMessage>.FromResult(
301+
new HttpResponseMessage(HttpStatusCode.OK)
302+
{
303+
Content = new StringContent("{\"clusterFQDN\": \"resolved.westcentralus.asazure.windows.net\"}")
304+
}));
305+
mockAsAzureHttpClient.Setup(obj => obj.CallGetAsync(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<string>())).Returns(
306+
Task<HttpResponseMessage>.FromResult(
307+
new HttpResponseMessage(HttpStatusCode.OK)
308+
{
309+
Content = new StringContent("MOCKED STREAM CONTENT")
310+
}));
311+
312+
var mockTokenCacheItemProvider = new Mock<ITokenCacheItemProvider>();
313+
mockTokenCacheItemProvider
314+
.Setup(obj => obj.GetTokenFromTokenCache(It.IsAny<TokenCache>(), It.IsAny<string>()))
315+
.Returns(testToken);
316+
317+
var exportLogCmdlet = new ExportAzureAnalysisServerLog(mockAsAzureHttpClient.Object, mockTokenCacheItemProvider.Object)
318+
{
319+
CommandRuntime = commandRuntimeMock.Object
320+
};
321+
322+
var addAmdlet = new AddAzureASAccountCommand()
323+
{
324+
CommandRuntime = commandRuntimeMock.Object
325+
};
326+
327+
DoLogin(addAmdlet);
328+
exportLogCmdlet.Instance = testServer;
329+
try
330+
{
331+
exportLogCmdlet.OutputPath = System.IO.Path.GetTempFileName();
332+
exportLogCmdlet.InvokeBeginProcessing();
333+
exportLogCmdlet.ExecuteCmdlet();
334+
exportLogCmdlet.InvokeEndProcessing();
335+
}
336+
finally
337+
{
338+
if (System.IO.File.Exists(exportLogCmdlet.OutputPath))
339+
{
340+
System.IO.File.Delete(exportLogCmdlet.OutputPath);
341+
}
342+
}
343+
}
344+
272345
private void DoLogin(AddAzureASAccountCommand addAmdlet)
273346
{
274347
Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();

0 commit comments

Comments
 (0)