diff --git a/app/config.php b/app/config.php index bc02fbcf..cb047456 100644 --- a/app/config.php +++ b/app/config.php @@ -138,7 +138,6 @@ $c->get(ResultAggregator::class), $c->get(EventDispatcher::class), $c->get(CheckRepository::class), - $c->get(Sandbox::class), ); }, ResultAggregator::class => create(ResultAggregator::class), diff --git a/composer.json b/composer.json index 46b37bf3..098600a2 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "nikic/php-parser": "^5.0", "guzzlehttp/guzzle": "^7.9 | ^8.0", "psr/log": "^1.1 | ^2.0 | ^3.0", - "ext-json": "*" + "ext-json": "*", + "symfony/lock": "^6.4 | ^7.0 | ^8.0" }, "require-dev": { "phpunit/phpunit": "^11.5", diff --git a/composer.lock b/composer.lock index 664f8eed..e028e70a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e56e46a1cfc2be6b0655246c48614d85", + "content-hash": "504c9387f09dcb02dcf5932b79f46deb", "packages": [ { "name": "beberlei/assert", @@ -1920,6 +1920,88 @@ ], "time": "2026-10-06T20:05:19+00:00" }, + { + "name": "symfony/lock", + "version": "v8.1.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/lock.git", + "reference": "86767bf6c112741d39118cc4c946f1a7ee98fa6d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/lock/zipball/86767bf6c112741d39118cc4c946f1a7ee98fa6d", + "reference": "86767bf6c112741d39118cc4c946f1a7ee98fa6d", + "shasum": "" + }, + "require": { + "php": ">=8.4.1", + "psr/log": "^1|^2|^3" + }, + "conflict": { + "doctrine/dbal": "<4.3" + }, + "require-dev": { + "doctrine/dbal": "^4.3", + "predis/predis": "^1.1|^2.0", + "symfony/serializer": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Lock\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jérémy Derussé", + "email": "jeremy@derusse.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Creates and manages locks, a mechanism to provide exclusive access to a shared resource", + "homepage": "https://symfony.com", + "keywords": [ + "cas", + "flock", + "locking", + "mutex", + "redlock", + "semaphore" + ], + "support": { + "source": "https://github.com/symfony/lock/tree/v8.1.10" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2026-10-25T14:40:25+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.37.0", diff --git a/src/Event/CliExecuteEvent.php b/src/Event/CliExecuteEvent.php index b9ea0277..2ce351c9 100644 --- a/src/Event/CliExecuteEvent.php +++ b/src/Event/CliExecuteEvent.php @@ -6,6 +6,7 @@ use PhpSchool\PhpWorkshop\Exercise\Scenario\CliScenario; use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext; +use PhpSchool\PhpWorkshop\Process\SandboxExecution; use PhpSchool\PhpWorkshop\Utils\Collection; /** @@ -56,6 +57,17 @@ public function appendArg(string $arg): void $this->args = $this->args->append($arg); } + /** + * The running program, eg. to connect to a port it listens on with getAddress(). Only on the executing events. + */ + public function getExecution(): SandboxExecution + { + /** @var SandboxExecution $execution */ + $execution = $this->getParameter('execution'); + + return $execution; + } + /** * Get the arguments to be passed to the program. * diff --git a/src/ExerciseDispatcher.php b/src/ExerciseDispatcher.php index 8570fc2d..7afa33d0 100644 --- a/src/ExerciseDispatcher.php +++ b/src/ExerciseDispatcher.php @@ -15,8 +15,6 @@ use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException; use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext; -use PhpSchool\PhpWorkshop\Process\HostSandbox; -use PhpSchool\PhpWorkshop\Process\Sandbox; use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager; use PhpSchool\PhpWorkshop\Input\Input; use PhpSchool\PhpWorkshop\Output\OutputInterface; @@ -45,14 +43,12 @@ class ExerciseDispatcher * @param ResultAggregator $results * @param EventDispatcher $eventDispatcher * @param CheckRepository $checkRepository - * @param Sandbox $sandbox Where the code runs, exercises get its host from the context. */ public function __construct( private RunnerManager $runnerManager, private ResultAggregator $results, private EventDispatcher $eventDispatcher, private CheckRepository $checkRepository, - private Sandbox $sandbox = new HostSandbox(), ) {} /** @@ -110,7 +106,7 @@ public function requireCheck(string $requiredCheck): void */ public function verify(ExerciseInterface $exercise, Input $input): ResultAggregator { - $context = ExecutionContext::fromInputAndExercise($input, $exercise, $this->sandbox->getHost()); + $context = ExecutionContext::fromInputAndExercise($input, $exercise); $runner = $this->runnerManager->getRunner($exercise); @@ -165,7 +161,7 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega */ public function run(ExerciseInterface $exercise, Input $input, OutputInterface $output): bool { - $context = ExecutionContext::fromInputAndExercise($input, $exercise, $this->sandbox->getHost()); + $context = ExecutionContext::fromInputAndExercise($input, $exercise); $runner = $this->runnerManager->getRunner($exercise); diff --git a/src/ExerciseRunner/CliRunner.php b/src/ExerciseRunner/CliRunner.php index 0dab8b1b..76a3a7b6 100644 --- a/src/ExerciseRunner/CliRunner.php +++ b/src/ExerciseRunner/CliRunner.php @@ -229,7 +229,7 @@ public function run(ExecutionContext $context, OutputInterface $output): bool $execution->start(); $this->eventDispatcher->dispatch( - new CliExecuteEvent('cli.run.student.executing', $context, $scenario, $args, ['output' => $output]), + new CliExecuteEvent('cli.run.student.executing', $context, $scenario, $args, ['output' => $output, 'execution' => $execution]), ); $execution->wait(function ($outputType, $outputBuffer) use ($output) { $output->write($outputBuffer); @@ -262,7 +262,7 @@ private function executePhpFile(ExecutionContext $context, CliScenario $scenario $execution->start(); $this->eventDispatcher->dispatch( - new CliExecuteEvent(sprintf('cli.verify.%s.executing', $type), $context, $scenario, $args), + new CliExecuteEvent(sprintf('cli.verify.%s.executing', $type), $context, $scenario, $args, ['execution' => $execution]), ); $execution->wait(); diff --git a/src/ExerciseRunner/Context/ExecutionContext.php b/src/ExerciseRunner/Context/ExecutionContext.php index c1852ccc..ba1c300c 100644 --- a/src/ExerciseRunner/Context/ExecutionContext.php +++ b/src/ExerciseRunner/Context/ExecutionContext.php @@ -4,7 +4,6 @@ use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; use PhpSchool\PhpWorkshop\Input\Input; -use PhpSchool\PhpWorkshop\Process\SandboxHost; use PhpSchool\PhpWorkshop\Utils\Path; use PhpSchool\PhpWorkshop\Utils\System; @@ -15,10 +14,9 @@ public function __construct( private string $referenceExecutionDirectory, private ExerciseInterface $exercise, private Input $input, - private SandboxHost $sandboxHost = new SandboxHost(), ) {} - public static function fromInputAndExercise(Input $input, ExerciseInterface $exercise, SandboxHost $sandboxHost): ExecutionContext + public static function fromInputAndExercise(Input $input, ExerciseInterface $exercise): ExecutionContext { $program = $input->hasArgument('program') ? dirname($input->getRequiredArgument('program')) : (string) getcwd(); @@ -27,7 +25,6 @@ public static function fromInputAndExercise(Input $input, ExerciseInterface $exe System::randomTempDir(), $exercise, $input, - $sandboxHost, ); } @@ -63,11 +60,6 @@ public function getStudentExecutionDirectory(): string return $this->studentExecutionDirectory; } - public function getSandboxHost(): SandboxHost - { - return $this->sandboxHost; - } - public function getReferenceExecutionDirectory(): string { return $this->referenceExecutionDirectory; diff --git a/src/Process/DockerExecution.php b/src/Process/DockerExecution.php index d7ea71a8..bee68a6c 100644 --- a/src/Process/DockerExecution.php +++ b/src/Process/DockerExecution.php @@ -15,11 +15,15 @@ final class DockerExecution implements SandboxExecution /** * @param list $before * @param list $after + * @param string $host the docker host's address that ports are published on, eg. 10.0.0.5 + * @param array $ports container port => published port on the host, eg. [54321 => 47213] */ public function __construct( public readonly Process $process, public readonly array $before = [], public readonly array $after = [], + private string $host = '127.0.0.1', + private array $ports = [], ) {} public function start(): void @@ -65,6 +69,15 @@ public function getErrorOutput(): string return $this->process->getErrorOutput(); } + public function getAddress(int $port): string + { + if (!isset($this->ports[$port])) { + throw new \RuntimeException(sprintf('Port %d is not published', $port)); + } + + return $this->host . ':' . $this->ports[$port]; + } + private function runAfter(): void { foreach ($this->after as $step) { diff --git a/src/Process/DockerSandbox.php b/src/Process/DockerSandbox.php index 8c0bff1d..82a09cc3 100644 --- a/src/Process/DockerSandbox.php +++ b/src/Process/DockerSandbox.php @@ -21,6 +21,7 @@ final class DockerSandbox implements Sandbox private string $projectName; private string $portsNetwork; private string $portsHost; + private PortPicker $portPicker; public function __construct( string $basePath, @@ -28,6 +29,7 @@ public function __construct( ?ExecutableFinder $executableFinder = null, string $portsNetwork = 'bridge', string $portsHost = '127.0.0.1', + ?PortPicker $portPicker = null, ) { $this->executableFinder = $executableFinder ?? new ExecutableFinder(); $this->basePath = $basePath; @@ -35,8 +37,10 @@ public function __construct( //for exercises that expose ports, eg a server the check connects to. Pass a network without internet access //to stop student code reaching out, the published ports still work $this->portsNetwork = $portsNetwork; - //the address ports are published on, the docker host's private address when it is remote + //the address ports are published on, the docker host's private address when it is remote. IPv6 in brackets, + //eg. [fdaa::2] $this->portsHost = $portsHost; + $this->portPicker = $portPicker ?? new FreePortPicker(); } public function getImageName(): string @@ -58,19 +62,17 @@ public function createBuildProcess(): Process ); } - public function getHost(): SandboxHost - { - return new SandboxHost($this->portsHost); - } - public function create(ProcessInput $processInput): DockerExecution { $network = ['--network', 'none']; + $ports = []; if (count($processInput->getExposedPorts()) > 0) { $network = ['--network', $this->portsNetwork]; foreach ($processInput->getExposedPorts() as $port) { + $hostPort = $this->portPicker->pick(); $network[] = '-p'; - $network[] = $this->portsHost . ':' . $port . ':' . $port; + $network[] = $this->portsHost . ':' . $hostPort . ':' . $port; + $ports[$port] = $hostPort; } } @@ -124,7 +126,7 @@ public function create(ProcessInput $processInput): DockerExecution null, $processInput->getInput(), self::TIMEOUT + 5, - )); + ), [], [], $this->portsHost, $ports); } private function findDocker(): string diff --git a/src/Process/FreePortPicker.php b/src/Process/FreePortPicker.php new file mode 100644 index 00000000..1ace14f4 --- /dev/null +++ b/src/Process/FreePortPicker.php @@ -0,0 +1,29 @@ +process->getErrorOutput(); } + + public function getAddress(int $port): string + { + return '127.0.0.1:' . $port; + } } diff --git a/src/Process/HostSandbox.php b/src/Process/HostSandbox.php index fce86247..8351e2fa 100644 --- a/src/Process/HostSandbox.php +++ b/src/Process/HostSandbox.php @@ -21,12 +21,6 @@ public function __construct(?ExecutableFinder $executableFinder = null) $this->executableFinder = $executableFinder ?? new ExecutableFinder(); } - - public function getHost(): SandboxHost - { - return new SandboxHost(); - } - public function create(ProcessInput $processInput): HostExecution { $executablePath = $this->executableFinder->find($processInput->getExecutable()); diff --git a/src/Process/LockPortPicker.php b/src/Process/LockPortPicker.php new file mode 100644 index 00000000..018a65db --- /dev/null +++ b/src/Process/LockPortPicker.php @@ -0,0 +1,34 @@ +locks->createLock('php-school-port-' . $port, self::LOCK_SECONDS, false)->acquire()) { + return $port; + } + } + + throw new RuntimeException('No free port to publish on'); + } +} diff --git a/src/Process/PortPicker.php b/src/Process/PortPicker.php new file mode 100644 index 00000000..97e3fb89 --- /dev/null +++ b/src/Process/PortPicker.php @@ -0,0 +1,13 @@ +address; - } - - /** - * A port that is free to publish. Asks this machine, so it needs another way when the host is remote. - */ - public function getFreePort(): int - { - $server = @stream_socket_server('tcp://127.0.0.1:0'); - - if ($server === false) { - throw new RuntimeException('Cannot create socket'); - } - - //eg. 127.0.0.1:54321 - $name = (string) stream_socket_get_name($server, false); - fclose($server); - - return (int) substr($name, (int) strrpos($name, ':') + 1); - } -} diff --git a/test/ExerciseRunner/CliRunnerTest.php b/test/ExerciseRunner/CliRunnerTest.php index 46ff49d5..f3418d81 100644 --- a/test/ExerciseRunner/CliRunnerTest.php +++ b/test/ExerciseRunner/CliRunnerTest.php @@ -10,7 +10,6 @@ use PhpSchool\PhpWorkshop\Listener\OutputRunInfoListener; use PhpSchool\PhpWorkshop\Process\HostSandbox; use PhpSchool\PhpWorkshop\Process\SandboxExecution; -use PhpSchool\PhpWorkshop\Process\SandboxHost; use PhpSchool\PhpWorkshop\Output\NullOutput; use PhpSchool\PhpWorkshop\Process\ProcessInput; use PhpSchool\PhpWorkshop\Process\Sandbox; @@ -87,6 +86,28 @@ public function testVerifyReturnsSuccessIfSolutionOutputMatchesUserOutput(): voi $this->assertTrue($result->isSuccessful()); } + public function testExecutingEventsHaveTheExecution(): void + { + $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution.php')); + $this->exercise->setSolution($solution); + $this->exercise->setScenario((new CliScenario())->withExecution([1, 2, 3])); + + $executions = []; + $this->eventDispatcher->listen( + ['cli.verify.reference.executing', 'cli.verify.student.executing', 'cli.run.student.executing'], + function (CliExecuteEvent $event) use (&$executions) { + $executions[] = $event->getExecution(); + }, + ); + + $context = TestContext::fromExerciseAndStudentSolution($this->exercise, __DIR__ . '/../res/cli/user.php'); + $this->runner->verify($context); + $this->runner->run($context, new NullOutput()); + + $this->assertCount(3, $executions); + $this->assertContainsOnlyInstancesOf(SandboxExecution::class, $executions); + } + public function testSuccessWithSingleSetOfArgsForBC(): void { $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/cli/solution.php')); @@ -184,11 +205,6 @@ public function create(ProcessInput $processInput): SandboxExecution return (new HostSandbox())->create($processInput); } - - public function getHost(): SandboxHost - { - return new SandboxHost(); - } }; $runner = new CliRunner($this->exercise, $this->eventDispatcher, $sandbox, new EnvironmentManager(new Filesystem(), $this->eventDispatcher)); diff --git a/test/ExerciseRunner/Context/ExecutionContextTest.php b/test/ExerciseRunner/Context/ExecutionContextTest.php index ad0ea33b..09a0e291 100644 --- a/test/ExerciseRunner/Context/ExecutionContextTest.php +++ b/test/ExerciseRunner/Context/ExecutionContextTest.php @@ -6,7 +6,6 @@ use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext; use PhpSchool\PhpWorkshop\ExerciseRunner\Context\NoEntryPoint; use PhpSchool\PhpWorkshop\Input\Input; -use PhpSchool\PhpWorkshop\Process\SandboxHost; use PhpSchool\PhpWorkshop\Utils\System; use PHPUnit\Framework\TestCase; @@ -91,7 +90,7 @@ public function testFactory(): void $input = new Input('test', ['program' => $temporaryDirectory . '/solution.php']); $exercise = new MockExercise(); - $context = ExecutionContext::fromInputAndExercise($input, $exercise, new SandboxHost('10.0.0.1')); + $context = ExecutionContext::fromInputAndExercise($input, $exercise); //check that student execution directory uses the parent directory of the program from the input static::assertSame($temporaryDirectory, $context->getStudentExecutionDirectory()); @@ -99,7 +98,5 @@ public function testFactory(): void //check that reference execution directory is a random temporary directory static::assertTrue(str_starts_with($context->getReferenceExecutionDirectory(), System::tempDir())); - - static::assertSame('10.0.0.1', $context->getSandboxHost()->getAddress()); } } diff --git a/test/Process/DockerSandboxTest.php b/test/Process/DockerSandboxTest.php index 3a523190..cd42f043 100644 --- a/test/Process/DockerSandboxTest.php +++ b/test/Process/DockerSandboxTest.php @@ -3,6 +3,7 @@ namespace PhpSchool\PhpWorkshopTest\Process; use PhpSchool\PhpWorkshop\Process\DockerSandbox; +use PhpSchool\PhpWorkshop\Process\PortPicker; use PhpSchool\PhpWorkshop\Process\ProcessInput; use PhpSchool\PhpWorkshop\Process\ProcessNotFoundException; use PHPUnit\Framework\TestCase; @@ -12,13 +13,20 @@ class DockerSandboxTest extends TestCase { private function factory(string $portsNetwork = 'bridge', string $portsHost = '127.0.0.1'): DockerSandbox { + $ports = new class implements PortPicker { + public function pick(): int + { + return 47213; + } + }; + $finder = $this->createMock(ExecutableFinder::class); $finder->expects($this->once()) ->method('find') ->with('docker') ->willReturn('/usr/local/bin/docker'); - return new DockerSandbox('/docker-dir', 'PHP8Appreciate', $finder, $portsNetwork, $portsHost); + return new DockerSandbox('/docker-dir', 'PHP8Appreciate', $finder, $portsNetwork, $portsHost, $ports); } /** @@ -77,7 +85,7 @@ public function testExposedPortsArePublishedOnLoopbackOnly(): void $process = $this->factory()->create(new ProcessInput('php', [], __DIR__, [], [8000]))->process; $expected = $this->expectedCommand( - ['--network', 'bridge', '-p', '127.0.0.1:8000:8000'], + ['--network', 'bridge', '-p', '127.0.0.1:47213:8000'], ['-v', __DIR__ . ':/solution'], ['php'], ); @@ -89,7 +97,7 @@ public function testExposedPortsUseTheGivenNetwork(): void $process = $this->factory('php-school-sandbox')->create(new ProcessInput('php', [], __DIR__, [], [8000]))->process; $expected = $this->expectedCommand( - ['--network', 'php-school-sandbox', '-p', '127.0.0.1:8000:8000'], + ['--network', 'php-school-sandbox', '-p', '127.0.0.1:47213:8000'], ['-v', __DIR__ . ':/solution'], ['php'], ); @@ -101,18 +109,21 @@ public function testExposedPortsArePublishedOnTheGivenHost(): void $process = $this->factory('bridge', '172.19.0.2')->create(new ProcessInput('php', [], __DIR__, [], [8000]))->process; $expected = $this->expectedCommand( - ['--network', 'bridge', '-p', '172.19.0.2:8000:8000'], + ['--network', 'bridge', '-p', '172.19.0.2:47213:8000'], ['-v', __DIR__ . ':/solution'], ['php'], ); static::assertSame($expected, $process->getCommandLine()); } - public function testHostIsWherePortsArePublished(): void + public function testAddressIsThePickedPortOnTheGivenHost(): void { - $sandbox = new DockerSandbox('/docker-dir', 'php8appreciate', null, 'bridge', '172.19.0.2'); + $execution = $this->factory('bridge', '172.19.0.2')->create(new ProcessInput('php', [], __DIR__, [], [8000])); + static::assertSame('172.19.0.2:47213', $execution->getAddress(8000)); - static::assertSame('172.19.0.2', $sandbox->getHost()->getAddress()); + $execution = $this->factory('bridge', '[fdaa::2]')->create(new ProcessInput('php', [], __DIR__, [], [8000])); + static::assertStringContainsString("'-p' '[fdaa::2]:47213:8000'", $execution->process->getCommandLine()); + static::assertSame('[fdaa::2]:47213', $execution->getAddress(8000)); } public function testCreateWithEnv(): void diff --git a/test/Process/FreePortPickerTest.php b/test/Process/FreePortPickerTest.php new file mode 100644 index 00000000..7ead740e --- /dev/null +++ b/test/Process/FreePortPickerTest.php @@ -0,0 +1,18 @@ +pick()); + static::assertNotFalse($server); + fclose($server); + } +} diff --git a/test/Process/HostSandboxTest.php b/test/Process/HostSandboxTest.php index 191ec899..bc0ecbc0 100644 --- a/test/Process/HostSandboxTest.php +++ b/test/Process/HostSandboxTest.php @@ -41,6 +41,13 @@ public function testCreate(): void static::assertSame("'/usr/local/bin/composer'", $process->getCommandLine()); } + public function testAddressIsThePortOnThisMachine(): void + { + $execution = (new HostSandbox())->create(new ProcessInput('php', [], __DIR__, [], [54321])); + + static::assertSame('127.0.0.1:54321', $execution->getAddress(54321)); + } + public function testCreateWithArgs(): void { $finder = $this->createMock(ExecutableFinder::class); diff --git a/test/Process/LockPortPickerTest.php b/test/Process/LockPortPickerTest.php new file mode 100644 index 00000000..3d05075e --- /dev/null +++ b/test/Process/LockPortPickerTest.php @@ -0,0 +1,25 @@ +pick(); + + static::assertGreaterThanOrEqual(20000, $port); + static::assertLessThanOrEqual(59999, $port); + //another run can not take it + static::assertFalse($locks->createLock('php-school-port-' . $port)->acquire()); + } +} diff --git a/test/Process/SandboxHostTest.php b/test/Process/SandboxHostTest.php deleted file mode 100644 index d9e72138..00000000 --- a/test/Process/SandboxHostTest.php +++ /dev/null @@ -1,26 +0,0 @@ -getAddress()); - static::assertSame('10.0.0.1', (new SandboxHost('10.0.0.1'))->getAddress()); - } - - public function testGetFreePortCanBeListenedOn(): void - { - $port = (new SandboxHost())->getFreePort(); - - $server = stream_socket_server('tcp://127.0.0.1:' . $port); - static::assertNotFalse($server); - fclose($server); - } -}