Skip to content

Commit c13d61b

Browse files
committed
Test cleanup and parallelization
1 parent 24c035c commit c13d61b

File tree

4 files changed

+56
-68
lines changed

4 files changed

+56
-68
lines changed

API.IntegrationTests/API.IntegrationTests.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
<PackageReference Include="Testcontainers.Redis" />
1616
<PackageReference Include="TUnit" />
1717
</ItemGroup>
18-
<ItemGroup>
19-
<Folder Include="Seeders\" />
20-
</ItemGroup>
2118

2219
<!-- Git stuff -->
2320
<Target Name="SetHash" AfterTargets="InitializeSourceControlInformation">

API.IntegrationTests/Docker/InMemoryDatabase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Testcontainers.PostgreSql;
1+
using OpenShock.Common.Utils;
2+
using Testcontainers.PostgreSql;
23
using TUnit.Core.Interfaces;
34

45
namespace OpenShock.API.IntegrationTests.Docker;
@@ -17,10 +18,9 @@ public PostgreSqlContainer Container
1718
.WithNetwork(DockerNetwork.Instance)
1819
.WithName($"tunit-postgresql-{Guid.CreateVersion7()}")
1920
.WithImage("postgres:latest")
20-
.WithPortBinding(5432, 5432)
2121
.WithDatabase("openshock")
2222
.WithUsername("openshock")
23-
.WithPassword("superSecurePassword")
23+
.WithPassword(CryptoUtils.RandomAlphaNumericString(32))
2424
.Build();
2525

2626
return _container;

API.IntegrationTests/Docker/InMemoryRedis.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public RedisContainer Container
1717
.WithNetwork(DockerNetwork.Instance)
1818
.WithName($"tunit-redis-{Guid.CreateVersion7()}")
1919
.WithImage("redis/redis-stack-server:latest")
20-
.WithPortBinding(6379, 6379)
2120
.Build();
2221

2322
return _container;
Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
using System.Net;
22
using System.Net.Http.Json;
3+
using System.Net.Mime;
34
using Microsoft.AspNetCore.Hosting;
45
using Microsoft.EntityFrameworkCore;
56
using Microsoft.Extensions.DependencyInjection;
6-
using Newtonsoft.Json;
77
using OpenShock.API.Models.Response;
8-
using OpenShock.Common.Models;
98
using OpenShock.Common.OpenShockDb;
109
using OpenShock.Common.Redis;
1110
using OpenShock.Common.Utils;
1211
using Redis.OM.Contracts;
13-
using JsonSerializer = System.Text.Json.JsonSerializer;
1412

1513
namespace OpenShock.API.IntegrationTests.Tests;
1614

@@ -19,106 +17,100 @@ public sealed class LcgAssignmentTests
1917
[ClassDataSource<WebApplicationFactory>(Shared = SharedType.PerTestSession)]
2018
public required WebApplicationFactory WebApplicationFactory { get; init; }
2119

22-
private static readonly Guid UserId = Guid.Parse("11111111-1111-1111-1111-111111111111");
23-
private static readonly Guid HubId = Guid.Parse("11111111-1111-1111-1111-111111111111");
24-
private const string HubToken = "test";
20+
private Guid _userId;
21+
private Guid _hubId;
22+
private string _hubToken = string.Empty;
2523

2624
[Before(Test)]
2725
public async Task Setup()
2826
{
27+
// Dependency Resolution
2928
await using var context = WebApplicationFactory.Services.CreateAsyncScope();
3029
var db = context.ServiceProvider.GetRequiredService<OpenShockContext>();
30+
var redisConnectionProvider = context.ServiceProvider.GetRequiredService<IRedisConnectionProvider>();
31+
var webHostEnvironment = context.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
32+
var lcgNodesCollection = redisConnectionProvider.RedisCollection<LcgNode>(saveState: true);
3133

32-
var user = new User
33-
{
34-
Id = UserId,
35-
Name = "TestUser",
36-
Email = "test@test.org",
37-
PasswordHash = HashingUtils.HashPassword("password"),
38-
CreatedAt = DateTime.UtcNow,
39-
ActivatedAt = DateTime.UtcNow
40-
};
34+
// Set up variables
35+
_userId = Guid.CreateVersion7();
36+
_hubId = Guid.CreateVersion7();
37+
_hubToken = CryptoUtils.RandomAlphaNumericString(256);
4138

42-
db.Users.Add(user);
43-
44-
var hub = new Device
39+
// Create mock data
40+
db.Users.Add(new User
4541
{
46-
Id = HubId,
42+
Id = _userId,
43+
Name = _userId.ToString("N"),
44+
Email = $"{_userId}@test.org",
45+
PasswordHash = HashingUtils.HashPassword("password")
46+
});
47+
db.Devices.Add(new Device
48+
{
49+
Id = _hubId,
4750
Name = "TestHub",
48-
OwnerId = UserId,
49-
Token = HubToken,
51+
OwnerId = _userId,
52+
Token = _hubToken,
5053
CreatedAt = DateTime.UtcNow
51-
};
54+
});
55+
await db.SaveChangesAsync();
5256

