Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FeatureContext.php « bootstrap « features « tests - github.com/nextcloud/updater.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f0090c2237273d65e97d33a46fe04f517c29258 (plain)
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
<?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';
	/** @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)[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 (strpos($version, 'RC') !== false || strpos($version, 'beta') !== false) {
				$url = $this->prereleasesDownloadURL . 'nextcloud-' . $version . '.zip';
			} elseif (strpos($version, 'stable') !== false || strpos($version, 'master') !== false) {
				$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 - 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() {
		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 . join(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', $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
	 * @return string
	 */
	public function getSignatureForVersion($version) {
		$signatures = [
			'19.0.0beta4' => 'Cum4wIKpCRHNZuOQ/SfDYsIp39/4/Z3EIiMLTV7vFLf1pjsn+q1FRwk7HbT0ileU
9eGbmpJHrmbNFk73g6k2YLOeosznMD89+AtRrRRn7C+sJmXx90+Eejs1aWWBRi8j
wnC4PNYGZCqV10z+bMWNFWuZlkO2o2c+o8I/mIM93trRgnciFzA2YYAILnz4vZ5L
ZPLKMJmGqSbhxocFUKayXoyeUY1jv/rStYijfdLgcqY4EudstDJjFaLPsKLuTRQW
Ha/+m01i2qnkHvWuai0qaD7qYKMn21Se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qDn0IxHvtELnV6qNAGjFghH5FC3Tnqu
Gm/nFuznVsq84U0YQKVVAA+ci0yXLB6cQCRwz5H3Qfn45v6ZivL7xjrk9aX423yK
CN45I0sFnm6aWxGPN1GegA==',
			'21.0.0' => 'Jd0TVUqo3cVPKWaN7/5dEbIcJKcS9Z0V334sBaJAfXe+YgV6lRmPHrGuTK34+tUc
l9/OHJrazOO7tEDRdtM4zpFE7y9kHsqTTR2MTEVLwIZ5JsgTPHE2/im0wlWLk3UI
4Oo5TPt1EOgNbYHfEBY8FPKCL6JyT2ySBaQKYdfJMgkxUZw00BMjplhNUgXEhvd2
f8tPNZ3cs7hyr0SQFx96gyFcCQ4+5KohnLGqZnNSyV7ovHbYEzXUCmmULlm02zSu
V367c1FDks7iKs/V96u21NuF14IQzI0mEzMzysPINrKbHC+OU1BKHKOHqRJjkHwQ
fNJsXi16UkYMGUXyQWQXHg==',
		];

		if (isset($signatures[$version])) {
			return $signatures[$version];
		}

		return '';
	}

	/**
	 * @Given /there is an update to version ([0-9.]+) available/
	 */
	public function thereIsAnUpdateToVersionAvailable($version) {
		if ($this->skipIt) {
			return;
		}
		$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) {
		if ($this->skipIt) {
			return;
		}
		$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) {
		if ($this->skipIt) {
			return;
		}
		$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) {
		if ($this->skipIt) {
			return;
		}
		/** @var $OC_Version */
		require $this->serverDir . 'nextcloud/version.php';

		$installedVersion = join('.', $OC_Version);

		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) {
		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 || 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) {
		if ($this->skipIt) {
			return;
		}

		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) {
		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 = 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->skipIt) {
			return;
		}
		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 ($this->skipIt) {
			return;
		}
		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() {
		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 = !version_compare($version, PHP_VERSION, '<');
	}
}