|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Utility; |
|
6
|
|
|
|
|
7
|
|
|
use Ktomk\Pipelines\Cli\Streams; |
|
8
|
|
|
use Ktomk\Pipelines\TestCase; |
|
9
|
|
|
use Ktomk\Pipelines\Yaml\Yaml; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @covers \Ktomk\Pipelines\Utility\App |
|
13
|
|
|
*/ |
|
14
|
|
|
class AppTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testCreation() |
|
17
|
|
|
{ |
|
18
|
|
|
$app = App::create(); |
|
19
|
|
|
self::assertNotNull($app); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testMainExceptionHandlingArgsException() |
|
23
|
|
|
{ |
|
24
|
|
|
$app = new App(new Streams(null, null, 'php://output')); |
|
25
|
|
|
$this->expectOutputRegex( |
|
26
|
|
|
'{^pipelines: option --prefix requires an argument\npipelines: version .*\n--------\nclass....:}' |
|
27
|
|
|
); |
|
28
|
|
|
$actual = $app->main(array('cmd', '--debug', '--prefix')); |
|
29
|
|
|
self::assertNotSame(0, $actual); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testMainExceptionHandlingParseException() |
|
33
|
|
|
{ |
|
34
|
|
|
$app = new App(new Streams()); |
|
35
|
|
|
$actual = $app->main(array( |
|
36
|
|
|
'cmd', '--file', 'test/data/yml/invalid-pipeline.yml', |
|
37
|
|
|
'--pipeline', 'custom/unit-tests', |
|
38
|
|
|
)); |
|
39
|
|
|
self::assertSame(2, $actual); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* --verbose as old test behaviour, |
|
44
|
|
|
* --prefix misses argument so exit status is 1 and usage information is shown |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testMainVerbosePrinter() |
|
47
|
|
|
{ |
|
48
|
|
|
$app = new App(new Streams(null, 'php://output', null)); |
|
49
|
|
|
$this->expectOutputRegex('{^usage: pipelines }m'); |
|
50
|
|
|
$actual = $app->main(array('cmd', '--verbose', '--prefix')); |
|
51
|
|
|
self::assertSame(1, $actual); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* --prefix has invalid argument so exit status is 1 |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testInvalidPrefix() |
|
58
|
|
|
{ |
|
59
|
|
|
$app = new App(new Streams(null, null, 'php://output')); |
|
60
|
|
|
$this->expectOutputRegex('{^pipelines: invalid prefix: \'123\'\n}'); |
|
61
|
|
|
$actual = $app->main(array('cmd', '--prefix', '123')); |
|
62
|
|
|
self::assertSame(1, $actual); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* --basename has invalid argument so exit status is 1 |
|
67
|
|
|
*/ |
|
68
|
|
|
public function testInvalidEmptyBasename() |
|
69
|
|
|
{ |
|
70
|
|
|
$app = new App(new Streams(null, null, 'php://output')); |
|
71
|
|
|
$this->expectOutputRegex('{^pipelines: not a basename: \'\'\n}'); |
|
72
|
|
|
$actual = $app->main(array('cmd', '--basename', '')); |
|
73
|
|
|
self::assertSame(1, $actual); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testEmptyFileStatus() |
|
77
|
|
|
{ |
|
78
|
|
|
$app = new App(new Streams()); |
|
79
|
|
|
$actual = $app->main(array('cmd', '--file', '')); |
|
80
|
|
|
self::assertSame(1, $actual); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testFileNonDefaultBasenameChange() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->expectOutputRegex('{^info: --file overrides non-default --basename$}m'); |
|
86
|
|
|
$app = new App(new Streams(null, 'php://output')); |
|
87
|
|
|
$actual = $app->main(array('cmd', '--basename', 'pipelines.yml', '--file', 'my-pipelines.yml', '-v')); |
|
88
|
|
|
self::assertSame(1, $actual); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testFileAbsolutePath() |
|
92
|
|
|
{ |
|
93
|
|
|
$file = __DIR__ . '/my-pipelines.yml'; |
|
94
|
|
|
$this->expectOutputRegex(sprintf('{^pipelines: not a readable file: %s}', preg_quote($file, '{}'))); |
|
95
|
|
|
$app = new App(new Streams(null, null, 'php://output')); |
|
96
|
|
|
$actual = $app->main(array('cmd', '--file', $file)); |
|
97
|
|
|
self::assertSame(1, $actual); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* passing '-' as file for stdin is fine, but parsing won't work as it waits for input |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testFileIsStdinSpecialFile() |
|
104
|
|
|
{ |
|
105
|
|
|
Yaml::$classes = array(''); |
|
106
|
|
|
|
|
107
|
|
|
$this->expectOutputRegex( |
|
108
|
|
|
sprintf('{^info: reading pipelines from stdin\npipelines: fatal: No YAML parser available$}m') |
|
109
|
|
|
); |
|
110
|
|
|
$app = new App(new Streams(null, 'php://output', 'php://output')); |
|
111
|
|
|
$actual = $app->main(array('cmd', '--verbose', '--file', '-')); |
|
112
|
|
|
self::assertSame(2, $actual); |
|
113
|
|
|
|
|
114
|
|
|
Yaml::$classes = array(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testBasenameLookupWorkingDirectoryChange() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->expectOutputRegex(sprintf( |
|
120
|
|
|
'{^info: changing working directory to %s$}m', |
|
121
|
|
|
preg_quote(dirname(dirname(dirname(__DIR__))), '{}') |
|
122
|
|
|
)); |
|
123
|
|
|
$app = new App(new Streams(null, 'php://output')); |
|
124
|
|
|
$actual = $app->main(array('cmd', '--working-dir', __DIR__, '-v', '--no-run')); |
|
125
|
|
|
self::assertSame(0, $actual); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testInvalidWorkingDirStatus() |
|
129
|
|
|
{ |
|
130
|
|
|
$app = new App(new Streams()); |
|
131
|
|
|
$actual = @$app->main(array('cmd', '--working-dir', '/foo/comes/bar/and/thanks/for/the-fish')); |
|
132
|
|
|
self::assertSame(2, $actual); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testNoPipelineToRunStatus() |
|
136
|
|
|
{ |
|
137
|
|
|
$app = new App(new Streams()); |
|
138
|
|
|
$actual = $app->main(array('cmd', '--file', 'test/data/yml/no-default-pipeline.yml')); |
|
139
|
|
|
self::assertSame(1, $actual); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testShowPipelinesWithError() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->expectOutputRegex('{ERROR\s+\'image\' invalid Docker image}'); |
|
145
|
|
|
$app = new App(new Streams(null, 'php://output')); |
|
146
|
|
|
$actual = $app->main(array('cmd', '--file', 'test/data/yml/invalid-pipeline.yml', '--show')); |
|
147
|
|
|
self::assertSame(1, $actual); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testUnknownDeployMode() |
|
151
|
|
|
{ |
|
152
|
|
|
$this->expectOutputRegex('{^pipelines: unknown deploy mode \'flux-compensate\'}'); |
|
153
|
|
|
$app = new App(new Streams(null, null, 'php://output')); |
|
154
|
|
|
$actual = $app->main(array('cmd', '--deploy', 'flux-compensate')); |
|
155
|
|
|
self::assertSame(1, $actual); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function testUnknownOption() |
|
159
|
|
|
{ |
|
160
|
|
|
$option = '--meltdown-trampoline-kernel-patch'; |
|
161
|
|
|
$this->expectOutputRegex(sprintf('{^pipelines: unknown option: %s}', preg_quote($option, '{}'))); |
|
162
|
|
|
$app = new App(new Streams(null, null, 'php://output')); |
|
163
|
|
|
$actual = $app->main(array('cmd', $option)); |
|
164
|
|
|
self::assertSame(1, $actual); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function provideArguments() |
|
168
|
|
|
{ |
|
169
|
|
|
return array( |
|
170
|
|
|
array(array('--version')), |
|
171
|
|
|
array(array('--help')), |
|
172
|
|
|
array(array('--show')), |
|
173
|
|
|
array(array('--images')), |
|
174
|
|
|
array(array('--list')), |
|
175
|
|
|
array(array('--dry-run')), |
|
176
|
|
|
array(array('--verbose', '--dry-run')), |
|
177
|
|
|
array(array('--keep', '--dry-run')), |
|
178
|
|
|
array(array('--no-keep', '--no-run')), |
|
179
|
|
|
array(array('--error-keep', '--dry-run')), |
|
180
|
|
|
array(array('--docker-list', '--dry-run')), |
|
181
|
|
|
array(array('--verbatim', '--dry-run')), |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param array $arguments |
|
187
|
|
|
* @dataProvider provideArguments |
|
188
|
|
|
*/ |
|
189
|
|
|
public function testSuccessfulCommands(array $arguments) |
|
190
|
|
|
{ |
|
191
|
|
|
$app = new App(new Streams()); |
|
192
|
|
|
$args = array_merge((array)'pipelines-test', $arguments); |
|
193
|
|
|
$status = $app->main($args); |
|
194
|
|
|
self::assertSame(0, $status); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function provideErroneousArguments() |
|
198
|
|
|
{ |
|
199
|
|
|
return array( |
|
200
|
|
|
array( |
|
201
|
|
|
"pipelines: --keep and --no-keep are exclusive\n", |
|
202
|
|
|
array('--keep', '--no-keep', '--no-run'), |
|
203
|
|
|
), |
|
204
|
|
|
array( |
|
205
|
|
|
"pipelines: pipeline 'fromage/baguette' unavailable\n", |
|
206
|
|
|
array('--pipeline', 'fromage/baguette'), |
|
207
|
|
|
), |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param string $output expected error output |
|
213
|
|
|
* @param array $arguments |
|
214
|
|
|
* @dataProvider provideErroneousArguments |
|
215
|
|
|
*/ |
|
216
|
|
|
public function testErroneousCommands($output, array $arguments) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->expectOutputString($output); |
|
219
|
|
|
$app = new App(new Streams(null, null, 'php://output')); |
|
220
|
|
|
$args = array_merge((array)'pipelines-test', $arguments); |
|
221
|
|
|
$status = $app->main($args); |
|
222
|
|
|
self::assertSame(1, $status); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|