Skip to content

Commit

Permalink
CLI-1370: PHP warning in pull:db when backup fails (#1776)
Browse files Browse the repository at this point in the history
* CLI-1370: PHP warning in pull:db when backup fails

* add tests

* fix tests
  • Loading branch information
danepowell authored Aug 6, 2024
1 parent 938f87b commit 402fbef
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/Command/Env/EnvCertCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Exception\AcquiaCliException;
use AcquiaCloudApi\Endpoints\SslCertificates;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -53,7 +54,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$legacy
);
$notificationUuid = CommandBase::getNotificationUuidFromResponse($response);
$this->waitForNotificationToComplete($acquiaCloudClient, $notificationUuid, 'Installing certificate');
$success = $this->waitForNotificationToComplete($acquiaCloudClient, $notificationUuid, 'Installing certificate');
if (!$success) {
throw new AcquiaCliException('Cloud API failed to install certificate');
}
return Command::SUCCESS;
}
}
5 changes: 4 additions & 1 deletion src/Command/Env/EnvCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
]);
}
};
$this->waitForNotificationToComplete($acquiaCloudClient, $notificationUuid, "Waiting for the environment to be ready. This usually takes 2 - 15 minutes.", $success);
$success = $this->waitForNotificationToComplete($acquiaCloudClient, $notificationUuid, "Waiting for the environment to be ready. This usually takes 2 - 15 minutes.", $success);
if (!$success) {
throw new AcquiaCliException('Cloud API failed to create environment');
}

return Command::SUCCESS;
}
Expand Down
17 changes: 9 additions & 8 deletions src/Command/Env/EnvMirrorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Acquia\Cli\Attribute\RequireAuth;
use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Exception\AcquiaCliException;
use Acquia\Cli\Output\Checklist;
use AcquiaCloudApi\Connector\Client;
use AcquiaCloudApi\Endpoints\Databases;
Expand Down Expand Up @@ -78,17 +79,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$configCopyResponse = $this->mirrorConfig($sourceEnvironment, $destinationEnvironment, $environmentsResource, $destinationEnvironmentUuid, $outputCallback);
}

