/** * @test */ public function canCommandCollectionManageCommands() { $collection = new CommandCollection(); $collection->add(new Command('cd', ['/home/root'])); $collection->add(new Command('unlink', ['install.log'])); $compiled = $collection->getCompiled(); $this->assertEquals('cd /home/root; unlink install.log;', $compiled); }
/** * @test */ public function canOutputReadableScripts() { $a = new CommandCollection(); $a->add(new Command('yum update', ['-y'])); $a->add(new Command('service nginx restart')); $b = new CommandCollection(); $b->add(new Command('yum install', ['sl'])); $batch = new Batch(); $batch->addCommandCollection($a); $batch->addCommandCollection($b); $compiler = new ScriptCompiler(); $response = $compiler->compile($batch); $expect = implode(PHP_EOL, [ScriptCompiler::COMPILE_HEADER, '', 'yum update -y;', 'service nginx restart;', '', 'yum install sl;', '']); $this->assertEquals($expect, $response, 'Compiled script is not formatted as expected'); }
/** * @test */ public function canBatchAssertGivenResponse() { $collection = new CommandCollection(); $collection->add(new Command('sudo apt-get update')); $batch = new Batch(); $batch->addCommandCollection($collection); $batch->addAssertion(new Regex('/Done$/')); $response = ['Get:18 http://archive.ubuntu.com trusty-security/main amd64 Packages [388 kB]', 'Get:19 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14.8 kB]', 'Get:20 http://archive.ubuntu.com trusty-security/universe amd64 Packages [143 kB]', 'Get:21 http://archive.ubuntu.com trusty-security/main i386 Packages [370 kB]', 'Get:22 http://archive.ubuntu.com trusty-security/restricted i386 Packages [14.8 kB]', 'Get:23 http://archive.ubuntu.com trusty-security/universe i386 Packages [143 kB]', 'Hit http://downloads-distro.mongodb.org dist/10gen i386 Packages', 'Hit http://linux.dropbox.com precise/main amd64 Packages', 'Get:24 http://toolbelt.heroku.com ./ Release [1609 B]', 'Hit http://linux.dropbox.com precise/main i386 Packages', 'Get:25 http://toolbelt.heroku.com ./ Packages [1061 B]', 'Fetched 4010 kB in 3s (1004 kb/s)', 'Reading package lists... Done']; $batch->setProcessResponse(implode(PHP_EOL, $response)); $this->assertTrue($batch->isValid(), 'Batch should have been validated on new response data given'); $batch->addAssertion(new Regex('/ubuntu\\.com/')); $batch->addAssertion(new Assertion('Failed')); $this->assertTrue($batch->isValid(), 'Batch should still be valid after new assertions are added'); $batch->validate(); $this->assertFalse($batch->isValid(), 'Batch should not be invalid as validation is ran again'); }
/** * @test */ public function canConnectionHandleProcessErrors() { $collection = new CommandCollection(); $collection->add(new Command('ls -l')); $batch = new Batch(); $batch->addCommandCollection($collection); $connection = new Connection(); $connection->addBatch($batch); $authentication = new PrivateKeyAuthentication('user', 'host', '/path/to/key'); $connection->setAuthentication($authentication); $builder = $this->getMockBuilder(Process::CLASS); $builder->disableOriginalConstructor(); $builder->setMethods(['run', 'getOutput', 'isSuccessful']); /** @var Process $process */ $process = $builder->getMock(); $process->expects($this->once())->method('run')->will($this->throwException(new LogicException())); $successful = $connection->execute($process); $this->assertFalse($successful, 'Process errors should result in a false response'); }