Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Process/DockerSandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@ final class DockerSandbox implements Sandbox
private string $portsNetwork;
private string $portsHost;

/**
* @var array<string, string>
*/
private array $vendorVolumes;

/**
* @param array<string, string> $vendorVolumes folder => volume, for runs in a folder directly inside it, so vendor
* stays on the docker host between runs, eg. ['/tmp/php-school/<student id>' => 'php-school-vendor-<student id>-<exercise>']
*/
public function __construct(
string $basePath,
string $projectName,
?ExecutableFinder $executableFinder = null,
string $portsNetwork = 'bridge',
string $portsHost = '127.0.0.1',
array $vendorVolumes = [],
) {
$this->executableFinder = $executableFinder ?? new ExecutableFinder();
$this->basePath = $basePath;
Expand All @@ -39,6 +49,7 @@ public function __construct(
$this->portsNetwork = $portsNetwork;
//the address ports are published on, the docker host's private address when it is remote
$this->portsHost = $portsHost;
$this->vendorVolumes = $vendorVolumes;
}

public function getImageName(): string
Expand Down Expand Up @@ -71,6 +82,13 @@ public function create(ProcessInput $processInput): DockerExecution

$args = $processInput->getArgs();
$mounts = [$processInput->getWorkingDirectory() . ':/solution'];

$vendorVolume = $this->vendorVolumes[dirname($processInput->getWorkingDirectory())] ?? null;
if ($vendorVolume !== null) {
//only composer writes to it
$mounts[] = $vendorVolume . ':/solution/vendor' . ($isComposer ? '' : ':ro');
}

$env = ['COMPOSER_HOME' => '/composer'];

if ($isComposer) {
Expand Down
30 changes: 30 additions & 0 deletions test/Process/DockerSandboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ public function testComposerGetsNetworkCacheAndNoPluginsOrScripts(): void
static::assertSame($expected, $process->getCommandLine());
}

public function testVendorVolumeIsWritableForComposerOnly(): void
{
$sandbox = new DockerSandbox('/docker-dir', 'php8appreciate', $this->anyFinder(), vendorVolumes: ['/tmp/student' => 'vendor-student']);

$composer = $sandbox->create(new ProcessInput('composer', ['update'], '/tmp/student/abc', [], []))->process;
static::assertStringContainsString("'-v' 'vendor-student:/solution/vendor' ", $composer->getCommandLine());

$php = $sandbox->create(new ProcessInput('php', ['solution.php'], '/tmp/student/def', [], []))->process;
static::assertStringContainsString("'-v' 'vendor-student:/solution/vendor:ro' ", $php->getCommandLine());
}

public function testFoldersOutsideTheVendorVolumesKeepTheirOwnVendor(): void
{
$sandbox = new DockerSandbox('/docker-dir', 'php8appreciate', $this->anyFinder(), vendorVolumes: ['/tmp/student' => 'vendor-student']);

//eg. the reference solution, a sibling folder whose name starts the same, or the listed folder itself
foreach (['/tmp/reference/abc', '/tmp/student-2/abc', '/tmp/student'] as $directory) {
$process = $sandbox->create(new ProcessInput('composer', ['install'], $directory, [], []))->process;
static::assertStringNotContainsString('vendor-student', $process->getCommandLine());
}
}

private function anyFinder(): ExecutableFinder
{
$finder = $this->createMock(ExecutableFinder::class);
$finder->method('find')->with('docker')->willReturn('/usr/local/bin/docker');

return $finder;
}

public function testExposedPortsArePublishedOnLoopbackOnly(): void
{
$process = $this->factory()->create(new ProcessInput('php', [], __DIR__, [], [8000]))->process;
Expand Down
Loading