11using System ;
2- using System . Linq ;
32using System . Net . Http ;
43using System . Threading . Tasks ;
5- using Docker . DotNet ;
6- using Docker . DotNet . Models ;
74using DotNet . Testcontainers . Builders ;
85using DotNet . Testcontainers . Containers ;
96using ManagedCode . Database . Core ;
107using ManagedCode . Database . Cosmos ;
118using ManagedCode . Database . Tests . Common ;
129using Microsoft . Azure . Cosmos ;
13- using Xunit ;
1410
1511namespace ManagedCode . Database . Tests . TestContainers ;
1612
17- [ CollectionDefinition ( nameof ( CosmosTestContainer ) ) ]
18- public class CosmosTestContainer : ITestContainer < string , TestCosmosItem > ,
19- ICollectionFixture < CosmosTestContainer > , IDisposable
13+ public class CosmosTestContainer : ITestContainer < string , TestCosmosItem >
2014{
21- private readonly TestcontainersContainer _cosmosTestContainer ;
2215 private CosmosDatabase _database ;
23- private DockerClient _dockerClient ;
24- private const string containerName = "cosmosContainer" ;
25- private const ushort privatePort = 8081 ;
26- private bool containerExsist = false ;
27- private string containerId ;
16+ private readonly TestcontainersContainer _cosmosContainer ;
2817
2918 public CosmosTestContainer ( )
3019 {
31- _cosmosTestContainer = new TestcontainersBuilder < TestcontainersContainer > ( )
20+ // Docker container for cosmos db is not working at all, to test database use local windows emulator
21+ _cosmosContainer = new TestcontainersBuilder < TestcontainersContainer > ( )
3222 . WithImage ( "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator" )
33- . WithName ( containerName )
34- . WithExposedPort ( 8081 )
35- . WithExposedPort ( 10251 )
36- . WithExposedPort ( 10252 )
37- . WithExposedPort ( 10253 )
38- . WithExposedPort ( 10254 )
39- . WithExposedPort ( 10255 )
40- . WithPortBinding ( 8081 , 8081 )
41- . WithPortBinding ( 10251 , 10251 )
42- . WithPortBinding ( 10252 , 10252 )
43- . WithPortBinding ( 10253 , 10253 )
44- . WithPortBinding ( 10254 , 10254 )
45- . WithPortBinding ( 8081 , 8081 )
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 )
4630 . WithEnvironment ( "AZURE_COSMOS_EMULATOR_PARTITION_COUNT" , "1" )
4731 . WithEnvironment ( "AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE" , "127.0.0.1" )
4832 . WithEnvironment ( "AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE" , "false" )
49- . WithCleanUp ( false )
33+ // .WithCleanUp(true )
5034 . WithWaitStrategy ( Wait . ForUnixContainer ( )
51- . UntilPortIsAvailable ( privatePort ) )
35+ . UntilPortIsAvailable ( 8081 ) )
5236 . Build ( ) ;
53-
54- _dockerClient = new DockerClientConfiguration ( ) . CreateClient ( ) ;
5537 }
5638
5739 public IDatabaseCollection < string , TestCosmosItem > Collection =>
@@ -64,79 +46,38 @@ public string GenerateId()
6446
6547 public async Task InitializeAsync ( )
6648 {
67- ushort publicPort = privatePort ;
68-
69- try
70- {
71- await _cosmosTestContainer . StartAsync ( ) ;
72-
73- containerExsist = false ;
74- }
75- catch ( Exception ex ) //TODO catch name already using exception
76- {
77- containerExsist = true ;
78- }
79-
80- if ( ! containerExsist )
81- {
82- publicPort = _cosmosTestContainer . GetMappedPublicPort ( privatePort ) ;
83- containerId = _cosmosTestContainer . Id ;
84- }
85- else
86- {
87- var listContainers = await _dockerClient . Containers . ListContainersAsync ( new ContainersListParameters ( ) ) ;
88-
89- ContainerListResponse containerListResponse = listContainers . FirstOrDefault ( container => container . Names . Contains ( $ "/{ containerName } ") ) ;
90-
91- if ( containerListResponse != null )
92- {
93- publicPort = containerListResponse . Ports . Single ( port => port . PrivatePort == privatePort ) . PublicPort ;
94-
95- containerId = containerListResponse . ID ;
96- }
97- }
98-
99- var httpMessageHandler = new HttpClientHandler ( )
100- {
101- ServerCertificateCustomValidationCallback = HttpClientHandler . DangerousAcceptAnyServerCertificateValidator
102- } ;
103-
104-
49+ await _cosmosContainer . StartAsync ( ) ;
50+ Console . WriteLine ( $ "Cosmos container State:{ _cosmosContainer . State } ") ;
10551 _database = new CosmosDatabase ( new CosmosOptions
10652 {
10753 ConnectionString =
108- $ "AccountEndpoint=https://localhost:{ publicPort } /;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
54+ $ "AccountEndpoint=https://localhost:{ _cosmosContainer . GetMappedPublicPort ( 8081 ) } /;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
10955 DatabaseName = "database" ,
110- CollectionName = $ "testContainer",
56+ CollectionName = "testContainer" ,
11157 AllowTableCreation = true ,
11258 CosmosClientOptions = new CosmosClientOptions ( )
11359 {
114- HttpClientFactory = ( ) => new HttpClient ( httpMessageHandler ) ,
115- ConnectionMode = ConnectionMode . Gateway ,
116- //RequestTimeout = TimeSpan.FromMinutes(3)
60+ HttpClientFactory = ( ) =>
61+ {
62+ HttpMessageHandler httpMessageHandler = new HttpClientHandler ( )
63+ {
64+ ServerCertificateCustomValidationCallback = ( _ , _ , _ , _ ) => true
65+ } ;
66+
67+ return new HttpClient ( httpMessageHandler ) ;
68+ } ,
69+ ConnectionMode = ConnectionMode . Gateway
11770 } ,
11871 } ) ;
11972
120-
12173 await _database . InitializeAsync ( ) ;
12274 }
12375
12476 public async Task DisposeAsync ( )
12577 {
126- // await _database.DeleteAsync();
12778 await _database . DisposeAsync ( ) ;
128-
129- /* _testOutputHelper.WriteLine($"Cosmos container State:{_cosmosContainer.State}");
130- _testOutputHelper.WriteLine("=STOP=");*/
131- }
132-
133- public async void Dispose ( )
134- {
135-
136- await _dockerClient . Containers . RemoveContainerAsync ( containerId ,
137- new ContainerRemoveParameters
138- {
139- Force = true
140- } ) ;
79+ await _cosmosContainer . StopAsync ( ) ;
80+ await _cosmosContainer . CleanUpAsync ( ) ;
81+ Console . WriteLine ( $ "Cosmos container State:{ _cosmosContainer . State } ") ;
14182 }
142- }
83+ }
0 commit comments