Skip to content

Commit 5e809ec

Browse files
authored
Merge pull request #4219 from AuxMon/preview
Implementing change request (issue #4215): removing limit of 15 days in the time window for Azure-GetRmLog cmdlet. Also fixing some typos in unit test names.
2 parents 09e9b94 + 4cd2e50 commit 5e809ec

File tree

9 files changed

+140
-46
lines changed

9 files changed

+140
-46
lines changed

src/ResourceManager/Insights/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
* Issue $4215 (change request) remove the 15 days limit in the time window for the Get-AzureRmLog cmdlet. Also minor changes to the unit test names.
2122
* Issue #3957 fixed for Get-AzureRmLog
2223
- 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.
2324
- Issue #2: The documentation contained incorrect data about this cmdlet, e.g.: the default timewindow was 1 hour.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<Compile Include="Autoscale\NewAzureRmAutoscaleWebhookTests.cs" />
140140
<Compile Include="Autoscale\RemoveAzureRmAutoscaleSettingTests.cs" />
141141
<None Include="Diagnostics\RemoveDiagnosticSettingCommandTests.cs" />
142+
<Compile Include="CustomPrinterTests.cs" />
142143
<Compile Include="Diagnostics\SetDiagnosticSettingCommandTests.cs" />
143144
<Compile Include="Diagnostics\GetDiagnosticSettingCommandTests.cs" />
144145
<Compile Include="ActivityLogs\GetAzureRmLogTests.cs" />
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Diagnostics;
18+
using System.Xml;
19+
using Microsoft.Azure.ServiceManagemenet.Common.Models;
20+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
21+
using Xunit;
22+
using Xunit.Abstractions;
23+
24+
namespace Microsoft.Azure.Commands.Insights.Test
25+
{
26+
public class CustomPrinterTests
27+
{
28+
public CustomPrinterTests(ITestOutputHelper output)
29+
{
30+
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
31+
}
32+
33+
[Fact]
34+
[Trait(Category.AcceptanceType, Category.CheckIn)]
35+
public void CustomPrinterTest_SimpleTypes()
36+
{
37+
const int x = 1;
38+
const double y = 1.2;
39+
const string s = "hello";
40+
const bool b = true;
41+
TimeSpan ts = TimeSpan.FromHours(13);
42+
DateTime dt = DateTime.Now;
43+
DateTime dtUtc = DateTime.UtcNow;
44+
45+
// The following should remove the " : " and the "\r\n" in the future
46+
Assert.Equal(" : 1\r\n", OutputClasses.CustomPrinter.Print(x));
47+
Assert.Equal(" : 1.2\r\n", OutputClasses.CustomPrinter.Print(y));
48+
Assert.Equal(" : hello\r\n", OutputClasses.CustomPrinter.Print(s));
49+
Assert.Equal(" : True\r\n", OutputClasses.CustomPrinter.Print(b));
50+
Assert.Equal(" : " + XmlConvert.ToString(ts) + "\r\n", OutputClasses.CustomPrinter.Print(ts));
51+
52+
// Must be dt.ToUniversalTime().ToString("O") in the future
53+
Assert.Equal(" : " + dt + "\r\n", OutputClasses.CustomPrinter.Print(dt));
54+
55+
// Must be dtUtc.ToString("O") in the future
56+
Assert.Equal(" : " + dtUtc + "\r\n", OutputClasses.CustomPrinter.Print(dtUtc));
57+
58+
// Both must be string.Empty in the future
59+
Assert.Equal(" :\r\n", OutputClasses.CustomPrinter.Print(null));
60+
Assert.Equal(" : \r\n", OutputClasses.CustomPrinter.Print(""));
61+
}
62+
63+
[Fact]
64+
[Trait(Category.AcceptanceType, Category.CheckIn)]
65+
public void CustomPrinterTest_ComplexTypes()
66+
{
67+
var stringList = new List<string>();
68+
var dictionarySS = new Dictionary<string, string>();
69+
var dictionarySO = new Dictionary<string, object>();
70+
var eventData = Test.Utilities.CreateFakeEvent(id: null, newDates: false);
71+
72+
// Must be {} in the future
73+
Assert.Equal("", OutputClasses.CustomPrinter.Print(stringList));
74+
75+
// Must be {} in the future
76+
Assert.Equal("", OutputClasses.CustomPrinter.Print(dictionarySS));
77+
78+
// Must be {} in the future
79+
Assert.Equal("", OutputClasses.CustomPrinter.Print(dictionarySO));
80+
81+
string result = OutputClasses.CustomPrinter.Print(eventData);
82+
Debug.WriteLine("EventData: ");
83+
Debug.WriteLine(result);
84+
85+
// Must be [\r\nAuthorization : [\r\n Action : PUT\r\n Role : Sender\r\n Scope : None\r\n ]\r\nClaims : {\r\n [aud, https://management.core.windows.net/]\r\n [iss, https://sts.windows.net/123456/]\r\n [iat, h123445]\r\n }\r\nCaller : caller\r\nDescription : fake event\r\nId : ac7d2ab5-698a-4c33-9c19-0a93d3d7f527\r\nEventDataId : \r\nCorrelationId : correlation\r\nEventName : [\r\n Value : Start request\r\n LocalizedValue : Start request\r\n ]\r\nCategory : [\r\n Value : Microsoft Resources\r\n LocalizedValue : Microsoft Resources\r\n ]\r\nHttpRequest : [\r\n ClientRequestId : 1234\r\n ClientIpAddress : 123.123.123.123\r\n Method : PUT\r\n Uri : http://path/subscriptions/ffce8037-a374-48bf-901d-dac4e3ea8c09/resourcegroups/foo/deployments/testdeploy\r\n ]\r\nLevel : Informational\r\nResourceGroupName : Default-Web-EastUS\r\nResourceProviderName : [\r\n Value : Microsoft Resources\r\n LocalizedValue : Microsoft Resources\r\n ]\r\nResourceId : /subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/garyyang1\r\nResourceType : \r\nOperationId : c0f2e85f-efb0-47d0-bf90-f983ec8be91d\r\nOperationName : [\r\n Value : Microsoft.Resources/subscriptions/resourcegroups/deployments/write\r\n LocalizedValue : Microsoft.Resources/subscriptions/resourcegroups/deployments/write\r\n ]\r\nProperties : {}\r\nStatus : [\r\n Value : Succeeded\r\n LocalizedValue : Succeeded\r\n ]\r\nSubStatus : [\r\n Value : Created\r\n LocalizedValue : Created\r\n ]\r\nEventTimestamp : 2017-06-07T22:54:00.0000000Z\r\nSubmissionTimestamp : 2017-06-07T22:54:00.0000000Z\r\nSubscriptionId : \r\nTenantId : \r\n] in the future
86+
Assert.Equal(
87+
expected: "Authorization \r\nAction : PUT\r\nRole : Sender\r\nScope : None\r\n\r\nClaims \r\n : [aud, https://management.core.windows.net/]\r\n : [iss, https://sts.windows.net/123456/]\r\n : [iat, h123445]\r\nCaller : caller\r\nDescription : fake event\r\nId : ac7d2ab5-698a-4c33-9c19-0a93d3d7f527\r\nEventDataId :\r\nCorrelationId : correlation\r\nEventName \r\nValue : Start request\r\nLocalizedValue : Start request\r\n\r\nCategory \r\nValue : Microsoft Resources\r\nLocalizedValue : Microsoft Resources\r\n\r\nHttpRequest \r\nClientRequestId : 1234\r\nClientIpAddress : 123.123.123.123\r\nMethod : PUT\r\nUri : http://path/subscriptions/ffce8037-a374-48bf-901d-dac4e3ea8c09/resourcegroups/foo/deployments/testdeploy\r\n\r\nLevel : Informational\r\nResourceGroupName : Default-Web-EastUS\r\nResourceProviderName\r\nValue : Microsoft Resources\r\nLocalizedValue : Microsoft Resources\r\n\r\nResourceId : /subscriptions/a93fb07c-6c93-40be-bf3b-4f0deba10f4b/resourceGroups/Default-Web-EastUS/providers/microsoft.web/sites/garyyang1\r\nResourceType :\r\nOperationId : c0f2e85f-efb0-47d0-bf90-f983ec8be91d\r\nOperationName \r\nValue : Microsoft.Resources/subscriptions/resourcegroups/deployments/write\r\nLocalizedValue : Microsoft.Resources/subscriptions/resourcegroups/deployments/write\r\n\r\nProperties \r\nStatus \r\nValue : Succeeded\r\nLocalizedValue : Succeeded\r\n\r\nSubStatus \r\nValue : Created\r\nLocalizedValue : Created\r\n\r\nEventTimestamp : 6/7/2017 10:54:00 PM\r\nSubmissionTimestamp : 6/7/2017 10:54:00 PM\r\nSubscriptionId :\r\nTenantId :\r\n\r\n",
88+
actual: result);
89+
90+
dictionarySS.Add("k1", "v1");
91+
dictionarySS.Add("k2", "v2");
92+
result = OutputClasses.CustomPrinter.Print(dictionarySS);
93+
Debug.WriteLine("Dictionary<string, string>: ");
94+
Debug.WriteLine(result);
95+
96+
// Must be "{\r\n[k1, v1]\r\n[k2, v2]\r\n}" in the future
97+
Assert.Equal(
98+
expected: " : [k1, v1]\r\n : [k2, v2]\r\n",
99+
actual: result);
100+
101+
dictionarySO.Add("k1", eventData);
102+
dictionarySO.Add("k2", Test.Utilities.CreateFakeEvent(id: "secondId", newDates: false));
103+
result = OutputClasses.CustomPrinter.Print(dictionarySO);
104+
Debug.WriteLine("Dictionary<string, object>: ");
105+
Debug.WriteLine(result);
106+
// Must be "{\r\n[k1, Microsoft.Azure.Management.Monitor.Models.EventData]\r\n[k2, Microsoft.Azure.Management.Monitor.Models.EventData]\r\n}" in the future
107+
Assert.Equal(
108+
expected: " : [k1, Microsoft.Azure.Management.Monitor.Models.EventData]\r\n : [k2, Microsoft.Azure.Management.Monitor.Models.EventData]\r\n",
109+
actual: result);
110+
}
111+
}
112+
}
113+

src/ResourceManager/Insights/Commands.Insights.Test/Utilities.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public static class Utilities
4343

4444
#region Events
4545

46-
public static EventData CreateFakeEvent(string id = null)
46+
public static EventData CreateFakeEvent(string id = null, bool newDates = true)
4747
{
48+
var fixedDate = new DateTime(2017, 06, 07, 22, 54, 0, DateTimeKind.Utc);
4849
return new EventData(
4950
id: id ?? "ac7d2ab5-698a-4c33-9c19-0a93d3d7f527",
5051
eventName: new LocalizableString(
@@ -67,7 +68,7 @@ public static EventData CreateFakeEvent(string id = null)
6768
correlationId: Correlation,
6869
description: "fake event",
6970
level: EventLevel.Informational,
70-
eventTimestamp: DateTime.Now,
71+
eventTimestamp: newDates ? DateTime.Now : fixedDate,
7172
operationId: "c0f2e85f-efb0-47d0-bf90-f983ec8be91d",
7273
operationName: new LocalizableString(
7374
localizedValue: "Microsoft.Resources/subscriptions/resourcegroups/deployments/write",
@@ -89,7 +90,7 @@ public static EventData CreateFakeEvent(string id = null)
8990
clientRequestId: "1234",
9091
clientIpAddress: "123.123.123.123"),
9192
properties: new Dictionary<string, string>(),
92-
submissionTimestamp: DateTime.Now);
93+
submissionTimestamp: newDates ? DateTime.Now : fixedDate);
9394
}
9495

9596
public static List<EventData> CreateListOfFakeEvents(int numEvents = 1)
@@ -318,11 +319,6 @@ public static void ExecuteVerifications(LogsCmdletBase cmdlet, Mock<IActivityLog
318319
cmdlet.EndTime = DateTime.Now.Subtract(TimeSpan.FromSeconds(21));
319320
nextLink = null;
320321
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
321-
322-
cmdlet.StartTime = DateTime.Now.Subtract(TimeSpan.FromDays(30));
323-
cmdlet.EndTime = DateTime.Now.Subtract(TimeSpan.FromDays(14));
324-
nextLink = null;
325-
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
326322
}
327323

328324
#endregion

src/ResourceManager/Insights/Commands.Insights/Alerts/AddAzureRmWebtestAlertRuleCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private RuleCondition CreateRuleCondition()
7171
{
7272
DataSource = dataSource,
7373
FailedLocationCount = this.FailedLocationCount,
74-
WindowSize = this.WindowSize,
74+
WindowSize = this.WindowSize
7575
};
7676
}
7777

