Skip to content

Commit

Permalink
Implemented tests and fixed default timeout value to not soo huge (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalabka committed Aug 20, 2015
1 parent 9f3a804 commit dc1d48e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.idea/
/nbproject/
*.phar
/.vagrant
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ matrix:
- php: hhvm

install:
- eval `ssh-agent -s`
- test/travis/setup-secure-shell.sh
- composer self-update
- composer install --no-interaction --prefer-source

Expand Down
2 changes: 1 addition & 1 deletion src/Server/Remote/PhpSecLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(Configuration $configuration)
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:
Expand Down
11 changes: 9 additions & 2 deletions test/recipe/Helper/RecipeTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

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
Expand Down Expand Up @@ -51,14 +53,19 @@ public function setUp()
$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()
{
Expand Down
93 changes: 93 additions & 0 deletions test/recipe/RemoteServerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/* (c) Dmitry Balabka <dmitry.balabka@gmail.com>
*
* 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
{
/**
* @var \Deployer\Type\Result
*/
private $result;

public static function setUpBeforeClass()
{
// Prepare FS
self::$deployPath = __DIR__ . '/../../localhost';
self::cleanUp();
mkdir(self::$deployPath);
self::$deployPath = realpath(self::$deployPath);
}

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()
{
$this->exec('deploy:ssh_test', ['stage' => 'remote_auth_by_password']);
$this->assertRegExp('#^127\.0\.0\.1#', $this->result->getOutput());
}

public function testAuthByIdentityFile()
{
$this->exec('deploy:ssh_test', ['stage' => 'remote_auth_by_identity_file']);
$this->assertRegExp('#^127\.0\.0\.1#', $this->result->getOutput());
}

public function testAuthByPemFile()
{
$this->markTestIncomplete('Will be implemented later');
$this->exec('deploy:ssh_test', ['stage' => 'remote_auth_by_pem_file']);
$this->assertRegExp('#^127\.0\.0\.1#', $this->result->getOutput());
}

public function testAuthByAgent()
{
$this->exec('deploy:agent_test', ['stage' => 'remote_auth_by_agent']);
$this->assertRegExp('#^127\.0\.0\.1#', $this->result->getOutput());
}

public function testTimeout()
{
$this->exec('deploy:timeout_test', ['stage' => 'remote_auth_by_agent']);
$this->assertRegExp('#^127\.0\.0\.1#', $this->result->getOutput());
}
}
27 changes: 27 additions & 0 deletions test/travis/setup-secure-shell.sh
Original file line number Diff line number Diff line change
@@ -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
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
mkdir -p "/home/$DEPLOYER_USERNAME/.ssh/"
cat "$HOME/.ssh/id_rsa.pub" >> "/home/$DEPLOYER_USERNAME/.ssh/authorized_keys"
ssh-keyscan -t rsa localhost > "/tmp/known_hosts"
cp "/tmp/known_hosts" "/home/$DEPLOYER_USERNAME/.ssh/known_hosts"
chown "$DEPLOYER_USERNAME:$DEPLOYER_USERNAME" "/home/$DEPLOYER_USERNAME/.ssh/" -R

0 comments on commit dc1d48e

Please sign in to comment.