Browse Source

[Nostromo] Improve Tests and Coverage

pull/1/head
Andrés Montañez 8 years ago
parent
commit
8c49823e15
  1. 1
      .gitignore
  2. 3
      CONTRIBUTING.md
  3. 8
      src/Mage/Command/BuiltIn/Releases/ListCommand.php
  4. 69
      src/Mage/Tests/Command/BuiltIn/Releases/ListCommandTest.php
  5. 14
      src/Mage/Tests/Resources/testhost-fail-get-current.yml
  6. 14
      src/Mage/Tests/Resources/testhost-fail-get-releases.yml
  7. 12
      src/Mage/Tests/Resources/testhost-no-hosts.yml
  8. 14
      src/Mage/Tests/Resources/testhost-no-releases.yml
  9. 20
      src/Mage/Tests/Runtime/ProcessMockup.php

1
.gitignore vendored

@ -1 +1,2 @@
/vendor/
/build

3
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.

8
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);
}
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 <fg=black;options=bold>%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()));

69
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());
}
}

14
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

14
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

12
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

14
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

20
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 '';

Loading…
Cancel
Save