src/ResourceManager/Insights/Commands.Insights/LogsCmdletBase.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ namespace Microsoft.Azure.Commands.Insights
3232
/// </summary>
3333
public abstract class LogsCmdletBase : MonitorClientCmdletBase
3434
{
35-
private static readonly TimeSpan MaximumDateDifferenceAllowedInDays = TimeSpan.FromDays(15);
3635
private static readonly TimeSpan DefaultQueryTimeRange = TimeSpan.FromDays(7);
3736
private const int MaxNumberOfReturnedRecords = 1000;
3837
private int MaxRecords = 0;
@@ -117,16 +116,19 @@ protected virtual void SetMaxEventsIfPresent(string currentQueryFilter, int valu
117116
/// <returns>A query filter string with the time conditions</returns>
118117
private string ValidateDateTimeRangeAndAddDefaults()
119118
{
120-
// EndTime is optional
121-
DateTime endTime = this.EndTime.HasValue ? this.EndTime.Value : DateTime.Now;
119+
// Removing time in the default current date, but including the whole day: date will be Now + 1 day, the time 00:00:00 AM
120+
var currentDateTime = DateTime.Now;
121+
122+
// EndTime is optional.
123+
DateTime endTime = this.EndTime ?? currentDateTime.AddDays(1).Date;
122124

123125
// StartTime is optional
124-
DateTime startTime = this.StartTime.HasValue ? this.StartTime.Value : endTime.Subtract(this.GetDefaultQueryTimeRange());
126+
DateTime startTime = this.StartTime ?? endTime.Subtract(this.GetDefaultQueryTimeRange());
125127

126128
// Check the value of StartTime
127-
if (startTime > DateTime.Now)
129+
if (startTime > currentDateTime)
128130
{
129-
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ResourcesForEventCmdlets.StartDateLaterThanNow, startTime, DateTime.Now));
131+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ResourcesForEventCmdlets.StartDateLaterThanNow, startTime, currentDateTime));
130132
}
131133

