Skip to content

Commit 0766b8f

Browse files
committed
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.
1 parent 0c00632 commit 0766b8f

File tree

8 files changed

+119
-43
lines changed

8 files changed

+119
-43
lines changed

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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
Assert.Equal("1", OutputClasses.CustomPrinter.Print(x));
46+
Assert.Equal("1.2", OutputClasses.CustomPrinter.Print(y));
47+
Assert.Equal("hello", OutputClasses.CustomPrinter.Print(s));
48+
Assert.Equal("True", OutputClasses.CustomPrinter.Print(b));
49+
Assert.Equal(XmlConvert.ToString(ts), OutputClasses.CustomPrinter.Print(ts));
50+
Assert.Equal(dt.ToUniversalTime().ToString("O"), OutputClasses.CustomPrinter.Print(dt));
51+
Assert.Equal(dtUtc.ToString("O"), OutputClasses.CustomPrinter.Print(dtUtc));
52+
53+
Assert.Equal(string.Empty, OutputClasses.CustomPrinter.Print(null));
54+
Assert.Equal(string.Empty, OutputClasses.CustomPrinter.Print(""));
55+
}
56+
57+
[Fact]
58+
[Trait(Category.AcceptanceType, Category.CheckIn)]
59+
public void CustomPrinterTest_ComplexTypes()
60+
{
61+
var stringList = new List<string>();
62+
var dictionarySS = new Dictionary<string, string>();
63+
var dictionarySO = new Dictionary<string, object>();
64+
var eventData = Test.Utilities.CreateFakeEvent(id: null, newDates: false);
65+
66+
Assert.Equal("{}", OutputClasses.CustomPrinter.Print(stringList));
67+
Assert.Equal("{}", OutputClasses.CustomPrinter.Print(dictionarySS));
68+
Assert.Equal("{}", OutputClasses.CustomPrinter.Print(dictionarySO));
69+
70+
string result = OutputClasses.CustomPrinter.Print(eventData);
71+
Debug.WriteLine("EventData: ");
72+
Debug.WriteLine(result);
73+
Assert.Equal(
74+
expected: "[\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]",
75+
actual: result);
76+
77+
dictionarySS.Add("k1", "v1");
78+
dictionarySS.Add("k2", "v2");
79+
result = OutputClasses.CustomPrinter.Print(dictionarySS);
80+
Debug.WriteLine("Dictionary<string, string>: ");
81+
Debug.WriteLine(result);
82+
Assert.Equal(
83+
expected: "{\r\n[k1, v1]\r\n[k2, v2]\r\n}",
84+
actual: result);
85+
86+
dictionarySO.Add("k1", eventData);
87+
dictionarySO.Add("k2", Test.Utilities.CreateFakeEvent(id: "secondId", newDates: false));
88+
result = OutputClasses.CustomPrinter.Print(dictionarySO);
89+
Debug.WriteLine("Dictionary<string, object>: ");
90+
Debug.WriteLine(result);
91+
Assert.Equal(
92+
expected: "{\r\n[k1, Microsoft.Azure.Management.Monitor.Models.EventData]\r\n[k2, Microsoft.Azure.Management.Monitor.Models.EventData]\r\n}",
93+
actual: result);
94+
}
95+
}
96+
}
97+

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,6 @@ public static void ExecuteVerifications(LogsCmdletBase cmdlet, Mock<IActivityLog
318318
cmdlet.EndTime = DateTime.Now.Subtract(TimeSpan.FromSeconds(21));
319319
nextLink = null;
320320
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());
326321
}
327322

328323
#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)