Skip to content

Commit c73b287

Browse files
committed
Merge pull request #35 from clue-labs/execstart
Fix execStart() to resolve with buffered string contents
2 parents 4c2b232 + 806e55b commit c73b287

File tree

4 files changed

+120
-11
lines changed

4 files changed

+120
-11
lines changed

examples/exec.php examples/exec-inspect.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@
55

66
use React\EventLoop\Factory as LoopFactory;
77
use Clue\React\Docker\Factory;
8-
use Clue\React\Docker\ExecHelper;
9-
use React\Stream\Stream;
108
use Clue\React\Buzz\Message\ResponseException;
119

12-
$container = isset($argv[1]) ? $argv[1] : 'asd';
10+
$container = 'asd';
11+
//$cmd = array('echo', 'hello world');
12+
//$cmd = array('sleep', '2');
13+
$cmd = array('sh', '-c', 'echo -n hello && sleep 1 && echo world && sleep 1 && env');
14+
//$cmd = array('cat', 'invalid-path');
15+
16+
if (isset($argv[1])) {
17+
$container = $argv[1];
18+
$cmd = array_slice($argv, 2);
19+
}
1320

1421
$loop = LoopFactory::create();
1522

1623
$factory = new Factory($loop);
1724
$client = $factory->createClient();
1825

19-
$client->execCreate($container, array('Cmd' => array('sleep', '2'), 'AttachStdout' => true))->then(function ($info) use ($client) {
26+
$client->execCreate($container, array('Cmd' => $cmd, 'AttachStdout' => true, 'AttachStderr' => true, 'Tty' => true))->then(function ($info) use ($client) {
2027
echo 'Created with info: ' . json_encode($info) . PHP_EOL;
2128

2229
return $client->execInspect($info['Id']);
2330
})->then(function ($info) use ($client) {
2431
echo 'Inspected after creation: ' . json_encode($info, JSON_PRETTY_PRINT) . PHP_EOL;
2532

26-
return $client->execStart($info['ID'], array())->then(function ($out) use ($client, $info) {
33+
return $client->execStart($info['ID'], array('Tty' => true))->then(function ($out) use ($client, $info) {
2734
echo 'Starting returned: ';
2835
var_dump($out);
2936

src/Client.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -903,15 +903,20 @@ public function execCreate($container, $config)
903903
/**
904904
* Starts a previously set up exec instance id.
905905
*
906+
* This resolves with a string of the command output, i.e. STDOUT and STDERR
907+
* as set up in the `execCreate()` call.
908+
*
909+
* Keep in mind that this means the whole string has to be kept in memory.
910+
*
906911
* If detach is true, this API returns after starting the exec command.
907912
* Otherwise, this API sets up an interactive session with the exec command.
908913
*
909914
* @param string $exec exec ID
910915
* @param array $config (see link)
911-
* @return PromiseInterface Promise<array> stream of message objects
916+
* @return PromiseInterface Promise<string> buffered exec data
912917
* @link https://docs.docker.com/reference/api/docker_remote_api_v1.15/#exec-start
913918
*/
914-
public function execStart($exec, $config)
919+
public function execStart($exec, $config = array())
915920
{
916921
return $this->postJson(
917922
$this->uri->expand(
@@ -921,7 +926,7 @@ public function execStart($exec, $config)
921926
)
922927
),
923928
$config
924-
)->then(array($this->parser, 'expectJson'));
929+
)->then(array($this->parser, 'expectPlain'));
925930
}
926931

927932
/**

tests/ClientTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,11 @@ public function testExecCreate()
386386

387387
public function testExecStart()
388388
{
389-
$json = array();
389+
$data = 'hello world';
390390
$config = array();
391-
$this->expectRequestFlow('post', '/exec/123/start', $this->createResponseJson($json), 'expectJson');
391+
$this->expectRequestFlow('post', '/exec/123/start', $this->createResponse($data), 'expectPlain');
392392

393-
$this->expectPromiseResolveWith($json, $this->client->execStart(123, $config));
393+
$this->expectPromiseResolveWith($data, $this->client->execStart(123, $config));
394394
}
395395

396396
public function testExecResize()

tests/FunctionalClientTest.php

+97
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,103 @@ public function testCreateStartAndRemoveContainer()
7272
$this->assertEquals('destroy', $ret[3]['status']);
7373
}
7474

75+
public function testStartRunning()
76+
{
77+
$config = array(
78+
'Image' => 'busybox',
79+
'Tty' => true,
80+
'Cmd' => array('sleep', '10')
81+
);
82+
83+
$promise = $this->client->containerCreate($config);
84+
$container = Block\await($promise, $this->loop);
85+
86+
$this->assertNotNull($container['Id']);
87+
$this->assertNull($container['Warnings']);
88+
89+
$start = microtime(true);
90+
91+
$promise = $this->client->containerStart($container['Id']);
92+
$ret = Block\await($promise, $this->loop);
93+
94+
$this->assertEquals('', $ret);
95+
96+
return $container['Id'];
97+
}
98+
99+
/**
100+
* @depends testStartRunning
101+
* @param string $container
102+
* @return string
103+
*/
104+
public function testExecCreateWhileRunning($container)
105+
{
106+
$promise = $this->client->execCreate($container, array(
107+
'Cmd' => array('echo', '-n', 'hello', 'world'),
108+
'AttachStdout' => true,
109+
'AttachStderr' => true,
110+
'Tty' => true
111+
));
112+
$exec = Block\await($promise, $this->loop);
113+
114+
$this->assertTrue(is_array($exec));
115+
$this->assertTrue(is_string($exec['Id']));
116+
117+
return $exec['Id'];
118+
}
119+
120+
/**
121+
* @depends testExecCreateWhileRunning
122+
* @param string $exec
123+
*/
124+
public function testExecInspectBeforeRunning($exec)
125+
{
126+
$promise = $this->client->execInspect($exec);
127+
$info = Block\await($promise, $this->loop);
128+
129+
$this->assertTrue(is_array($info));
130+
$this->assertFalse($info['Running']);
131+
$this->assertEquals(null, $info['ExitCode']);
132+
}
133+
134+
/**
135+
* @depends testExecCreateWhileRunning
136+
* @param string $exec
137+
*/
138+
public function testExecStartWhileRunning($exec)
139+
{
140+
$promise = $this->client->execStart($exec, array('Tty' => true));
141+
$output = Block\await($promise, $this->loop);
142+
143+
$this->assertEquals('hello world', $output);
144+
}
145+
146+
/**
147+
* @depends testExecCreateWhileRunning
148+
* @param string $exec
149+
*/
150+
public function testExecInspectAfterRunning($exec)
151+
{
152+
$promise = $this->client->execInspect($exec);
153+
$info = Block\await($promise, $this->loop);
154+
155+
$this->assertTrue(is_array($info));
156+
$this->assertFalse($info['Running']);
157+
$this->assertEquals(0, $info['ExitCode']);
158+
}
159+
160+
/**
161+
* @depends testStartRunning
162+
* @param string $container
163+
*/
164+
public function testRemoveRunning($container)
165+
{
166+
$promise = $this->client->containerRemove($container, true, true);
167+
$ret = Block\await($promise, $this->loop);
168+
169+
$this->assertEquals('', $ret);
170+
}
171+
75172
/**
76173
* @expectedException RuntimeException
77174
*/

0 commit comments

Comments
 (0)