@@ -12,46 +12,27 @@ namespace ManagedCode.Database.Tests.TestContainers;
12
12
13
13
public class CosmosTestContainer : ITestContainer < string , TestCosmosItem >
14
14
{
15
- private CosmosDatabase _database ;
15
+ private static int _port = 20000 ;
16
+
17
+ private readonly CosmosDatabase _database ;
16
18
private readonly TestcontainersContainer _cosmosContainer ;
17
19
18
20
public CosmosTestContainer ( )
19
21
{
20
- // Docker container for cosmos db is not working at all, to test database use local windows emulator
22
+ var port = ++ _port ;
23
+
21
24
_cosmosContainer = new TestcontainersBuilder < TestcontainersContainer > ( )
22
- . WithImage ( "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator" )
23
- . WithName ( $ "azure-cosmos-emulator{ Guid . NewGuid ( ) . ToString ( "N" ) } ")
24
- //.WithExposedPort(8081)
25
- . WithPortBinding ( 8081 , true )
26
- . WithPortBinding ( 10251 , true )
27
- . WithPortBinding ( 10252 , true )
28
- . WithPortBinding ( 10253 , true )
29
- . WithPortBinding ( 10254 , true )
30
- . WithEnvironment ( "AZURE_COSMOS_EMULATOR_PARTITION_COUNT" , "1" )
31
- . WithEnvironment ( "AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE" , "127.0.0.1" )
25
+ . WithImage ( "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest" )
26
+ . WithPortBinding ( port , 8081 )
27
+ . WithEnvironment ( "AZURE_COSMOS_EMULATOR_PARTITION_COUNT" , "30" )
32
28
. WithEnvironment ( "AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE" , "false" )
33
- //.WithCleanUp(true)
34
- . WithWaitStrategy ( Wait . ForUnixContainer ( )
35
- . UntilPortIsAvailable ( 8081 ) )
29
+ . WithWaitStrategy ( Wait . ForUnixContainer ( ) )
36
30
. Build ( ) ;
37
- }
38
31
39
- public IDatabaseCollection < string , TestCosmosItem > Collection =>
40
- _database . GetCollection < TestCosmosItem > ( ) ;
41
-
42
- public string GenerateId ( )
43
- {
44
- return $ "{ Guid . NewGuid ( ) : N} ";
45
- }
46
-
47
- public async Task InitializeAsync ( )
48
- {
49
- await _cosmosContainer . StartAsync ( ) ;
50
- Console . WriteLine ( $ "Cosmos container State:{ _cosmosContainer . State } ") ;
51
32
_database = new CosmosDatabase ( new CosmosOptions
52
33
{
53
34
ConnectionString =
54
- $ "AccountEndpoint=https://localhost:{ _cosmosContainer . GetMappedPublicPort ( 8081 ) } / ;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
35
+ $ "AccountEndpoint=https://localhost:{ port } ;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
55
36
DatabaseName = "database" ,
56
37
CollectionName = "testContainer" ,
57
38
AllowTableCreation = true ,
@@ -61,23 +42,34 @@ public async Task InitializeAsync()
61
42
{
62
43
HttpMessageHandler httpMessageHandler = new HttpClientHandler ( )
63
44
{
64
- ServerCertificateCustomValidationCallback = ( _ , _ , _ , _ ) => true
45
+ ServerCertificateCustomValidationCallback =
46
+ HttpClientHandler . DangerousAcceptAnyServerCertificateValidator
65
47
} ;
66
48
67
49
return new HttpClient ( httpMessageHandler ) ;
68
50
} ,
69
51
ConnectionMode = ConnectionMode . Gateway
70
52
} ,
71
53
} ) ;
54
+ }
72
55
56
+ public IDatabaseCollection < string , TestCosmosItem > Collection =>
57
+ _database . GetCollection < TestCosmosItem > ( ) ;
58
+
59
+ public string GenerateId ( )
60
+ {
61
+ return $ "{ Guid . NewGuid ( ) : N} ";
62
+ }
63
+
64
+ public async Task InitializeAsync ( )
65
+ {
66
+ await _cosmosContainer . StartAsync ( ) ;
73
67
await _database . InitializeAsync ( ) ;
74
68
}
75
69
76
70
public async Task DisposeAsync ( )
77
71
{
78
72
await _database . DisposeAsync ( ) ;
79
73
await _cosmosContainer . StopAsync ( ) ;
80
- await _cosmosContainer . CleanUpAsync ( ) ;
81
- Console . WriteLine ( $ "Cosmos container State:{ _cosmosContainer . State } ") ;
82
74
}
83
75
}
0 commit comments