Completed
Push — 1.x ( d4bf15...1f27e1 )
by Joel
02:59
created

ContainerManagerTest::testAttachWebsocket()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 43
rs 8.8571
cc 2
eloc 26
nc 2
nop 0
1
<?php
2
3
namespace Docker\Tests\Manager;
4
5
use Docker\API\Model\ContainerConfig;
6
use Docker\API\Model\ContainerCreateResult;
7
use Docker\Manager\ContainerManager;
8
use Docker\Tests\TestCase;
9
10
class ContainerManagerTest extends TestCase
11
{
12
    /**
13
     * Return the container manager
14
     *
15
     * @return ContainerManager
16
     */
17
    private function getManager()
18
    {
19
        return self::getDocker()->getContainerManager();
20
    }
21
22
    /**
23
     * Be sure to have image before doing test
24
     */
25
    public static function setUpBeforeClass()
26
    {
27
        self::getDocker()->getImageManager()->create([
28
            'fromImage' => 'busybox:latest'
29
        ]);
30
    }
31
32
    public function testFindAll()
33
    {
34
        $manager    = $this->getManager();
35
        $containers = $manager->findAll();
36
37
        $this->assertInternalType('array', $containers);
38
    }
39
40
    public function testCreate()
41
    {
42
        $containerConfig = new ContainerConfig();
43
        $containerConfig->setImage('busybox:latest');
44
        $containerConfig->setCmd(['echo', '1']);
45
        $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
46
47
        $manager = $this->getManager();
48
        $containerCreateResult = $manager->create($containerConfig);
49
50
        $this->assertInstanceOf('\Docker\API\Model\ContainerCreateResult', $containerCreateResult);
51
        $this->assertNotEmpty($containerCreateResult->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ContainerCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
52
    }
53
54
    public function testInspect()
55
    {
56
        $containerConfig = new ContainerConfig();
57
        $containerConfig->setImage('busybox:latest');
58
        $containerConfig->setCmd(['echo', '1']);
59
        $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
60
61
        $containerCreateResult = $this->getManager()->create($containerConfig);
62
        $container = $this->getManager()->find($containerCreateResult->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ContainerCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
63
64
        $this->assertInstanceOf('\Docker\API\Model\Container', $container);
65
        $this->assertEquals($container->getId(), $containerCreateResult->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\Container, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66
        $this->assertEquals('busybox:latest', $container->getConfig()->getImage());
0 ignored issues
show
Bug introduced by
The method getConfig does only exist in Docker\API\Model\Container, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
        $this->assertEquals(['echo', '1'], $container->getConfig()->getCmd());
68
    }
69
70 View Code Duplication
    public function testStart()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $containerConfig = new ContainerConfig();
73
        $containerConfig->setImage('busybox:latest');
74
        $containerConfig->setCmd(['/bin/true']);
75
        $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
76
77
        $manager = $this->getManager();
78
        $containerCreateResult = $manager->create($containerConfig);
79
        $manager->start($containerCreateResult->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ContainerCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
80
        $container = $manager->find($containerCreateResult->getId());
81
82
        $this->assertEquals(0, $container->getState()->getExitCode());
0 ignored issues
show
Bug introduced by
The method getState does only exist in Docker\API\Model\Container, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
83
    }
84
85 View Code Duplication
    public function testListProcesses()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $containerConfig = new ContainerConfig();
88
        $containerConfig->setImage('busybox:latest');
89
        $containerConfig->setCmd(['sleep', '10']);
90
        $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
91
92
        $manager = $this->getManager();
93
        $containerCreateResult = $manager->create($containerConfig);
94
        $manager->start($containerCreateResult->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ContainerCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
95
96
        $processes = $manager->listProcesses($containerCreateResult->getId());
97
98
        $this->assertInstanceOf('\Docker\API\Model\ContainerTop', $processes);
99
        $this->assertCount(1, $processes->getProcesses());
0 ignored issues
show
Bug introduced by
The method getProcesses does only exist in Docker\API\Model\ContainerTop, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
    }
101
102
    public function testAttach()
103
    {
104
        $containerConfig = new ContainerConfig();
105
        $containerConfig->setImage('busybox:latest');
106
        $containerConfig->setCmd(['echo', '-n', 'output']);
107
        $containerConfig->setAttachStdout(true);
108
        $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
109
110
        $containerCreateResult = $this->getManager()->create($containerConfig);
111
        $dockerRawStream = $this->getManager()->attach($containerCreateResult->getId(), [
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ContainerCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
112
            'stream' => true,
113
            'stdout' => true,
114
        ], ContainerManager::FETCH_STREAM);
115
116
        $stdoutFull = "";
117
        $dockerRawStream->onStdout(function ($stdout) use (&$stdoutFull) {
0 ignored issues
show
Bug introduced by
The method onStdout does only exist in Docker\Stream\DockerRawStream, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
118
            $stdoutFull .= $stdout;
119
        });
120
121
        $this->getManager()->start($containerCreateResult->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ContainerCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
122
        $this->getManager()->wait($containerCreateResult->getId());
123
124
        $dockerRawStream->wait();
0 ignored issues
show
Bug introduced by
The method wait does only exist in Docker\Stream\DockerRawStream, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
125
126
        $this->assertEquals("output", $stdoutFull);
127
    }
128
129
    public function testAttachWebsocket()
130
    {
131
        $containerConfig = new ContainerConfig();
132
        $containerConfig->setImage('ubuntu:precise');
133
        $containerConfig->setCmd(['/bin/bash']);
134
        $containerConfig->setAttachStdout(true);
135
        $containerConfig->setAttachStderr(true);
136
        $containerConfig->setAttachStdin(false);
137
        $containerConfig->setOpenStdin(true);
138
        $containerConfig->setTty(true);
139
        $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
140
141
        $containerCreateResult = $this->getManager()->create($containerConfig);
142
        $webSocketStream       = $this->getManager()->attachWebsocket($containerCreateResult->getId(), [
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ContainerCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
143
            'stream' => true,
144
            'stdout' => true,
145
            'stderr' => true,
146
            'stdin'  => true,
147
        ], ContainerManager::FETCH_STREAM);
148
149
        $this->getManager()->start($containerCreateResult->getId());
150
151
        // Read the bash first line
152
        $webSocketStream->read();
0 ignored issues
show
Bug introduced by
The method read does only exist in Docker\Stream\AttachWebsocketStream, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
153
154
        // No output after that so it should be false
155
        $this->assertFalse($webSocketStream->read());
156
157
        // Write something to the container
158
        $webSocketStream->write("echo test\n");
0 ignored issues
show
Bug introduced by
The method write does only exist in Docker\Stream\AttachWebsocketStream, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
159
160
        // Test for echo present (stdin)
161
        $output = "";
162
163
        while (($data = $webSocketStream->read()) != false) {
164
            $output .= $data;
165
        }
166
167
        $this->assertContains("echo", $output);
168
169
        // Exit the container
170
        $webSocketStream->write("exit\n");
171
    }
172
173
    /**
174
    public function testInteract()
175
    {
176
        $container = new Container([
177
            'Image' => 'ubuntu:precise',
178
            'Cmd'   => ['/bin/bash'],
179
            'AttachStdin'  => false,
180
            'AttachStdout' => true,
181
            'AttachStderr' => true,
182
            'OpenStdin'    => true,
183
            'Tty'          => true,
184
        ]);
185
186
        $manager = $this->getManager();
187
        $manager->create($container);
188
        $stream = $manager->interact($container);
189
        $manager->start($container);
190
191
        $this->assertNotEmpty($container->getId());
192
        $this->assertInstanceOf('\Docker\Http\Stream\InteractiveStream', $stream);
193
194
        stream_set_blocking($stream->getSocket(), 0);
195
196
        $read   = [$stream->getSocket()];
197
        $write  = null;
198
        $expect = null;
199
200
        $stream->write("echo test\n");
201
        $data = "";
202
        do {
203
            $frame = $stream->receive(true);
204
            $data .= $frame['data'];
205
        } while (stream_select($read, $write, $expect, 1) > 0);
206
207
        $manager->stop($container, 1);
208
209
        $this->assertRegExp('#root@'.substr($container->getId(), 0, 12).':/\# echo test#', $data, $data);
210
    }
211
212
    public function testCreateThrowsRightFormedException()
213
    {
214
        $container = new Container(['Image' => 'non-existent']);
215
216
        $manager = $this->getManager();
217
218
        try {
219
            $manager->create($container);
220
        } catch (\GuzzleHttp\Exception\RequestException $e) {
221
            $this->assertTrue($e->hasResponse());
222
            $this->assertEquals("404", $e->getResponse()->getStatusCode());
223
            $this->assertContains('No such image', $e->getMessage());
224
        }
225
    }
226
227
    public function testStart()
228
    {
229
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]);
230
231
        $manager = $this->getManager();
232
        $manager->create($container);
233
        $manager->start($container);
234
235
        $runtimeInformations = $container->getRuntimeInformations();
236
237
        $this->assertEquals(0, $runtimeInformations['State']['ExitCode']);
238
    }
239
240
    public function testRunDefault()
241
    {
242
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]);
243
        $manager = $this
244
            ->getMockBuilder('\Docker\Manager\ContainerManager')
245
            ->setMethods(['create', 'start', 'wait'])
246
            ->disableOriginalConstructor()
247
            ->getMock();
248
249
        $container->setExitCode(0);
250
251
        $manager->expects($this->once())
252
            ->method('create')
253
            ->with($this->isInstanceOf('\Docker\Container'))
254
            ->will($this->returnSelf());
255
256
        $manager->expects($this->once())
257
            ->method('start')
258
            ->with($this->isInstanceOf('\Docker\Container'))
259
            ->will($this->returnSelf());
260
261
        $manager->expects($this->once())
262
            ->method('wait')
263
            ->with($this->isInstanceOf('\Docker\Container'))
264
            ->will($this->returnSelf());
265
266
        $this->assertTrue($manager->run($container));
267
    }
268
269
    public function testRunAttach()
270
    {
271
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]);
272
        $manager = $this
273
            ->getMockBuilder('\Docker\Manager\ContainerManager')
274
            ->setMethods(['create', 'start', 'wait', 'attach'])
275
            ->disableOriginalConstructor()
276
            ->getMock();
277
278
        $response = $this->getMockBuilder('\GuzzleHttp\Message\Response')->disableOriginalConstructor()->getMock();
279
        $stream   = $this->getMockBuilder('\GuzzleHttp\Stream\Stream')->disableOriginalConstructor()->getMock();
280
281
        $container->setExitCode(0);
282
        $callback = function () {};
283
284
        $manager->expects($this->once())
285
            ->method('create')
286
            ->with($this->isInstanceOf('\Docker\Container'))
287
            ->will($this->returnSelf());
288
289
        $manager->expects($this->once())
290
            ->method('attach')
291
            ->with($this->isInstanceOf('\Docker\Container'), $this->equalTo($callback), $this->equalTo(true), $this->equalTo(true), $this->equalTo(true), $this->equalTo(true), $this->equalTo(true), $this->equalTo(null))
292
            ->will($this->returnValue($response));
293
294
        $manager->expects($this->once())
295
            ->method('start')
296
            ->with($this->isInstanceOf('\Docker\Container'))
297
            ->will($this->returnSelf());
298
299
        $response->expects($this->once())
300
            ->method('getBody')
301
            ->will($this->returnValue($stream));
302
303
        $manager->expects($this->once())
304
            ->method('wait')
305
            ->with($this->isInstanceOf('\Docker\Container'))
306
            ->will($this->returnSelf());
307
308
        $this->assertTrue($manager->run($container, $callback));
309
    }
310
311
    public function testRunDaemon()
312
    {
313
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]);
314
        $manager = $this
315
            ->getMockBuilder('\Docker\Manager\ContainerManager')
316
            ->setMethods(['create', 'start', 'wait'])
317
            ->disableOriginalConstructor()
318
            ->getMock();
319
320
        $container->setExitCode(0);
321
322
        $manager->expects($this->once())
323
            ->method('create')
324
            ->with($this->isInstanceOf('\Docker\Container'))
325
            ->will($this->returnSelf());
326
327
        $manager->expects($this->once())
328
            ->method('start')
329
            ->with($this->isInstanceOf('\Docker\Container'))
330
            ->will($this->returnSelf());
331
332
        $manager->expects($this->never())
333
            ->method('wait');
334
335
        $this->assertNull($manager->run($container, null, [], true));
336
    }
337
338
    public function testAttach()
339
    {
340
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/bash', '-c', 'echo -n "output"']]);
341
        $manager = $this->getManager();
342
343
        $type   = 0;
344
        $output = "";
345
346
        $manager->create($container);
347
        $response = $manager->attach($container, function ($log, $stdtype) use (&$type, &$output) {
348
            $type = $stdtype;
349
            $output = $log;
350
        });
351
        $manager->start($container);
352
353
        $response->getBody()->getContents();
354
355
        $this->assertEquals(1, $type);
356
        $this->assertEquals('output', $output);
357
    }
358
359
    public function testAttachStderr()
360
    {
361
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/bash', '-c', 'echo -n "error" 1>&2']]);
362
        $manager = $this->getManager();
363
364
        $type   = 0;
365
        $output = "";
366
367
        $manager->create($container);
368
        $response = $manager->attach($container, function ($log, $stdtype) use (&$type, &$output) {
369
            $type = $stdtype;
370
            $output = $log;
371
        });
372
        $manager->start($container);
373
374
        $response->getBody()->getContents();
375
376
        $this->assertEquals(2, $type);
377
        $this->assertEquals('error', $output);
378
    }
379
380
    public function testWait()
381
    {
382
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]);
383
384
        $manager = $this->getManager();
385
        $manager->run($container);
386
        $manager->wait($container);
387
388
        $runtimeInformations = $container->getRuntimeInformations();
389
390
        $this->assertEquals(0, $runtimeInformations['State']['ExitCode']);
391
    }
392
393
    public function testWaitWithTimeout()
394
    {
395
        if (getenv('DOCKER_TLS_VERIFY')) {
396
            $this->markTestSkipped('This test failed when using ssl due to this bug : https://bugs.php.net/bug.php?id=41631');
397
        }
398
399
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '2']]);
400
401
        $manager = $this->getManager();
402
        $manager->create($container);
403
        $manager->start($container);
404
        $manager->wait($container, 1);
405
    }
406
407
    public function testTimeoutExceptionHasRequest()
408
    {
409
        if (getenv('DOCKER_TLS_VERIFY')) {
410
            $this->markTestSkipped('This test failed when using ssl due to this bug : https://bugs.php.net/bug.php?id=41631');
411
        }
412
413
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '2']]);
414
415
        $manager = $this->getManager();
416
        $manager->run($container);
417
418
        try {
419
            $manager->wait($container, 1);
420
        } catch (RequestException $e) {
421
            $this->assertInstanceOf('Docker\\Http\\Request', $e->getRequest());
422
        }
423
    }
424
425
    public function testExposeFixedPort()
426
    {
427
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]);
428
429
        $port = new Port('8888:80/tcp');
430
431
        $container->setExposedPorts($port);
432
433
        $manager = $this->getManager();
434
        $manager->create($container);
435
        $manager->start($container, ['PortBindings' => $port->toSpec()]);
436
437
        $this->assertEquals(8888, $container->getMappedPort(80)->getHostPort());
438
    }
439
440
    public function testExposeRandomPort()
441
    {
442
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]);
443
444
        $port = new Port('80/tcp');
445
        $container->setExposedPorts($port);
446
447
        $manager = $this->getManager();
448
        $manager->create($container);
449
        $manager->start($container, ['PortBindings' => $port->toSpec()]);
450
451
        $this->assertInternalType('integer', $container->getMappedPort(80)->getHostPort());
452
    }
453
454
    public function testInspect()
455
    {
456
        $manager = $this->getManager();
457
458
        $this->assertEquals(null, $manager->find('foo'));
459
460
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]);
461
        $manager->create($container);
462
463
        $this->assertInstanceOf('Docker\\Container', $manager->find($container->getId()));
464
    }
465
466
    public function testRemove()
467
    {
468
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['date']]);
469
470
        $manager = $this->getManager();
471
        $manager->create($container);
472
        $manager->start($container);
473
        $manager->wait($container);
474
        $manager->remove($container);
475
476
        $this->setExpectedException('\\Docker\\Exception\\ContainerNotFoundException', 'Container not found');
477
        $manager->inspect($container);
478
    }
479
    
480
    public function testForceRemove()
481
    {
482
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['tail', '-f', '/var/log/lastlog']]);
483
484
        $manager = $this->getManager();
485
        $manager->create($container);
486
        $manager->start($container);
487
488
        try {
489
            $manager->remove($container);
490
            $this->assertTrue(false);
491
        } catch (APIException $e) {
492
            $this->assertTrue(true);
493
        }
494
495
        $manager->remove($container, false, true);
496
497
        try {
498
            $manager->inspect($container);
499
            $this->assertTrue(false);
500
        } catch (ContainerNotFoundException $e) {
501
            $this->assertTrue(true);
502
        }
503
    }
504
505
    public function testRemoveContainers()
506
    {
507
        $containers = ['3360ea744df2', 'a412d121d015'];
508
        $manager = $this
509
            ->getMockBuilder('\Docker\Manager\ContainerManager')
510
            ->setMethods(['remove'])
511
            ->disableOriginalConstructor()
512
            ->getMock();
513
514
        $manager->expects($this->exactly(2))
515
            ->method('remove')
516
            ->with($this->isInstanceOf('\Docker\Container'), false)
517
            ->will($this->returnSelf());
518
519
        $manager->removeContainers($containers);
520
    }
521
    
522
    public function testForceRemoveContainers()
523
    {
524
        $containers = ['3360ea744df2', 'a412d121d015'];
525
        $manager = $this
526
            ->getMockBuilder('\Docker\Manager\ContainerManager')
527
            ->setMethods(['remove'])
528
            ->disableOriginalConstructor()
529
            ->getMock();
530
531
        $manager->expects($this->exactly(2))
532
            ->method('remove')
533
            ->with($this->isInstanceOf('\Docker\Container'), false, true)
534
            ->will($this->returnSelf());
535
536
        $manager->removeContainers($containers, false, true);
537
    }
538
539
    public function testTop()
540
    {
541
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['sleep', '5']]);
542
        $manager = $this->getManager();
543
        $manager->run($container, null, [], true);
544
545
        sleep(1);
546
547
        $processes = $manager->top($container);
548
549
        $this->assertCount(1, $processes);
550
        $this->assertArrayHasKey('COMMAND', $processes[0]);
551
        $this->assertEquals('sleep 5', $processes[0]['COMMAND']);
552
553
        $manager->kill($container);
554
        $manager->remove($container);
555
    }
556
557
    public function testChanges()
558
    {
559
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['touch', '/docker-php-test']]);
560
        $manager = $this->getManager();
561
        $manager->run($container);
562
        $manager->wait($container);
563
564
        $changes = $manager->changes($container);
565
566
        $manager->remove($container);
567
568
        $this->assertCount(1, $changes);
569
        $this->assertEquals('/docker-php-test', $changes[0]['Path']);
570
        $this->assertEquals(1, $changes[0]['Kind']);
571
    }
572
573
    public function testExport()
574
    {
575
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['touch', '/docker-php-test']]);
576
        $manager = $this->getManager();
577
        $manager->run($container);
578
        $manager->wait($container);
579
580
        $exportStream = $manager->export($container);
581
582
        $this->assertInstanceOf('\GuzzleHttp\Stream\Stream', $exportStream);
583
584
        $tarFileName  = tempnam(sys_get_temp_dir(), 'docker-php-export-test-');
585
        $tarFile      = fopen($tarFileName, 'w+');
586
587
        stream_copy_to_stream($exportStream->detach(), $tarFile);
588
        fclose($tarFile);
589
590
        exec('/usr/bin/env tar -tf '.$tarFileName, $output);
591
592
        $this->assertContains('docker-php-test', $output);
593
        $this->assertContains('.dockerinit', $output);
594
595
        unlink($tarFileName);
596
        $manager->remove($container);
597
    }
598
599
    public function testCopyToDisk()
600
    {
601
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['touch', '/etc/default/docker-php-test']]);
602
        $manager = $this->getManager();
603
        $manager->run($container);
604
        $manager->wait($container);
605
606
        $tarFileName  = tempnam(sys_get_temp_dir(), 'testcopyToDisk.tar');
607
        $manager->copyToDisk($container, '/etc/default', $tarFileName);
608
609
        exec('/usr/bin/env tar -tf '.$tarFileName, $output);
610
        $this->assertContains('default/docker-php-test', $output);
611
612
        unlink($tarFileName);
613
        $manager->remove($container);
614
    }
615
616
    public function testLogs()
617
    {
618
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['echo', 'test']]);
619
        $manager = $this->getManager();
620
        $manager->run($container);
621
        $manager->stop($container);
622
        $logs = $manager->logs($container, false, true);
623
        $manager->remove($container);
624
625
        $this->assertGreaterThanOrEqual(1, count($logs));
626
627
        $logs = array_map(function ($value) {
628
            return $value['output'];
629
        }, $logs);
630
631
        $this->assertContains("test", implode("", $logs));
632
    }
633
634
    public function testRestart()
635
    {
636
        $manager = $this->getManager();
637
638
        $container = new Container(['Image' => 'busybox', 'Cmd' => ['sleep', '10']]);
639
640
        $manager->create($container);
641
        $manager->start($container);
642
        $this->assertEquals('0001-01-01T00:00:00Z', $container->getRuntimeInformations()['State']['FinishedAt']);
643
644
        $manager->restart($container);
645
        $this->assertNotEquals('0001-01-01T00:00:00Z', $container->getRuntimeInformations()['State']['FinishedAt']);
646
647
        $manager->stop($container);
648
        $manager->remove($container);
649
    }
650
651
    public function testKill()
652
    {
653
        $manager = $this->getManager();
654
        $dockerFileBuilder = new ContextBuilder();
655
        $dockerFileBuilder->from('ubuntu:precise');
656
        $dockerFileBuilder->add('/kill.sh', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'script' . DIRECTORY_SEPARATOR . 'kill.sh'));
657
        $dockerFileBuilder->run('chmod +x /kill.sh');
658
659
        $this->getDocker()->build($dockerFileBuilder->getContext(), 'docker-php-kill-test', null, true, false, true);
660
661
        $container = new Container(['Image' => 'docker-php-kill-test:latest', 'Cmd' => ['/kill.sh']]);
662
        $manager->create($container);
663
        $manager->start($container);
664
        $manager->kill($container, "SIGHUP");
665
        $manager->wait($container);
666
667
        $logs = $manager->logs($container, false, true);
668
        $logs = array_map(function ($value) {
669
            return $value['output'];
670
        }, $logs);
671
672
        $manager->remove($container);
673
        $this->getDocker()->getImageManager()->remove($container->getImage());
674
675
        $this->assertContains('HUP', implode("", $logs));
676
    }
677
678
    public function testExec()
679
    {
680
        $manager = $this->getManager();
681
        $dockerFileBuilder = new ContextBuilder();
682
        $dockerFileBuilder->from('ubuntu:precise');
683
        $dockerFileBuilder->add('/daemon.sh', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'script' . DIRECTORY_SEPARATOR . 'daemon.sh'));
684
        $dockerFileBuilder->run('chmod +x /daemon.sh');
685
686
        $this->getDocker()->build($dockerFileBuilder->getContext(), 'docker-php-restart-test', null, true, false, true);
687
688
        $container = new Container(['Image' => 'docker-php-restart-test', 'Cmd' => ['/daemon.sh']]);
689
        $manager->create($container);
690
        $manager->start($container);
691
692
        $type   = 0;
693
        $output = "";
694
        $execId = $manager->exec($container, ['/bin/bash', '-c', 'echo -n "output"']);
695
696
        $this->assertNotNull($execId);
697
698
        $response = $manager->execstart($execId, function ($log, $stdtype) use (&$type, &$output) {
699
            $type = $stdtype;
700
            $output = $log;
701
        });
702
703
        $response->getBody()->getContents();
704
705
        $manager->kill($container);
706
        $manager->remove($container);
707
708
        $this->assertEquals(1, $type);
709
        $this->assertEquals('output', $output);
710
    }
711
712
    public function testExecInspect()
713
    {
714
        $manager = $this->getManager();
715
        $dockerFileBuilder = new ContextBuilder();
716
        $dockerFileBuilder->from('ubuntu:precise');
717
        $dockerFileBuilder->add('/daemon.sh', file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'script' . DIRECTORY_SEPARATOR . 'daemon.sh'));
718
        $dockerFileBuilder->run('chmod +x /daemon.sh');
719
720
        $this->getDocker()->build($dockerFileBuilder->getContext(), 'docker-php-restart-test', null, true, false, true);
721
722
        $container = new Container(['Image' => 'docker-php-restart-test', 'Cmd' => ['/daemon.sh']]);
723
        $manager->create($container);
724
        $manager->start($container);
725
726
        $execId = $manager->exec($container, ['/bin/bash', '-c', 'echo -n "output"']);
727
        $inspection = $manager->execinspect($execId);
728
729
        $this->assertEquals(0, $inspection->ExitCode);
730
        $this->assertEquals(false, $inspection->Running);
731
    }
732
733
    public function testRename()
734
    {
735
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/true']]);
736
737
        $manager = $this->getManager();
738
        $manager->create($container);
739
        $manager->start($container);
740
        $manager->rename($container, 'FoobarRenamed');
741
742
        $runtimeInformations = $container->getRuntimeInformations();
743
		
744
        $this->assertInstanceOf('Docker\\Container', $manager->find('FoobarRenamed'));
745
        $manager->stop($container);   // cleanup
746
        $manager->remove($container);
747
    }
748
749
    public function testPause()
750
    {
751
        $container = new Container(['Image' => 'ubuntu:precise', 'Cmd' => ['/bin/sleep', '1']]);
752
753
        $manager = $this->getManager();
754
        $manager->create($container);
755
        $manager->start($container);
756
757
        $runtimeInformations = $container->getRuntimeInformations();
758
        $this->assertEquals(false, $runtimeInformations['State']['Paused']);
759
        $this->assertEquals(true, $runtimeInformations['State']['Running']);
760
761
        $manager->pause($container);
762
763
        $runtimeInformations = $container->getRuntimeInformations();
764
        $this->assertEquals(true, $runtimeInformations['State']['Paused']);
765
        $this->assertEquals(true, $runtimeInformations['State']['Running']);
766
767
        $manager->unpause($container);
768
769
        $runtimeInformations = $container->getRuntimeInformations();
770
        $this->assertEquals(false, $runtimeInformations['State']['Paused']);
771
        $this->assertEquals(true, $runtimeInformations['State']['Running']);
772
773
        // cleanup
774
        $manager->stop($container);
775
        $manager->remove($container);
776
    }*/
777
778
}
779