Skip to content

Metrics to release #303

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 2 commits into from
Mar 26, 2015
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@
<Compile Include="Events\GetAzureResourceProviderLogCommandTests.cs" />
<Compile Include="Events\GetAzureSubscriptionIdLogCommandTests.cs" />
<Compile Include="Events\GetAzureResourceGroupLogCommandTests.cs" />
<Compile Include="Metrics\FormatMetricsAsTableCommandTests.cs" />
<Compile Include="Metrics\GetMetricDefinitionsCommandTests.cs" />
<Compile Include="Metrics\GetMetricsCommandTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScenarioTests\AlertsTests.cs" />
<Compile Include="ScenarioTests\AutoscaleTests.cs" />
<Compile Include="ScenarioTests\EventsTests.cs" />
<Compile Include="ScenarioTests\MetricsTests.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Include="ScenarioTests\TestsController.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
Expand Down Expand Up @@ -161,6 +167,9 @@
<None Include="ScenarioTests\EventsTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\MetricsTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.AlertsTests\TestAddAlertRule.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -200,6 +209,12 @@
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.EventsTests\TestGetAzureSubscriptionIdLog.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.MetricsTests\TestGetMetrics.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Insights.Test.ScenarioTests.MetricsTests\TestGetMetricDefinitions.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Azure.Commands.Insights.Metrics;
using Microsoft.Azure.Insights.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.Metrics
{
public class FormatMetricsAsTableCommandTests
{
private readonly FormatMetricsAsTableCommand cmdlet;
private Mock<ICommandRuntime> commandRuntimeMock;

public FormatMetricsAsTableCommandTests()
{
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new FormatMetricsAsTableCommand()
{
CommandRuntime = commandRuntimeMock.Object,
};
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void FormatMetricsAsTableCommandParametersProcessing()
{
// Null parameter
var result = cmdlet.ProcessParameter();
Assert.True(result != null, "Non-null expected");
Assert.True(result.Length == 0, "0 length expected");

// Empty parameter
cmdlet.Metrics = new Metric[] {};
result = cmdlet.ProcessParameter();
Assert.True(result != null, "Non-null expected");
Assert.True(result.Length == 0, "0 length expected");

// Non empty parameter
DateTime timeStamp = DateTime.Parse("2015/03/01 01:30:00");
DateTime endTime = DateTime.Parse("2015/03/01 02:00:00");
DateTime startTime = DateTime.Parse("2015/03/01 00:00:00");
TimeSpan timeGrain = TimeSpan.FromMinutes(1);

cmdlet.Metrics = new Metric[]
{
new Metric()
{
EndTime = endTime,
Name = new LocalizableString() { Value = "metric1", LocalizedValue = "metric1" },
MetricValues = new MetricValue[]
{
new MetricValue()
{
Count = 1,
Timestamp = timeStamp,
},
},
Properties = new Dictionary<string, string>(),
ResourceId = "\\resourceid\\",
StartTime = startTime,
TimeGrain = timeGrain,
Unit = Unit.Count,
}
};
result = cmdlet.ProcessParameter();
Assert.True(result != null, "Non-null expected");
Assert.True(result.Length == 1, "length = 1 expected");
Assert.Equal("metric1", result[0].Name);
Assert.Equal("\\resourceid\\", result[0].ResourceId);
Assert.Equal(1, result[0].Count);
Assert.Equal(timeStamp.ToUniversalTime().ToString("u"), result[0].TimestampUTC);
Assert.Equal(endTime.ToUniversalTime().ToString("u"), result[0].EndTimeUTC);
Assert.Equal(startTime.ToUniversalTime().ToString("u"), result[0].StartTimeUTC);
Assert.Equal(timeGrain, result[0].TimeGrain);
Assert.Equal(Unit.Count, result[0].Unit);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Insights.Metrics;
using Microsoft.Azure.Insights;
using Microsoft.Azure.Insights.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.Metrics
{
public class GetMetricDefinitionsCommandTests
{
private readonly GetMetricDefinitionsCommand cmdlet;
private readonly Mock<InsightsClient> insightsClientMock;
private readonly Mock<IMetricDefinitionOperations> insightsMetricDefinitionOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private MetricDefinitionListResponse response;
private string resourceId;
private string filter;

public GetMetricDefinitionsCommandTests()
{
insightsMetricDefinitionOperationsMock = new Mock<IMetricDefinitionOperations>();
insightsClientMock = new Mock<InsightsClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetMetricDefinitionsCommand()
{
CommandRuntime = commandRuntimeMock.Object,
InsightsClient = insightsClientMock.Object
};

response = Utilities.InitializeMetricDefinitionResponse();

insightsMetricDefinitionOperationsMock.Setup(f => f.GetMetricDefinitionsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<MetricDefinitionListResponse>(response))
.Callback((string f, string s, CancellationToken t) =>
{
resourceId = f;
filter = s;
});

insightsClientMock.SetupGet(f => f.MetricDefinitionOperations).Returns(this.insightsMetricDefinitionOperationsMock.Object);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetMetricDefinitionsCommandParametersProcessing()
{
// Testting defaults and required parameters
cmdlet.ResourceId = Utilities.ResourceUri;

cmdlet.ExecuteCmdlet();
Assert.True(string.IsNullOrWhiteSpace(filter));
Assert.Equal(Utilities.ResourceUri, resourceId);

// Testing with optional parameters
cmdlet.MetricNames = new[] { "n1", "n2" };
const string expected = "name.value eq 'n1' or name.value eq 'n2'";

cmdlet.ExecuteCmdlet();
Assert.Equal(expected, filter);
Assert.Equal(Utilities.ResourceUri, resourceId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Insights.Metrics;
using Microsoft.Azure.Insights;
using Microsoft.Azure.Insights.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.Metrics
{
public class GetMetricsCommandTests
{
private readonly GetMetricsCommand cmdlet;
private readonly Mock<InsightsClient> insightsClientMock;
private readonly Mock<IMetricOperations> insightsMetricOperationsMock;
private Mock<ICommandRuntime> commandRuntimeMock;
private MetricListResponse response;
private string resourceId;
private string filter;

public GetMetricsCommandTests()
{
insightsMetricOperationsMock = new Mock<IMetricOperations>();
insightsClientMock = new Mock<InsightsClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetMetricsCommand()
{
CommandRuntime = commandRuntimeMock.Object,
InsightsClient = insightsClientMock.Object
};

response = Utilities.InitializeMetricResponse();

insightsMetricOperationsMock.Setup(f => f.GetMetricsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<MetricListResponse>(response))
.Callback((string f, string s, CancellationToken t) =>
{
resourceId = f;
filter = s;
});

insightsClientMock.SetupGet(f => f.MetricOperations).Returns(this.insightsMetricOperationsMock.Object);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetMetricsCommandParametersProcessing()
{
// Testting defaults and required parameters
cmdlet.ResourceId = Utilities.ResourceUri;
cmdlet.TimeGrain = TimeSpan.FromMinutes(1);

cmdlet.ExecuteCmdlet();
Assert.True(filter != null && filter.Contains("timeGrain eq duration'PT1M'") && filter.Contains(" and startTime eq ") && filter.Contains(" and endTime eq "));
Assert.Equal(Utilities.ResourceUri, resourceId);

cmdlet.TimeGrain = TimeSpan.FromMinutes(5);

cmdlet.ExecuteCmdlet();
Assert.True(filter != null && filter.Contains("timeGrain eq duration'PT5M'") && filter.Contains(" and startTime eq ") && filter.Contains(" and endTime eq "));
Assert.Equal(Utilities.ResourceUri, resourceId);

var endDate = DateTime.Now.AddMinutes(-1);
cmdlet.TimeGrain = TimeSpan.FromMinutes(5);
cmdlet.EndTime = endDate;

var startTime = endDate.Subtract(GetMetricsCommand.DefaultTimeRange).ToString("O");
var endTime = endDate.ToString("O");
var expected = "timeGrain eq duration'PT5M' and startTime eq " + startTime + " and endTime eq " + endTime;

// Remove the value assigned in the last execution
cmdlet.StartTime = default(DateTime);

cmdlet.ExecuteCmdlet();
Assert.Equal(expected, filter);
Assert.Equal(Utilities.ResourceUri, resourceId);

cmdlet.StartTime = endDate.Subtract(GetMetricsCommand.DefaultTimeRange).Subtract(GetMetricsCommand.DefaultTimeRange);
startTime = cmdlet.StartTime.ToString("O");
expected = "timeGrain eq duration'PT5M' and startTime eq " + startTime + " and endTime eq " + endTime;

cmdlet.ExecuteCmdlet();
Assert.Equal(expected, filter);
Assert.Equal(Utilities.ResourceUri, resourceId);

// Testing with optional parameters
cmdlet.MetricNames = new[] {"n1", "n2"};
expected = "(name.value eq 'n1' or name.value eq 'n2') and " + expected;

cmdlet.ExecuteCmdlet();
Assert.Equal(expected, filter);
Assert.Equal(Utilities.ResourceUri, resourceId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Microsoft.Azure.Commands.Insights.Test.ScenarioTests
{
public class MetricsTests
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetMetrics()
{
TestsController.NewInstance.RunPsTest("Test-GetMetrics");
}

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