9
9
using ManagedCode . Database . MongoDB ;
10
10
using ManagedCode . Database . Tests . Common ;
11
11
using MongoDB . Bson ;
12
+ using Xunit ;
12
13
using Xunit . Abstractions ;
13
14
14
15
namespace ManagedCode . Database . Tests . TestContainers ;
15
16
16
- public class MongoDBTestContainer : ITestContainer < ObjectId , TestMongoDBItem >
17
+ [ CollectionDefinition ( nameof ( MongoDBTestContainer ) ) ]
18
+ public class MongoDBTestContainer : ITestContainer < ObjectId , TestMongoDBItem > , IDisposable
17
19
{
18
20
//private readonly ITestOutputHelper _testOutputHelper;
19
21
private readonly TestcontainersContainer _mongoDBTestContainer ;
20
22
private MongoDBDatabase _dbDatabase ;
21
23
private DockerClient _dockerClient ;
22
- private string containerName = $ "mongoContainer{ Guid . NewGuid ( ) . ToString ( "N" ) } ";
24
+ private const string containerName = "mongoContainer" ;
23
25
private const ushort privatePort = 27017 ;
24
26
private bool containerExsist = false ;
27
+ private string containerId ;
25
28
26
29
public MongoDBTestContainer ( )
27
30
{
@@ -36,7 +39,7 @@ public MongoDBTestContainer()
36
39
. UntilPortIsAvailable ( privatePort ) )
37
40
. Build ( ) ;
38
41
39
- // _dockerClient = new DockerClientConfiguration().CreateClient();
42
+ _dockerClient = new DockerClientConfiguration ( ) . CreateClient ( ) ;
40
43
}
41
44
42
45
public IDatabaseCollection < ObjectId , TestMongoDBItem > Collection =>
@@ -51,10 +54,9 @@ public async Task InitializeAsync()
51
54
{
52
55
ushort publicPort = 0 ;
53
56
54
- await _mongoDBTestContainer . StartAsync ( ) ;
55
- /*
56
57
try
57
58
{
59
+ await _mongoDBTestContainer . StartAsync ( ) ;
58
60
59
61
containerExsist = false ;
60
62
}
@@ -66,19 +68,22 @@ public async Task InitializeAsync()
66
68
if ( ! containerExsist )
67
69
{
68
70
publicPort = _mongoDBTestContainer . GetMappedPublicPort ( privatePort ) ;
71
+ containerId = _mongoDBTestContainer . Id ;
69
72
}
70
73
else
71
74
{
72
75
var listContainers = await _dockerClient . Containers . ListContainersAsync ( new ContainersListParameters ( ) ) ;
73
76
74
77
ContainerListResponse containerListResponse = listContainers . Single ( container => container . Names . Contains ( $ "/{ containerName } ") ) ;
75
78
79
+ containerId = containerListResponse . ID ;
80
+
76
81
publicPort = containerListResponse . Ports . Single ( port => port . PrivatePort == privatePort ) . PublicPort ;
77
- }*/
82
+ }
78
83
79
84
_dbDatabase = new MongoDBDatabase ( new MongoDBOptions ( )
80
85
{
81
- ConnectionString = $ "mongodb://localhost:{ _mongoDBTestContainer . GetMappedPublicPort ( privatePort ) } ",
86
+ ConnectionString = $ "mongodb://localhost:{ publicPort } ",
82
87
DataBaseName = $ "db{ Guid . NewGuid ( ) . ToString ( "N" ) } ",
83
88
} ) ;
84
89
@@ -91,10 +96,17 @@ public async Task InitializeAsync()
91
96
public async Task DisposeAsync ( )
92
97
{
93
98
await _dbDatabase . DisposeAsync ( ) ;
94
- await _mongoDBTestContainer . StopAsync ( ) ;
95
- await _mongoDBTestContainer . CleanUpAsync ( ) ;
99
+ //await _dockerClient.Containers.StopContainerAsync(_mongoDBTestContainer.Id, new ContainerStopParameters());
96
100
97
101
// _testOutputHelper.WriteLine($"Mongo container State:{_mongoDBContainer.State}");
98
102
//_testOutputHelper.WriteLine("=STOP=");
99
103
}
104
+
105
+ public async void Dispose ( )
106
+ {
107
+ await _dockerClient . Containers . StopContainerAsync ( containerId , new ContainerStopParameters ( ) ) ;
108
+
109
+ // await _dockerClient.Containers.RemoveContainerAsync(containerId, new ContainerRemoveParameters());
110
+
111
+ }
100
112
}
0 commit comments