/** * get an SSH client to contact this host * * @param \DataSift\Storyplayer\PlayerLib\StoryTeller $st * our ubiquitous state object * @param \DataSift\Storyplayer\HostLib\HostDetails $hostDetails * details about the host that you want a client for * @return \DataSift\Storyplayer\CommandLib\SshClient * the SSH client for you to use */ public function getClient($st, $hostDetails) { // shorthand $hostId = $hostDetails->hostId; // do we already have a client? if (isset($this->sshClients[$hostId])) { // yes - reuse it return $this->sshClients[$hostId]; } // if we get here, we need to make a new client $sshClient = new SshClient($st, $hostDetails->sshOptions, $hostDetails->scpOptions); $sshClient->setIpAddress($hostDetails->ipAddress); $sshClient->setSshUsername($hostDetails->sshUsername); if (isset($hostDetails->sshKey)) { $sshClient->setSshKey($hostDetails->sshKey); } // all done $this->sshClients[$hostId] = $sshClient; return $sshClient; }
/** * @covers DataSift\Storyplayer\CommandLib\SshClient::runCommand */ public function testCanRunCommands() { // ---------------------------------------------------------------- // setup your test // the SSH username we are going to use $username = '******'; // the ipAddress we are going to use $ipAddress = '10.0.45.254'; // the command we are going to run $command = "ls /tmp"; // the full SSH command should be ... $fullCommand = 'ssh -o StrictHostKeyChecking=no' . ' -n ' . $username . '@' . $ipAddress . ' "' . $command . '"'; // the CommandResult we want for this test $expectedReturnCode = 0; $expectedOutput = 'success!'; $cmdResult = new CommandResult($expectedReturnCode, $expectedOutput); // our mocked $st $st = Mockery::mock("DataSift\\Storyplayer\\PlayerLib\\StoryTeller"); // our fake CommandRunner $cmdRunner = Mockery::mock("DataSift\\Storyplayer\\CommandLib\\CommandRunner"); $cmdRunner->shouldReceive('runSilently')->once()->with($st, $fullCommand)->andReturn($cmdResult); // our fake Output for logging purposes $output = Mockery::mock("DataSift\\Storyplayer\\Output"); $output->shouldReceive('logPhaseActivity'); // our fake injectables container $injectables = new Injectables(); $injectables->dataFormatter = new DataFormatter(); $injectables->dataFormatter->setIsVerbose(true); $injectables->output = $output; // our mocked $st needs to do things too // // we have to put this down here, because we need to pass $st into // our mocked CommandRunner $st->shouldReceive('startAction')->once()->andReturn(new Action_LogItem($injectables, 1)); $st->shouldReceive('getNewCommandRunner')->andReturn($cmdRunner); // our test subject $obj = new SshClient($st); // the things we need to set for the command to be attempted $obj->setSshUsername($username); $obj->setIpAddress($ipAddress); // ---------------------------------------------------------------- // perform the change $result = $obj->runCommand($command); // ---------------------------------------------------------------- // test the results $this->assertTrue($result instanceof CommandResult); $this->assertEquals($expectedReturnCode, $result->returnCode); $this->assertEquals($expectedOutput, $result->output); // all done Mockery::close(); }