Skip to content

Commit e7f84ba

Browse files
committed
Use new TestContainers library directly
There was a weird issue while trying to start the database container. To solve this, TestContainers is now being used directly inside menu-api driver, instead of using DataBaseController from EvoMaster which was based on Dotnet.TestContainers which is deprecated.
1 parent 51dcdfa commit e7f84ba

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

dotnet_3/em/embedded/rest/MenuAPIDriver/EmbeddedEvoMasterController.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.Common;
34
using System.Threading.Tasks;
45
using EvoMaster.Controller;
56
using EvoMaster.Controller.Api;
67
using EvoMaster.Controller.Problem;
78
using Npgsql;
89
using EvoMaster.Controller.Controllers.db;
9-
using EvoMaster.DatabaseController;
10-
using EvoMaster.DatabaseController.Abstractions;
10+
using Testcontainers.PostgreSql;
1111

1212
namespace Menu {
1313
public class EmbeddedEvoMasterController : EmbeddedSutController {
1414
private bool _isSutRunning;
1515
private static int _sutPort;
16-
private NpgsqlConnection _connection;
17-
private IDatabaseController _databaseController;
16+
private DbConnection _connection;
17+
private readonly PostgreSqlContainer _postgreSqlContainer = new PostgreSqlBuilder().Build();
1818

1919
private static void Main(string[] args) {
2020
var embeddedEvoMasterController = new EmbeddedEvoMasterController();
@@ -57,34 +57,35 @@ public override IProblemInfo GetProblemInfo() =>
5757
public override bool IsSutRunning() => _isSutRunning;
5858

5959
public override void ResetStateOfSut() {
60-
DbCleaner.ClearDatabase_Postgres(_connection);
60+
DbCleaner.ClearDatabase(_connection, null, DatabaseType.POSTGRES);
6161
}
6262

6363
public override string StartSut() {
6464

6565
Task.Run(async () => {
6666
// TODO why is this not taken from Docker???
6767
var dbPort = GetEphemeralTcpPort();
68+
69+
await _postgreSqlContainer.StartAsync();
6870

69-
_databaseController = new PostgresDatabaseController("restaurant_menu_database", dbPort, "password123");
71+
await using (DbConnection connection = new NpgsqlConnection(_postgreSqlContainer.GetConnectionString()))
72+
{
73+
_connection = connection;
74+
API.Program.Main(new[] {$"{_sutPort}", connection.ConnectionString});
7075

71-
var (connectionString, dbConnection) = await _databaseController.StartAsync();
72-
73-
_connection = dbConnection as NpgsqlConnection;
74-
75-
API.Program.Main(new[] {$"{_sutPort}", connectionString});
76+
}
7677
});
7778

78-
WaitUntilSutIsRunning(_sutPort);
79+
WaitUntilSutIsRunning(_sutPort, 45);
7980

8081
_isSutRunning = true;
8182

8283
return $"http://localhost:{_sutPort}";
8384
}
8485

85-
public override void StopSut() {
86+
public override async void StopSut() {
8687
API.Program.Shutdown();
87-
_databaseController.Stop();
88+
await _postgreSqlContainer.StopAsync();
8889
_isSutRunning = false;
8990
}
9091

dotnet_3/em/embedded/rest/MenuAPIDriver/MenuAPIDriver.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
</PropertyGroup>
1010

1111
<Import Project="../../../common.props" />
12-
13-
<ItemGroup>
14-
<PackageReference Include="DotNet.Testcontainers" Version="1.4.0" />
15-
</ItemGroup>
1612

1713
<ItemGroup>
1814
<ProjectReference Include="../../../../cs/rest/Menu.API/Menu.API/Menu.API.csproj" />
@@ -37,6 +33,10 @@
3733
<TempDirectory Include="$(ProjectDir)bin-temp" />
3834
<InstrumentationRuntimeConfig Include="$(OutputPath)EvoMaster.Instrumentation.runtimeconfig.json" />
3935
</ItemGroup>
36+
37+
<ItemGroup>
38+
<PackageReference Include="TestContainers.postgresql" Version="3.1.0" />
39+
</ItemGroup>
4040

4141
<Target Name="Instrument" AfterTargets="Build">
4242

@@ -48,7 +48,8 @@
4848
<Copy SourceFiles="@(CurrentRuntimeConfig)" DestinationFiles="@(InstrumentationRuntimeConfig)" />
4949

5050
<!-- Run the instrumentation and specify bin-temp as output directory -->
51-
<Exec Command="cd $(OutputPath)&#xD;&#xA; dotnet EvoMaster.Instrumentation.dll @(Sut) @(TempDirectory)" />
51+
<Exec Command="cd $(OutputPath)
52+
dotnet EvoMaster.Instrumentation.dll @(Sut) @(TempDirectory)" />
5253

5354
</Target>
5455

0 commit comments

Comments
 (0)