From b9b0532b70cd500f08a0353c4d86f419e2738e97 Mon Sep 17 00:00:00 2001 From: Mudi Ugbowanko Date: Sun, 1 Jun 2014 17:10:29 +0100 Subject: [PATCH] simplified testExecWithFailingScript --- src/Util/InstanceAccess/SSHAccess.php | 8 ++-- test/src/Unit/Util/SSHAccessTest.php | 53 ++++----------------------- 2 files changed, 12 insertions(+), 49 deletions(-) diff --git a/src/Util/InstanceAccess/SSHAccess.php b/src/Util/InstanceAccess/SSHAccess.php index 8b089b9..b4d6993 100644 --- a/src/Util/InstanceAccess/SSHAccess.php +++ b/src/Util/InstanceAccess/SSHAccess.php @@ -68,7 +68,7 @@ public function exec($code, \Closure $callback = null) { $sftp = new SFTP($this->host, 22); $sftp->login($this->get('user'), $this->getPassword()); - $this->info('Executing code ...'); + $this->info(sprintf('Executing code `%s ...', substr($code, 0, 100))); $sftp->put('/tmp/execute.sh', $code); $sftp->chmod(0550, '/tmp/execute.sh'); $this->conn->exec('/tmp/execute.sh', function($output) { @@ -78,8 +78,8 @@ public function exec($code, \Closure $callback = null) { $exitCode = $this->conn->getExitStatus(); if($exitCode !== 0) { - $this->critical('Erronous code detected', ['script' => $code]); - throw new \RuntimeException('Script exit code was not 0!', $exitCode); + $this->critical('Erronous code detected', ['script' => $code, 'code' => $exitCode]); + throw new \RuntimeException(sprintf('Script exit code was %s', $exitCode), $exitCode); } } @@ -93,9 +93,11 @@ public function get($path, $default = null, $deep = false) { public function getPassword() { if(!($password = $this->get('password'))) { + $this->info('No ssh password found. Lets try with a private key (if it exits!) ...'); $key = $this->get('key'); $password = new \Crypt_RSA(); $password->loadKey($key); + $this->credentials->set('password', $password); } return $password; diff --git a/test/src/Unit/Util/SSHAccessTest.php b/test/src/Unit/Util/SSHAccessTest.php index 8c3e6fe..1f58e7b 100644 --- a/test/src/Unit/Util/SSHAccessTest.php +++ b/test/src/Unit/Util/SSHAccessTest.php @@ -130,7 +130,7 @@ public function testExecWithSSHKey() { $this->patchClassMethod('Crypt_RSA::loadKey', function($key) { $this->assertEquals('--key--123456--key--', $key); - }, 2); + }, 1); $this->patchClassMethod('App\Util\Net\SFTP::login', function($user, $password) { $this->assertEquals('root', $user); @@ -154,55 +154,16 @@ public function testExecWithFailingScript() { $this->patchClassMethod('App\Util\Net\SSH2::disconnect'); $this->patchClassMethod('App\Util\Net\SSH2::Net_SSH2'); $this->patchClassMethod('App\Util\Net\SSH2::login'); + $this->patchClassMethod('App\Util\Net\SFTP::login'); + $this->patchClassMethod('App\Util\Net\SFTP::Net_SFTP'); + $this->patchClassMethod('App\Util\Net\SFTP::put'); + $this->patchClassMethod('App\Util\Net\SFTP::chmod'); + $this->patchClassMethod('App\Util\Net\SSH2::exec'); $this->patchClassMethod('App\Util\Net\SSH2::getExitStatus', 1, 1); - - $mockHost = 'test.somewhere.com'; - $constructorCalled = false; - $this->patchClassMethod('App\Util\Net\SFTP::Net_SFTP', function($host, $port, $timeout) use (&$constructorCalled, $mockHost){ - $this->assertEquals($mockHost, $host); - $this->assertEquals(22, $port); - $constructorCalled = true; - }); - - $this->patchClassMethod('App\Util\Net\SFTP::login', function($user, $password) use (&$expectedMaxAttempts, &$constructorCalled){ - $this->assertTrue($constructorCalled); - $this->assertEquals('root', $user); - $this->assertEquals('test', $password); - }, 1); - - $this->patchClassMethod('App\Util\Net\SFTP::put', function($remotePath, $code) { - $this->assertEquals('/tmp/execute.sh', $remotePath); - $this->assertEquals("#!/bin/bash\ndate", $code); - }, 1); - - $this->patchClassMethod('App\Util\Net\SFTP::chmod', function($premissions, $remotePath) { - $this->assertEquals('/tmp/execute.sh', $remotePath); - $this->assertEquals(0550, $premissions); - }, 1); - - $isLogged = false; - $this->patchClassMethod('App\Util\Net\SSH2::exec', function($command, $cb) use (&$isLogged){ - $this->assertEquals('/tmp/execute.sh', $command); - $this->assertInstanceOf('Closure', $cb); - $isLogged = true; - $cb('test-output'); - $isLogged = false; - }, 1); - - $this->patchClassMethod('App\Util\InstanceAccess\SSHAccess::info', function($message) use ($mockHost, &$isLogged){ - if($isLogged) { - $this->assertEquals("[$mockHost] test-output", $message); - } - }); - $access = new SSHAccess(); - $access->setCredentials([ - 'user' => 'root', - 'password' => 'test' - ]); - $access->connect($mockHost); + $access->connect('test.somewhere.com'); $access->exec("#!/bin/bash\ndate"); } }