Skip to content

Commit

Permalink
simplified testExecWithFailingScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Mudi Ugbowanko committed Jun 1, 2014
1 parent c0e8c34 commit b9b0532
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 49 deletions.
8 changes: 5 additions & 3 deletions src/Util/InstanceAccess/SSHAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand All @@ -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;
Expand Down
53 changes: 7 additions & 46 deletions test/src/Unit/Util/SSHAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
}
}

0 comments on commit b9b0532

Please sign in to comment.