Skip to content

Added account metrics cmdlets for Batch #2274

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 5 commits into from
May 19, 2016
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 @@ -391,6 +391,90 @@ public static RequestInterceptor CreateFakeGetFileAndPropertiesFromComputeNodeRe
return response;
}

/// <summary>
/// Builds a CloudPoolUsageMetricsResponse object. Note: The lengths of all three lists must be the same.
/// </summary>
public static AzureOperationResponse<IPage<ProxyModels.PoolUsageMetrics>, ProxyModels.PoolListPoolUsageMetricsHeaders> CreatePoolListUsageMetricsResponse(
IEnumerable<string> poolIds,
IEnumerable<DateTime> startTimes,
IEnumerable<DateTime> endTimes)
{
var poolUsageList = new List<ProxyModels.PoolUsageMetrics>();

// Validate the lengths of the lists are equal
if (!(poolIds.Count() == startTimes.Count() && startTimes.Count() == endTimes.Count()))
{
throw new ArgumentException("The lists length are not equal.");
}

using (var startTimeEnumerator = startTimes.GetEnumerator())
using (var endTimeEnumerator = endTimes.GetEnumerator())
using (var poolIdEnumerator = poolIds.GetEnumerator())
{
while (startTimeEnumerator.MoveNext() && endTimeEnumerator.MoveNext() && poolIdEnumerator.MoveNext())
{
poolUsageList.Add(new ProxyModels.PoolUsageMetrics()
{
PoolId = poolIdEnumerator.Current,
StartTime = startTimeEnumerator.Current,
EndTime = endTimeEnumerator.Current
});
}
}

var response = new AzureOperationResponse
<IPage<ProxyModels.PoolUsageMetrics>, ProxyModels.PoolListPoolUsageMetricsHeaders>()
{
Response = new HttpResponseMessage(HttpStatusCode.OK),
Body = new MockPagedEnumerable<ProxyModels.PoolUsageMetrics>(poolUsageList)
};

return response;
}

/// <summary>
/// Builds a CloudPoolStatisticsResponse object. Note: Using avgCPUPercentage and startTime for validating if the pipeline return the correct values
/// </summary>
public static AzureOperationResponse<ProxyModels.PoolStatistics, ProxyModels.PoolGetAllPoolsLifetimeStatisticsHeaders> CreatePoolStatisticsResponse(
double avgCPUPercentage,
DateTime startTime)
{
var stats = new ProxyModels.PoolStatistics()
{
ResourceStats = new ProxyModels.ResourceStatistics() { AvgCPUPercentage = avgCPUPercentage },
UsageStats = new ProxyModels.UsageStatistics() { StartTime = startTime }
};

var response = new AzureOperationResponse
<ProxyModels.PoolStatistics, ProxyModels.PoolGetAllPoolsLifetimeStatisticsHeaders>()
{
Body = stats,
Response = new HttpResponseMessage(HttpStatusCode.Accepted)
};

return response;
}

/// <summary>
/// Builds a CloudJobStatisticsResponse object.Note: Using startTime for validating if the pipeline return the correct values
/// </summary>
public static AzureOperationResponse<ProxyModels.JobStatistics, ProxyModels.JobGetAllJobsLifetimeStatisticsHeaders> CreateJobStatisticsResponse(DateTime startTime)
{
var stats = new ProxyModels.JobStatistics()
{
StartTime = startTime
};

var response = new AzureOperationResponse
<ProxyModels.JobStatistics, ProxyModels.JobGetAllJobsLifetimeStatisticsHeaders>()
{
Body = stats,
Response = new HttpResponseMessage(HttpStatusCode.Accepted)
};

return response;
}

/// <summary>
/// Builds a GetRemoteLoginSettingsResponse object
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
<Compile Include="Jobs\DisableBatchJobCommandTests.cs" />
<Compile Include="Jobs\EnableBatchJobCommandTests.cs" />
<Compile Include="Jobs\GetBatchJobCommandTests.cs" />
<Compile Include="Jobs\GetBatchJobStatisticsCommandTests.cs" />
<Compile Include="Jobs\NewBatchJobCommandTests.cs" />
<Compile Include="Jobs\RemoveBatchJobCommandTests.cs" />
<Compile Include="Jobs\SetBatchJobCommandTests.cs" />
Expand All @@ -234,6 +235,8 @@
<Compile Include="Pools\DisableBatchAutoScaleCommandTests.cs" />
<Compile Include="Pools\EnableBatchAutoScaleCommandTests.cs" />
<Compile Include="Pools\GetBatchPoolCommandTests.cs" />
<Compile Include="Pools\GetBatchPoolStatisticsCommandTests.cs" />
<Compile Include="Pools\GetBatchPoolUsageMetricsCommandTests.cs" />
<Compile Include="Pools\NewBatchPoolCommandTests.cs" />
<Compile Include="Pools\RemoveBatchPoolCommandTests.cs" />
<Compile Include="Pools\SetBatchPoolCommandTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// ----------------------------------------------------------------------------------
//
// 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.Azure.Batch;
using Microsoft.Azure.Batch.Protocol;
using Microsoft.Azure.Commands.Batch.Models;
using Microsoft.Rest.Azure;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading.Tasks;
using Xunit;
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;

