|
| 1 | +using System.Text; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | + |
1 | 4 | namespace TestcontainersGCS; |
2 | 5 |
|
3 | 6 | /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> |
4 | 7 | [PublicAPI] |
5 | 8 | public sealed class GCSBuilder : ContainerBuilder<GCSBuilder, GCSContainer, GCSConfiguration> |
6 | 9 | { |
7 | | - public const string GCSImage = "fsouza/fake-gcs-server:1.47.5"; |
8 | | - public const ushort GCSPort = 30000; |
| 10 | + public const string FakeGCSServerImage = "fsouza/fake-gcs-server:1.47.5"; |
| 11 | + public const ushort FakeGCSServerPort = 4443; |
| 12 | + public const string StartupScriptFilePath = "/testcontainers.sh"; |
9 | 13 |
|
10 | 14 | /// <summary> |
11 | 15 | /// Initializes a new instance of the <see cref="GCSBuilder" /> class. |
@@ -40,14 +44,21 @@ public override GCSContainer Build() |
40 | 44 | protected override GCSBuilder Init() |
41 | 45 | { |
42 | 46 | return base.Init() |
43 | | - .WithImage(GCSImage) |
44 | | - .WithPortBinding(GCSPort, GCSPort) |
45 | | - .WithCommand("-scheme", "http") |
46 | | - .WithCommand("-backend", "memory") |
47 | | - .WithCommand("-external-url", $"http://localhost:{GCSPort}") |
48 | | - .WithCommand("-port", $"{GCSPort}") |
49 | | - .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => |
50 | | - request.ForPath("/").ForPort(GCSPort).ForStatusCode(HttpStatusCode.NotFound))); |
| 47 | + .WithImage(FakeGCSServerImage) |
| 48 | + .WithPortBinding(FakeGCSServerPort, true) |
| 49 | + .WithEntrypoint("/bin/sh", "-c") |
| 50 | + .WithCommand($"while [ ! -f {StartupScriptFilePath} ]; do sleep 0.1; done; sh {StartupScriptFilePath}") |
| 51 | + .WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged(new Regex("server started at.*", RegexOptions.IgnoreCase))) |
| 52 | + .WithStartupCallback((container, ct) => |
| 53 | + { |
| 54 | + const char lf = '\n'; |
| 55 | + var startupScript = new StringBuilder(); |
| 56 | + startupScript.Append("#!/bin/bash"); |
| 57 | + startupScript.Append(lf); |
| 58 | + startupScript.Append($"fake-gcs-server -backend memory -scheme http -port {FakeGCSServerPort} -external-url \"http://localhost:{container.GetMappedPublicPort(FakeGCSServerPort)}\""); |
| 59 | + startupScript.Append(lf); |
| 60 | + return container.CopyAsync(Encoding.Default.GetBytes(startupScript.ToString()), StartupScriptFilePath, Unix.FileMode755, ct); |
| 61 | + }); |
51 | 62 | } |
52 | 63 |
|
53 | 64 | /// <inheritdoc /> |
|
0 commit comments