@@ -12,46 +12,27 @@ namespace ManagedCode.Database.Tests.TestContainers;
1212
1313public class CosmosTestContainer : ITestContainer < string , TestCosmosItem >
1414{
15- private CosmosDatabase _database ;
15+ private static int _port = 20000 ;
16+
17+ private readonly CosmosDatabase _database ;
1618 private readonly TestcontainersContainer _cosmosContainer ;
1719
1820 public CosmosTestContainer ( )
1921 {
20- // Docker container for cosmos db is not working at all, to test database use local windows emulator
22+ var port = ++ _port ;
23+
2124 _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" )
3228 . WithEnvironment ( "AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE" , "false" )
33- //.WithCleanUp(true)
34- . WithWaitStrategy ( Wait . ForUnixContainer ( )
35- . UntilPortIsAvailable ( 8081 ) )
29+ . WithWaitStrategy ( Wait . ForUnixContainer ( ) )
3630 . Build ( ) ;
37- }
3831
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 } ") ;
5132 _database = new CosmosDatabase ( new CosmosOptions
5233 {
5334 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==",
5536 DatabaseName = "database" ,
5637 CollectionName = "testContainer" ,
5738 AllowTableCreation = true ,
@@ -61,23 +42,34 @@ public async Task InitializeAsync()
6142 {
6243 HttpMessageHandler httpMessageHandler = new HttpClientHandler ( )
6344 {
64- ServerCertificateCustomValidationCallback = ( _ , _ , _ , _ ) => true
45+ ServerCertificateCustomValidationCallback =
46+ HttpClientHandler . DangerousAcceptAnyServerCertificateValidator
6547 } ;
6648
6749 return new HttpClient ( httpMessageHandler ) ;
6850 } ,
6951 ConnectionMode = ConnectionMode . Gateway
7052 } ,
7153 } ) ;
54+ }
7255
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 ( ) ;
7367 await _database . InitializeAsync ( ) ;
7468 }
7569
7670 public async Task DisposeAsync ( )
7771 {
7872 await _database . DisposeAsync ( ) ;
7973 await _cosmosContainer . StopAsync ( ) ;
80- await _cosmosContainer . CleanUpAsync ( ) ;
81- Console . WriteLine ( $ "Cosmos container State:{ _cosmosContainer . State } ") ;
8274 }
8375}
0 commit comments