Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
First batch of discovery tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkerkhove committed Apr 15, 2020
1 parent 58390c3 commit eafa1e4
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


<ItemGroup>
<PackageReference Include="Arcus.Observability.Telemetry.Core" Version="0.1.0-preview-7" />
<PackageReference Include="Guard.NET" Version="1.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public ResourceRepository(AzureResourceGraph azureResourceGraph, IOptionsMonitor
public async Task<List<Resource>> GetResourcesAsync(string resourceCollectionName)
{
var resourceDeclaration = _resourceDeclarationMonitor.CurrentValue;
var resourceCollectionDefinition = resourceDeclaration.ResourceCollections.SingleOrDefault(coll =>
coll.Name.Equals(resourceCollectionName, StringComparison.InvariantCultureIgnoreCase));
var resourceCollectionDefinition = resourceDeclaration.ResourceCollections.SingleOrDefault(collection => collection.Name.Equals(resourceCollectionName, StringComparison.InvariantCultureIgnoreCase));
if (resourceCollectionDefinition == null)
{
// No collection found so nothing to return
return null;
}

var foundResources = await _azureResourceGraph.QueryAsync(resourceCollectionDefinition.Type, resourceCollectionDefinition.Criteria);
return foundResources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Arcus.Observability.Telemetry.Core" Version="0.1.0-preview-7" />
<PackageReference Include="Bogus" Version="29.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using GuardNet;
Expand Down Expand Up @@ -29,12 +30,30 @@ public ResourceDiscoveryClient(IConfiguration configuration, ILogger logger)

public async Task<HttpResponseMessage> GetResourceCollectionsAsync()
{
return await _httpClient.GetAsync("/api/v1/resources/collections");
return await GetAsync("/api/v1/resources/collections");
}

public async Task<HttpResponseMessage> GetHealthAsync()
{
return await _httpClient.GetAsync("/api/v1/health");
return await GetAsync("/api/v1/health");
}

public async Task<HttpResponseMessage> GetDiscoveredResourcesAsync(string resourceCollectionName)
{
return await GetAsync($"/api/v1/resources/collections/{resourceCollectionName}/discovery");
}

private async Task<HttpResponseMessage> GetAsync(string uri)
{
var request = new HttpRequestMessage(HttpMethod.Get, uri);

var stopwatch = Stopwatch.StartNew();
var response = await _httpClient.SendAsync(request);
stopwatch.Stop();

_logger.LogRequest(request, response, stopwatch.Elapsed);

return response;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Threading.Tasks;
using Bogus;
using Newtonsoft.Json;
using Promitor.ResourceDiscovery.Agent.Model;
using Xunit;
using Xunit.Abstractions;

namespace Promitor.ResourceDiscovery.Tests.Integration.Services
{
[Category("Integration")]
public class ResourceDiscoveryTests : IntegrationTest
{
private readonly Faker _bogusGenerator = new Faker();

public ResourceDiscoveryTests(ITestOutputHelper testOutput)
: base(testOutput)
{
}

[Fact]
public async Task ResourceDiscovery_GetServiceBusResources_ReturnsValidList()
{
// Arrange
const string resourceCollectionName = "service-bus";
var resourceDiscoveryClient = new ResourceDiscoveryClient(Configuration, Logger);

// Act
var response = await resourceDiscoveryClient.GetDiscoveredResourcesAsync(resourceCollectionName);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var rawResponseBody = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(rawResponseBody);
var resources = JsonConvert.DeserializeObject<List<Resource>>(rawResponseBody);
Assert.NotNull(resources);
Assert.NotEmpty(resources);
}

[Fact]
public async Task ResourceDiscovery_GetForUnexistingResourceCollection_ReturnsNotFound()
{
// Arrange
string resourceCollectionName = _bogusGenerator.Lorem.Word();
var resourceDiscoveryClient = new ResourceDiscoveryClient(Configuration, Logger);

// Act
var response = await resourceDiscoveryClient.GetDiscoveredResourcesAsync(resourceCollectionName);

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
}
}
5 changes: 5 additions & 0 deletions src/Promitor.ResourceDiscovery.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Promitor.ResourceDiscovery.
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{7DC59CA1-1698-4198-AD71-9234B064CC68}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Config", "Config", "{95A90D91-5832-4FCE-8ED7-904E1837BAE0}"
ProjectSection(SolutionItems) = preProject
..\tests\discovery-config.yaml = ..\tests\discovery-config.yaml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down

0 comments on commit eafa1e4

Please sign in to comment.