Skip to content

[Monitor] Fixing issue #3957: bug in Get-AzureRmLog cmdlet, returning… #4066

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 7 commits into from
Jun 26, 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
7 changes: 6 additions & 1 deletion src/ResourceManager/Insights/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
- Additional information about change #1
-->
## Current Release

* Issue #3957 fixed for Get-AzureRmLog
- Issue #1: The backend returns the records in pages of 200 records each, linked by the continuation token to the next page. The customers were seeing the cmdlet returning only 200 records when they knew there were more. This was happening regardless of the value they set for MaxEvents, unless that value was less than 200.
- Issue #2: The documentation contained incorrect data about this cmdlet, e.g.: the default timewindow was 1 hour.
- Fix #1: The cmdlet now follows the continuation token returned by the backend until it reaches MaxEvents or the end of the set.<br>The default value for MaxEvents is 1000 and its maximum is 100000. Any value for MaxEvents that is less than 1 is ignored and the default is used instead. These values and behavior did not change, now they are correctly documented.<br>An alias for MaxEvents has been added -MaxRecords- since the name of the cmdlet does not speak about events anymore, but only about Logs.
- Fix #2: The documentation contains correct and more detailed information: new alias, correct time window, correct default, minimum, and maximum values.

## Version 3.1.0

## Version 3.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public class GetAzureRmLogTests
private readonly Mock<IActivityLogsOperations> insightsEventOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private AzureOperationResponse<IPage<EventData>> response;
private AzureOperationResponse<IPage<EventData>> finalResponse;
private ODataQuery<EventData> filter;
private string selected;
private string nextLink;

public GetAzureRmLogTests(Xunit.Abstractions.ITestOutputHelper output)
{
Expand All @@ -51,6 +53,7 @@ public GetAzureRmLogTests(Xunit.Abstractions.ITestOutputHelper output)
};

response = Utilities.InitializeResponse();
finalResponse = Utilities.InitializeFinalResponse();

insightsEventOperationsMock.Setup(f => f.ListWithHttpMessagesAsync(It.IsAny<ODataQuery<EventData>>(), It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(response))
Expand All @@ -60,6 +63,13 @@ public GetAzureRmLogTests(Xunit.Abstractions.ITestOutputHelper output)
selected = s;
});

insightsEventOperationsMock.Setup(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(finalResponse))
.Callback((string next, Dictionary<string, List<string>> headers, CancellationToken t) =>
{
nextLink = next;
});

MonitorClientMock.SetupGet(f => f.ActivityLogs).Returns(this.insightsEventOperationsMock.Object);
}

Expand All @@ -77,7 +87,7 @@ public void GetAzureSubscriptionIdLogCommandParametersProcessing()
filter: ref this.filter,
selected: ref this.selected,
startDate: startDate,
response: response);
nextLink: ref this.nextLink);
}

[Fact]
Expand All @@ -100,7 +110,7 @@ public void GetAzureCorrelationIdLogCommandParametersProcessing()
filter: ref this.filter,
selected: ref this.selected,
startDate: startDate,
response: response);
nextLink: ref this.nextLink);
}

[Fact]
Expand All @@ -123,7 +133,7 @@ public void GetAzureResourceGroupLogCommandParametersProcessing()
filter: ref this.filter,
selected: ref this.selected,
startDate: startDate,
response: response);
nextLink: ref this.nextLink);
}

[Fact]
Expand All @@ -146,7 +156,7 @@ public void GetAzureResourceLogCommandParametersProcessing()
filter: ref this.filter,
selected: ref this.selected,
startDate: startDate,
response: response);
nextLink: ref this.nextLink);
}

[Fact]
Expand All @@ -169,7 +179,7 @@ public void GetAzureResourceProviderLogCommandParametersProcessing()
filter: ref this.filter,
selected: ref this.selected,
startDate: startDate,
response: response);
nextLink: ref this.nextLink);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ public class GetAzureRmAlertHistoryTests
private readonly Mock<IActivityLogsOperations> insightsEventOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private AzureOperationResponse<IPage<EventData>> response;
private AzureOperationResponse<IPage<EventData>> finalResponse;
private ODataQuery<EventData> filter;
private string selected;
private string nextLink;

