Skip to content

Commit be4cca8

Browse files
committed
Merge pull request #303 from AuxMon/metricsToRelease
Metrics to release
2 parents d6d4630 + 50aee9a commit be4cca8

37 files changed

+3055
-656
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,16 @@
125125
<Compile Include="Events\GetAzureResourceProviderLogCommandTests.cs" />
126126
<Compile Include="Events\GetAzureSubscriptionIdLogCommandTests.cs" />
127127
<Compile Include="Events\GetAzureResourceGroupLogCommandTests.cs" />
128+
<Compile Include="Metrics\FormatMetricsAsTableCommandTests.cs" />
129+
<Compile Include="Metrics\GetMetricDefinitionsCommandTests.cs" />
130+
<Compile Include="Metrics\GetMetricsCommandTests.cs" />
128131
<Compile Include="Properties\AssemblyInfo.cs" />
129132
<Compile Include="ScenarioTests\AlertsTests.cs" />
130133
<Compile Include="ScenarioTests\AutoscaleTests.cs" />
131134
<Compile Include="ScenarioTests\EventsTests.cs" />
135+
<Compile Include="ScenarioTests\MetricsTests.cs">
136+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
137+
</Compile>
132138
<Compile Include="ScenarioTests\TestsController.cs" />
133139
<Compile Include="Utilities.cs" />
134140
</ItemGroup>
@@ -161,6 +167,9 @@
161167
<None Include="ScenarioTests\EventsTests.ps1">
162168
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
163169
</None>
170+
<None Include="ScenarioTests\MetricsTests.ps1">
171+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
172+
</None>
164173
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AlertsTests\TestAddAlertRule.json">
165174
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
166175
</None>
@@ -200,6 +209,12 @@
200209
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.EventsTests\TestGetAzureSubscriptionIdLog.json">
201210
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
202211
</None>
212+
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.MetricsTests\TestGetMetrics.json">
213+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
214+
</None>
215+
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.MetricsTests\TestGetMetricDefinitions.json">
216+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
217+
</None>
203218
</ItemGroup>
204219
<ItemGroup>
205220
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.Management.Automation;
18+
using Microsoft.Azure.Commands.Insights.Metrics;
19+
using Microsoft.Azure.Insights.Models;
20+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
21+
using Moq;
22+
using Xunit;
23+
24+
namespace Microsoft.Azure.Commands.Insights.Test.Metrics
25+
{
26+
public class FormatMetricsAsTableCommandTests
27+
{
28+
private readonly FormatMetricsAsTableCommand cmdlet;
29+
private Mock<ICommandRuntime> commandRuntimeMock;
30+
31+
public FormatMetricsAsTableCommandTests()
32+
{
33+
commandRuntimeMock = new Mock<ICommandRuntime>();
34+
cmdlet = new FormatMetricsAsTableCommand()
35+
{
36+
CommandRuntime = commandRuntimeMock.Object,
37+
};
38+
}
39+
40+
[Fact]
41+
[Trait(Category.AcceptanceType, Category.CheckIn)]
42+
public void FormatMetricsAsTableCommandParametersProcessing()
43+
{
44+
// Null parameter
45+
var result = cmdlet.ProcessParameter();
46+
Assert.True(result != null, "Non-null expected");
47+
Assert.True(result.Length == 0, "0 length expected");
48+
49+
// Empty parameter
50+
cmdlet.Metrics = new Metric[] {};
51+
result = cmdlet.ProcessParameter();
52+
Assert.True(result != null, "Non-null expected");
53+
Assert.True(result.Length == 0, "0 length expected");
54+
55+
// Non empty parameter
56+
DateTime timeStamp = DateTime.Parse("2015/03/01 01:30:00");
57+
DateTime endTime = DateTime.Parse("2015/03/01 02:00:00");
58+
DateTime startTime = DateTime.Parse("2015/03/01 00:00:00");
59+
TimeSpan timeGrain = TimeSpan.FromMinutes(1);
60+
61+
cmdlet.Metrics = new Metric[]
62+
{
63+
new Metric()
64+
{
65+
EndTime = endTime,
66+
Name = new LocalizableString() { Value = "metric1", LocalizedValue = "metric1" },
67+
MetricValues = new MetricValue[]
68+
{
69+
new MetricValue()
70+
{
71+
Count = 1,
72+
Timestamp = timeStamp,
73+
},
74+
},
75+
Properties = new Dictionary<string, string>(),
76+
ResourceId = "\\resourceid\\",
77+
StartTime = startTime,
78+
TimeGrain = timeGrain,
79+
Unit = Unit.Count,
80+
}
81+
};
82+
result = cmdlet.ProcessParameter();
83+
Assert.True(result != null, "Non-null expected");
84+
Assert.True(result.Length == 1, "length = 1 expected");
85+
Assert.Equal("metric1", result[0].Name);
86+
Assert.Equal("\\resourceid\\", result[0].ResourceId);
87+
Assert.Equal(1, result[0].Count);
88+
Assert.Equal(timeStamp.ToUniversalTime().ToString("u"), result[0].TimestampUTC);
89+
Assert.Equal(endTime.ToUniversalTime().ToString("u"), result[0].EndTimeUTC);
90+
Assert.Equal(startTime.ToUniversalTime().ToString("u"), result[0].StartTimeUTC);
91+
Assert.Equal(timeGrain, result[0].TimeGrain);
92+
Assert.Equal(Unit.Count, result[0].Unit);
93+
}
94+
}
95+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.Management.Automation;
17+
using System.Threading;
18+
using System.Threading.Tasks;
19+
using Microsoft.Azure.Commands.Insights.Metrics;
20+
using Microsoft.Azure.Insights;
21+
using Microsoft.Azure.Insights.Models;
22+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
23+
using Moq;
24+
using Xunit;
25+
26+
namespace Microsoft.Azure.Commands.Insights.Test.Metrics
27+
{
28+
public class GetMetricDefinitionsCommandTests
29+
{
30+
private readonly GetMetricDefinitionsCommand cmdlet;
31+
private readonly Mock<InsightsClient> insightsClientMock;
32+
private readonly Mock<IMetricDefinitionOperations> insightsMetricDefinitionOperationsMock;
33+
private Mock<ICommandRuntime> commandRuntimeMock;
34+
private MetricDefinitionListResponse response;
35+
private string resourceId;
36+
private string filter;
37+
38+
public GetMetricDefinitionsCommandTests()
39+
{
40+
insightsMetricDefinitionOperationsMock = new Mock<IMetricDefinitionOperations>();
41+
insightsClientMock = new Mock<InsightsClient>();
42+
commandRuntimeMock = new Mock<ICommandRuntime>();
43+
cmdlet = new GetMetricDefinitionsCommand()
44+
{
45+
CommandRuntime = commandRuntimeMock.Object,
46+
InsightsClient = insightsClientMock.Object
47+
};
48+
49+
response = Utilities.InitializeMetricDefinitionResponse();
50+
51+
insightsMetricDefinitionOperationsMock.Setup(f => f.GetMetricDefinitionsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
52+
.Returns(Task.FromResult<MetricDefinitionListResponse>(response))
53+
.Callback((string f, string s, CancellationToken t) =>
54+
{
55+
resourceId = f;
56+
filter = s;
57+
});
58+
59+
insightsClientMock.SetupGet(f => f.MetricDefinitionOperations).Returns(this.insightsMetricDefinitionOperationsMock.Object);
60+
}
61+
62+
[Fact]
63+
[Trait(Category.AcceptanceType, Category.CheckIn)]
64+
public void GetMetricDefinitionsCommandParametersProcessing()
65+
{
66+
// Testting defaults and required parameters
67+
cmdlet.ResourceId = Utilities.ResourceUri;
68+
69+
cmdlet.ExecuteCmdlet();
70+
Assert.True(string.IsNullOrWhiteSpace(filter));
71+
Assert.Equal(Utilities.ResourceUri, resourceId);
72+
73+
// Testing with optional parameters
74+
cmdlet.MetricNames = new[] { "n1", "n2" };
75+
const string expected = "name.value eq 'n1' or name.value eq 'n2'";
76+
77+
cmdlet.ExecuteCmdlet();
78+
Assert.Equal(expected, filter);
79+
Assert.Equal(Utilities.ResourceUri, resourceId);
80+
}
81+
}
82+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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.Management.Automation;
17+
using System.Threading;
18+
using System.Threading.Tasks;
19+
using Microsoft.Azure.Commands.Insights.Metrics;
20+
using Microsoft.Azure.Insights;
21+
using Microsoft.Azure.Insights.Models;
22+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
23+
using Moq;
24+
using Xunit;
25+
26+
namespace Microsoft.Azure.Commands.Insights.Test.Metrics
27+
{
28+
public class GetMetricsCommandTests
29+
{
30+
private readonly GetMetricsCommand cmdlet;
31+
private readonly Mock<InsightsClient> insightsClientMock;
32+
private readonly Mock<IMetricOperations> insightsMetricOperationsMock;
33+
private Mock<ICommandRuntime> commandRuntimeMock;
34+
private MetricListResponse response;
35+
private string resourceId;
36+
private string filter;
37+
38+
public GetMetricsCommandTests()
39+
{
40+
insightsMetricOperationsMock = new Mock<IMetricOperations>();
41+
insightsClientMock = new Mock<InsightsClient>();
42+
commandRuntimeMock = new Mock<ICommandRuntime>();
43+
cmdlet = new GetMetricsCommand()
44+
{
45+
CommandRuntime = commandRuntimeMock.Object,
46+
InsightsClient = insightsClientMock.Object
47+
};
48+
49+
response = Utilities.InitializeMetricResponse();
50+
51+
insightsMetricOperationsMock.Setup(f => f.GetMetricsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
52+
.Returns(Task.FromResult<MetricListResponse>(response))
53+
.Callback((string f, string s, CancellationToken t) =>
54+
{
55+
resourceId = f;
56+
filter = s;
57+
});
58+
59+
insightsClientMock.SetupGet(f => f.MetricOperations).Returns(this.insightsMetricOperationsMock.Object);
60+
}
61+
62+
[Fact]
63+
[Trait(Category.AcceptanceType, Category.CheckIn)]
64+
public void GetMetricsCommandParametersProcessing()
65+
{
66+
// Testting defaults and required parameters
67+
cmdlet.ResourceId = Utilities.ResourceUri;
68+
cmdlet.TimeGrain = TimeSpan.FromMinutes(1);
69+
70+
cmdlet.ExecuteCmdlet();
71+
Assert.True(filter != null && filter.Contains("timeGrain eq duration'PT1M'") && filter.Contains(" and startTime eq ") && filter.Contains(" and endTime eq "));
72+
Assert.Equal(Utilities.ResourceUri, resourceId);
73+
74+
cmdlet.TimeGrain = TimeSpan.FromMinutes(5);
75+
76+
cmdlet.ExecuteCmdlet();
77+
Assert.True(filter != null && filter.Contains("timeGrain eq duration'PT5M'") && filter.Contains(" and startTime eq ") && filter.Contains(" and endTime eq "));
78+
Assert.Equal(Utilities.ResourceUri, resourceId);
79+
80+
var endDate = DateTime.Now.AddMinutes(-1);
81+
cmdlet.TimeGrain = TimeSpan.FromMinutes(5);
82+
cmdlet.EndTime = endDate;
83+
84+
var startTime = endDate.Subtract(GetMetricsCommand.DefaultTimeRange).ToString("O");
85+
var endTime = endDate.ToString("O");
86+
var expected = "timeGrain eq duration'PT5M' and startTime eq " + startTime + " and endTime eq " + endTime;
87+
88+
// Remove the value assigned in the last execution
89+
cmdlet.StartTime = default(DateTime);
90+
91+
cmdlet.ExecuteCmdlet();
92+
Assert.Equal(expected, filter);
93+
Assert.Equal(Utilities.ResourceUri, resourceId);
94+
95+
cmdlet.StartTime = endDate.Subtract(GetMetricsCommand.DefaultTimeRange).Subtract(GetMetricsCommand.DefaultTimeRange);
96+
startTime = cmdlet.StartTime.ToString("O");
97+
expected = "timeGrain eq duration'PT5M' and startTime eq " + startTime + " and endTime eq " + endTime;
98+
99+
cmdlet.ExecuteCmdlet();
100+
Assert.Equal(expected, filter);
101+
Assert.Equal(Utilities.ResourceUri, resourceId);
102+
103+
// Testing with optional parameters
104+
cmdlet.MetricNames = new[] {"n1", "n2"};
105+
expected = "(name.value eq 'n1' or name.value eq 'n2') and " + expected;
106+
107+
cmdlet.ExecuteCmdlet();
108+
Assert.Equal(expected, filter);
109+
Assert.Equal(Utilities.ResourceUri, resourceId);
110+
}
111+
}
112+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 Microsoft.WindowsAzure.Commands.ScenarioTest;
16+
using Xunit;
17+
18+
namespace Microsoft.Azure.Commands.Insights.Test.ScenarioTests
19+
{
20+
public class MetricsTests
21+
{
22+
[Fact]
23+
[Trait(Category.AcceptanceType, Category.CheckIn)]
24+
public void TestGetMetrics()
25+
{
26+
TestsController.NewInstance.RunPsTest("Test-GetMetrics");
27+
}
28+
29+
[Fact]
30+
[Trait(Category.AcceptanceType, Category.CheckIn)]
31+
public void TestGetMetricDefinitions()
32+
{
33+
TestsController.NewInstance.RunPsTest("Test-GetMetricDefinitions");
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)