Skip to content

Commit 9877237

Browse files
authored
Merge pull request #4066 from AuxMon/preview
[Monitor] Fixing issue #3957: bug in Get-AzureRmLog cmdlet, returning…
2 parents faef1da + 06cb443 commit 9877237

File tree

13 files changed

+533
-90
lines changed

13 files changed

+533
-90
lines changed

src/ResourceManager/Insights/ChangeLog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21-
21+
* Issue #3957 fixed for Get-AzureRmLog
22+
- 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.
23+
- Issue #2: The documentation contained incorrect data about this cmdlet, e.g.: the default timewindow was 1 hour.
24+
- 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.
25+
- Fix #2: The documentation contains correct and more detailed information: new alias, correct time window, correct default, minimum, and maximum values.
26+
2227
## Version 3.1.0
2328

2429
## Version 3.0.1

src/ResourceManager/Insights/Commands.Insights.Test/ActivityLogs/GetAzureRmLogTests.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public class GetAzureRmLogTests
3535
private readonly Mock<IActivityLogsOperations> insightsEventOperationsMock;
3636
private Mock<ICommandRuntime> commandRuntimeMock;
3737
private AzureOperationResponse<IPage<EventData>> response;
38+
private AzureOperationResponse<IPage<EventData>> finalResponse;
3839
private ODataQuery<EventData> filter;
3940
private string selected;
41+
private string nextLink;
4042

4143
public GetAzureRmLogTests(Xunit.Abstractions.ITestOutputHelper output)
4244
{
@@ -51,6 +53,7 @@ public GetAzureRmLogTests(Xunit.Abstractions.ITestOutputHelper output)
5153
};
5254

5355
response = Utilities.InitializeResponse();
56+
finalResponse = Utilities.InitializeFinalResponse();
5457

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

66+
insightsEventOperationsMock.Setup(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
67+
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(finalResponse))
68+
.Callback((string next, Dictionary<string, List<string>> headers, CancellationToken t) =>
69+
{
70+
nextLink = next;
71+
});
72+
6373
MonitorClientMock.SetupGet(f => f.ActivityLogs).Returns(this.insightsEventOperationsMock.Object);
6474
}
6575

@@ -77,7 +87,7 @@ public void GetAzureSubscriptionIdLogCommandParametersProcessing()
7787
filter: ref this.filter,
7888
selected: ref this.selected,
7989
startDate: startDate,
80-
response: response);
90+
nextLink: ref this.nextLink);
8191
}
8292

8393
[Fact]
@@ -100,7 +110,7 @@ public void GetAzureCorrelationIdLogCommandParametersProcessing()
100110
filter: ref this.filter,
101111
selected: ref this.selected,
102112
startDate: startDate,
103-
response: response);
113+
nextLink: ref this.nextLink);
104114
}
105115

106116
[Fact]
@@ -123,7 +133,7 @@ public void GetAzureResourceGroupLogCommandParametersProcessing()
123133
filter: ref this.filter,
124134
selected: ref this.selected,
125135
startDate: startDate,
126-
response: response);
136+
nextLink: ref this.nextLink);
127137
}
128138

129139
[Fact]
@@ -146,7 +156,7 @@ public void GetAzureResourceLogCommandParametersProcessing()
146156
filter: ref this.filter,
147157
selected: ref this.selected,
148158
startDate: startDate,
149-
response: response);
159+
nextLink: ref this.nextLink);
150160
}
151161

152162
[Fact]
@@ -169,7 +179,7 @@ public void GetAzureResourceProviderLogCommandParametersProcessing()
169179
filter: ref this.filter,
170180
selected: ref this.selected,
171181
startDate: startDate,
172-
response: response);
182+
nextLink: ref this.nextLink);
173183
}
174184
}
175185
}

src/ResourceManager/Insights/Commands.Insights.Test/Alerts/GetAzureRmAlertHistoryTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public class GetAzureRmAlertHistoryTests
3939
private readonly Mock<IActivityLogsOperations> insightsEventOperationsMock;
4040
private Mock<ICommandRuntime> commandRuntimeMock;
4141
private AzureOperationResponse<IPage<EventData>> response;
42+
private AzureOperationResponse<IPage<EventData>> finalResponse;
4243
private ODataQuery<EventData> filter;
4344
private string selected;
45+
private string nextLink;
4446

4547
public GetAzureRmAlertHistoryTests(ITestOutputHelper output)
4648
{
@@ -56,6 +58,7 @@ public GetAzureRmAlertHistoryTests(ITestOutputHelper output)
5658
};
5759

