@@ -2,57 +2,77 @@ namespace Testcontainers.Tests;
22
33public sealed class DependsOnTest : IAsyncLifetime
44{
5- private const string DependsOnKey = "org.testcontainers.depends-on" ;
5+ private readonly FilterByProperty _filters = new FilterByProperty ( ) ;
66
7- private const string DependsOnValue = "true" ;
7+ private readonly IList < IAsyncDisposable > _disposables = new List < IAsyncDisposable > ( ) ;
88
9- private readonly IContainer _container = new ContainerBuilder ( )
10- . DependsOn ( new ContainerBuilder ( )
9+ private readonly string _labelKey = Guid . NewGuid ( ) . ToString ( "D" ) ;
10+
11+ private readonly string _labelValue = Guid . NewGuid ( ) . ToString ( "D" ) ;
12+
13+ public DependsOnTest ( )
14+ {
15+ _filters . Add ( "label" , string . Join ( "=" , _labelKey , _labelValue ) ) ;
16+ }
17+
18+ public async Task InitializeAsync ( )
19+ {
20+ var childContainer1 = new ContainerBuilder ( )
1121 . WithImage ( CommonImages . Alpine )
12- . WithLabel ( DependsOnKey , DependsOnValue )
13- . Build ( ) )
14- . DependsOn ( new ContainerBuilder ( )
22+ . WithLabel ( _labelKey , _labelValue )
23+ . Build ( ) ;
24+
25+ var childContainer2 = new ContainerBuilder ( )
1526 . WithImage ( CommonImages . Alpine )
16- . WithLabel ( DependsOnKey , DependsOnValue )
17- . Build ( ) )
18- . DependsOn ( new NetworkBuilder ( )
19- . WithLabel ( DependsOnKey , DependsOnValue )
20- . Build ( ) )
21- . DependsOn ( new VolumeBuilder ( )
22- . WithLabel ( DependsOnKey , DependsOnValue )
23- . Build ( ) , "/workdir" )
24- . WithImage ( CommonImages . Alpine )
25- . WithLabel ( DependsOnKey , DependsOnValue )
26- . Build ( ) ;
27-
28- public Task InitializeAsync ( )
29- {
30- return _container . StartAsync ( ) ;
27+ . WithLabel ( _labelKey , _labelValue )
28+ . Build ( ) ;
29+
30+ var network = new NetworkBuilder ( )
31+ . WithLabel ( _labelKey , _labelValue )
32+ . Build ( ) ;
33+
34+ var volume = new VolumeBuilder ( )
35+ . WithLabel ( _labelKey , _labelValue )
36+ . Build ( ) ;
37+
38+ var parentContainer = new ContainerBuilder ( )
39+ . DependsOn ( childContainer1 )
40+ . DependsOn ( childContainer2 )
41+ . DependsOn ( network )
42+ . DependsOn ( volume , "/workdir" )
43+ . WithImage ( CommonImages . Alpine )
44+ . WithLabel ( _labelKey , _labelValue )
45+ . Build ( ) ;
46+
47+ await parentContainer . StartAsync ( )
48+ . ConfigureAwait ( false ) ;
49+
50+ _disposables . Add ( parentContainer ) ;
51+ _disposables . Add ( childContainer1 ) ;
52+ _disposables . Add ( childContainer2 ) ;
53+ _disposables . Add ( network ) ;
54+ _disposables . Add ( volume ) ;
3155 }
3256
3357 public Task DisposeAsync ( )
3458 {
35- return _container . DisposeAsync ( ) . AsTask ( ) ;
59+ return Task . WhenAll ( _disposables . Select ( disposable => disposable . DisposeAsync ( ) . AsTask ( ) ) ) ;
3660 }
3761
3862 [ Fact ]
3963 [ Trait ( nameof ( DockerCli . DockerPlatform ) , nameof ( DockerCli . DockerPlatform . Linux ) ) ]
4064 public async Task DependsOnCreatesDependentResources ( )
4165 {
4266 // Given
43- using var clientConfiguration = TestcontainersSettings . OS . DockerEndpointAuthConfig . GetDockerClientConfiguration ( ResourceReaper . DefaultSessionId ) ;
67+ using var clientConfiguration = TestcontainersSettings . OS . DockerEndpointAuthConfig . GetDockerClientConfiguration ( Guid . NewGuid ( ) ) ;
4468
4569 using var client = clientConfiguration . CreateClient ( ) ;
4670
47- var labelFilter = new Dictionary < string , bool > { { string . Join ( "=" , DependsOnKey , DependsOnValue ) , true } } ;
48-
49- var filters = new Dictionary < string , IDictionary < string , bool > > { { "label" , labelFilter } } ;
50-
51- var containersListParameters = new ContainersListParameters { All = true , Filters = filters } ;
71+ var containersListParameters = new ContainersListParameters { All = true , Filters = _filters } ;
5272
53- var networksListParameters = new NetworksListParameters { Filters = filters } ;
73+ var networksListParameters = new NetworksListParameters { Filters = _filters } ;
5474
55- var volumesListParameters = new VolumesListParameters { Filters = filters } ;
75+ var volumesListParameters = new VolumesListParameters { Filters = _filters } ;
5676
5777 // When
5878 var containers = await client . Containers . ListContainersAsync ( containersListParameters )
@@ -61,12 +81,12 @@ public async Task DependsOnCreatesDependentResources()
6181 var networks = await client . Networks . ListNetworksAsync ( networksListParameters )
6282 . ConfigureAwait ( true ) ;
6383
64- var volumesListResponse = await client . Volumes . ListAsync ( volumesListParameters )
84+ var response = await client . Volumes . ListAsync ( volumesListParameters )
6585 . ConfigureAwait ( true ) ;
6686
6787 // Then
6888 Assert . Equal ( 3 , containers . Count ) ;
6989 Assert . Single ( networks ) ;
70- Assert . Single ( volumesListResponse . Volumes ) ;
90+ Assert . Single ( response . Volumes ) ;
7191 }
7292}
0 commit comments