Skip to content
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

Report validation error if resources not specified #1107

Merged
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
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using System.Linq;
using Microsoft.Extensions.Logging;
using Promitor.Core.Scraping.Configuration.Model;
using Promitor.Core.Scraping.Configuration.Serialization.v1.Model;
using YamlDotNet.RepresentationModel;
Expand Down Expand Up @@ -77,6 +78,12 @@ private void DeserializeMetrics(YamlMappingNode node, MetricDefinitionV1 metricD
errorReporter.ReportError(resourceTypeNode, $"Could not find a deserializer for resource type '{metricDefinition.ResourceType}'.");
}
}

if ((metricDefinition.Resources == null || !metricDefinition.Resources.Any()) &&
(metricDefinition.ResourceCollections == null || !metricDefinition.ResourceCollections.Any()))
{
errorReporter.ReportError(node, "Either 'resources' or 'resourceCollections' must be specified.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class MetricDefinitionDeserializerTests
private readonly Mock<IDeserializer<ScrapingV1>> _scrapingDeserializer;
private readonly Mock<IAzureResourceDeserializerFactory> _resourceDeserializerFactory;
private readonly Mock<IErrorReporter> _errorReporter = new Mock<IErrorReporter>();
private readonly Mock<IDeserializer<AzureResourceCollectionDefinitionV1>> _resourceCollectionsDeserializer =
new Mock<IDeserializer<AzureResourceCollectionDefinitionV1>>();

private readonly MetricDefinitionDeserializer _deserializer;

Expand All @@ -27,11 +29,10 @@ public MetricDefinitionDeserializerTests()
_scrapingDeserializer = new Mock<IDeserializer<ScrapingV1>>();
_resourceDeserializerFactory = new Mock<IAzureResourceDeserializerFactory>();

var resourceCollectionsDeserializer = new Mock<IDeserializer<AzureResourceCollectionDefinitionV1>>();
_deserializer = new MetricDefinitionDeserializer(
_azureMetricConfigurationDeserializer.Object,
_scrapingDeserializer.Object,
resourceCollectionsDeserializer.Object,
_resourceCollectionsDeserializer.Object,
_resourceDeserializerFactory.Object,
NullLogger<MetricDefinitionDeserializer>.Instance);
}
Expand Down Expand Up @@ -397,5 +398,116 @@ public void Deserialize_NoDeserializerForResourceType_ReportsError()
node.Children["resourceType"],
"Could not find a deserializer for resource type 'Generic'.");
}

[Fact]
public void Deserialize_ResourcesAndCollectionNotSupplied_ReportsError()
{
// Either a static list of resources, or a resource collection must be
// specified for the metric to be valid

// Arrange
var node = YamlUtils.CreateYamlNode("resourceType: Generic");

// Act
_deserializer.Deserialize(node, _errorReporter.Object);

// Assert
_errorReporter.Verify(
reporter => reporter.ReportError(node, "Either 'resources' or 'resourceCollections' must be specified."));
}

[Fact]
public void Deserialize_ResourcesSupplied_DoesNotReportError()
{
// Arrange
const string yamlText =
@"resourceType: Generic
resources:
- resourceUri: Microsoft.ServiceBus/namespaces/promitor-messaging
- resourceUri: Microsoft.ServiceBus/namespaces/promitor-messaging-2";
var node = YamlUtils.CreateYamlNode(yamlText);

SetupResourceDeserializer(node, new List<AzureResourceDefinitionV1> { new AzureResourceDefinitionV1() });

// Act
_deserializer.Deserialize(node, _errorReporter.Object);

// Assert
_errorReporter.Verify(
reporter => reporter.ReportError(node, "Either 'resources' or 'resourceCollections' must be specified."), Times.Never);
}

[Fact]
public void Deserialize_ResourcesEmpty_ReportsError()
{
// Arrange
const string yamlText =
@"resourceType: Generic
resources: []";
var node = YamlUtils.CreateYamlNode(yamlText);

SetupResourceDeserializer(node, new List<AzureResourceDefinitionV1>());

// Act
_deserializer.Deserialize(node, _errorReporter.Object);

// Assert
_errorReporter.Verify(
reporter => reporter.ReportError(node, "Either 'resources' or 'resourceCollections' must be specified."));
}

[Fact]
public void Deserialize_ResourceCollectionsSupplied_DoesNotReportError()
{
// Arrange
const string yamlText =
@"resourceType: Generic
resourceCollections:
- name: orders";
var node = YamlUtils.CreateYamlNode(yamlText);
var resourceCollections = new List<AzureResourceCollectionDefinitionV1>
{
new AzureResourceCollectionDefinitionV1()
};
_resourceCollectionsDeserializer.Setup(
d => d.Deserialize(It.IsAny<YamlSequenceNode>(), It.IsAny<IErrorReporter>())).Returns(resourceCollections);

// Act
_deserializer.Deserialize(node, _errorReporter.Object);

// Assert
_errorReporter.Verify(
reporter => reporter.ReportError(node, "Either 'resources' or 'resourceCollections' must be specified."), Times.Never);
}

[Fact]
public void Deserialize_ResourceCollectionsEmpty_ReportsError()
{
// Arrange
const string yamlText =
@"resourceType: Generic
resourceCollections: []";
var node = YamlUtils.CreateYamlNode(yamlText);
_resourceCollectionsDeserializer.Setup(
d => d.Deserialize(It.IsAny<YamlSequenceNode>(), It.IsAny<IErrorReporter>())).Returns(new List<AzureResourceCollectionDefinitionV1>());

// Act
_deserializer.Deserialize(node, _errorReporter.Object);

// Assert
_errorReporter.Verify(
reporter => reporter.ReportError(node, "Either 'resources' or 'resourceCollections' must be specified."));
}

private void SetupResourceDeserializer(YamlMappingNode node, IReadOnlyCollection<AzureResourceDefinitionV1> resources)
{
var resourceDeserializer = new Mock<IDeserializer<AzureResourceDefinitionV1>>();
_resourceDeserializerFactory.Setup(
f => f.GetDeserializerFor(It.IsAny<ResourceType>())).Returns(resourceDeserializer.Object);

resourceDeserializer.Setup(
d => d.Deserialize((YamlSequenceNode)node.Children["resources"], _errorReporter.Object))
.Returns(resources);
}
}
}