/
FeatureContext.php
460 lines (405 loc) · 15.4 KB
/
FeatureContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
use Behat\Behat\Context\SnippetAcceptingContext;
/**
* Defines application features from the specific context.
*/
class FeatureContext implements SnippetAcceptingContext
{
protected $buildDir;
protected $serverDir;
protected $updateServerDir;
protected $tmpDownloadDir;
protected $downloadURL = 'https://download.nextcloud.com/server/releases/';
protected $dailyDownloadURL = 'https://download.nextcloud.com/server/daily/latest-';
protected $prereleasesDownloadURL = 'https://download.nextcloud.com/server/prereleases/';
/** @var resource */
protected $updaterServerProcess = null;
/** @var string[] */
protected $CLIOutput;
/** @var integer */
protected $CLIReturnCode;
/** @var string */
protected $autoupdater = '1';
public function __construct()
{
$baseDir = __DIR__ . '/../../data/';
$this->serverDir = $baseDir . 'server/';
$this->tmpDownloadDir = $baseDir . 'downloads/';
$this->updateServerDir = $baseDir . 'update-server/';
$this->buildDir = $baseDir . '../../';
if(!file_exists($baseDir) && !mkdir($baseDir)) {
throw new RuntimeException('Creating tmp download dir failed');
}
if(!file_exists($this->serverDir) && !mkdir($this->serverDir)) {
throw new RuntimeException('Creating server dir failed');
}
if(!file_exists($this->tmpDownloadDir) && !mkdir($this->tmpDownloadDir)) {
throw new RuntimeException('Creating tmp download dir failed');
}
if(!file_exists($this->updateServerDir) && !mkdir($this->updateServerDir)) {
throw new RuntimeException('Creating update server dir failed');
}
}
/**
* @AfterScenario
*/
public function stopUpdateServer()
{
if(is_resource($this->updaterServerProcess)) {
proc_terminate($this->updaterServerProcess);
proc_close($this->updaterServerProcess);
}
}
/**
* @Given /the current (installed )?version is ([0-9.]+((beta|RC)[0-9]?)?|stable[0-9]+|master)/
*/
public function theCurrentInstalledVersionIs($installed, $version)
{
// recursive deletion of server folder
if(file_exists($this->serverDir)) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->serverDir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $fileInfo) {
$action = $fileInfo->isDir() ? 'rmdir' : 'unlink';
$action($fileInfo->getRealPath());
}
$state = rmdir($this->serverDir);
if($state === false) {
throw new \Exception('Could not rmdir ' . $this->serverDir);
}
}
$filename = 'nextcloud-' . $version . '.zip';
if (!file_exists($this->tmpDownloadDir . $filename)) {
$fp = fopen($this->tmpDownloadDir . $filename, 'w+');
$url = $this->downloadURL . $filename;
if (strpos($version, 'RC') !== false || strpos($version, 'beta') !== false) {
$url = $this->prereleasesDownloadURL . 'nextcloud-' . $version . '.zip';
} else if(strpos($version, 'stable') !== false || strpos($version, 'master') !== false) {
$url = $this->dailyDownloadURL . $version . '.zip';
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
if(curl_exec($ch) === false) {
throw new \Exception('Curl error: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode !== 200) {
throw new \Exception('Download failed - HTTP code: ' . $httpCode);
}
curl_close($ch);
fclose($fp);
}
$zip = new ZipArchive;
$zipState = $zip->open($this->tmpDownloadDir . $filename);
if ($zipState === true) {
$zip->extractTo($this->serverDir);
$zip->close();
} else {
throw new \Exception('Cant handle ZIP file. Error code is: '.$zipState);
}
if($installed === '') {
// the instance should not be installed
return;
}
chdir($this->serverDir . 'nextcloud');
shell_exec('chmod +x occ');
exec('./occ maintenance:install --admin-user=admin --admin-pass=admin 2>&1', $output, $returnCode);
if ($returnCode !== 0) {
throw new Exception('Install failed' . PHP_EOL . join(PHP_EOL, $output));
}
}
/**
* @Given there is no update available
*/
public function thereIsNoUpdateAvailable()
{
$this->runUpdateServer();
$content = '';
file_put_contents($this->updateServerDir . 'index.php', $content);
}
/**
* @Given the autoupdater is disabled
*/
public function theAutoupdaterIsDisabled() {
$this->autoupdater = '0';
}
/**
* @When the CLI updater is run successfully
*/
public function theCliUpdaterIsRunSuccessfully()
{
$this->theCliUpdaterIsRun();
if ($this->CLIReturnCode !== 0) {
throw new Exception('updater failed' . PHP_EOL . join(PHP_EOL, $this->CLIOutput));
}
}
/**
* @When the CLI updater is run
*/
public function theCliUpdaterIsRun()
{
if(!file_exists($this->buildDir . 'updater.phar')) {
throw new Exception('updater.phar not available - please build it in advance via "box build -c box.json"');
}
copy($this->buildDir . 'updater.phar', $this->serverDir . 'nextcloud/updater/updater');
chdir($this->serverDir . 'nextcloud/updater');
chmod($this->serverDir . 'nextcloud/updater/updater', 0755);
exec('./updater -n', $output, $returnCode);
$this->CLIOutput = $output;
$this->CLIReturnCode = $returnCode;
}
/**
* @param $version
* @return string
*/
public function getSignatureForVersion($version) {
$signatures = [
'9.0.54' => 'wVgtLXquICXsmfJwRUD8tJiFzYJAIdQfjAcOzvKGDYh96NMT6MGVEMYQgAfyYvq0
tuAcqsU87CDc1IQ14y2GmkSnwnXJrCXEJFaYqBGyXJtbzRukby5k+IVx2NTBaNjL
XMC1irGa7tnCC/pyn9K+RDDHSTa3aQ7W0Z2MIq+TpNuASwshOGaep9IP7bmVvEsS
CC8df8qp8xBkUA6PLxDkrHHGe1dTauuMYc0hUkzclC+fD2wFzV4ks1RpX6V4dLlZ
4+nzlDeepIAVOnoaIaxLv4DmITD5Mg408z/CCB2YBBntFC9wIlfErr9X4JqQWVEQ
Xpo6Rlr6bSFMHcDn3Bjn+Q==',
'9.0.55RC1' => 'uUGqJ6shKJCxoP9UvPclGewzN3jxX1blMyB27RsLN/jEf1Y6nnxz3kVyeXQcUuvV
P54w4cyKBnj8+mMJV57bnIUf42B1GFIiHTILpauDcC6KqsEt/kGIUDiHtZjZAJKr
jzuUjSAXKjTxeAQv0l9H7nQaa7Xs7LgWTV9LycQVLksYoV0MDBMOjBuxH017jgQH
AAzqdiQguM2pv5+j6AZcP6q1YRueLePTcM2q+AlDB52LYA1Dfmj81jsP1J9WmbXJ
IfbjOzrkifG5mJB+Q7/HOJjQYevaxjL6prSp0yym7nqSy63vH8GDxKa+gIK5Gfj+
0mo/sSnvB5vobDDb/TO8og==',
'10.0.1' => 'w64N/qoNW2jiStnNXWJBhVkD0eLs75ELvbAnDY2wCyFM5TlqTeKlBnwK7zuIOzwD
h5/k0tExXra1fBPic6qUF30Z9n1O6C60z0zKQYOaHrR4I4EN1SdHFsjbLyXxm/Ua
F3Dw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3xConuKfcyojJ3PGsO8x7awtRNA7
2GBBBTNKY9/q8eONztsBWY7O2pWMTqJKEuXiXQyAQ3IkqxQkMWoyxKU/zGWBdZad
QYwN9zVszIoWo31nlVz7QSzUZ4iyEVnbY4gT5EVPKfflIPQblp2bN485pYamuPe5
CYB6S4AUzuyYS2zxt7MMdoaWHXY8hxdst1AF/kGKZ5Bdct4qVw7pup77c+uMNBvT
jFSE6+KKI+HAE132eaXY5A==',
];
if(isset($signatures[$version])) {
return $signatures[$version];
}
return '';
}
/**
* @Given /there is an update to version ([0-9.]+) available/
*/
public function thereIsAnUpdateToVersionAvailable($version)
{
$this->runUpdateServer();
$content = '<?php
header("Content-Type: application/xml");
?>
<?xml version="1.0" encoding="UTF-8"?>
<nextcloud>
<version>' . str_replace(['9.1', '9.2'], ['10.0', '11.0'], $version) . '</version>
<versionstring>Nextcloud ' . $version . '</versionstring>
<url>https://download.nextcloud.com/server/releases/nextcloud-' . $version . '.zip</url>
<web>https://docs.nextcloud.org/server/10/admin_manual/maintenance/manual_upgrade.html</web>
<autoupdater>' . $this->autoupdater . '</autoupdater>
<signature>'.$this->getSignatureForVersion($version).'</signature>
</nextcloud>
';
file_put_contents($this->updateServerDir . 'index.php', $content);
}
/**
* @Given there is an update to prerelease version :version available
*/
public function thereIsAnUpdateToPrereleaseVersionAvailable($version)
{
$this->runUpdateServer();
$content = '<?php
header("Content-Type: application/xml");
?>
<?xml version="1.0" encoding="UTF-8"?>
<nextcloud>
<version>' . str_replace(['9.1', '9.2'], ['10.0', '11.0'], $version) . '</version>
<versionstring>Nextcloud ' . $version . '</versionstring>
<url>https://download.nextcloud.com/server/prereleases/nextcloud-' . $version . '.zip</url>
<web>https://docs.nextcloud.org/server/10/admin_manual/maintenance/manual_upgrade.html</web>
<autoupdater>1</autoupdater>
<signature>'.$this->getSignatureForVersion($version).'</signature>
</nextcloud>
';
file_put_contents($this->updateServerDir . 'index.php', $content);
}
/**
* @Given /there is an update to daily version of (.*) available/
*/
public function thereIsAnUpdateToDailyVersionAvailable($version)
{
$this->runUpdateServer();
$content = '<?php
header("Content-Type: application/xml");
?>
<?xml version="1.0" encoding="UTF-8"?>
<nextcloud>
<version>100.0.0.0</version>
<versionstring>Nextcloud ' . $version . '</versionstring>
<url>https://download.nextcloud.com/server/daily/latest-' . $version . '.zip</url>
<web>https://docs.nextcloud.org/server/10/admin_manual/maintenance/manual_upgrade.html</web>
<autoupdater>1</autoupdater>
</nextcloud>
';
file_put_contents($this->updateServerDir . 'index.php', $content);
}
/**
* runs the updater server
* @throws Exception
*/
protected function runUpdateServer()
{
$configFile = $this->serverDir . 'nextcloud/config/config.php';
$content = file_get_contents($configFile);
$content = preg_replace('!\$CONFIG\s*=\s*array\s*\(!', "\$CONFIG = array(\n 'updater.server.url' => 'http://localhost:8870/',", $content );
file_put_contents($configFile, $content);
if (!is_null($this->updaterServerProcess)) {
throw new Exception('Update server already started');
}
$cmd = "php -S localhost:8870 -t " . $this->updateServerDir . " 2>/dev/null 1>/dev/null";
$this->updaterServerProcess = proc_open($cmd, [], $pipes, $this->updateServerDir);
if(!is_resource($this->updaterServerProcess)) {
throw new Exception('Update server could not be started');
}
// to let the server start
sleep(1);
}
/**
* @Then /the installed version should be ([0-9.]+)/
*/
public function theInstalledVersionShouldBe2($version)
{
/** @var $OC_Version */
require $this->serverDir . 'nextcloud/version.php';
$installedVersion = join('.', $OC_Version);
// Hack for version number mapping
$installedVersion = str_replace(['9.1', '9.2'], ['10.0', '11.0'], $installedVersion);
if (strpos($installedVersion, $version) !== 0) {
throw new Exception('Version mismatch - Installed: ' . $installedVersion . ' Wanted: ' . $version);
}
}
/**
* @Then /maintenance mode should be (on|off)/
*/
public function maintenanceModeShouldBe($state)
{
chdir($this->serverDir . 'nextcloud');
shell_exec('chmod +x occ');
exec('./occ maintenance:mode', $output, $returnCode);
$expectedOutput = 'Maintenance mode is currently ' .
($state === 'on' ? 'enabled' : 'disabled');
if ($returnCode !== 0 || strpos(join(PHP_EOL, $output), $expectedOutput) === false) {
throw new Exception('Maintenance mode does not match ' . PHP_EOL . join(PHP_EOL, $output));
}
}
/**
* @Given the current channel is :channel
* @param string $channel
*/
public function theCurrentChannelIs($channel)
{
chdir($this->serverDir . 'nextcloud');
shell_exec('chmod +x occ');
exec('./occ config:system:set --value '.$channel.' updater.release.channel');
}
/**
* @Then /upgrade is (not required|required)/
*/
public function upgradeIs($state)
{
chdir($this->serverDir . 'nextcloud');
shell_exec('chmod +x occ');
exec('./occ status', $output, $returnCode);
$upgradeOutput = 'Nextcloud or one of the apps require upgrade';
$outputString = join(PHP_EOL, $output);
if ($returnCode !== 0) {
throw new Exception('Return code of status output does not match ' . PHP_EOL . $outputString);
}
if ($state === 'not required') {
if (strpos($outputString, $upgradeOutput) !== false) {
throw new Exception('Upgrade is required ' . PHP_EOL . join(PHP_EOL, $output));
}
} else {
if (strpos($outputString, $upgradeOutput) === false) {
throw new Exception('Upgrade is not required ' . PHP_EOL . join(PHP_EOL, $output));
}
}
}
/**
* @Then /the return code should not be (\S*)/
*/
public function theReturnCodeShouldNotBe($expectedReturnCode)
{
if ($this->CLIReturnCode === (int)$expectedReturnCode) {
throw new Exception('Return code does match but should not match: ' . $this->CLIReturnCode . PHP_EOL . join(PHP_EOL, $this->CLIOutput));
}
}
/**
* @Then /the output should contain "(.*)"/
*/
public function theOutputShouldBe($expectedOutput)
{
if (strpos(join(PHP_EOL, $this->CLIOutput), $expectedOutput) === false) {
throw new Exception('Output does not match: ' . PHP_EOL . join(PHP_EOL, $this->CLIOutput));
}
}
/**
* @Given /the version number is decreased in the config.php to enforce upgrade/
*/
public function theVersionNumberIsDecreasedInTheConfigPHPToEnforceUpgrade()
{
$configFile = $this->serverDir . 'nextcloud/config/config.php';
$content = file_get_contents($configFile);
$content = preg_replace("!'version'\s*=>\s*'(\d+\.\d+\.\d+)\.\d+!", "'version' => '$1", $content);
file_put_contents($configFile, $content);
}
/**
* @Given there is a folder called :name
*/
public function thereIsAFolderCalled($name)
{
mkdir($this->serverDir . 'nextcloud/' . $name);
}
/**
* @Given there is a config for a secondary apps directory called :name
*/
public function thereIsAConfigForASecondaryAppsDirectoryCalled($name)
{
$configFile = $this->serverDir . 'nextcloud/config/config.php';
$content = file_get_contents($configFile);
$appsPaths = <<<EOF
'apps_paths' => [
[
'path'=> dirname(__DIR__) . '/apps',
'url' => '/apps',
'writable' => true,
],
[
'path'=> dirname(__DIR__) . '/%s',
'url' => '/%s',
'writable' => true,
],
],
EOF;
$appsPaths = sprintf($appsPaths, $name, $name);
$content = preg_replace("!\);!", $appsPaths . ');', $content);
file_put_contents($configFile, $content);
}
}