Skip to content

Commit

Permalink
Merge pull request #2781 from nextcloud/fix/nextcloud_test/docker-con…
Browse files Browse the repository at this point in the history
…tainer-port-allocation
  • Loading branch information
provokateurin authored Jan 31, 2025
2 parents 43a5c59 + deec18b commit f2e352a
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ final class DockerContainerFactory extends TestTargetFactory<DockerContainerInst
}

late int port;
late String id;

// Retry a few times because the random port could already be allocated.
for (var i = 0; i < 3; i++) {
while (true) {
port = _randomPort();
result = await runExecutableArguments(
'docker',
Expand All @@ -56,17 +56,30 @@ final class DockerContainerFactory extends TestTargetFactory<DockerContainerInst
'0.0.0.0:$port',
],
);
// The command will not fail if the port is already allocated since we run in detached mode.
if (result.exitCode != 0) {
throw Exception('Failed to run docker container: ${result.stderr}');
}

id = result.stdout.toString().replaceAll('\n', '');

// Ensure container is still up and didn't crash because the port was already allocated.
result = await runExecutableArguments(
'docker',
[
'exec',
'-i',
id,
'true',
],
);
if (result.exitCode == 0) {
break;
}
}

if (result.exitCode != 0) {
throw Exception('Failed to run docker container: ${result.stderr}');
}

return DockerContainerInstance(
id: result.stdout.toString().replaceAll('\n', ''),
id: id,
port: port,
);
}
Expand Down

0 comments on commit f2e352a

Please sign in to comment.