From 8c49823e15dd20b490aa198f5da193053fcde79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Thu, 5 Jan 2017 21:22:38 -0300 Subject: [PATCH] [Nostromo] Improve Tests and Coverage --- .gitignore | 1 + CONTRIBUTING.md | 3 +- .../Command/BuiltIn/Releases/ListCommand.php | 12 ++-- .../BuiltIn/Releases/ListCommandTest.php | 69 +++++++++++++++++++ .../Resources/testhost-fail-get-current.yml | 14 ++++ .../Resources/testhost-fail-get-releases.yml | 14 ++++ .../Tests/Resources/testhost-no-hosts.yml | 12 ++++ .../Tests/Resources/testhost-no-releases.yml | 14 ++++ src/Mage/Tests/Runtime/ProcessMockup.php | 20 +++++- 9 files changed, 152 insertions(+), 7 deletions(-) create mode 100644 src/Mage/Tests/Resources/testhost-fail-get-current.yml create mode 100644 src/Mage/Tests/Resources/testhost-fail-get-releases.yml create mode 100644 src/Mage/Tests/Resources/testhost-no-hosts.yml create mode 100644 src/Mage/Tests/Resources/testhost-no-releases.yml diff --git a/.gitignore b/.gitignore index 57872d0..d5a5bd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +/build diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0ac02e2..81d5911 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -59,7 +59,8 @@ We use [PSR2](http://www.php-fig.org/psr/psr-2/) as PHP coding standard. ## Testing and quality We use PHPUnit to test our code. Most of the project is covered with tests, so if you want your code to be merged push it with proper testing and coverage (at least 95%). To execute the tests with code coverage report: ``` -vendor/bin/phpunit --coverage-text +vendor/bin/phpunit --coverage-clover build/logs/coverage.xml +vendor/bin/coveralls -v --coverage_clover build/logs/coverage.xml ``` Tests structure follow almost the same structure as production code with `Test` suffix in class and file name. Follow the tests already made as guidelines. diff --git a/src/Mage/Command/BuiltIn/Releases/ListCommand.php b/src/Mage/Command/BuiltIn/Releases/ListCommand.php index a1d15db..83d73cc 100644 --- a/src/Mage/Command/BuiltIn/Releases/ListCommand.php +++ b/src/Mage/Command/BuiltIn/Releases/ListCommand.php @@ -89,11 +89,15 @@ class ListCommand extends AbstractCommand /** @var Process $process */ $process = $this->runtime->runRemoteCommand($cmdListReleases, false); if (!$process->isSuccessful()) { - throw new RuntimeException(sprintf('Unable to retrieve releases from host %s', $host), 80); + throw new RuntimeException(sprintf('Unable to retrieve releases from host "%s"', $host), 80); } - $releases = explode(PHP_EOL, trim($process->getOutput())); - rsort($releases); + if (trim($process->getOutput()) != '') { + $releases = explode(PHP_EOL, trim($process->getOutput())); + rsort($releases); + } else { + $releases = []; + } if (count($releases) == 0) { $output->writeln(sprintf(' No releases available on host %s:', $host)); @@ -104,7 +108,7 @@ class ListCommand extends AbstractCommand /** @var Process $process */ $process = $this->runtime->runRemoteCommand($cmdCurrentRelease, false); if (!$process->isSuccessful()) { - throw new RuntimeException(sprintf('Unable to retrieve current release from host %s', $host), 85); + throw new RuntimeException(sprintf('Unable to retrieve current release from host "%s"', $host), 85); } $currentReleaseId = explode('/', trim($process->getOutput())); diff --git a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php index 5261fc9..2d21cc6 100644 --- a/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php +++ b/src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php @@ -12,6 +12,7 @@ namespace Mage\Tests\Command\BuiltIn\Releases; use Mage\Command\BuiltIn\Releases\ListCommand; use Mage\Runtime\Exception\DeploymentException; +use Mage\Runtime\Exception\RuntimeException; use Mage\Command\AbstractCommand; use Mage\Tests\MageApplicationMockup; use Symfony\Component\Console\Tester\CommandTester; @@ -82,4 +83,72 @@ class ListCommandTest extends TestCase $this->assertEquals('Releases are not enabled', $exception->getMessage()); } } + + public function testFailToGetCurrentRelease() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost-fail-get-current.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:list'); + $this->assertTrue($command instanceof ListCommand); + + $tester = new CommandTester($command); + + try { + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof RuntimeException); + $this->assertEquals('Unable to retrieve current release from host "host1"', $exception->getMessage()); + } + } + + public function testNoReleasesAvailable() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost-no-releases.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:list'); + $this->assertTrue($command instanceof ListCommand); + + $tester = new CommandTester($command); + + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertContains('No releases available on host host2', $tester->getDisplay()); + } + + public function testFailGetReleases() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost-fail-get-releases.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:list'); + $this->assertTrue($command instanceof ListCommand); + + $tester = new CommandTester($command); + + try { + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + } catch (Exception $exception) { + $this->assertTrue($exception instanceof RuntimeException); + $this->assertEquals('Unable to retrieve releases from host "host3"', $exception->getMessage()); + } + } + + public function testNoHosts() + { + $application = new MageApplicationMockup(); + $application->configure(__DIR__ . '/../../../Resources/testhost-no-hosts.yml'); + + /** @var AbstractCommand $command */ + $command = $application->find('releases:list'); + $this->assertTrue($command instanceof ListCommand); + + $tester = new CommandTester($command); + + $tester->execute(['command' => $command->getName(), 'environment' => 'test']); + $this->assertContains('No hosts defined', $tester->getDisplay()); + } } diff --git a/src/Mage/Tests/Resources/testhost-fail-get-current.yml b/src/Mage/Tests/Resources/testhost-fail-get-current.yml new file mode 100644 index 0000000..635b457 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-fail-get-current.yml @@ -0,0 +1,14 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - host1 diff --git a/src/Mage/Tests/Resources/testhost-fail-get-releases.yml b/src/Mage/Tests/Resources/testhost-fail-get-releases.yml new file mode 100644 index 0000000..b9c4e88 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-fail-get-releases.yml @@ -0,0 +1,14 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - host3 diff --git a/src/Mage/Tests/Resources/testhost-no-hosts.yml b/src/Mage/Tests/Resources/testhost-no-hosts.yml new file mode 100644 index 0000000..a6c4a5a --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-no-hosts.yml @@ -0,0 +1,12 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php diff --git a/src/Mage/Tests/Resources/testhost-no-releases.yml b/src/Mage/Tests/Resources/testhost-no-releases.yml new file mode 100644 index 0000000..88f9da0 --- /dev/null +++ b/src/Mage/Tests/Resources/testhost-no-releases.yml @@ -0,0 +1,14 @@ +magephp: + log_dir: /tmp + environments: + test: + user: tester + branch: test + host_path: /var/www/test + releases: 4 + exclude: + - ./var/cache/* + - ./var/log/* + - ./web/app_dev.php + hosts: + - host2 diff --git a/src/Mage/Tests/Runtime/ProcessMockup.php b/src/Mage/Tests/Runtime/ProcessMockup.php index 5e10a0c..0ff7df6 100644 --- a/src/Mage/Tests/Runtime/ProcessMockup.php +++ b/src/Mage/Tests/Runtime/ProcessMockup.php @@ -16,6 +16,7 @@ class ProcessMockup extends Process { protected $commandline; protected $timeout; + protected $success = true; public function __construct($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array()) { @@ -29,11 +30,18 @@ class ProcessMockup extends Process public function run($callback = null) { + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host1 sh -c \"readlink -f /var/www/test/current\"') { + $this->success = false; + } + + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host3 sh -c \"ls -1 /var/www/test/releases\"') { + $this->success = false; + } } public function isSuccessful() { - return true; + return $this->success; } public function getErrorOutput() @@ -52,7 +60,15 @@ class ProcessMockup extends Process } if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost sh -c \"readlink -f /var/www/test/current\"') { - return '/var/www/test/releases/20170101015120'; + return '/var/www/test/releases/20170101015117'; + } + + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host1 sh -c \"ls -1 /var/www/test/releases\"') { + return implode(PHP_EOL, ['20170101015110', '20170101015111', '20170101015112', '20170101015113', '20170101015114', '20170101015115', '20170101015116', '20170101015117']); + } + + if ($this->commandline == 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@host2 sh -c \"ls -1 /var/www/test/releases\"') { + return ''; } return '';