namespace Microsoft.Azure.Commands.Batch.Test.Jobs
{
public class GetBatchJobStatisticsCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
{
private GetBatchJobStatisticsCommand cmdlet;
private Mock<BatchClient> batchClientMock;
private Mock<ICommandRuntime> commandRuntimeMock;

public GetBatchJobStatisticsCommandTests(Xunit.Abstractions.ITestOutputHelper output)
{
ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
batchClientMock = new Mock<BatchClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetBatchJobStatisticsCommand()
{
CommandRuntime = commandRuntimeMock.Object,
BatchClient = batchClientMock.Object,
};
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetBatchJobStatisticsTest()
{
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;

DateTime startTime = DateTime.UtcNow;

AzureOperationResponse<
ProxyModels.JobStatistics,
ProxyModels.JobGetAllJobsLifetimeStatisticsHeaders> response =
BatchTestHelpers.CreateJobStatisticsResponse(startTime);

RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
ProxyModels.JobGetAllJobsLifetimeStatisticsOptions,
AzureOperationResponse<ProxyModels.JobStatistics, ProxyModels.JobGetAllJobsLifetimeStatisticsHeaders>>(responseToUse: response);

cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Setup the cmdlet to write pipeline output to a variable that can be examined later
PSJobStatistics statistics = null;
commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSJobStatistics>())).Callback<object>(c => statistics = (PSJobStatistics)c);

cmdlet.ExecuteCmdlet();

Assert.Equal(startTime, statistics.StartTime);
}
}
}
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 Microsoft.Azure.Batch;
using Microsoft.Azure.Batch.Protocol;
using Microsoft.Azure.Commands.Batch.Models;
using Microsoft.Rest.Azure;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading.Tasks;
using Xunit;
using BatchClient = Microsoft.Azure.Commands.Batch.Models.BatchClient;
using ProxyModels = Microsoft.Azure.Batch.Protocol.Models;

namespace Microsoft.Azure.Commands.Batch.Test.Pools
{
public class GetBatchPoolStatisticsCommandTests : WindowsAzure.Commands.Test.Utilities.Common.RMTestBase
{
private GetBatchPoolStatisticsCommand cmdlet;
private Mock<BatchClient> batchClientMock;
private Mock<ICommandRuntime> commandRuntimeMock;

public GetBatchPoolStatisticsCommandTests(Xunit.Abstractions.ITestOutputHelper output)
{
ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
batchClientMock = new Mock<BatchClient>();
commandRuntimeMock = new Mock<ICommandRuntime>();
cmdlet = new GetBatchPoolStatisticsCommand()
{
CommandRuntime = commandRuntimeMock.Object,
BatchClient = batchClientMock.Object,
};
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void GetBatchPoolStatisticsTest()
{
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
cmdlet.BatchContext = context;

double avgCPUPercentage = 10;
DateTime startTime = DateTime.UtcNow;

AzureOperationResponse<
ProxyModels.PoolStatistics,
ProxyModels.PoolGetAllPoolsLifetimeStatisticsHeaders> response =
BatchTestHelpers.CreatePoolStatisticsResponse(avgCPUPercentage, startTime);

RequestInterceptor interceptor = BatchTestHelpers.CreateFakeServiceResponseInterceptor<
ProxyModels.PoolGetAllPoolsLifetimeStatisticsOptions,
AzureOperationResponse<ProxyModels.PoolStatistics, ProxyModels.PoolGetAllPoolsLifetimeStatisticsHeaders>>(responseToUse: response);

cmdlet.AdditionalBehaviors = new List<BatchClientBehavior>() { interceptor };

// Setup the cmdlet to write pipeline output to a variable that can be examined later
PSPoolStatistics statistics = null;
commandRuntimeMock.Setup(r => r.WriteObject(It.IsAny<PSPoolStatistics>())).Callback<object>(c => statistics = (PSPoolStatistics)c);

cmdlet.ExecuteCmdlet();

Assert.NotNull(statistics);
Assert.Equal(startTime, statistics.UsageStatistics.StartTime);
Assert.Equal(avgCPUPercentage, statistics.ResourceStatistics.AverageCpuPercentage);
}
}
}
Loading