Skip to content

Commit

Permalink
Provide integration tests for other operations (#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkerkhove authored Nov 18, 2020
1 parent 457d354 commit 640e185
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 24 deletions.
1 change: 1 addition & 0 deletions build/azure-devops/agents-ci-discovery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pr:
- build/azure-devops/agents-ci-discovery.yml
variables:
- template: ./variables/build.yml
- template: ./variables/tests.yml
- name: Image.Name
value: 'tomkerkhove/promitor-agent-resource-discovery-ci'
- name: Image.TaggedName
Expand Down
1 change: 1 addition & 0 deletions build/azure-devops/agents-ci-scraper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pr:
- build/azure-devops/agents-ci-scraper.yml
variables:
- template: ./variables/build.yml
- template: ./variables/tests.yml
- template: ./variables/helm.yml
- name: Image.Name
value: 'tomkerkhove/promitor-agent-scraper-ci'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ parameters:
default: true

variables:
- template: ./variables/tests.yml
- template: ./variables/build.yml
- template: ./variables/containers.yml
- name: 'Image.Version'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ parameters:
default: 0.1.0

variables:
- template: ./variables/tests.yml
- name: 'Release.TagName'
value: 'helm-$(Helm.Chart.Version)'
- name: 'Release.Title'
Expand Down
1 change: 1 addition & 0 deletions build/azure-devops/agents-scraper-release-official.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ parameters:
default: Promitor Scraper v$(Image.Version)

variables:
- template: ./variables/tests.yml
- template: ./variables/build.yml
- name: 'Image.Name'
value: 'tomkerkhove/promitor-agent-scraper'
Expand Down
3 changes: 3 additions & 0 deletions build/azure-devops/variables/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variables:
- name: Agent.Scraper.Prometheus.ScrapeUri
value: metrics
4 changes: 3 additions & 1 deletion src/Promitor.Tests.Integration/Clients/AgentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Promitor.Tests.Integration.Clients
{
public class AgentClient
{
protected IConfiguration Configuration { get; }
protected HttpClient HttpClient { get; }
protected string AgentName{ get; }
protected ILogger Logger { get; }
Expand All @@ -23,11 +24,12 @@ public AgentClient(string agentName, string baseUrlConfigKey, IConfiguration con
var baseUrl = configuration[baseUrlConfigKey];
logger.LogInformation("Base URL for {AgentName} is '{Url}'", agentName, baseUrl);

AgentName = agentName;
HttpClient = new HttpClient
{
BaseAddress = new Uri(baseUrl)
};
Configuration = configuration;
AgentName = agentName;
Logger = logger;
}

Expand Down
22 changes: 20 additions & 2 deletions src/Promitor.Tests.Integration/Clients/ScraperClient.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
using Microsoft.Extensions.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Promitor.Tests.Integration.Clients
{
public class ScraperClient: AgentClient
public class ScraperClient : AgentClient
{
public ScraperClient(IConfiguration configuration, ILogger logger)
:base("Scraper", "Agents:Scraper:BaseUrl", configuration,logger)
{
}

public async Task<HttpResponseMessage> GetRuntimeConfigurationAsync()
{
return await GetAsync("/api/v1/configuration/runtime");
}

public async Task<HttpResponseMessage> GetMetricDeclarationAsync()
{
return await GetAsync("/api/v1/configuration/metric-declaration");
}

public async Task<HttpResponseMessage> ScrapeAsync()
{
var scrapeUri = Configuration["Agents:Scraper:Prometheus:ScrapeUri"];
return await GetAsync($"/{scrapeUri}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Linq;
using System.Net.Http.Headers;
using GuardNet;

namespace Promitor.Tests.Integration.Extensions
{
public static class HttpResponseHeadersExtensions
{
/// <summary>
/// Gets the first or default value for a given HTTP header
/// </summary>
public static string GetFirstOrDefaultHeaderValue(this HttpResponseHeaders headers, string headerName)
{
Guard.NotNull(headers, nameof(headers));
Guard.NotNullOrWhitespace(headerName, nameof(headerName));

return headers.GetValues(headerName)?.FirstOrDefault();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

<ItemGroup>
<ProjectReference Include="..\Promitor.Agents.ResourceDiscovery\Promitor.Agents.ResourceDiscovery.csproj" />
<ProjectReference Include="..\Promitor.Agents.Scraper\Promitor.Agents.Scraper.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Linq;
using System.Net;
using System.Net;
using System.Threading.Tasks;
using Promitor.Agents.Core;
using Promitor.Tests.Integration.Clients;
using Promitor.Tests.Integration.Extensions;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -27,7 +27,7 @@ public async Task Health_Get_ReturnsOk()
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetValues(HttpHeaders.AgentVersion).First());
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Promitor.Agents.Core;
using Promitor.Agents.ResourceDiscovery.Configuration;
using Promitor.Tests.Integration.Clients;
using Promitor.Tests.Integration.Extensions;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -48,7 +48,7 @@ public async Task ResourceDiscoveryGroup_SuccessfulCall_ReturnsVersionHeader()
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetValues(HttpHeaders.AgentVersion).First());
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Bogus;
using Newtonsoft.Json;
using Promitor.Agents.Core;
using Promitor.Agents.ResourceDiscovery.Graph.Model;
using Promitor.Tests.Integration.Clients;
using Promitor.Tests.Integration.Extensions;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -34,7 +34,7 @@ public async Task ResourceDiscovery_GetForUnexistingResourceDiscoveryGroup_Retur
// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetValues(HttpHeaders.AgentVersion).First());
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}

[Fact]
Expand All @@ -50,7 +50,7 @@ public async Task ResourceDiscovery_SuccessfulCall_ReturnsVersionHeader()
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetValues(HttpHeaders.AgentVersion).First());
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Linq;
using System.Net;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Promitor.Agents.Core;
using Promitor.Agents.Core.Contracts;
using Promitor.Tests.Integration.Clients;
using Promitor.Tests.Integration.Extensions;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -34,7 +34,7 @@ public async Task System_GetInfo_ReturnsOk()
Assert.NotNull(systemInfo);
Assert.Equal(ExpectedVersion, systemInfo.Version);
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetValues(HttpHeaders.AgentVersion).First());
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Promitor.Agents.Core;
using Promitor.Tests.Integration.Clients;
using Promitor.Tests.Integration.Extensions;
using Xunit;
using Xunit.Abstractions;
using Promitor.Agents.Scraper.Configuration;

namespace Promitor.Tests.Integration.Services.Scraper
{
public class ConfigurationTests : ScraperIntegrationTest
{
public ConfigurationTests(ITestOutputHelper testOutput)
: base(testOutput)
{
}

[Fact]
public async Task RuntimeConfiguration_Get_ReturnsOk()
{
// Arrange
var scraperClient = new ScraperClient(Configuration, Logger);

// Act
var response = await scraperClient.GetRuntimeConfigurationAsync();

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var rawPayload = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(rawPayload);
var metrics = JsonConvert.DeserializeObject<ScraperRuntimeConfiguration>(rawPayload);
Assert.NotNull(metrics);
}

[Fact]
public async Task RuntimeConfiguration_Get_ReturnsVersionHeader()
{
// Arrange
var scraperClient = new ScraperClient(Configuration, Logger);

// Act
var response = await scraperClient.GetRuntimeConfigurationAsync();

// Assert
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}

[Fact]
public async Task MetricDeclaration_Get_ReturnsOk()
{
// Arrange
var scraperClient = new ScraperClient(Configuration, Logger);

// Act
var response = await scraperClient.GetMetricDeclarationAsync();

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public async Task MetricDeclaration_Get_ReturnsVersionHeader()
{
// Arrange
var scraperClient = new ScraperClient(Configuration, Logger);

// Act
var response = await scraperClient.GetMetricDeclarationAsync();

// Assert
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}
}
}
10 changes: 5 additions & 5 deletions src/Promitor.Tests.Integration/Services/Scraper/HealthTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Linq;
using System.Net;
using System.Net;
using System.Threading.Tasks;
using Promitor.Agents.Core;
using Promitor.Tests.Integration.Clients;
using Promitor.Tests.Integration.Extensions;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -19,15 +19,15 @@ public HealthTests(ITestOutputHelper testOutput)
public async Task Health_Get_ReturnsOk()
{
// Arrange
var resourceDiscoveryClient = new ScraperClient(Configuration, Logger);
var scraperClient = new ScraperClient(Configuration, Logger);

// Act
var response = await resourceDiscoveryClient.GetHealthAsync();
var response = await scraperClient.GetHealthAsync();

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetValues(HttpHeaders.AgentVersion).First());
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Net;
using System.Threading.Tasks;
using Promitor.Agents.Core;
using Promitor.Tests.Integration.Clients;
using Promitor.Tests.Integration.Extensions;
using Xunit;
using Xunit.Abstractions;

namespace Promitor.Tests.Integration.Services.Scraper
{
public class MetricSinksTests : ScraperIntegrationTest
{
public MetricSinksTests(ITestOutputHelper testOutput)
: base(testOutput)
{
}

[Fact]
public async Task Prometheus_Scrape_ReturnsOk()
{
// Arrange
var scraperClient = new ScraperClient(Configuration, Logger);

// Act
var response = await scraperClient.ScrapeAsync();

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

[Fact]
public async Task Prometheus_Scrape_Get_ReturnsVersionHeader()
{
// Arrange
var scraperClient = new ScraperClient(Configuration, Logger);

// Act
var response = await scraperClient.ScrapeAsync();

// Assert
Assert.True(response.Headers.Contains(HttpHeaders.AgentVersion));
Assert.Equal(ExpectedVersion, response.Headers.GetFirstOrDefaultHeaderValue(HttpHeaders.AgentVersion));
}
}
}
Loading

0 comments on commit 640e185

Please sign in to comment.