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