|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Runner\Containers; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Cli\Exec; |
|
8
|
|
|
use Ktomk\Pipelines\Cli\ExecTester; |
|
9
|
|
|
use Ktomk\Pipelines\LibTmp; |
|
10
|
|
|
use Ktomk\Pipelines\Runner\Containers; |
|
11
|
|
|
use Ktomk\Pipelines\Runner\Env; |
|
12
|
|
|
use Ktomk\Pipelines\Runner\RunnerTestCase; |
|
13
|
|
|
use Ktomk\Pipelines\Runner\RunOpts; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class StepContainerTest |
|
17
|
|
|
* |
|
18
|
|
|
* @package Ktomk\Pipelines\Runner |
|
19
|
|
|
* @covers \Ktomk\Pipelines\Runner\Containers\StepContainer |
|
20
|
|
|
*/ |
|
21
|
|
|
class StepContainerTest extends RunnerTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
public function testCreation() |
|
24
|
|
|
{ |
|
25
|
|
|
$step = $this->getStepMock(); |
|
26
|
|
|
|
|
27
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
28
|
|
|
$runner->method('getExec')->willReturn(new Exec()); |
|
29
|
|
|
|
|
30
|
|
|
$container = new StepContainer('test-step-container', $step, $runner); |
|
31
|
|
|
self::assertNotNull($container); |
|
32
|
|
|
self::assertInstanceOf('Ktomk\Pipelines\Runner\Containers\StepContainer', $container); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testGetName() |
|
36
|
|
|
{ |
|
37
|
|
|
$expected = 'pipelines-1.no-name.null.test-project'; |
|
38
|
|
|
|
|
39
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
40
|
|
|
$runner->method('getExec')->willReturn(new ExecTester($this)); |
|
41
|
|
|
|
|
42
|
|
|
$container = new StepContainer($expected, $this->getStepMock(), $runner); |
|
43
|
|
|
$actual = $container->getName(); |
|
44
|
|
|
self::assertSame($expected, $actual); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testKeepOrKill() |
|
48
|
|
|
{ |
|
49
|
|
|
$exec = new ExecTester($this); |
|
50
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
51
|
|
|
$runner->method('getExec')->willReturn($exec); |
|
52
|
|
|
|
|
53
|
|
|
$name = 'pipelines-1.no-name.null.test-project'; |
|
54
|
|
|
$container = new StepContainer($name, $this->getStepMock(), $runner); |
|
55
|
|
|
|
|
56
|
|
|
$exec->expect('capture', 'docker'); |
|
57
|
|
|
self::assertNull($container->keepOrKill(false)); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$exec->expect('capture', 'docker', '1234567'); |
|
60
|
|
|
self::assertSame('1234567', $container->keepOrKill(true)); |
|
61
|
|
|
|
|
62
|
|
|
self::assertSame('1234567', $container->getId()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testKillAndRemoveThrowsNot() |
|
66
|
|
|
{ |
|
67
|
|
|
$exec = new ExecTester($this); |
|
68
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
69
|
|
|
$runner->method('getExec')->willReturn($exec); |
|
70
|
|
|
|
|
71
|
|
|
$container = new StepContainer('test-step-container', $this->getStepMock(), $runner); |
|
72
|
|
|
|
|
73
|
|
|
$container->killAndRemove(false, false); |
|
74
|
|
|
$this->addToAssertionCount(1); |
|
75
|
|
|
|
|
76
|
|
|
$exec->expect('capture', 'docker', 0, 'rm'); |
|
77
|
|
|
$container->killAndRemove(false, true); |
|
78
|
|
|
$this->addToAssertionCount(1); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testKillAndRemove() |
|
82
|
|
|
{ |
|
83
|
|
|
$exec = new ExecTester($this); |
|
84
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
85
|
|
|
$runner->method('getExec')->willReturn($exec); |
|
86
|
|
|
|
|
87
|
|
|
$name = 'pipelines-1.no-name.null.test-project'; |
|
88
|
|
|
$container = new StepContainer($name, $this->getStepMock(), $runner); |
|
89
|
|
|
|
|
90
|
|
|
$exec->expect('capture', 'docker', '1234567', 'fake container id'); |
|
91
|
|
|
$container->keepOrKill(true); |
|
92
|
|
|
|
|
93
|
|
|
$exec->expect('capture', 'docker'); |
|
94
|
|
|
$exec->expect('capture', 'docker'); |
|
95
|
|
|
$container->killAndRemove(true, true); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testRun() |
|
99
|
|
|
{ |
|
100
|
|
|
$exec = new ExecTester($this); |
|
101
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
102
|
|
|
$runner->method('getExec')->willReturn($exec); |
|
103
|
|
|
|
|
104
|
|
|
$container = new StepContainer('test-step-container', $this->getStepMock(), $runner); |
|
105
|
|
|
self::assertNull($container->getId(), 'precondition'); |
|
106
|
|
|
|
|
107
|
|
|
$exec->expect('capture', 'docker', '1234567', 'run'); |
|
108
|
|
|
$actual = $container->run(array()); |
|
109
|
|
|
self::assertIsArray($actual); |
|
110
|
|
|
self::assertCount(3, $actual); |
|
111
|
|
|
self::assertSame('1234567', $container->getId()); |
|
112
|
|
|
self::assertSame('1234567', $container->getDisplayId()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testRunDryRun() |
|
116
|
|
|
{ |
|
117
|
|
|
$exec = new ExecTester($this); |
|
118
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
119
|
|
|
$runner->method('getExec')->willReturn($exec); |
|
120
|
|
|
$container = new StepContainer('test-step-container', $this->getStepMock(), $runner); |
|
121
|
|
|
self::assertNull($container->getId(), 'precondition'); |
|
122
|
|
|
|
|
123
|
|
|
$exec->expect('capture', 'docker', '', 'run'); |
|
124
|
|
|
$actual = $container->run(array()); |
|
125
|
|
|
self::assertIsArray($actual); |
|
126
|
|
|
self::assertCount(3, $actual); |
|
127
|
|
|
self::assertNull($container->getId()); |
|
128
|
|
|
self::assertSame('*dry-run*', $container->getDisplayId()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testExecRunServiceContainerAttached() |
|
132
|
|
|
{ |
|
133
|
|
|
$exec = new ExecTester($this); |
|
134
|
|
|
$exec |
|
135
|
|
|
->expect('pass', 'docker', 0, 'docker run service'); |
|
136
|
|
|
|
|
137
|
|
|
$step = $this->createTestStepFromFixture('service-definitions.yml'); |
|
138
|
|
|
list($first) = $step->getServices()->getServiceNames(); |
|
139
|
|
|
$service = $step->getFile()->getDefinitions()->getServices()->getByName($first); |
|
140
|
|
|
|
|
141
|
|
|
$actual = Containers::execRunServiceContainerAttached( |
|
142
|
|
|
$exec, |
|
143
|
|
|
$service, |
|
|
|
|
|
|
144
|
|
|
function ($a) { |
|
145
|
|
|
return $a; |
|
146
|
|
|
}, |
|
147
|
|
|
'prefix', |
|
148
|
|
|
'project', |
|
149
|
|
|
array() |
|
150
|
|
|
); |
|
151
|
|
|
$expected = array(0, array('--network', 'host')); |
|
152
|
|
|
self::assertSame($expected, $actual); |
|
153
|
|
|
|
|
154
|
|
|
$messages = $exec->getDebugMessages(); |
|
155
|
|
|
self::assertCount(1, $messages); |
|
156
|
|
|
self::assertStringContainsString(' --rm ', $messages[0]); |
|
157
|
|
|
self::assertStringNotContainsString(' --detached ', $messages[0]); |
|
158
|
|
|
self::assertStringNotContainsString(' -d ', $messages[0]); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testObtainUserOptions() |
|
162
|
|
|
{ |
|
163
|
|
|
$step = $this->getStepMock(); |
|
164
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
165
|
|
|
$runOpts = new RunOpts(); |
|
166
|
|
|
$runner->method('getRunOpts')->willReturn($runOpts); |
|
167
|
|
|
|
|
168
|
|
|
$container = new StepContainer('name', $step, $runner); |
|
169
|
|
|
|
|
170
|
|
|
$expected = array(); |
|
171
|
|
|
self::assertSame($expected, $container->obtainUserOptions(), 'no user option'); |
|
172
|
|
|
|
|
173
|
|
|
$runOpts->setUser('1000:1000'); |
|
174
|
|
|
|
|
175
|
|
|
$expected = array( |
|
176
|
|
|
0 => '--user', |
|
177
|
|
|
1 => '1000:1000', |
|
178
|
|
|
2 => '-v', |
|
179
|
|
|
3 => '/etc/passwd:/etc/passwd:ro', |
|
180
|
|
|
4 => '-v', |
|
181
|
|
|
5 => '/etc/group:/etc/group:ro', |
|
182
|
|
|
); |
|
183
|
|
|
self::assertSame($expected, $container->obtainUserOptions(), 'user option'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function testObtainLabelOptions() |
|
187
|
|
|
{ |
|
188
|
|
|
$step = $this->getStepMock(); |
|
189
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
190
|
|
|
$runOpts = new RunOpts(); |
|
191
|
|
|
$runner->method('getRunOpts')->willReturn($runOpts); |
|
192
|
|
|
$directories = $this->createMock('Ktomk\Pipelines\Runner\Directories'); |
|
193
|
|
|
$runner->method('getDirectories')->willReturn($directories); |
|
194
|
|
|
$runner->method('getPrefix')->willReturn('pipelines'); |
|
195
|
|
|
|
|
196
|
|
|
$container = new StepContainer('name', $step, $runner); |
|
197
|
|
|
|
|
198
|
|
|
$expected = array( |
|
199
|
|
|
0 => '-l', |
|
200
|
|
|
1 => 'pipelines.prefix=', |
|
201
|
|
|
2 => '-l', |
|
202
|
|
|
3 => 'pipelines.role=step', |
|
203
|
|
|
4 => '-l', |
|
204
|
|
|
5 => 'pipelines.project.name', |
|
205
|
|
|
6 => '-l', |
|
206
|
|
|
7 => 'pipelines.project.path', |
|
207
|
|
|
); |
|
208
|
|
|
|
|
209
|
|
|
self::assertSame($expected, $container->obtainLabelOptions(), 'label options'); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function testSshOption() |
|
213
|
|
|
{ |
|
214
|
|
|
$step = $this->getStepMock(); |
|
215
|
|
|
$runner = $this->createMock('Ktomk\Pipelines\Runner\Runner'); |
|
216
|
|
|
$runOpts = new RunOpts('foo'); |
|
217
|
|
|
$env = new Env(); |
|
218
|
|
|
$runner->method('getEnv')->willReturn($env); |
|
219
|
|
|
$runner->method('getRunOpts')->willReturn($runOpts); |
|
220
|
|
|
|
|
221
|
|
|
$container = new StepContainer('name', $step, $runner); |
|
222
|
|
|
|
|
223
|
|
|
$actual = $container->obtainSshOptions(); |
|
224
|
|
|
self::assertSame(array(), $actual, 'no ssh option'); |
|
225
|
|
|
|
|
226
|
|
|
$runOpts->setSsh(true); |
|
227
|
|
|
list($handle, $file) = LibTmp::tmpFile(); |
|
228
|
|
|
$env->initDefaultVars(array('SSH_AUTH_SOCK' => $file)); |
|
229
|
|
|
$actual = $container->obtainSshOptions(); |
|
230
|
|
|
$expected = array( |
|
231
|
|
|
0 => '-v', |
|
232
|
|
|
1 => $file . ':/var/run/ssh-auth.sock:ro', |
|
233
|
|
|
2 => '-e', |
|
234
|
|
|
3 => 'SSH_AUTH_SOCK=/var/run/ssh-auth.sock', |
|
235
|
|
|
); |
|
236
|
|
|
self::assertSame($expected, $actual, 'ssh option'); |
|
237
|
|
|
unset($handle); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @return \Ktomk\Pipelines\File\Pipeline\Step|\PHPUnit\Framework\MockObject\MockObject |
|
242
|
|
|
*/ |
|
243
|
|
|
private function getStepMock() |
|
244
|
|
|
{ |
|
245
|
|
|
$step = $this->createPartialMock( |
|
246
|
|
|
'Ktomk\Pipelines\File\Pipeline\Step', |
|
247
|
|
|
array('getPipeline') |
|
248
|
|
|
); |
|
249
|
|
|
$step->method('getPipeline') |
|
250
|
|
|
->willReturn( |
|
251
|
|
|
$this->createMock('Ktomk\Pipelines\File\Pipeline') |
|
252
|
|
|
); |
|
253
|
|
|
|
|
254
|
|
|
return $step; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.