Skip to content

Commit 42097f7

Browse files
committed
Add external LocalStack detection to prevent container conflicts in CI
1 parent 181ec3c commit 42097f7

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎tests/SourceFlow.Cloud.AWS.Tests/TestHelpers/AwsTestConfiguration.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ public async Task<bool> IsLocalStackAvailableAsync(TimeSpan timeout)
254254
var config = new AmazonSQSConfig
255255
{
256256
ServiceURL = LocalStackEndpoint,
257-
RegionEndpoint = Region
257+
RegionEndpoint = Region,
258+
Timeout = timeout,
259+
MaxErrorRetry = 0 // Don't retry, fail fast
258260
};
259261

260262
var credentials = new BasicAWSCredentials("test", "test");

‎tests/SourceFlow.Cloud.AWS.Tests/TestHelpers/LocalStackManager.cs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public async Task StartAsync(LocalStackConfiguration config)
4949
}
5050

5151
_configuration = config ?? throw new ArgumentNullException(nameof(config));
52+
53+
// Check if LocalStack is already running externally (e.g., in GitHub Actions)
54+
if (await IsExternalLocalStackAvailableAsync(config.Endpoint))
55+
{
56+
_logger.LogInformation("Detected existing LocalStack instance at {Endpoint}, using it instead of starting new container", config.Endpoint);
57+
// Don't start a new container, just use the existing one
58+
return;
59+
}
60+
5261
_logger.LogInformation("Starting LocalStack container with services: {Services}", string.Join(", ", config.EnabledServices));
5362

5463
// Ensure port is available before starting
@@ -358,6 +367,29 @@ public async Task<string> GetLogsAsync(int tail = 100)
358367
}
359368
}
360369

370+
/// <summary>
371+
/// Check if an external LocalStack instance is already available
372+
/// </summary>
373+
/// <param name="endpoint">LocalStack endpoint to check</param>
374+
/// <returns>True if external LocalStack is available</returns>
375+
private async Task<bool> IsExternalLocalStackAvailableAsync(string endpoint)
376+
{
377+
try
378+
{
379+
using var httpClient = new HttpClient();
380+
httpClient.Timeout = TimeSpan.FromSeconds(3);
381+
382+
var healthUrl = $"{endpoint}/_localstack/health";
383+
var response = await httpClient.GetAsync(healthUrl);
384+
385+
return response.IsSuccessStatusCode;
386+
}
387+
catch
388+
{
389+
return false;
390+
}
391+
}
392+
361393
/// <summary>
362394
/// Find an available port starting from the specified port
363395
/// </summary>

‎tests/SourceFlow.Cloud.AWS.Tests/TestHelpers/LocalStackTestFixture.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ public async Task InitializeAsync()
5858
}
5959

6060
// Check if LocalStack is already running (e.g., in GitHub Actions)
61-
bool isAlreadyRunning = await _configuration.IsLocalStackAvailableAsync(TimeSpan.FromSeconds(2));
61+
// Use a short timeout to avoid hanging
62+
bool isAlreadyRunning = false;
63+
try
64+
{
65+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
66+
isAlreadyRunning = await _configuration.IsLocalStackAvailableAsync(TimeSpan.FromSeconds(3));
67+
}
68+
catch
69+
{
70+
// If check fails, assume not running
71+
isAlreadyRunning = false;
72+
}
6273

6374
if (!isAlreadyRunning)
6475
{

0 commit comments

Comments
 (0)