2
2
3
3
using System ;
4
4
using System . Collections . Generic ;
5
+ using System . Runtime . InteropServices ;
5
6
using System . Threading . Tasks ;
6
7
using Docker . DotNet . Models ;
7
8
using DotNet . Testcontainers . Builders ;
@@ -17,15 +18,14 @@ namespace WireMock.Net.Testcontainers;
17
18
/// </summary>
18
19
public sealed class WireMockContainerBuilder : ContainerBuilder < WireMockContainerBuilder , WireMockContainer , WireMockConfiguration >
19
20
{
20
- private readonly Dictionary < bool , ContainerInfo > _info = new ( )
21
+ private const string DefaultLogger = "WireMockConsoleLogger" ;
22
+ private readonly Dictionary < OSPlatform , ContainerInfo > _info = new ( )
21
23
{
22
- { false , new ContainerInfo ( "sheyenrath/wiremock.net:latest " , "/app/__admin/mappings" ) } ,
23
- { true , new ContainerInfo ( "sheyenrath/wiremock.net-windows:latest " , @"c:\app\__admin\mappings" ) }
24
+ { OSPlatform . Linux , new ContainerInfo ( "sheyenrath/wiremock.net-alpine " , "/app/__admin/mappings" ) } ,
25
+ { OSPlatform . Windows , new ContainerInfo ( "sheyenrath/wiremock.net-windows" , @"c:\app\__admin\mappings" ) }
24
26
} ;
25
27
26
- private const string DefaultLogger = "WireMockConsoleLogger" ;
27
-
28
- private readonly Lazy < Task < bool > > _isWindowsAsLazy = new ( async ( ) =>
28
+ private readonly Lazy < Task < OSPlatform > > _getOSAsLazy = new ( async ( ) =>
29
29
{
30
30
if ( TestcontainersSettings . OS . DockerEndpointAuthConfig == null )
31
31
{
@@ -36,9 +36,12 @@ public sealed class WireMockContainerBuilder : ContainerBuilder<WireMockContaine
36
36
using var dockerClient = dockerClientConfig . CreateClient ( ) ;
37
37
38
38
var version = await dockerClient . System . GetVersionAsync ( ) ;
39
- return version . Os . IndexOf ( "Windows" , StringComparison . OrdinalIgnoreCase ) > - 1 ;
39
+ return version . Os . IndexOf ( "Windows" , StringComparison . OrdinalIgnoreCase ) >= 0 ? OSPlatform . Windows : OSPlatform . Linux ;
40
40
} ) ;
41
41
42
+ private OSPlatform ? _imageOS ;
43
+ private string ? _staticMappingsPath ;
44
+
42
45
/// <summary>
43
46
/// Initializes a new instance of the <see cref="ContainerBuilder" /> class.
44
47
/// </summary>
@@ -48,14 +51,36 @@ public WireMockContainerBuilder() : this(new WireMockConfiguration())
48
51
}
49
52
50
53
/// <summary>
51
- /// Automatically set the correct image (Linux or Windows) for WireMock which to create the container.
54
+ /// Automatically use the correct image for WireMock.
55
+ /// For Linux this is "sheyenrath/wiremock.net-alpine:latest"
56
+ /// For Windows this is "sheyenrath/wiremock.net-windows:latest"
52
57
/// </summary>
53
58
/// <returns>A configured instance of <see cref="WireMockContainerBuilder"/></returns>
54
59
[ PublicAPI ]
55
60
public WireMockContainerBuilder WithImage ( )
56
61
{
57
- var isWindows = _isWindowsAsLazy . Value . GetAwaiter ( ) . GetResult ( ) ;
58
- return WithImage ( _info [ isWindows ] . Image ) ;
62
+ _imageOS ??= _getOSAsLazy . Value . GetAwaiter ( ) . GetResult ( ) ;
63
+ return WithImage ( _imageOS . Value ) ;
64
+ }
65
+
66
+ /// <summary>
67
+ /// Automatically use a Linux image for WireMock. This is "sheyenrath/wiremock.net-alpine:latest"
68
+ /// </summary>
69
+ /// <returns>A configured instance of <see cref="WireMockContainerBuilder"/></returns>
70
+ [ PublicAPI ]
71
+ public WireMockContainerBuilder WithLinuxImage ( )
72
+ {
73
+ return WithImage ( OSPlatform . Linux ) ;
74
+ }
75
+
76
+ /// <summary>
77
+ /// Automatically use a Windows image for WireMock. This is "sheyenrath/wiremock.net-windows:latest"
78
+ /// </summary>
79
+ /// <returns>A configured instance of <see cref="WireMockContainerBuilder"/></returns>
80
+ [ PublicAPI ]
81
+ public WireMockContainerBuilder WithWindowsImage ( )
82
+ {
83
+ return WithImage ( OSPlatform . Windows ) ;
59
84
}
60
85
61
86
/// <summary>
@@ -118,13 +143,9 @@ public WireMockContainerBuilder WithWatchStaticMappings(bool includeSubDirectori
118
143
[ PublicAPI ]
119
144
public WireMockContainerBuilder WithMappings ( string path , bool includeSubDirectories = false )
120
145
{
121
- Guard . NotNullOrEmpty ( path ) ;
122
-
123
- var isWindows = _isWindowsAsLazy . Value . GetAwaiter ( ) . GetResult ( ) ;
146
+ _staticMappingsPath = Guard . NotNullOrEmpty ( path ) ;
124
147
125
- return WithReadStaticMappings ( )
126
- . WithCommand ( $ "--WatchStaticMappingsInSubdirectories { includeSubDirectories } ")
127
- . WithBindMount ( path , _info [ isWindows ] . MappingsPath ) ;
148
+ return WithReadStaticMappings ( ) . WithCommand ( $ "--WatchStaticMappingsInSubdirectories { includeSubDirectories } ") ;
128
149
}
129
150
130
151
private WireMockContainerBuilder ( WireMockConfiguration dockerResourceConfiguration ) : base ( dockerResourceConfiguration )
@@ -138,24 +159,41 @@ private WireMockContainerBuilder(WireMockConfiguration dockerResourceConfigurati
138
159
/// <inheritdoc />
139
160
public override WireMockContainer Build ( )
140
161
{
141
- Validate ( ) ;
162
+ var builder = this ;
142
163
143
- return new WireMockContainer ( DockerResourceConfiguration ) ;
164
+ // In case no image has been set, set the image using internal logic.
165
+ if ( DockerResourceConfiguration . Image == null )
166
+ {
167
+ builder = WithImage ( ) ;
168
+ }
169
+
170
+ // In case the _imageOS is not set, determine it from the Image FullName.
171
+ if ( _imageOS == null )
172
+ {
173
+ if ( builder . DockerResourceConfiguration . Image . FullName . IndexOf ( "wiremock.net" , StringComparison . OrdinalIgnoreCase ) < 0 )
174
+ {
175
+ throw new InvalidOperationException ( ) ;
176
+ }
177
+
178
+ _imageOS = builder . DockerResourceConfiguration . Image . FullName . IndexOf ( "windows" , StringComparison . OrdinalIgnoreCase ) >= 0 ? OSPlatform . Windows : OSPlatform . Linux ;
179
+ }
180
+
181
+ if ( ! string . IsNullOrEmpty ( _staticMappingsPath ) )
182
+ {
183
+ builder = builder . WithBindMount ( _staticMappingsPath , _info [ _imageOS . Value ] . MappingsPath ) ;
184
+ }
185
+
186
+ builder . Validate ( ) ;
187
+
188
+ return new WireMockContainer ( builder . DockerResourceConfiguration ) ;
144
189
}
145
190
146
191
/// <inheritdoc />
147
192
protected override WireMockContainerBuilder Init ( )
148
193
{
149
194
var builder = base . Init ( ) ;
150
195
151
- // In case no image has been set, set the image using internal logic.
152
- if ( builder . DockerResourceConfiguration . Image == null )
153
- {
154
- builder = builder . WithImage ( ) ;
155
- }
156
-
157
- var isWindows = _isWindowsAsLazy . Value . GetAwaiter ( ) . GetResult ( ) ;
158
- var waitForContainerOS = isWindows ? Wait . ForWindowsContainer ( ) : Wait . ForUnixContainer ( ) ;
196
+ var waitForContainerOS = _imageOS == OSPlatform . Windows ? Wait . ForWindowsContainer ( ) : Wait . ForUnixContainer ( ) ;
159
197
return builder
160
198
. WithPortBinding ( WireMockContainer . ContainerPort , true )
161
199
. WithCommand ( $ "--WireMockLogger { DefaultLogger } ")
@@ -179,4 +217,10 @@ protected override WireMockContainerBuilder Merge(WireMockConfiguration oldValue
179
217
{
180
218
return new WireMockContainerBuilder ( new WireMockConfiguration ( oldValue , newValue ) ) ;
181
219
}
220
+
221
+ private WireMockContainerBuilder WithImage ( OSPlatform os )
222
+ {
223
+ _imageOS = os ;
224
+ return WithImage ( _info [ os ] . Image ) ;
225
+ }
182
226
}
0 commit comments