Skip to content

Commit

Permalink
tests: basic deployment test
Browse files Browse the repository at this point in the history
  • Loading branch information
yhortuk committed Aug 13, 2023
1 parent d1ca330 commit 8b6ef4d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
3 changes: 2 additions & 1 deletion app/Aws/ContinuousIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ public function deleteStack(): ?PendingStack
{
try {
$this->cloudformation->describeStacks(['StackName' => $this->unload->ciStackName()])->get('Stacks');
$this->cloudformation->updateTerminationProtection(['StackName' => $this->unload->ciStackName(), 'EnableTerminationProtection' => false]);
$this->cloudformation->deleteStack(['StackName' => $this->unload->ciStackName()]);
return new PendingStack($this->unload->ciStackName(), $this->cloudformation);
} catch (CloudFormationException) {
} catch (CloudFormationException $e) {
return null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Commands/DestroyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class DestroyCommand extends Command
{
protected $signature = 'destroy {--env=}';
protected $signature = 'destroy {--force}';
protected $description = 'Destroy provisioned application resources and artifacts';

public function handle(): void
Expand All @@ -19,7 +19,7 @@ public function handle(): void
$this->comment("2) It will remove the application variables in {$this->unload->env()} environment");
$this->comment("3) It will remove the continuous integration stack for {$this->unload->env()} environment");

if (!$this->confirm('Are you sure you want to processed?')) {
if (!$this->option('force') && !$this->confirm('Are you sure you want to processed?')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Templates/SamTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected function bootstrap(): self
'WEB_CONCURRENCY' => $this->unloadConfig->defaultWarm(),
],
],
'Runtime' => 'nodejs18.x',
'Runtime' => 'nodejs16.x',
'Timeout' => 900,
'InlineCode' => Cloudformation::get('deploy.js'),
'Handler' => 'index.handler',
Expand Down
42 changes: 31 additions & 11 deletions tests/Deployments/BasicDeploymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Deployments;

use Aws\CloudFormation\CloudFormationClient;
use GuzzleHttp\Client;
use Tests\DeploysApplication;
use Tests\TestCase;

Expand All @@ -16,18 +17,16 @@ public function test_can_bootstrap_and_deploy_basic_application()

$this->setupTempDirectory();

$this->process()->run('composer create-project laravel/laravel .');
$this->process()->run('composer create-project laravel/laravel . 10.0');

$this->setupLocalPackageRepository();

$this->process()->run('composer require unload/unload-laravel');
$this->process()->run('composer require unload/unload-laravel --update-with-dependencies');

$bootstrap = $this->process()->run('../../../unload bootstrap --app=test --php=8.1 --env=production --provider=github --profile=integration --repository=test/example --no-interaction');

$this->assertTrue(
$bootstrap->seeInOutput('Project test bootstrapping has been completed.'),
'Cloudformation environment configured succesfully.'
);
$bootstrap = $this->process()->run('../../../unload bootstrap --app=test --php=8.1 --env=production --provider=github --profile=integration --repository=test/example --region=eu-central-1 --no-interaction');
if (!$bootstrap->seeInOutput('Project test bootstrapping has been completed.')) {
$this->fail($bootstrap->output());
}

$this->assertContains(
$cloudformation->describeStacks(['StackName' => 'unload-production-test-ci'])->search('Stacks[0].StackStatus'),
Expand All @@ -40,10 +39,31 @@ public function test_can_bootstrap_and_deploy_basic_application()
);

$deploy = $this->process()->timeout(3600)->run('../../../unload deploy --config=unload.yaml --no-interaction');
if ($deploy->seeInOutput('ErrorException')) {
$this->fail($deploy->output());
}

$appStack = $cloudformation->describeStacks(['StackName' => 'unload-production-test-app'])->search('Stacks[0]');

$this->assertFalse(
$deploy->seeInOutput('AWS sam failed to deploy the stack. Please check cloudformation output for exact reason.'),
'Application was deployed successfully.'
$this->assertContains(
$appStack['StackStatus'],
['UPDATE_COMPLETE', 'CREATE_COMPLETE'],
);

$browserResponse = (new Client)->get(collect($appStack['Outputs'])->pluck('OutputValue','OutputKey')->get('AppCloudfrontURL'));

$this->assertEquals(200, $browserResponse->getStatusCode());
$this->assertStringContainsString('Laravel News', $browserResponse->getBody()->getContents());

$destroy = $this->process()->timeout(3600)->run('../../../unload destroy --config=unload.yaml --force --no-interaction');
if ($destroy->seeInOutput('Error')) {
$this->fail($deploy->output());
}

$this->expectExceptionMessage('Stack with id unload-production-test-app does not exist');
$cloudformation->describeStacks(['StackName' => 'unload-production-test-app']);

$this->expectExceptionMessage('Stack with id unload-production-test-app does not exist');
$cloudformation->describeStacks(['StackName' => 'unload-production-test-ci']);
}
}
4 changes: 2 additions & 2 deletions tests/DeploysApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait DeploysApplication
{
public function process(): PendingProcess
{
return Process::path(base_path('tests/Fixtures/tmp'))->env([
return Process::timeout(3600)->path(base_path('tests/Fixtures/tmp'))->env([
'AWS_SHARED_CREDENTIALS_FILE' => getenv('AWS_SHARED_CREDENTIALS_FILE'),
]);
}
Expand All @@ -36,6 +36,6 @@ protected function setupLocalPackageRepository(): void
]
];

file_put_contents('./composer.json', json_encode($composer));
file_put_contents('./composer.json', json_encode($composer, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
}
}

0 comments on commit 8b6ef4d

Please sign in to comment.