Differential D11692 Diff 28134 src/applications/harbormaster/step/HarbormasterTestBuildStepImplementation.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/harbormaster/step/HarbormasterTestBuildStepImplementation.php
- This file was added.
<?php | |||||
final class HarbormasterTestBuildStepImplementation | |||||
extends HarbormasterBuildStepImplementation { | |||||
public function getName() { | |||||
return pht('Run Tests'); | |||||
} | |||||
public function getGenericDescription() { | |||||
return pht('Run \'arc unit\' on Drydock host.'); | |||||
} | |||||
public function getDescription() { | |||||
return pht( | |||||
'Run \'arc unit\' on host %s.', | |||||
$this->formatSettingForDescription('hostartifact')); | |||||
} | |||||
public function execute( | |||||
HarbormasterBuild $build, | |||||
HarbormasterBuildTarget $build_target) { | |||||
$settings = $this->getSettings(); | |||||
$variables = $build_target->getVariables(); | |||||
$artifact = $build->loadArtifact($settings['hostartifact']); | |||||
$lease = $artifact->loadDrydockLease(); | |||||
$interface = $lease->getInterface('command'); | |||||
$future = $interface->getExecFuture( | |||||
'cd %s && arc unit --output json-realtime --everything', | |||||
$settings['directory']); | |||||
$log_stderr = $build->createLog($build_target, 'remote', 'stderr'); | |||||
$start_stderr = $log_stderr->start(); | |||||
$build_update = 5; | |||||
$stdout_buffer = ''; | |||||
$err = 0; | |||||
$futures = new FutureIterator(array($future)); | |||||
foreach ($futures->setUpdateInterval(1) as $key => $future_iter) { | |||||
if ($future_iter !== null) { | |||||
list($err) = $future->resolve(); | |||||
} else { | |||||
// Check to see if we should abort. | |||||
if ($build_update <= 0) { | |||||
$build->reload(); | |||||
if ($this->shouldAbort($build, $build_target)) { | |||||
$future->resolveKill(); | |||||
throw new HarbormasterBuildAbortedException(); | |||||
} else { | |||||
$build_update = 5; | |||||
} | |||||
} else { | |||||
$build_update -= 1; | |||||
} | |||||
} | |||||
list($stdout, $stderr) = $future->read(); | |||||
$stdout_buffer .= $stdout; | |||||
$log_stderr->append($stderr); | |||||
$future->discardBuffers(); | |||||
$lines = phutil_split_lines($stdout_buffer); | |||||
if (strlen($stdout_buffer) > 0 && | |||||
$stdout_buffer[strlen($stdout_buffer) - 1] !== "\n") { | |||||
$stdout_buffer = $lines[count($lines) - 1]; | |||||
array_pop($lines); | |||||
} else { | |||||
$stdout_buffer = ''; | |||||
} | |||||
foreach ($lines as $line) { | |||||
if (strlen($line) > strlen('##arc-unit## ') && | |||||
substr($line, 0, strlen('##arc-unit## ')) === '##arc-unit## ') { | |||||
// We have output from 'arc unit'. | |||||
$data = phutil_json_decode(substr($line, strlen('##arc-unit## '))); | |||||
$name = idx($data, 'name'); | |||||
if (idx($data, 'namespace') !== null) { | |||||
$name = idx($data, 'namespace').'::'.$name; | |||||
} | |||||
$item = HarbormasterBuildItem::initializeNewBuildItem( | |||||
$build_target, | |||||
HarbormasterBuildItem::TYPE_UNIT); | |||||
$item->setName($name); | |||||
$item->setDetails($data); | |||||
$item->save(); | |||||
} | |||||
} | |||||
if ($future_iter !== null) { | |||||
break; | |||||
} | |||||
} | |||||
$log_stderr->finalize($start_stderr); | |||||
if ($err) { | |||||
throw new HarbormasterBuildFailureException(); | |||||
} | |||||
} | |||||
public function getArtifactInputs() { | |||||
return array( | |||||
array( | |||||
'name' => pht('Run on Host'), | |||||
'key' => $this->getSetting('hostartifact'), | |||||
'type' => HarbormasterBuildArtifact::TYPE_HOST, | |||||
), | |||||
); | |||||
} | |||||
public function getFieldSpecifications() { | |||||
return array( | |||||
'directory' => array( | |||||
'name' => pht('Directory'), | |||||
'type' => 'text', | |||||
'required' => true, | |||||
'caption' => pht( | |||||
'The relative directory to run \'arc unit\' in.'), | |||||
), | |||||
'hostartifact' => array( | |||||
'name' => pht('Host'), | |||||
'type' => 'text', | |||||
'required' => true, | |||||
), | |||||
); | |||||
} | |||||
} |