|
2 | 2 |
|
3 | 3 | namespace CodedMonkey\Dirigent\Tests\Docker\Standalone; |
4 | 4 |
|
5 | | -class InitTest extends DockerStandaloneTestCase |
| 5 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 6 | +use Symfony\Component\Filesystem\Filesystem; |
| 7 | +use Testcontainers\Container\GenericContainer; |
| 8 | +use Testcontainers\Wait\WaitForLog; |
| 9 | + |
| 10 | +class InitTest extends DockerStandaloneIsolatedTestCase |
6 | 11 | { |
| 12 | + protected Filesystem $filesystem; |
| 13 | + |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + $this->filesystem = new Filesystem(); |
| 17 | + } |
| 18 | + |
| 19 | + protected function tearDown(): void |
| 20 | + { |
| 21 | + parent::tearDown(); |
| 22 | + |
| 23 | + $this->filesystem->remove(__DIR__ . '/config'); |
| 24 | + } |
| 25 | + |
7 | 26 | public function testKernelSecretGenerated(): void |
8 | 27 | { |
| 28 | + $this->setUpDefaultContainer(); |
| 29 | + |
| 30 | + $logs = $this->container->logs(); |
| 31 | + |
| 32 | + $this->assertStringContainsString('Generated a new kernel secret', $logs); |
| 33 | + |
9 | 34 | $this->assertContainerFileExists( |
10 | 35 | '/srv/config/secrets/kernel_secret', |
11 | 36 | 'A kernel_secret file must be generated.', |
12 | 37 | ); |
13 | 38 | } |
| 39 | + |
| 40 | + public function testKernelSecretNotRegeneratedOnRestart(): void |
| 41 | + { |
| 42 | + $this->filesystem->mkdir(__DIR__ . '/config'); |
| 43 | + |
| 44 | + // Generate kernel secret first |
| 45 | + $this->container = (new GenericContainer('dirigent-standalone')) |
| 46 | + ->withMount(__DIR__ . '/config', '/srv/config') |
| 47 | + ->withMount(__DIR__ . '/scripts', '/srv/scripts/tests') |
| 48 | + ->withWait(new WaitForLog('ready to handle connections')) |
| 49 | + ->start(); |
| 50 | + |
| 51 | + $initialSecret = $this->filesystem->readFile(__DIR__ . '/config/secrets/kernel_secret'); |
| 52 | + |
| 53 | + $this->container->restart(); |
| 54 | + |
| 55 | + $logs = $this->container->logs(); |
| 56 | + |
| 57 | + $this->assertStringContainsString('Kernel secret exists', $logs); |
| 58 | + |
| 59 | + $secret = $this->filesystem->readFile(__DIR__ . '/config/secrets/kernel_secret'); |
| 60 | + |
| 61 | + $this->assertSame($initialSecret, $secret, 'The kernel_secret file must not be changed if it already exists.'); |
| 62 | + } |
| 63 | + |
| 64 | + public static function kernelSecretEnvVarProvider(): array |
| 65 | + { |
| 66 | + return [ |
| 67 | + ['KERNEL_SECRET', 'fernando'], |
| 68 | + ['KERNEL_SECRET_FILE', '/srv/config/secrets/kernel_secret'], |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + #[DataProvider('kernelSecretEnvVarProvider')] |
| 73 | + public function testKernelSecretNotGeneratedIfEnvVarExists(string $varName, string $varValue): void |
| 74 | + { |
| 75 | + $this->filesystem->mkdir(__DIR__ . '/config'); |
| 76 | + |
| 77 | + $this->container = (new GenericContainer('dirigent-standalone')) |
| 78 | + ->withMount(__DIR__ . '/config', '/srv/config') |
| 79 | + ->withMount(__DIR__ . '/scripts', '/srv/scripts/tests') |
| 80 | + ->withEnvironment([$varName => $varValue]) |
| 81 | + ->withWait(new WaitForLog('ready to handle connections')) |
| 82 | + ->start(); |
| 83 | + |
| 84 | + $logs = $this->container->logs(); |
| 85 | + |
| 86 | + $this->assertStringContainsString('Kernel secret is defined as an environment variable', $logs); |
| 87 | + |
| 88 | + $kernelSecretExists = $this->filesystem->exists(__DIR__ . '/config/secrets/kernel_secret'); |
| 89 | + $this->assertFalse($kernelSecretExists, 'The kernel_secret file must not be generated if the kernel secret is defined through an environment variable.'); |
| 90 | + } |
14 | 91 | } |
0 commit comments