|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
/** @noinspection ALL */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Ktomk\Pipelines\Cli; |
|
8
|
|
|
|
|
9
|
|
|
use Ktomk\Pipelines\TestCase; |
|
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @covers \Ktomk\Pipelines\Cli\Docker |
|
14
|
|
|
*/ |
|
15
|
|
|
class DockerTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function testCommandDetectionAndVersionPaths() |
|
18
|
|
|
{ |
|
19
|
|
|
$procFail = $this->createMock('Ktomk\Pipelines\Cli\Proc'); |
|
20
|
|
|
$procGood = $this->createMock('Ktomk\Pipelines\Cli\Proc'); |
|
21
|
|
|
$procGood->method('getStatus')->willReturn(0); |
|
22
|
|
|
$procGood->method('getStandardOutput')->willReturnCallback( |
|
23
|
|
|
function () use (&$procGoodOutput) { |
|
24
|
|
|
return $procGoodOutput; |
|
25
|
|
|
} |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
/** @var Exec|MockObject $exec */ |
|
29
|
|
|
$exec = $this->createMock('Ktomk\Pipelines\Cli\Exec'); |
|
30
|
|
|
$results = array( |
|
31
|
|
|
$procFail, |
|
32
|
|
|
$procFail, |
|
33
|
|
|
$procFail, |
|
34
|
|
|
$procGood, |
|
35
|
|
|
$procGood, |
|
36
|
|
|
$procGood, |
|
37
|
|
|
$procGood, |
|
38
|
|
|
); |
|
39
|
|
|
$exec->method('capture')->willReturnCallback( |
|
40
|
|
|
function ($command, $args, &$out, &$err = null) use ($results) { |
|
41
|
|
|
static $call = -1; |
|
42
|
|
|
$call++; |
|
43
|
|
|
/** @var Proc $proc */ |
|
44
|
|
|
$proc = $results[$call]; |
|
45
|
|
|
$out = $proc->getStandardOutput(); |
|
46
|
|
|
$err = $proc->getStandardError(); |
|
47
|
|
|
|
|
48
|
|
|
return $proc->getStatus(); |
|
49
|
|
|
} |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$docker = new Docker($exec); |
|
53
|
|
|
self::assertFalse($docker->hasCommand()); |
|
54
|
|
|
self::assertNull($docker->getVersion()); |
|
55
|
|
|
|
|
56
|
|
|
self::assertNull($docker->getVersion()); |
|
57
|
|
|
self::assertSame('0.0.0-err', @$docker->getVersion()); |
|
58
|
|
|
|
|
59
|
|
|
$procGoodOutput = "17.09.1-ce\n"; |
|
60
|
|
|
self::assertSame('17.09.1-ce', $docker->getVersion()); |
|
61
|
|
|
unset($procGoodOutput); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testHostDeviceMount() |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var Exec|MockObject $exec */ |
|
67
|
|
|
$exec = $this->createMock('Ktomk\Pipelines\Cli\Exec'); |
|
68
|
|
|
$exec->method('capture')->willReturnCallback(function ($cmd, $args, &$stdout) { |
|
69
|
|
|
$stdout = file_get_contents(__DIR__ . '/../../data/docker-inspect.json'); |
|
70
|
|
|
|
|
71
|
|
|
return 0; |
|
72
|
|
|
}); |
|
73
|
|
|
|
|
74
|
|
|
$docker = new Docker($exec); |
|
75
|
|
|
$actual = $docker->hostDevice('container-name', '/app'); |
|
76
|
|
|
self::assertSame('/home/user/workspace/projects/pipelines', $actual, 'extraction from json fixture'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testHostDeviceMountOnNonMountPoint() |
|
80
|
|
|
{ |
|
81
|
|
|
/** @var Exec|MockObject $exec */ |
|
82
|
|
|
$exec = $this->createMock('Ktomk\Pipelines\Cli\Exec'); |
|
83
|
|
|
$exec->method('capture')->willReturnCallback(function ($cmd, $args, &$stdout) { |
|
84
|
|
|
$stdout = file_get_contents(__DIR__ . '/../../data/docker-inspect.json'); |
|
85
|
|
|
|
|
86
|
|
|
return 0; |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
$docker = new Docker($exec); |
|
90
|
|
|
$actual = $docker->hostDevice('container-name', '/thanks-for-the-fish'); |
|
91
|
|
|
self::assertSame('/thanks-for-the-fish', $actual, 'fall back on non-mount-point'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testHostDeviceMountDockerInspectFails() |
|
95
|
|
|
{ |
|
96
|
|
|
/** @var Exec|MockObject $exec */ |
|
97
|
|
|
$exec = $this->createMock('Ktomk\Pipelines\Cli\Exec'); |
|
98
|
|
|
$exec->method('capture')->willReturn(1); |
|
99
|
|
|
|
|
100
|
|
|
$docker = new Docker($exec); |
|
101
|
|
|
$actual = $docker->hostDevice('container-name', '/app'); |
|
102
|
|
|
self::assertSame('/app', $actual, 'docker command fails'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testHostDeviceMountJsonParseFailure() |
|
106
|
|
|
{ |
|
107
|
|
|
/** @var Exec|MockObject $exec */ |
|
108
|
|
|
$exec = $this->createMock('Ktomk\Pipelines\Cli\Exec'); |
|
109
|
|
|
$exec->method('capture')->willReturnCallback(function ($cmd, $args, &$stdout) { |
|
110
|
|
|
$stdout = 'Error: file not found or whatever'; |
|
111
|
|
|
|
|
112
|
|
|
return 0; |
|
113
|
|
|
}); |
|
114
|
|
|
|
|
115
|
|
|
$docker = new Docker($exec); |
|
116
|
|
|
$actual = $docker->hostDevice('container-name', '/app'); |
|
117
|
|
|
self::assertSame('/app', $actual, 'extraction from json fixture'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testGetProcessManager() |
|
121
|
|
|
{ |
|
122
|
|
|
self::assertInstanceOf( |
|
123
|
|
|
'Ktomk\Pipelines\Cli\Docker\ProcessManager', |
|
124
|
|
|
Docker::create()->getProcessManager() |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|