132134
// Check that the dateTime range makes sense
@@ -135,13 +137,6 @@ private string ValidateDateTimeRangeAndAddDefaults()
135137
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ResourcesForEventCmdlets.EndDateEarlierThanStartDate, endTime, startTime));
136138
}
137139

138-
// Validate start and end dates difference is reasonable (<= MaximumDateDifferenceAllowedInDays)
139-
var dateDifference = endTime.Subtract(startTime);
140-
if (dateDifference > MaximumDateDifferenceAllowedInDays)
141-
{
142-
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ResourcesForEventCmdlets.StartAndEndDatesTooFarAppart, MaximumDateDifferenceAllowedInDays.TotalDays, dateDifference.TotalDays));
143-
}
144-
145140
return string.Format("eventTimestamp ge '{0:o}' and eventTimestamp le '{1:o}'", startTime.ToUniversalTime(), endTime.ToUniversalTime());
146141
}
147142

src/ResourceManager/Insights/Commands.Insights/Properties/ResourcesForEventCmdlets.Designer.cs

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Insights/Commands.Insights/Properties/ResourcesForEventCmdlets.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@
120120
<data name="EndDateEarlierThanStartDate" xml:space="preserve">
121121
<value>EndDate '{0:o}' is earlier than StartDate '{1:o}'.</value>
122122
</data>
123-
<data name="StartAndEndDatesTooFarAppart" xml:space="preserve">
124-
<value>The longest supported query time is '{0}' days, but '{1}' days found.</value>
125-
</data>
126123
<data name="StartDateLaterThanNow" xml:space="preserve">
127124
<value>Invalid StartDate '{0:o}', since it is later than the current time '{1:o}'.</value>
128125
</data>

0 commit comments

Comments
 (0)