-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathFeatureContext.php
More file actions
617 lines (512 loc) · 17.5 KB
/
FeatureContext.php
File metadata and controls
617 lines (512 loc) · 17.5 KB
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
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;
/** @var string[] */
protected $CLIOutput;
/** @var integer */
protected $CLIReturnCode;
/** @var string */
protected $autoupdater = '1';
/** @var bool */
protected $skipIt = false;
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|rc)[0-9]?)?|stable[0-9]+|master)/
*/
public function theCurrentInstalledVersionIs($installed, $version) {
if ($this->skipIt) {
return;
}
// 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 (str_contains((string)$version, 'RC') || str_contains((string)$version, 'rc') || str_contains((string)$version, 'beta')) {
$url = $this->prereleasesDownloadURL . 'nextcloud-' . $version . '.zip';
} elseif (str_contains((string)$version, 'stable') || str_contains((string)$version, 'master')) {
$url = $this->dailyDownloadURL . $version . '.zip';
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_USERAGENT, 'Nextcloud Updater');
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 for ' . $url . ' - 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 . implode(PHP_EOL, $output));
}
}
/**
* @Given there is no update available
*/
public function thereIsNoUpdateAvailable() {
if ($this->skipIt) {
return;
}
$this->runUpdateServer();
$content = '';
file_put_contents($this->updateServerDir . 'index.php', $content);
}
/**
* @Given the autoupdater is disabled
*/
public function theAutoupdaterIsDisabled() {
if ($this->skipIt) {
return;
}
$this->autoupdater = '0';
}
/**
* @When the CLI updater is run successfully
*/
public function theCliUpdaterIsRunSuccessfully() {
if ($this->skipIt) {
return;
}
$this->theCliUpdaterIsRun();
if ($this->CLIReturnCode !== 0) {
throw new Exception('updater failed' . PHP_EOL . implode(PHP_EOL, $this->CLIOutput));
}
}
/**
* @When the CLI updater is run
*/
public function theCliUpdaterIsRun() {
if ($this->skipIt) {
return;
}
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 2>&1', $output, $returnCode);
// sleep to let the opcache do it's work and invalidate the status.php
sleep(5);
$this->CLIOutput = $output;
$this->CLIReturnCode = $returnCode;
}
/**
* @param $version
*/
public function getSignatureForVersion(string $version): string {
$signatures = [
'25.0.1' => 'gPMcRBmMW875VGEEtP/qfpjbS8kYwVsW0DwK1DdDBrW+DduIUiwiOM4PVi99pVq2
zJ01A6pWPwx9adIiqnm7tBTgBYtrmS6S4KY4DWp+kx7ndflvO/xIH1B8PyvLuFXM
2FKj5i45YhUv8rTJ3jgmNj6UrfFB0CJTo+slHAPw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LH0hW99Jf9aFv/qqj56T0GSGlGS3qv/HNimmeC0sZfLNDdxjaDWBpVJkt+45Z
vJJ8XVSDOlNNKjcgvcUMrsDItXioSwBst6vTdR5IKLAFivlb7HYLUN48R9h57QM2
v8X/N49mF+Wk3PQa19wBVsUFYkaQuG9FTjUVgvp8bgv3s9rhrOLJa5KUOpdcodgZ
faeql723PcZEzPJ3dzisSw==',
'28.0.0beta4' => 'tguTYQ9w6cpQITNbVphOYsHGTvYPDi8aznjUM8Xyxi0HTIuK3WPBTdgrn7jPTC5+
JlwoyTQTRI2ut0SvEzVK5OrKTotPtNaNRSwpo0VAtuavEAWK6ZtH0g5oujHDyn/7
7S149qpPkbir6Lf7qMSSje92CF1LFOQDEqXW9HibfRVzMvTk2iTz//cTVcnyTxgi
QbK5O5wLmo7Gp8UNZsHL6CXTHo7p8zd8I2T86poJAttgwGIGJ0rQe1AYh/kJEOEz
CAzl6Rd033pBht1t9Y9mFfnWd70a4v9stSdhCwVo08fqxOcoJrCZQ4wwEWN3ReYj
/xB2sIdvkLkDyESNNzmhmg==',
'28.0.14' => 'e3wnEZE0ooyNX8CpsSEgXafLoOU/U+zORUyeqKczWuuf2Srq4edl2SCaQgvdSLsG
DZo8h9LLEsh544/NyS8VOY7aJVqR2JOC4bUyztfNTnlppRLVTCIXx053Eht9+neN
pYlPy8hBK+KBLoN7q3WYcWL1QOIrUAzgxhjwshMrTxNrHi8Nq7g37iZUzhPU5HWw
MUID9gsQnT+aFurooLVvWMM8Ad0RkU72i5Y7I80c+v/2MYE9rxUmNC54noVePvrj
R8zf/PC+Yj1vxFZ0hYAtweLgBxfwU5cNBYfH7M1I9FLlb88p/XDWx6XaBz4Ql6LK
lbpDxNE9UiM09JG1dU7Ebg==',
];
return $signatures[$version] ?? '';
}
private function writeUpdaterIndex(
string $version,
string $name,
string $url,
int $autoupdater,
) {
$signature = $this->getSignatureForVersion($version);
$content = <<<XML
<?php header("Content-Type: application/xml"); ?>
<nextcloud>
<version>{$version}</version>
<versionstring>{$name}</versionstring>
<url>{$url}</url>
<web>https://docs.nextcloud.org/server/10/admin_manual/maintenance/manual_upgrade.html</web>
<autoupdater>{$autoupdater}</autoupdater>
<signature>{$signature}</signature>
</nextcloud>
XML;
file_put_contents($this->updateServerDir . 'index.php', $content);
}
/**
* @Given /there is an update to version ([0-9.]+) available/
*/
public function thereIsAnUpdateToVersionAvailable($version) {
if ($this->skipIt) {
return;
}
$this->runUpdateServer();
$this->writeUpdaterIndex(
str_replace(['9.1', '9.2'], ['10.0', '11.0'], $version),
'Nextcloud ' . $version,
'https://download.nextcloud.com/server/releases/nextcloud-' . $version . '.zip',
$this->autoupdater,
);
}
/**
* @Given there is an update to prerelease version :version available
*/
public function thereIsAnUpdateToPrereleaseVersionAvailable($version) {
if ($this->skipIt) {
return;
}
$this->runUpdateServer();
$this->writeUpdaterIndex(
str_replace(['9.1', '9.2'], ['10.0', '11.0'], $version),
'Nextcloud ' . $version,
'https://download.nextcloud.com/server/prereleases/nextcloud-' . $version . '.zip',
$this->autoupdater,
);
}
/**
* @Given /there is an update to daily version of (.*) available/
*/
public function thereIsAnUpdateToDailyVersionAvailable($version) {
if ($this->skipIt) {
return;
}
$this->runUpdateServer();
$this->writeUpdaterIndex(
'100.0.0',
'Nextcloud ' . $version,
'https://download.nextcloud.com/server/daily/latest-' . $version . '.zip',
1,
);
}
/**
* 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) {
if ($this->skipIt) {
return;
}
/** @var $OC_Version */
require $this->serverDir . 'nextcloud/version.php';
$installedVersion = implode('.', $OC_Version);
if (!str_starts_with($installedVersion, (string)$version)) {
throw new Exception('Version mismatch - Installed: ' . $installedVersion . ' Wanted: ' . $version);
}
}
/**
* @Then /maintenance mode should be (on|off)/
*/
public function maintenanceModeShouldBe($state) {
if ($this->skipIt) {
return;
}
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 || in_array(str_contains(implode(PHP_EOL, $output), $expectedOutput), [0, false], true)) {
throw new Exception('Maintenance mode does not match ' . PHP_EOL . implode(PHP_EOL, $output));
}
}
/**
* @Given the current channel is :channel
* @param string $channel
*/
public function theCurrentChannelIs($channel) {
if ($this->skipIt) {
return;
}
chdir($this->serverDir . 'nextcloud');
shell_exec('chmod +x occ');
exec('./occ config:system:set --value ' . $channel . ' updater.release.channel');
}
/**
* @Given the config key :key is set to :value of type :type
* @param string $type ('string', 'boolean', 'integer', 'double')
*/
public function theConfigKeyIsSetTo(string $key, mixed $value, string $type = 'string') {
if ($this->skipIt) {
return;
}
if (!in_array($type, ['string', 'boolean', 'integer', 'double'])) {
throw new Exception('Invalid type given: ' . $type);
}
chdir($this->serverDir . 'nextcloud');
shell_exec('chmod +x occ');
exec(sprintf("./occ config:system:set %s --value '%s' --type '%s'", $key, $value, $type));
}
/**
* @Then the user ini file contains :content
*/
public function theUserIniFileContains(string $content) {
if ($this->skipIt) {
return;
}
$userIniFile = $this->serverDir . 'nextcloud/.user.ini';
if (!file_exists($userIniFile)) {
throw new Exception('User ini file does not exist: ' . $userIniFile);
}
$contents = file_get_contents($userIniFile);
if (!str_contains($contents, $content)) {
throw new Exception('Content not found in user ini file: ' . $content);
}
}
/**
* @Then /upgrade is (not required|required)/
*/
public function upgradeIs($state) {
if ($this->skipIt) {
return;
}
chdir($this->serverDir . 'nextcloud');
shell_exec('chmod +x occ');
exec('./occ status', $output, $returnCode);
$upgradeOutput = 'Nextcloud or one of the apps require upgrade';
$outputString = implode(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 (str_contains($outputString, $upgradeOutput)) {
throw new Exception('Upgrade is required ' . PHP_EOL . implode(PHP_EOL, $output));
}
} elseif (in_array(str_contains($outputString, $upgradeOutput), [0, false], true)) {
throw new Exception('Upgrade is not required ' . PHP_EOL . implode(PHP_EOL, $output));
}
}
/**
* @Then /the return code should not be (\S*)/
*/
public function theReturnCodeShouldNotBe($expectedReturnCode) {
if ($this->skipIt) {
return;
}
if ($this->CLIReturnCode === (int)$expectedReturnCode) {
throw new Exception('Return code does match but should not match: ' . $this->CLIReturnCode . PHP_EOL . implode(PHP_EOL, $this->CLIOutput));
}
}
/**
* @Then /the output should contain "(.*)"/
*/
public function theOutputShouldBe($expectedOutput) {
if ($this->skipIt) {
return;
}
if (in_array(str_contains(implode(PHP_EOL, $this->CLIOutput), (string)$expectedOutput), [0, false], true)) {
throw new Exception('Output does not match: ' . PHP_EOL . implode(PHP_EOL, $this->CLIOutput));
}
}
/**
* @Given /the version number is decreased in the config.php to enforce upgrade/
*/
public function theVersionNumberIsDecreasedInTheConfigPHPToEnforceUpgrade() {
if ($this->skipIt) {
return;
}
$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) {
if ($this->skipIt) {
return;
}
mkdir($this->serverDir . 'nextcloud/' . $name);
}
/**
* @Given there is a config for a secondary apps directory called :name
*/
public function thereIsAConfigForASecondaryAppsDirectoryCalled($name) {
if ($this->skipIt) {
return;
}
$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);
}
/**
* @Given /PHP is at least in version ([0-9.]+)/
*/
public function phpIsAtLeastInVersion($version) {
$this->skipIt = in_array(version_compare($version, PHP_VERSION, '<'), [0, false], true);
}
/**
* @Given the update server returns HTTP status :statusCode
*/
public function theUpdateServerReturnsHttpStatus(int $statusCode) {
if ($this->skipIt) {
return;
}
$this->runUpdateServer();
$content = '<?php http_response_code(' . $statusCode . '); echo "Server Error";';
file_put_contents($this->updateServerDir . 'index.php', $content);
}
/**
* @Given the update server returns invalid XML
*/
public function theUpdateServerReturnsInvalidXml() {
if ($this->skipIt) {
return;
}
$this->runUpdateServer();
$content = '<?php header("Content-Type: application/xml"); echo "this is not valid xml <><><";';
file_put_contents($this->updateServerDir . 'index.php', $content);
}
/**
* @Given the update server is unreachable
*/
public function theUpdateServerIsUnreachable() {
if ($this->skipIt) {
return;
}
// Point updater.server.url at a port with nothing listening
$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:8871/',",
$content
);
file_put_contents($configFile, $content);
// Intentionally do NOT start any server on port 8871
}
}