53-
db.Devices.Add(hub);
57+
(string country, string fqdn)[] availableGateways = [
58+
("US","us1.example.com"),
59+
("DE", "de1.example.com"),
60+
("AS", "as1.example.com")
61+
];
5462

55-
await db.SaveChangesAsync();
63+
await lcgNodesCollection.InsertAsync(availableGateways.Select(x => new LcgNode
64+
{
65+
Country = x.country,
66+
Fqdn = x.fqdn,
67+
Load = 0,
68+
Environment = webHostEnvironment.EnvironmentName
69+
}));
5670
}
5771

5872
[After(Test)]
5973
public async Task Teardown()
6074
{
75+
// Dependency Resolution
6176
await using var context = WebApplicationFactory.Services.CreateAsyncScope();
6277
var db = context.ServiceProvider.GetRequiredService<OpenShockContext>();
63-
await db.Devices.Where(x => x.Id == HubId).ExecuteDeleteAsync();
64-
await db.Users.Where(x => x.Id == UserId).ExecuteDeleteAsync();
65-
6678
var redisConnectionProvider = context.ServiceProvider.GetRequiredService<IRedisConnectionProvider>();
67-
var webHostEnvironment = context.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
6879
var lcgNodesCollection = redisConnectionProvider.RedisCollection<LcgNode>(false);
69-
80+
81+
// Data cleanup
82+
await db.Devices.Where(x => x.Id == _hubId).ExecuteDeleteAsync();
83+
await db.Users.Where(x => x.Id == _userId).ExecuteDeleteAsync();
84+
7085
var allLcg = await lcgNodesCollection.ToArrayAsync();
7186
await lcgNodesCollection.DeleteAsync(allLcg);
7287
}
7388

7489
[Test]
75-
[NotInParallel]
76-
[Arguments("US", "us1.example.com", new[] { "US|us1.example.com", "DE|de1.example.com", "AS|as1.example.com" })]
77-
[Arguments("DE", "de1.example.com", new[] { "US|us1.example.com", "DE|de1.example.com", "AS|as1.example.com" })]
78-
[Arguments("CA", "us1.example.com", new[] { "US|us1.example.com", "DE|de1.example.com", "AS|as1.example.com" })]
79-
[Arguments("CA", "us1.example.com", new[] { "US|us1.example.com", "DE|de1.example.com", "AS|as1.example.com" })]
80-
[Arguments("AT", "de1.example.com", new[] { "US|us1.example.com", "DE|de1.example.com", "AS|as1.example.com" })]
81-
[Arguments("FR", "de1.example.com", new[] { "US|us1.example.com", "DE|de1.example.com", "AS|as1.example.com" })]
82-
public async Task GetLcgAssignment(string requesterCountry, string expectedHost, string[] availableGateways)
90+
[Arguments("US", "us1.example.com")]
91+
[Arguments("DE", "de1.example.com")]
92+
[Arguments("CA", "us1.example.com")]
93+
[Arguments("CA", "us1.example.com")]
94+
[Arguments("AT", "de1.example.com")]
95+
[Arguments("FR", "de1.example.com")]
96+
public async Task GetLcgAssignment(string requesterCountry, string expectedHost)
8397
{
8498
using var client = WebApplicationFactory.CreateClient();
8599

86100
await using var context = WebApplicationFactory.Services.CreateAsyncScope();
87-
var redisConnectionProvider = context.ServiceProvider.GetRequiredService<IRedisConnectionProvider>();
88-
var webHostEnvironment = context.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
89-
var lcgNodesCollection = redisConnectionProvider.RedisCollection<LcgNode>(false);
90-
91-
var testGateways = availableGateways.Select(x =>
92-
{
93-
var split = x.Split('|');
94-
if (split.Length != 2)
95-
throw new ArgumentException("Invalid gateway format");
96-
97-
return new LcgNode
98-
{
99-
Country = split[0],
100-
Fqdn = split[1],
101-
Load = 0,
102-
Environment = webHostEnvironment.EnvironmentName
103-
};
104-
});
105-
106-
await lcgNodesCollection.InsertAsync(testGateways);
107101

108102
var httpRequest = new HttpRequestMessage(HttpMethod.Get, "/2/device/assignLCG?version=1");
109-
httpRequest.Headers.Add("Device-Token", HubToken);
103+
httpRequest.Headers.Add("Device-Token", _hubToken);
110104
httpRequest.Headers.Add("CF-IPCountry", requesterCountry);
111105
var response = await client.SendAsync(httpRequest);
112106

113107
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
114108

115109
var mediaType = response.Content.Headers.ContentType?.MediaType;
116-
await Assert.That(mediaType).IsEqualTo("application/json");
110+
await Assert.That(mediaType).IsEqualTo(MediaTypeNames.Application.Json);
117111

118112
var data = await response.Content.ReadFromJsonAsync<LcgNodeResponseV2>();
119113
await Assert.That(data).IsNotNull();
120114
await Assert.That(data.Host).IsEqualTo(expectedHost);
121115
}
122-
123-
124116
}

0 commit comments

Comments
 (0)