if (isset($codeCopyResponse)) {
$this->waitForNotificationToComplete($acquiaCloudClient, CommandBase::getNotificationUuidFromResponse($codeCopyResponse), 'Waiting for code copy to complete');
if (isset($codeCopyResponse) && !$this->waitForNotificationToComplete($acquiaCloudClient, CommandBase::getNotificationUuidFromResponse($codeCopyResponse), 'Waiting for code copy to complete')) {
throw new AcquiaCliException('Cloud API failed to copy code');
}
if (isset($dbCopyResponse)) {
$this->waitForNotificationToComplete($acquiaCloudClient, CommandBase::getNotificationUuidFromResponse($dbCopyResponse), 'Waiting for database copy to complete');
if (isset($dbCopyResponse) && !$this->waitForNotificationToComplete($acquiaCloudClient, CommandBase::getNotificationUuidFromResponse($dbCopyResponse), 'Waiting for database copy to complete')) {
throw new AcquiaCliException('Cloud API failed to copy database');
}
if (isset($filesCopyResponse)) {
$this->waitForNotificationToComplete($acquiaCloudClient, CommandBase::getNotificationUuidFromResponse($filesCopyResponse), 'Waiting for files copy to complete');
if (isset($filesCopyResponse) && !$this->waitForNotificationToComplete($acquiaCloudClient, CommandBase::getNotificationUuidFromResponse($filesCopyResponse), 'Waiting for files copy to complete')) {
throw new AcquiaCliException('Cloud API failed to copy files');
}
if (isset($configCopyResponse)) {
$this->waitForNotificationToComplete($acquiaCloudClient, CommandBase::getNotificationUuidFromResponse($configCopyResponse), 'Waiting for config copy to complete');
if (isset($configCopyResponse) && !$this->waitForNotificationToComplete($acquiaCloudClient, CommandBase::getNotificationUuidFromResponse($configCopyResponse), 'Waiting for config copy to complete')) {
throw new AcquiaCliException('Cloud API failed to copy config');
}

$this->io->success([
Expand Down
5 changes: 4 additions & 1 deletion src/Command/Pull/PullCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,11 @@ protected function waitForBackup(string $notificationUuid, Client $acquiaCloudCl
$this->output->writeln('');
$this->output->writeln('<info>Database backup is ready!</info>');
};
$this->waitForNotificationToComplete($acquiaCloudClient, $notificationUuid, $spinnerMessage, $successCallback);
$success = $this->waitForNotificationToComplete($acquiaCloudClient, $notificationUuid, $spinnerMessage, $successCallback);
Loop::run();
if (!$success) {
throw new AcquiaCliException('Cloud API failed to create a backup');
}
}

private function connectToLocalDatabase(string $dbHost, string $dbUser, string $dbName, string $dbPassword, callable $outputCallback = null): void
Expand Down
10 changes: 8 additions & 2 deletions tests/phpunit/src/CommandTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,16 @@ protected function mockDatabaseBackupCreateResponse(
return $backupCreateResponse;
}

protected function mockNotificationResponseFromObject(object $responseWithNotificationLink): array|object
protected function mockNotificationResponseFromObject(object $responseWithNotificationLink, ?bool $success = true): array|object
{
$uuid = substr($responseWithNotificationLink->_links->notification->href, -36);
return $this->mockRequest('getNotificationByUuid', $uuid);
if ($success) {
return $this->mockRequest('getNotificationByUuid', $uuid);
}

return $this->mockRequest('getNotificationByUuid', $uuid, null, null, function ($response): void {
$response->status = 'failed';
});
}

protected function mockCreateMySqlDumpOnLocal(ObjectProphecy $localMachineHelper, bool $printOutput = true, bool $pv = true): void
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/src/Commands/Env/EnvCertCreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Acquia\Cli\Command\CommandBase;
use Acquia\Cli\Command\Env\EnvCertCreateCommand;
use Acquia\Cli\Exception\AcquiaCliException;
use Acquia\Cli\Tests\CommandTestBase;

class EnvCertCreateCommandTest extends CommandTestBase
Expand Down Expand Up @@ -74,6 +75,67 @@ public function testCreateCert(): void
);
}

public function testCreateCertFailed(): void
{
$applications = $this->mockRequest('getApplications');
$application = $this->mockRequest('getApplicationByUuid', $applications[self::$INPUT_DEFAULT_CHOICE]->uuid);
$environments = $this->mockRequest('getApplicationEnvironments', $application->uuid);
$localMachineHelper = $this->mockLocalMachineHelper();
$certContents = 'cert-contents';
$keyContents = 'key-contents';
$certName = 'cert.pem';
$keyName = 'key.pem';
$label = 'My certificate';
$csrId = 123;
$localMachineHelper->readFile($certName)
->willReturn($certContents)
->shouldBeCalled();
$localMachineHelper->readFile($keyName)
->willReturn($keyContents)
->shouldBeCalled();

$sslResponse = $this->getMockResponseFromSpec(
'/environments/{environmentId}/ssl/certificates',
'post',
'202'
);
$options = [
'json' => [
'ca_certificates' => null,
'certificate' => $certContents,
'csr_id' => $csrId,
'label' => $label,
'legacy' => false,
'private_key' => $keyContents,
],
];
$this->clientProphecy->request('post', "/environments/{$environments[1]->id}/ssl/certificates", $options)
->willReturn($sslResponse->{'Site is being imported'}->value)
->shouldBeCalled();
$this->mockNotificationResponseFromObject($sslResponse->{'Site is being imported'}->value, false);

$this->expectException(AcquiaCliException::class);
$this->expectExceptionMessage('Cloud API failed to install certificate');
$this->executeCommand(
[
'--csr-id' => $csrId,
'--label' => $label,
'--legacy' => false,
'certificate' => $certName,
'private-key' => $keyName,
],
[
// Would you like Acquia CLI to search for a Cloud application that matches your local git config?'.
'n',
// Select a Cloud Platform application: [Sample application 1]:
0,
'n',
1,
'',
]
);
}

public function testCreateCertNode(): void
{
$applications = $this->mockRequest('getApplications');
Expand Down
23 changes: 21 additions & 2 deletions tests/phpunit/src/Commands/Env/EnvCreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EnvCreateCommandTest extends CommandTestBase
{
private static string $validLabel = 'New CDE';

private function setupCdeTest(string $label): string
private function setupCdeTest(string $label, ?bool $apiSuccess = true): string
{
$applicationsResponse = $this->mockApplicationsRequest();
$applicationResponse = $this->mockApplicationRequest();
Expand Down Expand Up @@ -60,7 +60,7 @@ private function setupCdeTest(string $label): string
->willReturn($environmentsResponse->{'Adding environment'}->value)
->shouldBeCalled();

$this->mockNotificationResponseFromObject($environmentsResponse->{'Adding environment'}->value);
$this->mockNotificationResponseFromObject($environmentsResponse->{'Adding environment'}->value, $apiSuccess);
return $response2->_embedded->items[3]->domains[0];
}

Expand Down Expand Up @@ -160,4 +160,23 @@ public function testCreateCdeInvalidTag(): void
]
);
}

/**
* @group brokenProphecy
*/
public function testCreateCdeApiFailure(): void
{
$this->setupCdeTest(self::$validLabel, false);

$this->expectException(AcquiaCliException::class);
$this->expectExceptionMessage('Cloud API failed to create environment');

$this->executeCommand(
[
'applicationUuid' => $this->getApplication(),
'branch' => $this->getBranch(),
'label' => self::$validLabel,
]
);
}
}
Loading

0 comments on commit 402fbef

Please sign in to comment.