5860
response = Test.Utilities.InitializeResponse();
61+
finalResponse = Utilities.InitializeFinalResponse();
5962

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

71+
insightsEventOperationsMock.Setup(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
72+
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(finalResponse))
73+
.Callback((string next, Dictionary<string, List<string>> headers, CancellationToken t) =>
74+
{
75+
nextLink = next;
76+
});
77+
6878
MonitorClientMock.SetupGet(f => f.ActivityLogs).Returns(this.insightsEventOperationsMock.Object);
6979
}
7080

@@ -82,7 +92,7 @@ public void GetAlertHistoryCommandParametersProcessing()
8292
filter: ref this.filter,
8393
selected: ref this.selected,
8494
startDate: startDate,
85-
response: response);
95+
nextLink: ref this.nextLink);
8696
}
8797
}
8898
}

src/ResourceManager/Insights/Commands.Insights.Test/Autoscale/GetAzureRmAutoscaleHistoryTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
using System.Threading;
2626
using System.Threading.Tasks;
2727
using Xunit;
28-
using Microsoft.Azure.Commands.Common.Authentication;
29-
using Microsoft.Azure.Commands.ResourceManager.Common;
3028
using Microsoft.Azure.Commands.ScenarioTest;
3129

3230
namespace Microsoft.Azure.Commands.Insights.Test.Autoscale
@@ -38,8 +36,10 @@ public class GetAzureRmAutoscaleHistoryTests
3836
private readonly Mock<IActivityLogsOperations> insightsEventOperationsMock;
3937
private Mock<ICommandRuntime> commandRuntimeMock;
4038
private AzureOperationResponse<IPage<EventData>> response;
39+
private AzureOperationResponse<IPage<EventData>> finalResponse;
4140
private ODataQuery<EventData> filter;
4241
private string selected;
42+
private string nextLink;
4343

4444
public GetAzureRmAutoscaleHistoryTests(Xunit.Abstractions.ITestOutputHelper output)
4545
{
@@ -55,6 +55,7 @@ public GetAzureRmAutoscaleHistoryTests(Xunit.Abstractions.ITestOutputHelper outp
5555
};
5656

5757
response = Test.Utilities.InitializeResponse();
58+
finalResponse = Utilities.InitializeFinalResponse();
5859

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

68+
insightsEventOperationsMock.Setup(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
69+
.Returns(Task.FromResult<AzureOperationResponse<IPage<EventData>>>(finalResponse))
70+
.Callback((string next, Dictionary<string, List<string>> headers, CancellationToken t) =>
71+
{
72+
nextLink = next;
73+
});
74+
6775
MonitorClientMock.SetupGet(f => f.ActivityLogs).Returns(this.insightsEventOperationsMock.Object);
6876
}
6977

@@ -81,7 +89,7 @@ public void GetAutoscaleHistoryCommandParametersProcessing()
8189
filter: ref this.filter,
8290
selected: ref this.selected,
8391
startDate: startDate,
84-
response: response);
92+
nextLink: ref this.nextLink);
8593
}
8694
}
8795
}

src/ResourceManager/Insights/Commands.Insights.Test/Commands.Insights.Test.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@
212212
<None Include="ScenarioTests\UsageMetricsTests.ps1">
213213
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
214214
</None>
215+
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.ActivityLogsTests\TestGetAzureSubscriptionIdLogMaxEvents.json">
216+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
217+
</None>
218+
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.ActivityLogsTests\TestGetAzureSubscriptionIdLogPaged.json">
219+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
220+
</None>
215221
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AlertsTests\TestGetAzureRmAlertRuleByName.json">
216222
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
217223
</None>
@@ -314,4 +320,4 @@
314320
</ItemGroup>
315321
<ItemGroup />
316322
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
317-
</Project>
323+
</Project>

src/ResourceManager/Insights/Commands.Insights.Test/ScenarioTests/ActivityLogsTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
1516
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1617
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
1718
using Xunit;
@@ -59,5 +60,19 @@ public void TestGetAzureSubscriptionIdLog()
5960
{
6061
TestsController.NewInstance.RunPsTest("Test-GetAzureSubscriptionIdLog");
6162
}
63+
64+
[Fact]
65+
[Trait(Category.AcceptanceType, Category.CheckIn)]
66+
public void TestGetAzureSubscriptionIdLogMaxEvents()
67+
{
68+
TestsController.NewInstance.RunPsTest("Test-GetAzureSubscriptionIdLogMaxEvents");
69+
}
70+
71+
[Fact]
72+
[Trait(Category.AcceptanceType, Category.CheckIn)]
73+
public void TestGetAzureSubscriptionIdLogPaged()
74+
{
75+
TestsController.NewInstance.RunPsTest("Test-GetAzureSubscriptionIdLogPaged");
76+
}
6277
}
6378
}

