Skip to content

Commit

Permalink
[Debugger] Add Daemon options to Agent (#1016)
Browse files Browse the repository at this point in the history
* Add passthrough options for setting up the Daemon process when starting the Agent

* Adding test for setting debugger daemon options via the agent
  • Loading branch information
chingor13 authored and dwsupplee committed Apr 16, 2018
1 parent 85ed132 commit 008d87a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
19 changes: 11 additions & 8 deletions Debugger/src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,23 @@ class Agent
* **Defaults to** the directory of the calling file.
* @type LoggerInterface $logger PSR-3 compliant logger used to write
* logpoint records. **Defaults to** a new Stackdriver logger.
* @type array $daemonOptions Additional options to provide to the
* Daemon when registering.
* }
*/
public function __construct(array $options = [])
{
$storage = isset($options['storage'])
? $options['storage']
: $this->defaultStorage();

$this->sourceRoot = isset($options['sourceRoot'])
? $options['sourceRoot']
: dirname(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file']);
$options += [
'daemonOptions' => [],
'storage' => null,
'sourceRoot' => null
];
$storage = $options['storage'] ?: $this->defaultStorage();
$this->sourceRoot = $options['sourceRoot']
?: dirname(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file']);

if ($this->shouldStartDaemon()) {
$daemon = new Daemon([
$daemon = new Daemon($options['daemonOptions'] + [
'sourceRoot' => $this->sourceRoot,
'storage' => $storage,
'register' => true
Expand Down
49 changes: 43 additions & 6 deletions Debugger/tests/Unit/AgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

namespace Google\Cloud\Debugger\Tests\Unit;

use Google\Cloud\Core\Batch\ConfigStorageInterface;
use Google\Cloud\Core\Batch\JobConfig;
use Google\Cloud\Debugger\Agent;
use Google\Cloud\Debugger\Debuggee;
use Google\Cloud\Debugger\BreakpointStorage\BreakpointStorageInterface;
use Psr\Log\LoggerInterface;
use Prophecy\Argument;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -29,9 +32,13 @@
class AgentTest extends TestCase
{
private $storage;
private $oldDaemonEnv;

public function setUp()
{
parent::setUp();
$this->oldDaemonEnv = getenv('IS_BATCH_DAEMON_RUNNING');

if (PHP_MAJOR_VERSION < 7) {
$this->markTestSkipped('Can only run the Agent on PHP 7+');
}
Expand All @@ -43,31 +50,61 @@ public function setUp()
$this->storage = $this->prophesize(BreakpointStorageInterface::class);
}

public function tearDown()
{
if ($this->oldDaemonEnv === false) {
putenv('IS_BATCH_DAEMON_RUNNING');
} else {
putenv('IS_BATCH_DAEMON_RUNNING=' . $this->oldDaemonEnv);
}
parent::tearDown();
}

public function testSpecifyStorage()
{
$this->storage->load()->willReturn('debuggeeId', []);
$this->storage->load()->willReturn(['debuggeeId', []])->shouldBeCalled();
$agent = new Agent(['storage' => $this->storage->reveal()]);
}

public function testSpecifyLogger()
{
$this->storage->load()->willReturn('debuggeeId', []);
$this->storage->load()->willReturn(['debuggeeId', []])->shouldBeCalled();
$logger = $this->prophesize(LoggerInterface::class);
$logger->log('INFO', 'LOGPOINT: message', ['context' => 'value'])->shouldBeCalled();

$agent = new Agent([
'storage' => $this->storage->reveal(),
'logger' => $logger->reveal()
]);
$agent->handleLogpoint('INFO', 'message', ['context' => 'value']);
}

public function testSpecifyDebuggee()
/**
* @group focus
*/
public function testDaemonOptions()
{
$this->storage->load()->willReturn('debuggeeId', []);
$debuggee = $this->prophesize(Debuggee::class);
putenv('IS_BATCH_DAEMON_RUNNING=true');
$this->storage->load()->willReturn('debuggeeId', [])->shouldBeCalled();
$configStorage = $this->prophesize(ConfigStorageInterface::class);
$configStorage->lock()->willReturn(true)->shouldBeCalled();

$jobConfig = $this->prophesize(JobConfig::class);
$jobConfig->registerJob(
Argument::any(),
Argument::any()
);
$config = $jobConfig->reveal();
$configStorage->load()->willReturn($config)->shouldBeCalled();
$configStorage->unlock()->shouldBeCalled();
$configStorage->save($config)->shouldBeCalled();

$agent = new Agent([
'storage' => $this->storage->reveal(),
'debuggee' => $debuggee->reveal()
'daemonOptions' => [
'uniquifier' => 'some-value',
'configStorage' => $configStorage->reveal()
]
]);
}
}

0 comments on commit 008d87a

Please sign in to comment.