public GetAzureRmAlertHistoryTests(ITestOutputHelper output)
{
Expand All @@ -56,6 +58,7 @@ public GetAzureRmAlertHistoryTests(ITestOutputHelper output)
};

response = Test.Utilities.InitializeResponse();
finalResponse = Utilities.InitializeFinalResponse();

insightsEventOperationsMock.Setup(f => f.ListWithHttpMessagesAsync(It.IsAny<ODataQuery<EventData>>(), It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(response))
Expand All @@ -65,6 +68,13 @@ public GetAzureRmAlertHistoryTests(ITestOutputHelper output)
selected = s;
});

insightsEventOperationsMock.Setup(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(finalResponse))
.Callback((string next, Dictionary<string, List<string>> headers, CancellationToken t) =>
{
nextLink = next;
});

MonitorClientMock.SetupGet(f => f.ActivityLogs).Returns(this.insightsEventOperationsMock.Object);
}

Expand All @@ -82,7 +92,7 @@ public void GetAlertHistoryCommandParametersProcessing()
filter: ref this.filter,
selected: ref this.selected,
startDate: startDate,
response: response);
nextLink: ref this.nextLink);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Commands.ScenarioTest;

namespace Microsoft.Azure.Commands.Insights.Test.Autoscale
Expand All @@ -38,8 +36,10 @@ public class GetAzureRmAutoscaleHistoryTests
private readonly Mock<IActivityLogsOperations> insightsEventOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private AzureOperationResponse<IPage<EventData>> response;
private AzureOperationResponse<IPage<EventData>> finalResponse;
private ODataQuery<EventData> filter;
private string selected;
private string nextLink;

public GetAzureRmAutoscaleHistoryTests(Xunit.Abstractions.ITestOutputHelper output)
{
Expand All @@ -55,6 +55,7 @@ public GetAzureRmAutoscaleHistoryTests(Xunit.Abstractions.ITestOutputHelper outp
};

response = Test.Utilities.InitializeResponse();
finalResponse = Utilities.InitializeFinalResponse();

insightsEventOperationsMock.Setup(f => f.ListWithHttpMessagesAsync(It.IsAny<ODataQuery<EventData>>(), It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(response))
Expand All @@ -64,6 +65,13 @@ public GetAzureRmAutoscaleHistoryTests(Xunit.Abstractions.ITestOutputHelper outp
selected = s;
});

insightsEventOperationsMock.Setup(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(finalResponse))
.Callback((string next, Dictionary<string, List<string>> headers, CancellationToken t) =>
{
nextLink = next;
});

MonitorClientMock.SetupGet(f => f.ActivityLogs).Returns(this.insightsEventOperationsMock.Object);
}

