Skip to content

Commit 8017fb3

Browse files
committed
fix port contention by letting daemon assign one
1 parent 183bd01 commit 8017fb3

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/ContainerCli.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public static RunExeCommand InspectCommand(ITestOutputHelper log, params string[
3434
public static RunExeCommand LoadCommand(ITestOutputHelper log, params string[] args)
3535
=> CreateCommand(log, "load", args);
3636

37-
private static RunExeCommand CreateCommand(ITestOutputHelper log, string command, string[] args)
37+
public static RunExeCommand PortCommand(ITestOutputHelper log, string containerName, int port) =>
38+
CreateCommand(log, "port", containerName, port.ToString());
39+
40+
private static RunExeCommand CreateCommand(ITestOutputHelper log, string command, params string[] args)
3841
{
3942
string commandPath = IsPodman ? "podman" : "docker";
4043

src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void Dispose()
3737
{
3838
_loggerFactory.Dispose();
3939
}
40-
40+
4141
[DockerAvailableFact]
4242
public async Task ApiEndToEndWithRegistryPushAndPull()
4343
{
@@ -310,14 +310,12 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage
310310
"--rm",
311311
"--name",
312312
containerName,
313-
"--publish",
314-
"5017:8080",
313+
"-P",
315314
"--detach",
316315
$"{DockerRegistryManager.LocalRegistry}/{imageName}:{imageTag}")
317316
.Execute();
318317
processResult.Should().Pass();
319318
Assert.NotNull(processResult.StdOut);
320-
321319
string appContainerId = processResult.StdOut.Trim();
322320

323321
bool everSucceeded = false;
@@ -327,12 +325,17 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage
327325

328326
if (projectType == "webapi")
329327
{
328+
var portCommand =
329+
ContainerCli.PortCommand(_testOutput, containerName, 8080)
330+
.Execute();
331+
portCommand.Should().Pass();
332+
var appUri = new Uri("http://{portCommand.StdOut.Trim()}", UriKind.Absolute).Port;
330333
// Give the server a moment to catch up, but no more than necessary.
331334
for (int retry = 0; retry < 10; retry++)
332335
{
333336
try
334337
{
335-
var response = await client.GetAsync("http://localhost:5017/weatherforecast").ConfigureAwait(false);
338+
var response = await client.GetAsync($"{appUri}/weatherforecast").ConfigureAwait(false);
336339

337340
if (response.IsSuccessStatusCode)
338341
{
@@ -347,15 +350,15 @@ public async Task EndToEnd_NoAPI_ProjectType(string projectType, bool addPackage
347350
ContainerCli.LogsCommand(_testOutput, appContainerId)
348351
.Execute()
349352
.Should().Pass();
350-
Assert.True(everSucceeded, "http://localhost:5017/weatherforecast never responded.");
353+
Assert.True(everSucceeded, $"{appUri}/weatherforecast never responded.");
351354
}
352355
else
353356
{
354357
var containerLogs =
355358
ContainerCli.LogsCommand(_testOutput, appContainerId)
356-
.Execute()
357-
.Should().Pass()
358-
.And.HaveStdOutContaining("Worker running at");
359+
.Execute()
360+
.Should().Pass()
361+
.And.HaveStdOutContaining("Worker running at");
359362
}
360363

361364
ContainerCli.StopCommand(_testOutput, appContainerId)

0 commit comments

Comments
 (0)