|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Cli\Docker; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Cli\Exec; |
|
8
|
|
|
use Ktomk\Pipelines\Cli\ExecTester; |
|
9
|
|
|
use Ktomk\Pipelines\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class DockerProcessManagerTest |
|
13
|
|
|
* |
|
14
|
|
|
* @covers \Ktomk\Pipelines\Cli\Docker\ProcessManager |
|
15
|
|
|
*/ |
|
16
|
|
|
class ProcessManagerTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testCreation() |
|
19
|
|
|
{ |
|
20
|
|
|
$ps = new ProcessManager(new Exec()); |
|
21
|
|
|
self::assertInstanceOf('Ktomk\Pipelines\Cli\Docker\ProcessManager', $ps); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testFindAllContainerIdsByNamePrefix() |
|
25
|
|
|
{ |
|
26
|
|
|
$exec = new ExecTester($this); |
|
27
|
|
|
$exec->expect('capture', 'docker', 0); |
|
28
|
|
|
|
|
29
|
|
|
$ps = new ProcessManager($exec); |
|
30
|
|
|
self::assertIsArray($ps->findAllContainerIdsByNamePrefix('pipelines')); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testInvalidPrefix() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->expectException('InvalidArgumentException'); |
|
38
|
|
|
$this->expectExceptionMessage('Invalid container name prefix "**wrong**"'); |
|
39
|
|
|
|
|
40
|
|
|
$exec = new ExecTester($this); |
|
41
|
|
|
$ps = new ProcessManager($exec); |
|
42
|
|
|
$ps->findAllContainerIdsByNamePrefix('**wrong**'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testFindRunningContainerIdsByNamePrefix() |
|
46
|
|
|
{ |
|
47
|
|
|
$exec = new ExecTester($this); |
|
48
|
|
|
$exec->expect('capture', 'docker', 0); |
|
49
|
|
|
|
|
50
|
|
|
$ps = new ProcessManager($exec); |
|
51
|
|
|
self::assertIsArray($ps->findRunningContainerIdsByNamePrefix('pipelines')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testKill() |
|
55
|
|
|
{ |
|
56
|
|
|
$exec = new ExecTester($this); |
|
57
|
|
|
$exec->expect('capture', 'docker', 0); |
|
58
|
|
|
|
|
59
|
|
|
$ps = new ProcessManager($exec); |
|
60
|
|
|
self::assertIsInt($ps->kill( |
|
61
|
|
|
'fc2ed48d903ddba765dd89a6f82baaed4612c0c23aa2558320aad889dd29d74c' |
|
62
|
|
|
)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testRemove() |
|
66
|
|
|
{ |
|
67
|
|
|
$exec = new ExecTester($this); |
|
68
|
|
|
$exec->expect('capture', 'docker', 0); |
|
69
|
|
|
|
|
70
|
|
|
$ps = new ProcessManager($exec); |
|
71
|
|
|
self::assertIsInt($ps->remove( |
|
72
|
|
|
array('fc2ed48d903ddba765dd89a6f82baaed4612c0c23aa2558320aad889dd29d74c') |
|
73
|
|
|
)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testFindAllContainerIdsByName() |
|
77
|
|
|
{ |
|
78
|
|
|
$exec = new ExecTester($this); |
|
79
|
|
|
$exec->expect('capture', 'docker', 0, 'docke ps'); |
|
80
|
|
|
$pm = new ProcessManager($exec); |
|
81
|
|
|
self::assertSame( |
|
82
|
|
|
array(), |
|
83
|
|
|
$pm->findAllContainerIdsByName('foo') |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testZapContainersByName() |
|
88
|
|
|
{ |
|
89
|
|
|
$pm = $this->createPartialMock( |
|
90
|
|
|
'Ktomk\Pipelines\Cli\Docker\ProcessManager', |
|
91
|
|
|
array('findAllContainerIdsByName', 'kill', 'remove') |
|
92
|
|
|
); |
|
93
|
|
|
$pm->expects(self::once()) |
|
94
|
|
|
->method('kill'); |
|
95
|
|
|
$pm->expects(self::once()) |
|
96
|
|
|
->method('remove'); |
|
97
|
|
|
$pm->method('findAllContainerIdsByName') |
|
98
|
|
|
->willReturn(array('1234567')); |
|
99
|
|
|
$pm->zapContainersByName('foo'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testZapContainersByNameNoMatches() |
|
103
|
|
|
{ |
|
104
|
|
|
$pm = $this->createPartialMock( |
|
105
|
|
|
'Ktomk\Pipelines\Cli\Docker\ProcessManager', |
|
106
|
|
|
array('findAllContainerIdsByName') |
|
107
|
|
|
); |
|
108
|
|
|
$pm->zapContainersByName('foo'); |
|
109
|
|
|
$this->addToAssertionCount(1); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testFindContainerIdByName() |
|
113
|
|
|
{ |
|
114
|
|
|
$pm = $this->createPartialMock( |
|
115
|
|
|
'Ktomk\Pipelines\Cli\Docker\ProcessManager', |
|
116
|
|
|
array('findAllContainerIdsByName') |
|
117
|
|
|
); |
|
118
|
|
|
$pm->method('findAllContainerIdsByName')->willReturn(null); |
|
119
|
|
|
self::assertNull($pm->findContainerIdByName('foo')); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testFindContainerIdByNameNoMatches() |
|
123
|
|
|
{ |
|
124
|
|
|
$pm = $this->createPartialMock( |
|
125
|
|
|
'Ktomk\Pipelines\Cli\Docker\ProcessManager', |
|
126
|
|
|
array('findAllContainerIdsByName') |
|
127
|
|
|
); |
|
128
|
|
|
$pm->method('findAllContainerIdsByName')->willReturn(array()); |
|
129
|
|
|
self::assertNull($pm->findContainerIdByName('foo')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testFindContainerIdByNameSingleMatch() |
|
133
|
|
|
{ |
|
134
|
|
|
$pm = $this->createPartialMock( |
|
135
|
|
|
'Ktomk\Pipelines\Cli\Docker\ProcessManager', |
|
136
|
|
|
array('findAllContainerIdsByName') |
|
137
|
|
|
); |
|
138
|
|
|
$pm->method('findAllContainerIdsByName')->willReturn(array('1234567')); |
|
139
|
|
|
self::assertSame('1234567', $pm->findContainerIdByName('foo')); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testFindContainerIdByNameMultipleMatches() |
|
143
|
|
|
{ |
|
144
|
|
|
$pm = $this->createPartialMock( |
|
145
|
|
|
'Ktomk\Pipelines\Cli\Docker\ProcessManager', |
|
146
|
|
|
array('findAllContainerIdsByName') |
|
147
|
|
|
); |
|
148
|
|
|
$pm->method('findAllContainerIdsByName')->willReturn( |
|
149
|
|
|
array('1234567', '3456789') |
|
150
|
|
|
); |
|
151
|
|
|
self::assertNull($pm->findContainerIdByName('foo')); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|