Expand All @@ -81,7 +89,7 @@ public void GetAutoscaleHistoryCommandParametersProcessing()
filter: ref this.filter,
selected: ref this.selected,
startDate: startDate,
response: response);
nextLink: ref this.nextLink);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@
<None Include="ScenarioTests\UsageMetricsTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.ActivityLogsTests\TestGetAzureSubscriptionIdLogMaxEvents.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.ActivityLogsTests\TestGetAzureSubscriptionIdLogPaged.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AlertsTests\TestGetAzureRmAlertRuleByName.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -314,4 +320,4 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using Xunit;
Expand Down Expand Up @@ -59,5 +60,19 @@ public void TestGetAzureSubscriptionIdLog()
{
TestsController.NewInstance.RunPsTest("Test-GetAzureSubscriptionIdLog");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetAzureSubscriptionIdLogMaxEvents()
{
TestsController.NewInstance.RunPsTest("Test-GetAzureSubscriptionIdLogMaxEvents");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetAzureSubscriptionIdLogPaged()
{
TestsController.NewInstance.RunPsTest("Test-GetAzureSubscriptionIdLogPaged");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Test-GetAzureCorrelationIdLog
$actual = Get-AzureRmLog -CorrelationId $correlation -starttime 2015-03-02T18:00:00Z -endtime 2015-03-02T20:00:00Z -detailedOutput

# Assert TODO add more asserts
Assert-AreEqual $actual.Count 2
Assert-AreEqual 2 $actual.Count
}
finally
{
Expand All @@ -50,7 +50,7 @@ function Test-GetAzureResourceGroupLog
$actual = Get-AzureRmLog -ResourceGroup $rgname -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z

# Assert TODO add more asserts
Assert-AreEqual $actual.Count 2
Assert-AreEqual 2 $actual.Count
}
finally
{
Expand All @@ -74,7 +74,7 @@ function Test-GetAzureResourceLog

# Assert TODO add more asserts
# Assert-Throws { Set-AzureResourceGroup -Name $rgname -Tags @{"testtag" = "testval"} } "Invalid tag format. Expect @{Name = `"tagName`"} or @{Name = `"tagName`"; Value = `"tagValue`"}"
Assert-AreEqual $actual.Count 2
Assert-AreEqual 2 $actual.Count
}
finally
{
Expand All @@ -97,7 +97,7 @@ function Test-GetAzureResourceProviderLog
$actual = Get-AzureRmLog -ResourceProvider $rpname -startTime 2015-03-03T15:42:50Z -endTime 2015-03-03T16:42:50Z

# Assert
Assert-AreEqual $actual.Count 2
Assert-AreEqual 2 $actual.Count
}
finally
{
Expand All @@ -120,7 +120,102 @@ function Test-GetAzureSubscriptionIdLog
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z

# Assert
Assert-AreEqual $actual.Count 1
Assert-AreEqual 1 $actual.Count
}
finally
{
# Cleanup
# No cleanup needed for now
}
}

<#
.SYNOPSIS
Tests getting the logs for a subscription Id.
#>
function Test-GetAzureSubscriptionIdLogMaxEvents
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gucalder are these the tests containing the pages of size 10 that return 15 elements? (the test you mentioned in the email)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is one of the places where this is tested.
This is another scenario test for that. NOTE: the page here is set to 6 records, but the are 8 records in total.
Test-GetAzureSubscriptionIdLogPaged (same file as Test-GetAzureSubscriptionIdLogMaxEvents)

This is another place, the unit test below. Here the first page is 10 records and the second 5.
GetAzureSubscriptionIdLogCommandParametersProcessing()

I added more comments to clarify the goal of the individual tests and also added more cases to the existing scenario tests.

{
# No Setup needed

try
{
{
# There are 7 elements in the recorded sessions. The page is set to 6 elements.
# So if this succeeds, the command is using the default MaxEvent and following continuation token.
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z
Assert-AreEqual 7 $actual.Count
}

{
# There are 7 elements in the recorded sessions. The page is set to 6 elements.
# So if this succeeds, the command is using the default MaxEvent and following continuation token.
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents -3
Assert-AreEqual 7 $actual.Count
}

{
# There are 7 elements in the recorded sessions. The page is set to 6 elements.
# So if this succeeds, the command is using the default MaxEvent and following continuation token.
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 0
Assert-AreEqual 7 $actual.Count
}

{
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 3
Assert-AreEqual 3 $actual.Count
}
}
finally
{
# Cleanup
# No cleanup needed for now
}
}

<#
.SYNOPSIS
Tests getting the logs for a subscription Id.
#>
function Test-GetAzureSubscriptionIdLogPaged
{
# No Setup needed

try
{
{
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
# So if this succeeds, the commands is following the continuation token to get the records in the second page.
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z
Assert-AreEqual 8 $actual.Count
}

{
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
# So if this succeeds, the commands is following the continuation token to get only one record in the second page.
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 7
Assert-AreEqual 7 $actual.Count
}

{
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
# So if this succeeds, the commands could have followed the continuation token but did not because it reached MaxEvents first.
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 6
Assert-AreEqual 6 $actual.Count
}

{
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
# So if this succeeds, the commands could have followed the continuation token but did not because it reached MaxEvents first.
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 3
Assert-AreEqual 3 $actual.Count
}

{
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
# So if this succeeds, the commands is following the continuation token to get the records in the second page and reached the last record before reaching MaxEvents.
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 15
Assert-AreEqual 8 $actual.Count
}
}
finally
{
Expand Down
Loading