From 0cdc7382827211a529083d320748dd9df9a5c04d Mon Sep 17 00:00:00 2001 From: Dmitry Balabka Date: Thu, 20 Aug 2015 23:19:04 +0300 Subject: [PATCH] Implemented tests and fixed default timeout value to not soo huge (fixes #429) --- .gitignore | 1 + .travis.yml | 6 +- src/Server/Remote/PhpSecLib.php | 2 +- test/recipe/Helper/RecipeTester.php | 11 ++- test/recipe/RemoteServerTest.php | 101 ++++++++++++++++++++++++++++ test/travis/setup-secure-shell.sh | 27 ++++++++ 6 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 test/recipe/RemoteServerTest.php create mode 100755 test/travis/setup-secure-shell.sh diff --git a/.gitignore b/.gitignore index 1f098e26..7095e5ba 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /.idea/ /nbproject/ *.phar +/.vagrant diff --git a/.travis.yml b/.travis.yml index a8d956ac..38bd260b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: php +sudo: required + php: - 5.4 - 5.5 @@ -13,10 +15,10 @@ matrix: - php: hhvm install: + - eval `ssh-agent -s` + - test/travis/setup-secure-shell.sh - composer self-update - composer install --no-interaction --prefer-source script: - phpunit - -sudo: false diff --git a/src/Server/Remote/PhpSecLib.php b/src/Server/Remote/PhpSecLib.php index 7182d92f..735da9cc 100644 --- a/src/Server/Remote/PhpSecLib.php +++ b/src/Server/Remote/PhpSecLib.php @@ -46,7 +46,7 @@ class PhpSecLib implements ServerInterface public function connect() { $serverConfig = $this->getConfiguration(); - $this->sftp = new SFTP($serverConfig->getHost(), $serverConfig->getPort(), PHP_INT_MAX); + $this->sftp = new SFTP($serverConfig->getHost(), $serverConfig->getPort(), 3600); switch ($serverConfig->getAuthenticationMethod()) { case Configuration::AUTH_BY_PASSWORD: diff --git a/test/recipe/Helper/RecipeTester.php b/test/recipe/Helper/RecipeTester.php index ec686aa5..77078dd7 100644 --- a/test/recipe/Helper/RecipeTester.php +++ b/test/recipe/Helper/RecipeTester.php @@ -9,6 +9,8 @@ namespace Deployer\Helper; use Deployer\Console\Application; use Deployer\Deployer; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\ApplicationTester; abstract class RecipeTester extends \PHPUnit_Framework_TestCase @@ -51,14 +53,19 @@ abstract class RecipeTester extends \PHPUnit_Framework_TestCase $this->deployer = new Deployer($console, $input, $output); // Load recipe - localServer('localhost') - ->env('deploy_path', self::$deployPath); + $this->setUpServer(); $this->loadRecipe(); // Init Deployer $this->deployer->addConsoleCommands(); } + protected function setUpServer() + { + localServer('localhost') + ->env('deploy_path', self::$deployPath); + } + public static function tearDownAfterClass() { diff --git a/test/recipe/RemoteServerTest.php b/test/recipe/RemoteServerTest.php new file mode 100644 index 00000000..029ac172 --- /dev/null +++ b/test/recipe/RemoteServerTest.php @@ -0,0 +1,101 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use \Deployer\Helper\RecipeTester; + +class RemoteServerTest extends RecipeTester +{ + const IP_REG_EXP = '#^::1#'; + + /** + * @var \Deployer\Type\Result + */ + private $result; + + protected function setUpServer() + { + $username = getenv('DEPLOYER_USERNAME') ?: 'deployer'; + $password = getenv('DEPLOYER_PASSWORD') ?: 'deployer_password'; + server('remote_auth_by_password', 'localhost', 22) + ->env('deploy_path', self::$deployPath) + ->user($username) + ->password($password); + server('remote_auth_by_identity_file', 'localhost', 22) + ->env('deploy_path', self::$deployPath) + ->user($username) + ->identityFile(); + server('remote_auth_by_pem_file', 'localhost', 22) + ->env('deploy_path', self::$deployPath) + ->user($username) + ->pemFile('~/.ssh/id_rsa.pem'); + server('remote_auth_by_agent', 'localhost', 22) + ->env('deploy_path', self::$deployPath) + ->user($username) + ->forwardAgent(); + } + + protected function loadRecipe() + { + require __DIR__ . '/../../recipe/common.php'; + + task('deploy:timeout_test', function () { + $this->result = run('sleep 11 && echo $SSH_CLIENT'); + }); + task('deploy:ssh_test', function () { + $this->result = run('echo $SSH_CLIENT'); + }); + task('deploy:agent_test', function () { + $this->result = run('ssh -T deployer@localhost \'echo $SSH_CLIENT\''); + }); + } + + public function testAuthByPassword() + { + if (false !== getenv('SCRUTINIZER')) { + $this->markTestSkipped('Test skipped on scrutinizer environment'); + } + $this->exec('deploy:ssh_test', ['stage' => 'remote_auth_by_password']); + $this->assertRegExp(self::IP_REG_EXP, $this->result->getOutput()); + } + + public function testAuthByIdentityFile() + { + if (false !== getenv('SCRUTINIZER')) { + $this->markTestSkipped('Test skipped on scrutinizer environment'); + } + $this->exec('deploy:ssh_test', ['stage' => 'remote_auth_by_identity_file']); + $this->assertRegExp(self::IP_REG_EXP, $this->result->getOutput()); + } + + public function testAuthByPemFile() + { + if (false !== getenv('SCRUTINIZER')) { + $this->markTestSkipped('Test skipped on scrutinizer environment'); + } + $this->markTestIncomplete('Will be implemented later'); + $this->exec('deploy:ssh_test', ['stage' => 'remote_auth_by_pem_file']); + $this->assertRegExp(self::IP_REG_EXP, $this->result->getOutput()); + } + + public function testAuthByAgent() + { + if (false !== getenv('SCRUTINIZER')) { + $this->markTestSkipped('Test skipped on scrutinizer environment'); + } + $this->exec('deploy:agent_test', ['stage' => 'remote_auth_by_agent']); + $this->assertRegExp(self::IP_REG_EXP, $this->result->getOutput()); + } + + public function testTimeout() + { + if (false !== getenv('SCRUTINIZER')) { + $this->markTestSkipped('Test skipped on scrutinizer environment'); + } + $this->exec('deploy:timeout_test', ['stage' => 'remote_auth_by_agent']); + $this->assertRegExp(self::IP_REG_EXP, $this->result->getOutput()); + } +} diff --git a/test/travis/setup-secure-shell.sh b/test/travis/setup-secure-shell.sh new file mode 100755 index 00000000..d6a891e2 --- /dev/null +++ b/test/travis/setup-secure-shell.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -e +set -x + +export DEPLOYER_USERNAME='deployer' +export DEPLOYER_PASSWORD='deployer_password' + +# Create deployer user and home directory +sudo useradd --create-home --base-dir /home "$DEPLOYER_USERNAME" + +# Set deployer user password +echo "$DEPLOYER_USERNAME:$DEPLOYER_PASSWORD" | sudo chpasswd + +# Create a 1024 bit RSA SSH key pair without passphrase for the travis user +ssh-keygen -t rsa -b 1024 -f "$HOME/.ssh/id_rsa" -q -N "" +ssh-keygen -f "$HOME/.ssh/id_rsa.pub" -e -m pem > "$HOME/.ssh/id_rsa.pem" + +# Add the generated private key to SSH agent of travis user +ssh-add "$HOME/.ssh/id_rsa" + +# Allow the private key of the travis user to log in as deployer user +sudo mkdir -p "/home/$DEPLOYER_USERNAME/.ssh/" +sudo cp "$HOME/.ssh/id_rsa.pub" "/home/$DEPLOYER_USERNAME/.ssh/authorized_keys" +sudo ssh-keyscan -t rsa localhost > "/tmp/known_hosts" +sudo cp "/tmp/known_hosts" "/home/$DEPLOYER_USERNAME/.ssh/known_hosts" +sudo chown "$DEPLOYER_USERNAME:$DEPLOYER_USERNAME" "/home/$DEPLOYER_USERNAME/.ssh/" -R