From 557aef3f9644b25b9c9556cbfc326a00baa73c6b Mon Sep 17 00:00:00 2001 From: Adam Malone Date: Sun, 8 Mar 2020 12:50:31 +1100 Subject: [PATCH] Adds tests for pm, account, environments --- src/AcquiaCli.php | 4 + src/Commands/AccountCommand.php | 13 +-- src/Commands/DrushAliasesCommand.php | 4 +- src/Commands/EnvironmentsCommand.php | 11 ++ src/Commands/ProductionModeCommand.php | 11 +- src/Injector/AcquiaCliInjector.php | 3 + tests/AcquiaCliTest.php | 4 +- tests/AcquiaCliTestCase.php | 34 +++++- tests/CloudApiTest.php | 3 +- tests/Commands/AccountCommandTest.php | 37 +++++++ tests/Commands/ApplicationCommandTest.php | 23 +++- tests/Commands/EnvironmentCommandTest.php | 105 +++++++++++++++++++ tests/Commands/LiveDevCommandTest.php | 33 ++++++ tests/Commands/ProductionModeCommandTest.php | 46 ++++++++ 14 files changed, 310 insertions(+), 21 deletions(-) create mode 100644 tests/Commands/AccountCommandTest.php create mode 100644 tests/Commands/EnvironmentCommandTest.php create mode 100644 tests/Commands/LiveDevCommandTest.php create mode 100644 tests/Commands/ProductionModeCommandTest.php diff --git a/src/AcquiaCli.php b/src/AcquiaCli.php index b32ff64..22904ec 100644 --- a/src/AcquiaCli.php +++ b/src/AcquiaCli.php @@ -48,6 +48,7 @@ public function __construct( // Create application. $this->setConfig($config); + $application = new Application(self::NAME, $version); $application->getDefinition()->addOptions([ @@ -112,6 +113,9 @@ public function getContainer($input, $output, $application, $config, $client) 'AcquiaCloudApi\Endpoints\DatabaseBackups', new \AcquiaCli\Injector\AcquiaCliInjector ); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Crons', new \AcquiaCli\Injector\AcquiaCliInjector); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Account', new \AcquiaCli\Injector\AcquiaCliInjector); + return $container; } diff --git a/src/Commands/AccountCommand.php b/src/Commands/AccountCommand.php index 4ece441..a13a880 100644 --- a/src/Commands/AccountCommand.php +++ b/src/Commands/AccountCommand.php @@ -11,28 +11,19 @@ class AccountCommand extends AcquiaCommand { - protected $accountAdapter; - - public function __construct() - { - parent::__construct(); - - $this->accountAdapter = new Account($this->cloudapi); - } - /** * Gets information about the user's account. * * @command account */ - public function account() + public function account(Account $accountAdapter) { $extraConfig = $this->cloudapiService->getExtraConfig(); $tz = $extraConfig['timezone']; $format = $extraConfig['format']; $timezone = new \DateTimeZone($tz); - $account = $this->accountAdapter->get(); + $account = $accountAdapter->get(); $lastLogin = new \DateTime($account->last_login_at); $lastLogin->setTimezone($timezone); diff --git a/src/Commands/DrushAliasesCommand.php b/src/Commands/DrushAliasesCommand.php index ca0d59e..b9bbb72 100644 --- a/src/Commands/DrushAliasesCommand.php +++ b/src/Commands/DrushAliasesCommand.php @@ -17,9 +17,9 @@ class DrushAliasesCommand extends AccountCommand * * @command drush:aliases */ - public function downloadDrushAliases($options = ['install' => false]) + public function downloadDrushAliases(Account $accountAdapter, $options = ['install' => false]) { - $aliases = $this->accountAdapter->getDrushAliases(); + $aliases = $accountAdapter->getDrushAliases(); $drushArchive = tempnam(sys_get_temp_dir(), 'AcquiaDrushAliases') . '.tar.gz'; $this->say(sprintf('Acquia Cloud Drush Aliases archive downloaded to %s', $drushArchive)); if (file_put_contents($drushArchive, $aliases, LOCK_EX)) { diff --git a/src/Commands/EnvironmentsCommand.php b/src/Commands/EnvironmentsCommand.php index b359dde..07b182b 100644 --- a/src/Commands/EnvironmentsCommand.php +++ b/src/Commands/EnvironmentsCommand.php @@ -151,6 +151,16 @@ protected function renderEnvironmentInfo(EnvironmentResponse $environment, Serve 'APCu', 'Sendmail Path' ]); + + if (!isset($environment->configuration->php)) { + $environment->configuration = new \stdClass(); + $environment->configuration->php = new \stdClass(); + $environment->configuration->php->version = ' '; + $environment->configuration->php->memory_limit = ' '; + $environment->configuration->php->opcache = ' '; + $environment->configuration->php->apcu = ' '; + $environment->configuration->php->sendmail_path = ' '; + } $environmentTable ->addRows([ [ @@ -196,6 +206,7 @@ public function environmentDelete(Environments $environmentsAdapter, $uuid, $env { $environment = $this->cloudapiService->getEnvironment($uuid, $environment); if ($this->confirm("Are you sure you want to delete this environment?")) { + $this->say(sprintf('Deleting %s environment', $environment->label)); $response = $environmentsAdapter->delete($environment->uuid); $this->waitForNotification($response); } diff --git a/src/Commands/ProductionModeCommand.php b/src/Commands/ProductionModeCommand.php index 4d4492b..49129cb 100644 --- a/src/Commands/ProductionModeCommand.php +++ b/src/Commands/ProductionModeCommand.php @@ -24,11 +24,13 @@ class ProductionModeCommand extends EnvironmentsCommand */ public function productionModeEnable(Environments $environmentsAdapter, $uuid, $environment) { - $environment = $this->cloudapiService->getEnvironment($uuid, $environment); - if ('prod' !== $environment->name) { + if ('prod' !== $environment) { throw new \Exception('Production mode may only be enabled/disabled on the prod environment.'); } + + $environment = $this->cloudapiService->getEnvironment($uuid, $environment); + $this->say(sprintf('Enabling production mode for %s environment', $environment->label)); $environmentsAdapter->enableProductionMode($environment->uuid); } @@ -45,12 +47,13 @@ public function productionModeEnable(Environments $environmentsAdapter, $uuid, $ */ public function productionModeDisable(Environments $environmentsAdapter, $uuid, $environment) { - $environment = $this->cloudapiService->getEnvironment($uuid, $environment); - if ('prod' !== $environment->name) { + if ('prod' !== $environment) { throw new \Exception('Production mode may only be enabled/disabled on the prod environment.'); } + $environment = $this->cloudapiService->getEnvironment($uuid, $environment); + if ($this->confirm('Are you sure you want to disable production mode?')) { $this->say(sprintf('Disabling production mode for %s environment', $environment->label)); $environmentsAdapter->disableProductionMode($environment->uuid); diff --git a/src/Injector/AcquiaCliInjector.php b/src/Injector/AcquiaCliInjector.php index cd89323..08f8432 100644 --- a/src/Injector/AcquiaCliInjector.php +++ b/src/Injector/AcquiaCliInjector.php @@ -13,6 +13,7 @@ use AcquiaCloudApi\Endpoints\Code; use AcquiaCloudApi\Endpoints\DatabaseBackups; use AcquiaCloudApi\Endpoints\Crons; +use AcquiaCloudApi\Endpoints\Account; class AcquiaCliInjector implements ParameterInjector { @@ -51,6 +52,8 @@ public function get(CommandData $commandData, $interfaceName) return new DatabaseBackups($this->client); case 'AcquiaCloudApi\Endpoints\Crons': return new Crons($this->client); + case 'AcquiaCloudApi\Endpoints\Account': + return new Account($this->client); } return null; diff --git a/tests/AcquiaCliTest.php b/tests/AcquiaCliTest.php index 2f1da7a..d2f97e7 100644 --- a/tests/AcquiaCliTest.php +++ b/tests/AcquiaCliTest.php @@ -32,7 +32,7 @@ class AcquiaCliTest extends AcquiaCli public function getContainer($input, $output, $application, $config, $client) { - $container = Robo::createDefaultContainer($input, $output, $application); + $container = Robo::createDefaultContainer($input, $output, $application, $config); $container->add('client', $client); $container->add('cloudApi', \AcquiaCli\Tests\CloudApiTest::class) @@ -50,6 +50,7 @@ public function getContainer($input, $output, $application, $config, $client) new \AcquiaCli\Injector\AcquiaCliInjector ); $parameterInjection->register('AcquiaCloudApi\Endpoints\Databases', new \AcquiaCli\Injector\AcquiaCliInjector); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Servers', new \AcquiaCli\Injector\AcquiaCliInjector); $parameterInjection->register('AcquiaCloudApi\Endpoints\Domains', new \AcquiaCli\Injector\AcquiaCliInjector); $parameterInjection->register('AcquiaCloudApi\Endpoints\Code', new \AcquiaCli\Injector\AcquiaCliInjector); $parameterInjection->register( @@ -57,6 +58,7 @@ public function getContainer($input, $output, $application, $config, $client) new \AcquiaCli\Injector\AcquiaCliInjector ); $parameterInjection->register('AcquiaCloudApi\Endpoints\Crons', new \AcquiaCli\Injector\AcquiaCliInjector); + $parameterInjection->register('AcquiaCloudApi\Endpoints\Account', new \AcquiaCli\Injector\AcquiaCliInjector); return $container; } diff --git a/tests/AcquiaCliTestCase.php b/tests/AcquiaCliTestCase.php index 95d954a..6a48fdd 100644 --- a/tests/AcquiaCliTestCase.php +++ b/tests/AcquiaCliTestCase.php @@ -132,6 +132,9 @@ public function sendRequestCallback($verb, $path) public static function getFixtureMap() { return [ + '/account' => [ + 'get' => 'Account/getAccount.json' + ], '/applications' => [ 'get' => 'Applications/getAllApplications.json', ], @@ -162,6 +165,12 @@ public static function getFixtureMap() '/organizations/organisation/roles' => [ 'post' => 'Roles/createRole.json' ], + '/applications/uuid/environments' => [ + 'get' => 'Environments/getAllEnvironments.json' + ], + '/environments/bfcc7ad1-f987-41b8-9ea5-f26f0ef3838a' => [ + 'delete' => 'Environments/deleteCDEnvironment.json' + ], '/environments/bfcc7ad1-f987-41b8-9ea5-f26f0ef3838a/databases/database2/backups' => [ 'get' => 'DatabaseBackups/getAllDatabaseBackups.json', 'post' => 'DatabaseBackups/createDatabaseBackup.json' @@ -214,8 +223,31 @@ public static function getFixtureMap() ], '/applications/uuid/code' => [ 'get' => 'Code/getAllCode.json' + ], + '/environments/bfcc7ad1-f987-41b8-9ea5-f26f0ef3838a/livedev/actions/disable' => [ + 'post' => 'Environments/disableLiveDev.json' + ], + '/environments/bfcc7ad1-f987-41b8-9ea5-f26f0ef3838a/livedev/actions/enable' => [ + 'post' => 'Environments/enableLiveDev.json' + ], + '/environments/24-a47ac10b-58cc-4372-a567-0e02b2c3d470/servers' => [ + 'get' => 'Servers/getAllServers.json' + ], + '/environments/15-a47ac10b-58cc-4372-a567-0e02b2c3d470/servers' => [ + 'get' => 'Servers/getAllServers.json' + ], + '/environments/32-a47ac10b-58cc-4372-a567-0e02b2c3d470/servers' => [ + 'get' => 'Servers/getAllServers.json' + ], + '/environments/bfcc7ad1-f987-41b8-9ea5-f26f0ef3838a/actions/change-label' => [ + 'post' => 'Environments/renameEnvironment.json' + ], + '/environments/bfcc7ad1-f987-41b8-9ea5-f26f0ef3838a/production-mode/actions/disable' => [ + 'post' => 'Environments/disableProductionMode.json' + ], + '/environments/bfcc7ad1-f987-41b8-9ea5-f26f0ef3838a/production-mode/actions/enable' => [ + 'post' => 'Environments/disableProductionMode.json' ] - ]; diff --git a/tests/CloudApiTest.php b/tests/CloudApiTest.php index 80941c5..ed7cebf 100644 --- a/tests/CloudApiTest.php +++ b/tests/CloudApiTest.php @@ -15,13 +15,14 @@ class CloudApiTest extends CloudApi { + public function __construct(Config $config, Client $client) { - parent::__construct($config); $this->extraConfig = $config->get('extraconfig'); $this->acquia = $config->get('acquia'); $this->setClient($client); + parent::__construct($config); } public function getInstance() diff --git a/tests/Commands/AccountCommandTest.php b/tests/Commands/AccountCommandTest.php new file mode 100644 index 0000000..e1076cf --- /dev/null +++ b/tests/Commands/AccountCommandTest.php @@ -0,0 +1,37 @@ +execute($command); + $this->assertSame($expected, $actualResponse); + } + + public function accountProvider() + { + + $infoResponse = << Name: jane.doe +> Last login: 2017-03-29 05:07:54 +> Created at: 2017-03-29 05:07:54 +> Status: ✓ +> TFA: ✓ +INFO; + + return [ + [ + ['account'], + $infoResponse . PHP_EOL + ] + ]; + } +} diff --git a/tests/Commands/ApplicationCommandTest.php b/tests/Commands/ApplicationCommandTest.php index aae93e3..c552467 100644 --- a/tests/Commands/ApplicationCommandTest.php +++ b/tests/Commands/ApplicationCommandTest.php @@ -27,6 +27,24 @@ public function applicationProvider() +----------------------+--------------------------------------+--------------------+ TABLE; + // phpcs:disable Generic.Files.LineLength.TooLong + $applicationInfo = << 🔧 Git URL: qa10@svn-3.networkdev.ahserversdev.com:qa10.git +> 💻 indicates environment in livedev mode. +> 🔒 indicates environment in production mode. +TABLE; + // phpcs:enable + $getTags = <<
execute($command); + $this->assertSame($expected, $actualResponse); + } + + public function environmentProvider() + { + $getAllEnvironments = <<
Environment ID: 24-a47ac10b-58cc-4372-a567-0e02b2c3d470 ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ +| Role(s) | Name | FQDN | AMI | Region | IP | Memcache | Active | Primary | EIP | ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ +| web, db | ded-6 | ded-6.servers.acquia.com | c1.medium | us-west-1 | 10.0.0.1 | ✓ | ✓ | ✓ | | +| bal | bal-4 | bal-4.servers.acquia.com | m1.small | us-west-1 | 10.0.0.2 | | | ✓ | | ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ ++--------+-----+-------------+--------------+---------+------+---------------+ +| Branch | CDE | PHP Version | Memory Limit | OpCache | APCu | Sendmail Path | ++--------+-----+-------------+--------------+---------+------+---------------+ +| master | | 7.2 | 128 | 96 | 32 | | ++--------+-----+-------------+--------------+---------+------+---------------+ + + Production environment + +> Environment ID: 15-a47ac10b-58cc-4372-a567-0e02b2c3d470 +> 🔒 Production mode enabled. ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ +| Role(s) | Name | FQDN | AMI | Region | IP | Memcache | Active | Primary | EIP | ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ +| web, db | ded-6 | ded-6.servers.acquia.com | c1.medium | us-west-1 | 10.0.0.1 | ✓ | ✓ | ✓ | | +| bal | bal-4 | bal-4.servers.acquia.com | m1.small | us-west-1 | 10.0.0.2 | | | ✓ | | ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ ++-----------------+-----+-------------+--------------+---------+------+---------------+ +| Branch | CDE | PHP Version | Memory Limit | OpCache | APCu | Sendmail Path | ++-----------------+-----+-------------+--------------+---------+------+---------------+ +| tags/01-01-2015 | | | | | | | ++-----------------+-----+-------------+--------------+---------+------+---------------+ + + Stage environment + +> Environment ID: 32-a47ac10b-58cc-4372-a567-0e02b2c3d470 ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ +| Role(s) | Name | FQDN | AMI | Region | IP | Memcache | Active | Primary | EIP | ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ +| web, db | ded-6 | ded-6.servers.acquia.com | c1.medium | us-west-1 | 10.0.0.1 | ✓ | ✓ | ✓ | | +| bal | bal-4 | bal-4.servers.acquia.com | m1.small | us-west-1 | 10.0.0.2 | | | ✓ | | ++---------+-------+--------------------------+-----------+-----------+----------+----------+--------+---------+-----+ ++--------+-----+-------------+--------------+---------+------+---------------+ +| Branch | CDE | PHP Version | Memory Limit | OpCache | APCu | Sendmail Path | ++--------+-----+-------------+--------------+---------+------+---------------+ +| | | | | | | | ++--------+-----+-------------+--------------+---------+------+---------------+ +> Web servers not marked 'Active' are out of rotation. +> Load balancer servers not marked 'Active' are hot spares +> Database servers not marked 'Primary' are the passive master +TABLE; + + return [ + [ + ['environment:list', 'uuid'], + $getAllEnvironments . PHP_EOL + ], + [ + ['environment:info', 'uuid', 'environment'], + $getEnvironmentInfo . PHP_EOL + ], + [ + ['environment:rename', 'uuid', 'environment', 'name'], + '> Renaming Mock Env to name' . PHP_EOL + ], + [ + ['environment:delete', 'uuid', 'environment'], + '> Deleting Mock Env environment' . PHP_EOL + ] + ]; + } +} diff --git a/tests/Commands/LiveDevCommandTest.php b/tests/Commands/LiveDevCommandTest.php new file mode 100644 index 0000000..ae7f2e7 --- /dev/null +++ b/tests/Commands/LiveDevCommandTest.php @@ -0,0 +1,33 @@ +execute($command); + $this->assertSame($expected, $actualResponse); + } + + public function liveDevProvider() + { + + return [ + [ + ['livedev:enable', 'uuid', 'environment'], + '> Enabling livedev for Mock Env environment' . PHP_EOL + ], + [ + ['livedev:disable', 'uuid', 'environment'], + '> Disabling livedev for Mock Env environment' . PHP_EOL + ] + ]; + } +} diff --git a/tests/Commands/ProductionModeCommandTest.php b/tests/Commands/ProductionModeCommandTest.php new file mode 100644 index 0000000..572c4d8 --- /dev/null +++ b/tests/Commands/ProductionModeCommandTest.php @@ -0,0 +1,46 @@ +execute($command); + $this->assertSame($expected, $actualResponse); + } + + public function productionModeProvider() + { + + $infoResponse = << dev: ssh +> prod: ssh +> test: ssh +INFO; + return [ + [ + ['productionmode:enable', 'uuid', 'environment'], + ' [error] Production mode may only be enabled/disabled on the prod environment. ' . PHP_EOL + ], + [ + ['productionmode:disable', 'uuid', 'environment'], + ' [error] Production mode may only be enabled/disabled on the prod environment. ' . PHP_EOL + ], + [ + ['productionmode:enable', 'uuid', 'prod'], + '> Enabling production mode for Mock Env environment' . PHP_EOL + ], + [ + ['productionmode:disable', 'uuid', 'prod'], + '> Disabling production mode for Mock Env environment' . PHP_EOL + ] + ]; + } +}