src/ResourceManager/Insights/Commands.Insights.Test/ScenarioTests/ActivityLogsTests.ps1

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function Test-GetAzureCorrelationIdLog
2727
$actual = Get-AzureRmLog -CorrelationId $correlation -starttime 2015-03-02T18:00:00Z -endtime 2015-03-02T20:00:00Z -detailedOutput
2828

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

5252
# Assert TODO add more asserts
53-
Assert-AreEqual $actual.Count 2
53+
Assert-AreEqual 2 $actual.Count
5454
}
5555
finally
5656
{
@@ -74,7 +74,7 @@ function Test-GetAzureResourceLog
7474

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

9999
# Assert
100-
Assert-AreEqual $actual.Count 2
100+
Assert-AreEqual 2 $actual.Count
101101
}
102102
finally
103103
{
@@ -120,7 +120,102 @@ function Test-GetAzureSubscriptionIdLog
120120
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z
121121

122122
# Assert
123-
Assert-AreEqual $actual.Count 1
123+
Assert-AreEqual 1 $actual.Count
124+
}
125+
finally
126+
{
127+
# Cleanup
128+
# No cleanup needed for now
129+
}
130+
}
131+
132+
<#
133+
.SYNOPSIS
134+
Tests getting the logs for a subscription Id.
135+
#>
136+
function Test-GetAzureSubscriptionIdLogMaxEvents
137+
{
138+
# No Setup needed
139+
140+
try
141+
{
142+
{
143+
# There are 7 elements in the recorded sessions. The page is set to 6 elements.
144+
# So if this succeeds, the command is using the default MaxEvent and following continuation token.
145+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z
146+
Assert-AreEqual 7 $actual.Count
147+
}
148+
149+
{
150+
# There are 7 elements in the recorded sessions. The page is set to 6 elements.
151+
# So if this succeeds, the command is using the default MaxEvent and following continuation token.
152+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents -3
153+
Assert-AreEqual 7 $actual.Count
154+
}
155+
156+
{
157+
# There are 7 elements in the recorded sessions. The page is set to 6 elements.
158+
# So if this succeeds, the command is using the default MaxEvent and following continuation token.
159+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 0
160+
Assert-AreEqual 7 $actual.Count
161+
}
162+
163+
{
164+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 3
165+
Assert-AreEqual 3 $actual.Count
166+
}
167+
}
168+
finally
169+
{
170+
# Cleanup
171+
# No cleanup needed for now
172+
}
173+
}
174+
175+
<#
176+
.SYNOPSIS
177+
Tests getting the logs for a subscription Id.
178+
#>
179+
function Test-GetAzureSubscriptionIdLogPaged
180+
{
181+
# No Setup needed
182+
183+
try
184+
{
185+
{
186+
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
187+
# So if this succeeds, the commands is following the continuation token to get the records in the second page.
188+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z
189+
Assert-AreEqual 8 $actual.Count
190+
}
191+
192+
{
193+
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
194+
# So if this succeeds, the commands is following the continuation token to get only one record in the second page.
195+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 7
196+
Assert-AreEqual 7 $actual.Count
197+
}
198+
199+
{
200+
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
201+
# So if this succeeds, the commands could have followed the continuation token but did not because it reached MaxEvents first.
202+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 6
203+
Assert-AreEqual 6 $actual.Count
204+
}
205+
206+
{
207+
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
208+
# So if this succeeds, the commands could have followed the continuation token but did not because it reached MaxEvents first.
209+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 3
210+
Assert-AreEqual 3 $actual.Count
211+
}
212+
213+
{
214+
# There are 8 elements in the recorded sessions. The page is set to 6 elements.
215+
# 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.
216+
$actual = Get-AzureRmLog -starttime 2015-01-15T12:30:00Z -endtime 2015-01-15T20:30:00Z -MaxEvents 15
217+
Assert-AreEqual 8 $actual.Count
218+
}
124219
}
125220
finally
126221
{

0 commit comments

Comments
 (0)