Skip to content

Commit

Permalink
Update unit tests to handle new MetricDefinition
Browse files Browse the repository at this point in the history
- Updating `MetricsDeclarationBuilder` to use v1 Builder objects instead of the model objects.
- Updating the tests to handle the fact that a `MetricDefinition` isn't a resource anymore.
  • Loading branch information
adamconnelly committed Aug 15, 2019
1 parent 1d750e4 commit fd4a234
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
using AutoMapper;
using Microsoft.Azure.Management.Monitor.Fluent.Models;
using Microsoft.Extensions.Logging.Abstractions;
using Promitor.Core.Scraping.Configuration.Model;
using Promitor.Core.Scraping.Configuration.Model.Metrics;
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes;
using Promitor.Core.Scraping.Configuration.Serialization;
using Promitor.Core.Scraping.Configuration.Serialization.Enum;
using Promitor.Core.Scraping.Configuration.Serialization.v1.Model;
using Promitor.Core.Scraping.Configuration.Serialization.v1.Model.Metrics;
using Promitor.Core.Scraping.Configuration.Serialization.v1.Model.Metrics.ResourceTypes;
using Promitor.Integrations.AzureStorage;

namespace Promitor.Scraper.Tests.Unit.Builders.Metrics.v1
{
public class MetricsDeclarationBuilder
{
private readonly AzureMetadata _azureMetadata;
private readonly List<Core.Scraping.Configuration.Model.Metrics.MetricDefinition> _metrics = new List<Core.Scraping.Configuration.Model.Metrics.MetricDefinition>();
private MetricDefaults _metricDefaults = new MetricDefaults
private readonly AzureMetadataV1 _azureMetadata;
private readonly List<MetricDefinitionV1> _metrics = new List<MetricDefinitionV1>();
private MetricDefaultsV1 _metricDefaults = new MetricDefaultsV1
{
Scraping = new Scraping { Schedule = @"0 * * ? * *" }
Scraping = new ScrapingV1 { Schedule = @"0 * * ? * *" }
};

public MetricsDeclarationBuilder(AzureMetadata azureMetadata)
public MetricsDeclarationBuilder(AzureMetadataV1 azureMetadata)
{
_azureMetadata = azureMetadata;
}

public static MetricsDeclarationBuilder WithMetadata(string tenantId = "tenantId", string subscriptionId = "subscriptionId", string resourceGroupName = "resourceGroupName")
{
var azureMetadata = new AzureMetadata
var azureMetadata = new AzureMetadataV1
{
TenantId = tenantId,
SubscriptionId = subscriptionId,
Expand All @@ -42,7 +42,7 @@ public static MetricsDeclarationBuilder WithoutMetadata()
return new MetricsDeclarationBuilder(azureMetadata: null);
}

public MetricsDeclarationBuilder WithDefaults(MetricDefaults defaults)
public MetricsDeclarationBuilder WithDefaults(MetricDefaultsV1 defaults)
{
_metricDefaults = defaults;

Expand All @@ -51,7 +51,7 @@ public MetricsDeclarationBuilder WithDefaults(MetricDefaults defaults)

public string Build(IMapper mapper)
{
var metricsDeclaration = new MetricsDeclaration
var metricsDeclaration = new MetricsDeclarationV1
{
Version = SpecVersion.v1.ToString(),
AzureMetadata = _azureMetadata,
Expand All @@ -66,7 +66,7 @@ public string Build(IMapper mapper)
public MetricsDeclarationBuilder WithServiceBusMetric(string metricName = "promitor-service-bus", string metricDescription = "Description for a metric", string queueName = "promitor-queue", string serviceBusNamespace = "promitor-namespace", string azureMetricName = "Total")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new ServiceBusQueueMetricDefinition
var metric = new ServiceBusQueueMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -82,7 +82,7 @@ public string Build(IMapper mapper)
public MetricsDeclarationBuilder WithContainerInstanceMetric(string metricName = "promitor-container-instance", string metricDescription = "Description for a metric", string containerGroup = "promitor-group", string azureMetricName = "Total")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new ContainerInstanceMetricDefinition
var metric = new ContainerInstanceMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -98,7 +98,7 @@ public string Build(IMapper mapper)
public MetricsDeclarationBuilder WithContainerRegistryMetric(string metricName = "promitor-container-registry", string metricDescription = "Description for a metric", string registryName = "promitor-container-registry", string azureMetricName = "Total")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new ContainerRegistryMetricDefinition
var metric = new ContainerRegistryMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -113,7 +113,7 @@ public string Build(IMapper mapper)
public MetricsDeclarationBuilder WithCosmosDbMetric(string metricName = "promitor-cosmosdb", string metricDescription = "Description for a metric", string dbName = "promitor-cosmosdb", string azureMetricName = "TotalRequests")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new CosmosDbMetricDefinition
var metric = new CosmosDbMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -128,12 +128,12 @@ public string Build(IMapper mapper)
public MetricsDeclarationBuilder WithAzureStorageQueueMetric(string metricName = "promitor", string metricDescription = "Description for a metric", string queueName = "promitor-queue", string accountName = "promitor-account", string sasToken = "?sig=promitor", string azureMetricName = AzureStorageConstants.Queues.Metrics.MessageCount)
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var secret = new Secret
var secret = new SecretV1
{
RawValue = sasToken
};

var metric = new StorageQueueMetricDefinition
var metric = new StorageQueueMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -150,7 +150,7 @@ public string Build(IMapper mapper)
public MetricsDeclarationBuilder WithVirtualMachineMetric(string metricName = "promitor-virtual-machine", string metricDescription = "Description for a metric", string virtualMachineName = "promitor-virtual-machine-name", string azureMetricName = "Total")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new VirtualMachineMetricDefinition
var metric = new VirtualMachineMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -166,7 +166,7 @@ public string Build(IMapper mapper)
public MetricsDeclarationBuilder WithNetworkInterfaceMetric(string metricName = "promitor-network-interface", string metricDescription = "Description for a metric", string networkInterfaceName = "promitor-network-interface-name", string azureMetricName = "Total")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new NetworkInterfaceMetricDefinition
var metric = new NetworkInterfaceMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -182,7 +182,7 @@ public string Build(IMapper mapper)
public MetricsDeclarationBuilder WithGenericMetric(string metricName = "foo", string metricDescription = "Description for a metric", string resourceUri = "Microsoft.ServiceBus/namespaces/promitor-messaging", string filter = "EntityName eq \'orders\'", string azureMetricName = "Total")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new GenericAzureMetricDefinition
var metric = new GenericAzureMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -195,12 +195,12 @@ public string Build(IMapper mapper)
return this;
}

private AzureMetricConfiguration CreateAzureMetricConfiguration(string azureMetricName)
private AzureMetricConfigurationV1 CreateAzureMetricConfiguration(string azureMetricName)
{
return new AzureMetricConfiguration
return new AzureMetricConfigurationV1
{
MetricName = azureMetricName,
Aggregation = new MetricAggregation
Aggregation = new MetricAggregationV1
{
Type = AggregationType.Average
}
Expand All @@ -210,7 +210,7 @@ private AzureMetricConfiguration CreateAzureMetricConfiguration(string azureMetr
public MetricsDeclarationBuilder WithRedisCacheMetric(string metricName = "promitor-redis", string metricDescription = "Description for a metric", string cacheName = "promitor-redis", string azureMetricName = "CacheHits")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new RedisCacheMetricDefinition
var metric = new RedisCacheMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand All @@ -225,7 +225,7 @@ private AzureMetricConfiguration CreateAzureMetricConfiguration(string azureMetr
public MetricsDeclarationBuilder WithPostgreSqlMetric(string metricName = "promitor-postgresql", string metricDescription = "Description for a metric", string serverName = "promitor-postgresql", string azureMetricName = "cpu_percent")
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName);
var metric = new PostgreSqlMetricDefinition
var metric = new PostgreSqlMetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,24 @@ public void YamlSerialization_SerializeAndDeserializeValidConfigForAzureStorageQ
Assert.Single(deserializedConfiguration.Metrics);
var deserializedMetricDefinition = deserializedConfiguration.Metrics.FirstOrDefault();
AssertMetricDefinition(deserializedMetricDefinition, azureStorageQueueMetricDefinition);
var deserializedAzureStorageQueueMetricDefinition = deserializedMetricDefinition as StorageQueueMetricDefinition;
AssertAzureStorageQueueMetricDefinition(deserializedAzureStorageQueueMetricDefinition, azureStorageQueueMetricDefinition, deserializedMetricDefinition);
AssertAzureStorageQueueMetricDefinition(deserializedMetricDefinition, azureStorageQueueMetricDefinition);
}

private static void AssertAzureStorageQueueMetricDefinition(StorageQueueMetricDefinition deserializedStorageQueueMetricDefinition, StorageQueueMetricDefinitionV1 storageQueueMetricDefinition, MetricDefinition deserializedMetricDefinition)
private static void AssertAzureStorageQueueMetricDefinition(MetricDefinition deserializedStorageQueueMetricDefinition, StorageQueueMetricDefinitionV1 storageQueueMetricDefinition)
{
Assert.NotNull(deserializedStorageQueueMetricDefinition);
Assert.Equal(storageQueueMetricDefinition.AccountName, deserializedStorageQueueMetricDefinition.AccountName);
Assert.Equal(storageQueueMetricDefinition.QueueName, deserializedStorageQueueMetricDefinition.QueueName);
Assert.NotNull(deserializedStorageQueueMetricDefinition.SasToken);
Assert.Equal(storageQueueMetricDefinition.SasToken.RawValue, deserializedStorageQueueMetricDefinition.SasToken.RawValue);
Assert.Equal(storageQueueMetricDefinition.SasToken.EnvironmentVariable, deserializedStorageQueueMetricDefinition.SasToken.EnvironmentVariable);
Assert.NotNull(deserializedMetricDefinition.AzureMetricConfiguration);
Assert.Equal(storageQueueMetricDefinition.AzureMetricConfiguration.MetricName, deserializedMetricDefinition.AzureMetricConfiguration.MetricName);
Assert.NotNull(deserializedMetricDefinition.AzureMetricConfiguration.Aggregation);
Assert.Equal(storageQueueMetricDefinition.AzureMetricConfiguration.Aggregation.Type, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Type);
Assert.Equal(storageQueueMetricDefinition.AzureMetricConfiguration.Aggregation.Interval, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Interval);
var deserializedResource = deserializedStorageQueueMetricDefinition.Resources.Single() as StorageQueueMetricDefinition;

Assert.NotNull(deserializedResource);
Assert.Equal(storageQueueMetricDefinition.AccountName, deserializedResource.AccountName);
Assert.Equal(storageQueueMetricDefinition.QueueName, deserializedResource.QueueName);
Assert.NotNull(deserializedResource.SasToken);
Assert.Equal(storageQueueMetricDefinition.SasToken.RawValue, deserializedResource.SasToken.RawValue);
Assert.Equal(storageQueueMetricDefinition.SasToken.EnvironmentVariable, deserializedResource.SasToken.EnvironmentVariable);
Assert.NotNull(deserializedStorageQueueMetricDefinition.AzureMetricConfiguration);
Assert.Equal(storageQueueMetricDefinition.AzureMetricConfiguration.MetricName, deserializedStorageQueueMetricDefinition.AzureMetricConfiguration.MetricName);
Assert.NotNull(deserializedStorageQueueMetricDefinition.AzureMetricConfiguration.Aggregation);
Assert.Equal(storageQueueMetricDefinition.AzureMetricConfiguration.Aggregation.Type, deserializedStorageQueueMetricDefinition.AzureMetricConfiguration.Aggregation.Type);
Assert.Equal(storageQueueMetricDefinition.AzureMetricConfiguration.Aggregation.Interval, deserializedStorageQueueMetricDefinition.AzureMetricConfiguration.Aggregation.Interval);
}

private StorageQueueMetricDefinitionV1 GenerateBogusAzureStorageQueueMetricDefinition(string resourceGroupName, string metricScrapingInterval, string sasTokenRawValue, string sasTokenEnvironmentVariable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ public void YamlSerialization_SerializeAndDeserializeValidConfigForContainerInst
Assert.Single(deserializedConfiguration.Metrics);
var deserializedMetricDefinition = deserializedConfiguration.Metrics.FirstOrDefault();
AssertMetricDefinition(deserializedMetricDefinition, containerInstanceMetricDefinition);
var deserializedServiceBusMetricDefinition = deserializedMetricDefinition as ContainerInstanceMetricDefinition;
AssertContainerInstanceMetricDefinition(deserializedServiceBusMetricDefinition, containerInstanceMetricDefinition, deserializedMetricDefinition);
AssertContainerInstanceMetricDefinition(deserializedMetricDefinition, containerInstanceMetricDefinition);
}

private static void AssertContainerInstanceMetricDefinition(ContainerInstanceMetricDefinition deserializedContainerInstanceMetricDefinition, ContainerInstanceMetricDefinitionV1 containerInstanceMetricDefinition, MetricDefinition deserializedMetricDefinition)
private static void AssertContainerInstanceMetricDefinition(MetricDefinition deserializedContainerInstanceMetricDefinition, ContainerInstanceMetricDefinitionV1 containerInstanceMetricDefinition)
{
Assert.NotNull(deserializedContainerInstanceMetricDefinition);
Assert.Equal(containerInstanceMetricDefinition.ContainerGroup, deserializedContainerInstanceMetricDefinition.ContainerGroup);
Assert.NotNull(deserializedMetricDefinition.AzureMetricConfiguration);
Assert.Equal(containerInstanceMetricDefinition.AzureMetricConfiguration.MetricName, deserializedMetricDefinition.AzureMetricConfiguration.MetricName);
Assert.NotNull(deserializedMetricDefinition.AzureMetricConfiguration.Aggregation);
Assert.Equal(containerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation.Type, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Type);
Assert.Equal(containerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation.Interval, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Interval);
var deserializedResource = deserializedContainerInstanceMetricDefinition.Resources.Single() as ContainerInstanceMetricDefinition;

Assert.NotNull(deserializedResource);
Assert.Equal(containerInstanceMetricDefinition.ContainerGroup, deserializedResource.ContainerGroup);
Assert.NotNull(deserializedContainerInstanceMetricDefinition.AzureMetricConfiguration);
Assert.Equal(containerInstanceMetricDefinition.AzureMetricConfiguration.MetricName, deserializedContainerInstanceMetricDefinition.AzureMetricConfiguration.MetricName);
Assert.NotNull(deserializedContainerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation);
Assert.Equal(containerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation.Type, deserializedContainerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation.Type);
Assert.Equal(containerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation.Interval, deserializedContainerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation.Interval);
}
private ContainerInstanceMetricDefinitionV1 GenerateBogusContainerInstanceMetricDefinition(string resourceGroupName, string metricScrapingInterval)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Bogus;
using Microsoft.Extensions.Logging.Abstractions;
using Promitor.Core.Scraping.Configuration.Model;
using Promitor.Core.Scraping.Configuration.Model.Metrics;
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes;
using Promitor.Core.Scraping.Configuration.Serialization;
using Promitor.Core.Scraping.Configuration.Serialization.Enum;
Expand Down Expand Up @@ -50,14 +51,15 @@ public void YamlSerialization_SerializeAndDeserializeValidConfigForContainerRegi
Assert.Single(deserializedConfiguration.Metrics);
var deserializedMetricDefinition = deserializedConfiguration.Metrics.FirstOrDefault();
AssertMetricDefinition(deserializedMetricDefinition, containerRegistryMetricDefinition);
var deserializedContainerRegistryMetricDefinition = deserializedMetricDefinition as ContainerRegistryMetricDefinition;
AssertContainerRegistryMetricDefinition(deserializedContainerRegistryMetricDefinition, containerRegistryMetricDefinition);
AssertContainerRegistryMetricDefinition(deserializedMetricDefinition, containerRegistryMetricDefinition);
}

private static void AssertContainerRegistryMetricDefinition(ContainerRegistryMetricDefinition deserializedServiceBusMetricDefinition, ContainerRegistryMetricDefinitionV1 serviceBusMetricDefinition)
private static void AssertContainerRegistryMetricDefinition(MetricDefinition deserializedContainerRegistryMetricDefinition, ContainerRegistryMetricDefinitionV1 containerRegistryMetricDefinition)
{
Assert.NotNull(deserializedServiceBusMetricDefinition);
Assert.Equal(serviceBusMetricDefinition.RegistryName, deserializedServiceBusMetricDefinition.RegistryName);
var deserializedResource = deserializedContainerRegistryMetricDefinition.Resources.Single() as ContainerRegistryMetricDefinition;

Assert.NotNull(deserializedResource);
Assert.Equal(containerRegistryMetricDefinition.RegistryName, deserializedResource.RegistryName);
}

private ContainerRegistryMetricDefinitionV1 GenerateBogusContainerRegistryMetricDefinition(string resourceGroupName, string metricScrapingInterval)
Expand Down
Loading

0 comments on commit fd4a234

Please sign in to comment.