test: cover the deployment zip built by the Packager end to endUnverified
Packages a fixture application whose dependencies come from relative path repositories only, so the tests run Composer without network access.
cf7d882 parent: ec8387c added
tests/Fixtures/PackagedApp/app/.env +1 -0 | new file mode 100644 | ||
| @@ -0,0 +1 @@ | ||
| 1 | +APP_ENV=dev | |
| new file mode 100644 | |||
| @@ -0,0 +1 @@ | |||
| 1 | +APP_ENV=dev | ||
added
tests/Fixtures/PackagedApp/app/bin/console +14 -0 | new file mode 100755 | ||
| @@ -0,0 +1,14 @@ | ||
| 1 | +#!/usr/bin/env php | |
| 2 | +<?php | |
| 3 | + | |
| 4 | +// Stands in for "bin/console cache:warmup": records what the build dir looks like while the Packager runs it. | |
| 5 | +$buildDir = dirname(__DIR__); | |
| 6 | +file_put_contents((string) getenv('PACKAGER_TEST_RECORD'), json_encode([ | |
| 7 | + 'buildDir' => $buildDir, | |
| 8 | + 'perms' => fileperms($buildDir) & 0o777, | |
| 9 | + 'argv' => array_slice($argv, 1), | |
| 10 | + 'appEnv' => getenv('APP_ENV'), | |
| 11 | + 'lock' => file_get_contents($buildDir.'/composer.lock'), | |
| 12 | +])); | |
| 13 | + | |
| 14 | +exit(getenv('PACKAGER_TEST_FAIL') ? 1 : 0); | |
| new file mode 100755 | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | +#!/usr/bin/env php | ||
| 2 | +<?php | ||
| 3 | + | ||
| 4 | +// Stands in for "bin/console cache:warmup": records what the build dir looks like while the Packager runs it. | ||
| 5 | +$buildDir = dirname(__DIR__); | ||
| 6 | +file_put_contents((string) getenv('PACKAGER_TEST_RECORD'), json_encode([ | ||
| 7 | + 'buildDir' => $buildDir, | ||
| 8 | + 'perms' => fileperms($buildDir) & 0o777, | ||
| 9 | + 'argv' => array_slice($argv, 1), | ||
| 10 | + 'appEnv' => getenv('APP_ENV'), | ||
| 11 | + 'lock' => file_get_contents($buildDir.'/composer.lock'), | ||
| 12 | +])); | ||
| 13 | + | ||
| 14 | +exit(getenv('PACKAGER_TEST_FAIL') ? 1 : 0); | ||
added
tests/Fixtures/PackagedApp/app/composer.json +16 -0 | new file mode 100644 | ||
| @@ -0,0 +1,16 @@ | ||
| 1 | +{ | |
| 2 | + "name": "acme/packaged-app", | |
| 3 | + "description": "An application packaged without any network access", | |
| 4 | + "type": "project", | |
| 5 | + "require": { | |
| 6 | + "acme/lib": "*@dev" | |
| 7 | + }, | |
| 8 | + "require-dev": { | |
| 9 | + "acme/dev-tool": "*@dev" | |
| 10 | + }, | |
| 11 | + "repositories": [ | |
| 12 | + {"type": "path", "url": "../lib"}, | |
| 13 | + {"type": "path", "url": "../dev-tool"}, | |
| 14 | + {"packagist.org": false} | |
| 15 | + ] | |
| 16 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | +{ | ||
| 2 | + "name": "acme/packaged-app", | ||
| 3 | + "description": "An application packaged without any network access", | ||
| 4 | + "type": "project", | ||
| 5 | + "require": { | ||
| 6 | + "acme/lib": "*@dev" | ||
| 7 | + }, | ||
| 8 | + "require-dev": { | ||
| 9 | + "acme/dev-tool": "*@dev" | ||
| 10 | + }, | ||
| 11 | + "repositories": [ | ||
| 12 | + {"type": "path", "url": "../lib"}, | ||
| 13 | + {"type": "path", "url": "../dev-tool"}, | ||
| 14 | + {"packagist.org": false} | ||
| 15 | + ] | ||
| 16 | +} | ||
added
tests/Fixtures/PackagedApp/app/composer.lock +51 -0 | new file mode 100644 | ||
| @@ -0,0 +1,51 @@ | ||
| 1 | +{ | |
| 2 | + "_readme": [ | |
| 3 | + "This file locks the dependencies of your project to a known state", | |
| 4 | + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | |
| 5 | + "This file is @generated automatically" | |
| 6 | + ], | |
| 7 | + "content-hash": "ef10a352ccfe70682015a26ef11e5ddd", | |
| 8 | + "packages": [ | |
| 9 | + { | |
| 10 | + "name": "acme/lib", | |
| 11 | + "version": "dev-main", | |
| 12 | + "dist": { | |
| 13 | + "type": "path", | |
| 14 | + "url": "../lib", | |
| 15 | + "reference": "8b69b77cbf0c83ab7533fbbeb5f671fb065cfd3f" | |
| 16 | + }, | |
| 17 | + "type": "library", | |
| 18 | + "description": "A production dependency installed from a relative path repository", | |
| 19 | + "transport-options": { | |
| 20 | + "relative": true | |
| 21 | + } | |
| 22 | + } | |
| 23 | + ], | |
| 24 | + "packages-dev": [ | |
| 25 | + { | |
| 26 | + "name": "acme/dev-tool", | |
| 27 | + "version": "dev-main", | |
| 28 | + "dist": { | |
| 29 | + "type": "path", | |
| 30 | + "url": "../dev-tool", | |
| 31 | + "reference": "52d35093a9cc4d4a45e5a88e2ece54ac2b659b5a" | |
| 32 | + }, | |
| 33 | + "type": "library", | |
| 34 | + "description": "A dev dependency the deployment zip must not ship", | |
| 35 | + "transport-options": { | |
| 36 | + "relative": true | |
| 37 | + } | |
| 38 | + } | |
| 39 | + ], | |
| 40 | + "aliases": [], | |
| 41 | + "minimum-stability": "stable", | |
| 42 | + "stability-flags": { | |
| 43 | + "acme/dev-tool": 20, | |
| 44 | + "acme/lib": 20 | |
| 45 | + }, | |
| 46 | + "prefer-stable": false, | |
| 47 | + "prefer-lowest": false, | |
| 48 | + "platform": {}, | |
| 49 | + "platform-dev": {}, | |
| 50 | + "plugin-api-version": "2.11.0" | |
| 51 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | +{ | ||
| 2 | + "_readme": [ | ||
| 3 | + "This file locks the dependencies of your project to a known state", | ||
| 4 | + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | ||
| 5 | + "This file is @generated automatically" | ||
| 6 | + ], | ||
| 7 | + "content-hash": "ef10a352ccfe70682015a26ef11e5ddd", | ||
| 8 | + "packages": [ | ||
| 9 | + { | ||
| 10 | + "name": "acme/lib", | ||
| 11 | + "version": "dev-main", | ||
| 12 | + "dist": { | ||
| 13 | + "type": "path", | ||
| 14 | + "url": "../lib", | ||
| 15 | + "reference": "8b69b77cbf0c83ab7533fbbeb5f671fb065cfd3f" | ||
| 16 | + }, | ||
| 17 | + "type": "library", | ||
| 18 | + "description": "A production dependency installed from a relative path repository", | ||
| 19 | + "transport-options": { | ||
| 20 | + "relative": true | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + ], | ||
| 24 | + "packages-dev": [ | ||
| 25 | + { | ||
| 26 | + "name": "acme/dev-tool", | ||
| 27 | + "version": "dev-main", | ||
| 28 | + "dist": { | ||
| 29 | + "type": "path", | ||
| 30 | + "url": "../dev-tool", | ||
| 31 | + "reference": "52d35093a9cc4d4a45e5a88e2ece54ac2b659b5a" | ||
| 32 | + }, | ||
| 33 | + "type": "library", | ||
| 34 | + "description": "A dev dependency the deployment zip must not ship", | ||
| 35 | + "transport-options": { | ||
| 36 | + "relative": true | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + ], | ||
| 40 | + "aliases": [], | ||
| 41 | + "minimum-stability": "stable", | ||
| 42 | + "stability-flags": { | ||
| 43 | + "acme/dev-tool": 20, | ||
| 44 | + "acme/lib": 20 | ||
| 45 | + }, | ||
| 46 | + "prefer-stable": false, | ||
| 47 | + "prefer-lowest": false, | ||
| 48 | + "platform": {}, | ||
| 49 | + "platform-dev": {}, | ||
| 50 | + "plugin-api-version": "2.11.0" | ||
| 51 | +} | ||
added
tests/Fixtures/PackagedApp/app/handler.php +3 -0 | new file mode 100644 | ||
| @@ -0,0 +1,3 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| new file mode 100644 | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
added
tests/Fixtures/PackagedApp/app/src/Service.php +3 -0 | new file mode 100644 | ||
| @@ -0,0 +1,3 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| new file mode 100644 | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
added
tests/Fixtures/PackagedApp/dev-tool/composer.json +4 -0 | new file mode 100644 | ||
| @@ -0,0 +1,4 @@ | ||
| 1 | +{ | |
| 2 | + "name": "acme/dev-tool", | |
| 3 | + "description": "A dev dependency the deployment zip must not ship" | |
| 4 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | +{ | ||
| 2 | + "name": "acme/dev-tool", | ||
| 3 | + "description": "A dev dependency the deployment zip must not ship" | ||
| 4 | +} | ||
added
tests/Fixtures/PackagedApp/lib/composer.json +4 -0 | new file mode 100644 | ||
| @@ -0,0 +1,4 @@ | ||
| 1 | +{ | |
| 2 | + "name": "acme/lib", | |
| 3 | + "description": "A production dependency installed from a relative path repository" | |
| 4 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | +{ | ||
| 2 | + "name": "acme/lib", | ||
| 3 | + "description": "A production dependency installed from a relative path repository" | ||
| 4 | +} | ||
modified
tests/PackagerTest.php +145 -0 | @@ -7,9 +7,29 @@ namespace JDecool\ScalewayRuntime\Tests; | ||
| 7 | 7 | use JDecool\ScalewayRuntime\Packager; |
| 8 | 8 | use PHPUnit\Framework\Attributes\TestWith; |
| 9 | 9 | use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\Filesystem\Filesystem; | |
| 10 | 11 | |
| 11 | 12 | final class PackagerTest extends TestCase |
| 12 | 13 | { |
| 14 | + private Filesystem $filesystem; | |
| 15 | + private string $dir; | |
| 16 | + private string $appDir; | |
| 17 | + | |
| 18 | + protected function setUp(): void | |
| 19 | + { | |
| 20 | + $this->filesystem = new Filesystem(); | |
| 21 | + // Real paths, as bin/scaleway-package passes and as the build dir reports them (macOS /var is a symlink). | |
| 22 | + $this->dir = (string) realpath(sys_get_temp_dir()).'/scaleway-runtime-'.bin2hex(random_bytes(4)); | |
| 23 | + $this->appDir = $this->dir.'/project/app'; | |
| 24 | + $this->filesystem->mirror(__DIR__.'/Fixtures/PackagedApp', $this->dir.'/project'); | |
| 25 | + $this->filesystem->mkdir($this->dir.'/tmp'); | |
| 26 | + } | |
| 27 | + | |
| 28 | + protected function tearDown(): void | |
| 29 | + { | |
| 30 | + $this->filesystem->remove($this->dir); | |
| 31 | + } | |
| 32 | + | |
| 13 | 33 | #[TestWith(['vendor'])] |
| 14 | 34 | #[TestWith(['var'])] |
| 15 | 35 | #[TestWith(['tests'])] |
| @@ -55,4 +75,129 @@ final class PackagerTest extends TestCase | ||
| 55 | 75 | |
| 56 | 76 | self::assertSame($expected, Packager::resolvePathRepositories($lock, '/srv/app')); |
| 57 | 77 | } |
| 78 | + | |
| 79 | + public function testZipShipsTheApplicationWithProductionDependenciesAndTheCommittedLock(): void | |
| 80 | + { | |
| 81 | + $this->filesystem->dumpFile($this->appDir.'/var/cache/prod/stale.php', ''); | |
| 82 | + $this->filesystem->dumpFile($this->appDir.'/vendor/stale.php', ''); | |
| 83 | + $this->filesystem->dumpFile($this->appDir.'/tests/ServiceTest.php', ''); | |
| 84 | + $this->filesystem->dumpFile($this->appDir.'/.env.local', 'APP_SECRET=local'); | |
| 85 | + $this->filesystem->dumpFile($this->appDir.'/.git/HEAD', ''); | |
| 86 | + $this->filesystem->dumpFile($this->appDir.'/assets/node_modules/a.js', ''); | |
| 87 | + $this->filesystem->dumpFile($this->appDir.'/terraform/terraform.tfstate', '{}'); | |
| 88 | + // A previous build, in the application like the default "function.zip": it must not end up in itself. | |
| 89 | + $this->filesystem->dumpFile($this->appDir.'/function.zip', 'stale'); | |
| 90 | + $lock = (string) file_get_contents($this->appDir.'/composer.lock'); | |
| 91 | + | |
| 92 | + [$exitCode, $log] = $this->package($this->appDir.'/function.zip'); | |
| 93 | + | |
| 94 | + self::assertSame(0, $exitCode, $log); | |
| 95 | + $zip = new \ZipArchive(); | |
| 96 | + self::assertTrue($zip->open($this->appDir.'/function.zip')); | |
| 97 | + $names = []; | |
| 98 | + for ($i = 0; $i < $zip->numFiles; ++$i) { | |
| 99 | + $names[] = (string) $zip->getNameIndex($i); | |
| 100 | + } | |
| 101 | + $shipped = array_values(array_filter($names, static fn (string $name): bool => !str_starts_with($name, 'vendor/composer/'))); | |
| 102 | + sort($shipped); | |
| 103 | + self::assertSame([ | |
| 104 | + '.env', | |
| 105 | + 'bin/console', | |
| 106 | + 'composer.json', | |
| 107 | + 'composer.lock', | |
| 108 | + 'handler.php', | |
| 109 | + 'src/Service.php', | |
| 110 | + 'vendor/acme/lib/composer.json', | |
| 111 | + 'vendor/autoload.php', | |
| 112 | + ], $shipped); | |
| 113 | + // Relative "../lib" did not resolve from the build dir, yet the zip ships the lock exactly as committed. | |
| 114 | + self::assertSame($lock, $zip->getFromName('composer.lock')); | |
| 115 | + self::assertSame($lock, file_get_contents($this->appDir.'/composer.lock')); | |
| 116 | + self::assertTrue($zip->getExternalAttributesName('bin/console', $system, $attributes)); | |
| 117 | + self::assertIsInt($attributes); | |
| 118 | + self::assertSame(0o755, ($attributes >> 16) & 0o777); | |
| 119 | + $zip->close(); | |
| 120 | + | |
| 121 | + $record = $this->record(); | |
| 122 | + self::assertSame(['cache:warmup', '--no-interaction'], $record['argv']); | |
| 123 | + self::assertSame('prod', $record['appEnv']); | |
| 124 | + self::assertSame($lock, $record['lock']); | |
| 125 | + } | |
| 126 | + | |
| 127 | + public function testBuildDirIsPrivateAndRemovedAfterwards(): void | |
| 128 | + { | |
| 129 | + [$exitCode, $log] = $this->package($this->dir.'/function.zip'); | |
| 130 | + | |
| 131 | + self::assertSame(0, $exitCode, $log); | |
| 132 | + $record = $this->record(); | |
| 133 | + self::assertIsString($record['buildDir']); | |
| 134 | + self::assertStringStartsWith($this->dir.'/tmp/scaleway-package-', $record['buildDir']); | |
| 135 | + self::assertSame(0o700, $record['perms']); | |
| 136 | + self::assertSame([], $this->leftovers()); | |
| 137 | + } | |
| 138 | + | |
| 139 | + public function testFailedBuildLeavesNeitherBuildDirNorZip(): void | |
| 140 | + { | |
| 141 | + [$exitCode, $log] = $this->package($this->dir.'/function.zip', ['PACKAGER_TEST_FAIL' => '1']); | |
| 142 | + | |
| 143 | + self::assertNotSame(0, $exitCode); | |
| 144 | + self::assertStringContainsString('cache:warmup --no-interaction" failed with exit code 1', $log); | |
| 145 | + self::assertFileDoesNotExist($this->dir.'/function.zip'); | |
| 146 | + self::assertSame([], $this->leftovers()); | |
| 147 | + } | |
| 148 | + | |
| 149 | + public function testApplicationWithoutCommittedLockIsRefused(): void | |
| 150 | + { | |
| 151 | + $this->filesystem->remove($this->appDir.'/composer.lock'); | |
| 152 | + $log = fopen('php://memory', 'w'); | |
| 153 | + \assert(false !== $log); | |
| 154 | + | |
| 155 | + $this->expectExceptionMessage('"composer.lock" is missing'); | |
| 156 | + new Packager($this->appDir, $log)->package($this->dir.'/function.zip'); | |
| 157 | + } | |
| 158 | + | |
| 159 | + /** | |
| 160 | + * Runs in its own PHP process, whose sys_temp_dir is private to the test so leftovers are observable. | |
| 161 | + * | |
| 162 | + * @param array<string, string> $env | |
| 163 | + * | |
| 164 | + * @return array{int, string} the exit code and the log | |
| 165 | + */ | |
| 166 | + private function package(string $zipFile, array $env = []): array | |
| 167 | + { | |
| 168 | + $code = \sprintf('require %s; (new %s(%s, STDERR))->package(%s);', var_export(\dirname(__DIR__).'/vendor/autoload.php', true), Packager::class, var_export($this->appDir, true), var_export($zipFile, true)); | |
| 169 | + $env += [ | |
| 170 | + 'PACKAGER_TEST_RECORD' => $this->dir.'/record.json', | |
| 171 | + // Hermetic: path repositories only, and no global Composer config. | |
| 172 | + 'COMPOSER_HOME' => $this->dir.'/composer-home', | |
| 173 | + 'COMPOSER_CACHE_DIR' => $this->dir.'/composer-cache', | |
| 174 | + 'COMPOSER_DISABLE_NETWORK' => '1', | |
| 175 | + 'COMPOSER_NO_AUDIT' => '1', | |
| 176 | + ]; | |
| 177 | + $process = proc_open([\PHP_BINARY, '-d', 'sys_temp_dir='.$this->dir.'/tmp', '-r', $code], [0 => ['file', '/dev/null', 'r'], 1 => ['file', '/dev/null', 'w'], 2 => ['pipe', 'w']], $pipes, null, $env + getenv()); | |
| 178 | + \assert(false !== $process); | |
| 179 | + $log = (string) stream_get_contents($pipes[2]); | |
| 180 | + fclose($pipes[2]); | |
| 181 | + | |
| 182 | + return [proc_close($process), $log]; | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * @return array<mixed> | |
| 187 | + */ | |
| 188 | + private function record(): array | |
| 189 | + { | |
| 190 | + $record = json_decode((string) file_get_contents($this->dir.'/record.json'), true, 512, \JSON_THROW_ON_ERROR); | |
| 191 | + \assert(\is_array($record)); | |
| 192 | + | |
| 193 | + return $record; | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * @return list<string> | |
| 198 | + */ | |
| 199 | + private function leftovers(): array | |
| 200 | + { | |
| 201 | + return array_values(array_diff(scandir($this->dir.'/tmp') ?: [], ['.', '..'])); | |
| 202 | + } | |
| 58 | 203 | } |
| @@ -7,9 +7,29 @@ namespace JDecool\ScalewayRuntime\Tests; | |||
| 7 | use JDecool\ScalewayRuntime\Packager; | 7 | use JDecool\ScalewayRuntime\Packager; |
| 8 | use PHPUnit\Framework\Attributes\TestWith; | 8 | use PHPUnit\Framework\Attributes\TestWith; |
| 9 | use PHPUnit\Framework\TestCase; | 9 | use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\Filesystem\Filesystem; | ||
| 10 | 11 | ||
| 11 | final class PackagerTest extends TestCase | 12 | final class PackagerTest extends TestCase |
| 12 | { | 13 | { |
| 14 | + private Filesystem $filesystem; | ||
| 15 | + private string $dir; | ||
| 16 | + private string $appDir; | ||
| 17 | + | ||
| 18 | + protected function setUp(): void | ||
| 19 | + { | ||
| 20 | + $this->filesystem = new Filesystem(); | ||
| 21 | + // Real paths, as bin/scaleway-package passes and as the build dir reports them (macOS /var is a symlink). | ||
| 22 | + $this->dir = (string) realpath(sys_get_temp_dir()).'/scaleway-runtime-'.bin2hex(random_bytes(4)); | ||
| 23 | + $this->appDir = $this->dir.'/project/app'; | ||
| 24 | + $this->filesystem->mirror(__DIR__.'/Fixtures/PackagedApp', $this->dir.'/project'); | ||
| 25 | + $this->filesystem->mkdir($this->dir.'/tmp'); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + protected function tearDown(): void | ||
| 29 | + { | ||
| 30 | + $this->filesystem->remove($this->dir); | ||
| 31 | + } | ||
| 32 | + | ||
| 13 | #[TestWith(['vendor'])] | 33 | #[TestWith(['vendor'])] |
| 14 | #[TestWith(['var'])] | 34 | #[TestWith(['var'])] |
| 15 | #[TestWith(['tests'])] | 35 | #[TestWith(['tests'])] |
| @@ -55,4 +75,129 @@ final class PackagerTest extends TestCase | |||
| 55 | 75 | ||
| 56 | self::assertSame($expected, Packager::resolvePathRepositories($lock, '/srv/app')); | 76 | self::assertSame($expected, Packager::resolvePathRepositories($lock, '/srv/app')); |
| 57 | } | 77 | } |
| 78 | + | ||
| 79 | + public function testZipShipsTheApplicationWithProductionDependenciesAndTheCommittedLock(): void | ||
| 80 | + { | ||
| 81 | + $this->filesystem->dumpFile($this->appDir.'/var/cache/prod/stale.php', ''); | ||
| 82 | + $this->filesystem->dumpFile($this->appDir.'/vendor/stale.php', ''); | ||
| 83 | + $this->filesystem->dumpFile($this->appDir.'/tests/ServiceTest.php', ''); | ||
| 84 | + $this->filesystem->dumpFile($this->appDir.'/.env.local', 'APP_SECRET=local'); | ||
| 85 | + $this->filesystem->dumpFile($this->appDir.'/.git/HEAD', ''); | ||
| 86 | + $this->filesystem->dumpFile($this->appDir.'/assets/node_modules/a.js', ''); | ||
| 87 | + $this->filesystem->dumpFile($this->appDir.'/terraform/terraform.tfstate', '{}'); | ||
| 88 | + // A previous build, in the application like the default "function.zip": it must not end up in itself. | ||
| 89 | + $this->filesystem->dumpFile($this->appDir.'/function.zip', 'stale'); | ||
| 90 | + $lock = (string) file_get_contents($this->appDir.'/composer.lock'); | ||
| 91 | + | ||
| 92 | + [$exitCode, $log] = $this->package($this->appDir.'/function.zip'); | ||
| 93 | + | ||
| 94 | + self::assertSame(0, $exitCode, $log); | ||
| 95 | + $zip = new \ZipArchive(); | ||
| 96 | + self::assertTrue($zip->open($this->appDir.'/function.zip')); | ||
| 97 | + $names = []; | ||
| 98 | + for ($i = 0; $i < $zip->numFiles; ++$i) { | ||
| 99 | + $names[] = (string) $zip->getNameIndex($i); | ||
| 100 | + } | ||
| 101 | + $shipped = array_values(array_filter($names, static fn (string $name): bool => !str_starts_with($name, 'vendor/composer/'))); | ||
| 102 | + sort($shipped); | ||
| 103 | + self::assertSame([ | ||
| 104 | + '.env', | ||
| 105 | + 'bin/console', | ||
| 106 | + 'composer.json', | ||
| 107 | + 'composer.lock', | ||
| 108 | + 'handler.php', | ||
| 109 | + 'src/Service.php', | ||
| 110 | + 'vendor/acme/lib/composer.json', | ||
| 111 | + 'vendor/autoload.php', | ||
| 112 | + ], $shipped); | ||
| 113 | + // Relative "../lib" did not resolve from the build dir, yet the zip ships the lock exactly as committed. | ||
| 114 | + self::assertSame($lock, $zip->getFromName('composer.lock')); | ||
| 115 | + self::assertSame($lock, file_get_contents($this->appDir.'/composer.lock')); | ||
| 116 | + self::assertTrue($zip->getExternalAttributesName('bin/console', $system, $attributes)); | ||
| 117 | + self::assertIsInt($attributes); | ||
| 118 | + self::assertSame(0o755, ($attributes >> 16) & 0o777); | ||
| 119 | + $zip->close(); | ||
| 120 | + | ||
| 121 | + $record = $this->record(); | ||
| 122 | + self::assertSame(['cache:warmup', '--no-interaction'], $record['argv']); | ||
| 123 | + self::assertSame('prod', $record['appEnv']); | ||
| 124 | + self::assertSame($lock, $record['lock']); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + public function testBuildDirIsPrivateAndRemovedAfterwards(): void | ||
| 128 | + { | ||
| 129 | + [$exitCode, $log] = $this->package($this->dir.'/function.zip'); | ||
| 130 | + | ||
| 131 | + self::assertSame(0, $exitCode, $log); | ||
| 132 | + $record = $this->record(); | ||
| 133 | + self::assertIsString($record['buildDir']); | ||
| 134 | + self::assertStringStartsWith($this->dir.'/tmp/scaleway-package-', $record['buildDir']); | ||
| 135 | + self::assertSame(0o700, $record['perms']); | ||
| 136 | + self::assertSame([], $this->leftovers()); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + public function testFailedBuildLeavesNeitherBuildDirNorZip(): void | ||
| 140 | + { | ||
| 141 | + [$exitCode, $log] = $this->package($this->dir.'/function.zip', ['PACKAGER_TEST_FAIL' => '1']); | ||
| 142 | + | ||
| 143 | + self::assertNotSame(0, $exitCode); | ||
| 144 | + self::assertStringContainsString('cache:warmup --no-interaction" failed with exit code 1', $log); | ||
| 145 | + self::assertFileDoesNotExist($this->dir.'/function.zip'); | ||
| 146 | + self::assertSame([], $this->leftovers()); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + public function testApplicationWithoutCommittedLockIsRefused(): void | ||
| 150 | + { | ||
| 151 | + $this->filesystem->remove($this->appDir.'/composer.lock'); | ||
| 152 | + $log = fopen('php://memory', 'w'); | ||
| 153 | + \assert(false !== $log); | ||
| 154 | + | ||
| 155 | + $this->expectExceptionMessage('"composer.lock" is missing'); | ||
| 156 | + new Packager($this->appDir, $log)->package($this->dir.'/function.zip'); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + /** | ||
| 160 | + * Runs in its own PHP process, whose sys_temp_dir is private to the test so leftovers are observable. | ||
| 161 | + * | ||
| 162 | + * @param array<string, string> $env | ||
| 163 | + * | ||
| 164 | + * @return array{int, string} the exit code and the log | ||
| 165 | + */ | ||
| 166 | + private function package(string $zipFile, array $env = []): array | ||
| 167 | + { | ||
| 168 | + $code = \sprintf('require %s; (new %s(%s, STDERR))->package(%s);', var_export(\dirname(__DIR__).'/vendor/autoload.php', true), Packager::class, var_export($this->appDir, true), var_export($zipFile, true)); | ||
| 169 | + $env += [ | ||
| 170 | + 'PACKAGER_TEST_RECORD' => $this->dir.'/record.json', | ||
| 171 | + // Hermetic: path repositories only, and no global Composer config. | ||
| 172 | + 'COMPOSER_HOME' => $this->dir.'/composer-home', | ||
| 173 | + 'COMPOSER_CACHE_DIR' => $this->dir.'/composer-cache', | ||
| 174 | + 'COMPOSER_DISABLE_NETWORK' => '1', | ||
| 175 | + 'COMPOSER_NO_AUDIT' => '1', | ||
| 176 | + ]; | ||
| 177 | + $process = proc_open([\PHP_BINARY, '-d', 'sys_temp_dir='.$this->dir.'/tmp', '-r', $code], [0 => ['file', '/dev/null', 'r'], 1 => ['file', '/dev/null', 'w'], 2 => ['pipe', 'w']], $pipes, null, $env + getenv()); | ||
| 178 | + \assert(false !== $process); | ||
| 179 | + $log = (string) stream_get_contents($pipes[2]); | ||
| 180 | + fclose($pipes[2]); | ||
| 181 | + | ||
| 182 | + return [proc_close($process), $log]; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + /** | ||
| 186 | + * @return array<mixed> | ||
| 187 | + */ | ||
| 188 | + private function record(): array | ||
| 189 | + { | ||
| 190 | + $record = json_decode((string) file_get_contents($this->dir.'/record.json'), true, 512, \JSON_THROW_ON_ERROR); | ||
| 191 | + \assert(\is_array($record)); | ||
| 192 | + | ||
| 193 | + return $record; | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + /** | ||
| 197 | + * @return list<string> | ||
| 198 | + */ | ||
| 199 | + private function leftovers(): array | ||
| 200 | + { | ||
| 201 | + return array_values(array_diff(scandir($this->dir.'/tmp') ?: [], ['.', '..'])); | ||
| 202 | + } | ||
| 58 | } | 203 | } |