Skip to content

Commit df15859

Browse files
committed
feat: make Docker API version configurable via app config
Signed-off-by: Oleksander Piskun <oleksandr2088@icloud.com>
1 parent 1ac2e5b commit df15859

8 files changed

Lines changed: 2244 additions & 125 deletions

File tree

composer.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,32 @@
2323
"friendsofphp/php-cs-fixer": "3.94.1"
2424
},
2525
"scripts": {
26-
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
26+
"lint": "find . -name \\*.php -not -path './vendor/*' -not -path './vendor-bin/*' -print0 | xargs -0 -n1 php -l",
2727
"cs:check": "php-cs-fixer fix ./lib --dry-run --diff",
2828
"cs:fix": "php-cs-fixer fix ./lib",
2929
"psalm": "psalm.phar --threads=1",
3030
"psalm:update-baseline": "psalm.phar --threads=1 --update-baseline",
3131
"psalm:update-baseline:force": "psalm.phar --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
3232
"psalm:clear": "psalm.phar --clear-cache && psalm.phar --clear-global-cache",
33-
"psalm:fix": "psalm.phar --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType"
33+
"psalm:fix": "psalm.phar --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType",
34+
"test:unit": "vendor/bin/phpunit -c tests/php/phpunit.xml --colors=always --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations",
35+
"post-install-cmd": [
36+
"@composer bin all install --ansi"
37+
],
38+
"post-update-cmd": [
39+
"@composer bin all install --ansi"
40+
]
3441
},
3542
"config": {
43+
"allow-plugins": {
44+
"bamarni/composer-bin-plugin": true
45+
},
3646
"optimize-autoloader": true,
3747
"classmap-authoritative": true,
3848
"platform": {
3949
"php": "8.2"
40-
}
50+
},
51+
"sort-packages": true
4152
},
4253
"autoload": {
4354
"psr-4": {
@@ -50,6 +61,7 @@
5061
}
5162
},
5263
"require": {
64+
"bamarni/composer-bin-plugin": "^1.9",
5365
"ext-simplexml": "*",
5466
"php": ">=8.2.0"
5567
}

composer.lock

Lines changed: 260 additions & 121 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/DeployActions/DockerActions.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,16 @@ private function executeCommandInContainer(string $dockerUrl, string $containerI
418418
return (string)$startResponse->getBody();
419419
}
420420

421+
public function getDockerApiVersion(): string {
422+
$version = $this->appConfig->getValueString(Application::APP_ID, 'docker_api_version', lazy: true);
423+
if ($version !== '') {
424+
return $version;
425+
}
426+
return self::DOCKER_API_VERSION;
427+
}
428+
421429
public function buildApiUrl(string $dockerUrl, string $route): string {
422-
return sprintf('%s/%s/%s', $dockerUrl, self::DOCKER_API_VERSION, $route);
430+
return sprintf('%s/%s/%s', $dockerUrl, $this->getDockerApiVersion(), $route);
423431
}
424432

425433
public function buildBaseImageName(array $imageParams, DaemonConfig $daemonConfig): string {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\AppAPI\Tests\php\DeployActions;
11+
12+
use OCA\AppAPI\AppInfo\Application;
13+
use OCA\AppAPI\DeployActions\DockerActions;
14+
use OCA\AppAPI\Service\AppAPICommonService;
15+
use OCA\AppAPI\Service\ExAppDeployOptionsService;
16+
use OCA\AppAPI\Service\ExAppService;
17+
use OCP\App\IAppManager;
18+
use OCP\IAppConfig;
19+
use OCP\ICertificateManager;
20+
use OCP\IConfig;
21+
use OCP\ITempManager;
22+
use OCP\IURLGenerator;
23+
use OCP\Security\ICrypto;
24+
use PHPUnit\Framework\MockObject\MockObject;
25+
use Psr\Log\LoggerInterface;
26+
use Test\TestCase;
27+
28+
class DockerActionsTest extends TestCase {
29+
private DockerActions $dockerActions;
30+
private IAppConfig&MockObject $appConfig;
31+
32+
protected function setUp(): void {
33+
parent::setUp();
34+
35+
$this->appConfig = $this->createMock(IAppConfig::class);
36+
37+
$this->dockerActions = new DockerActions(
38+
$this->createMock(LoggerInterface::class),
39+
$this->appConfig,
40+
$this->createMock(IConfig::class),
41+
$this->createMock(ICertificateManager::class),
42+
$this->createMock(IAppManager::class),
43+
$this->createMock(IURLGenerator::class),
44+
$this->createMock(AppAPICommonService::class),
45+
$this->createMock(ExAppService::class),
46+
$this->createMock(ITempManager::class),
47+
$this->createMock(ICrypto::class),
48+
$this->createMock(ExAppDeployOptionsService::class),
49+
);
50+
}
51+
52+
public function testGetDockerApiVersionReturnsDefaultWhenNoConfigSet(): void {
53+
$this->appConfig->expects(self::once())
54+
->method('getValueString')
55+
->with(Application::APP_ID, 'docker_api_version', '', true)
56+
->willReturn('');
57+
58+
self::assertSame(DockerActions::DOCKER_API_VERSION, $this->dockerActions->getDockerApiVersion());
59+
}
60+
61+
public function testGetDockerApiVersionReturnsCustomVersionFromConfig(): void {
62+
$this->appConfig->expects(self::once())
63+
->method('getValueString')
64+
->with(Application::APP_ID, 'docker_api_version', '', true)
65+
->willReturn('v1.43');
66+
67+
self::assertSame('v1.43', $this->dockerActions->getDockerApiVersion());
68+
}
69+
70+
public function testBuildApiUrlUsesDefaultVersion(): void {
71+
$this->appConfig->method('getValueString')
72+
->with(Application::APP_ID, 'docker_api_version', '', true)
73+
->willReturn('');
74+
75+
$url = $this->dockerActions->buildApiUrl('http://localhost', '_ping');
76+
77+
self::assertSame('http://localhost/v1.44/_ping', $url);
78+
}
79+
80+
public function testBuildApiUrlUsesCustomVersion(): void {
81+
$this->appConfig->method('getValueString')
82+
->with(Application::APP_ID, 'docker_api_version', '', true)
83+
->willReturn('v1.43');
84+
85+
$url = $this->dockerActions->buildApiUrl('http://localhost', '_ping');
86+
87+
self::assertSame('http://localhost/v1.43/_ping', $url);
88+
}
89+
90+
public function testBuildApiUrlWithContainerRoute(): void {
91+
$this->appConfig->method('getValueString')
92+
->with(Application::APP_ID, 'docker_api_version', '', true)
93+
->willReturn('v1.41');
94+
95+
$url = $this->dockerActions->buildApiUrl(
96+
'http://localhost:8780',
97+
sprintf('containers/%s/json', 'nc_app_test')
98+
);
99+
100+
self::assertSame('http://localhost:8780/v1.41/containers/nc_app_test/json', $url);
101+
}
102+
}

tests/php/bootstrap.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
use OCP\App\IAppManager;
11+
use OCP\Server;
12+
13+
if (!defined('PHPUNIT_RUN')) {
14+
define('PHPUNIT_RUN', 1);
15+
}
16+
17+
require_once __DIR__ . '/../../../../lib/base.php';
18+
require_once __DIR__ . '/../../../../tests/autoload.php';
19+
20+
Server::get(IAppManager::class)->loadApp('app_api');

tests/php/phpunit.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
bootstrap="bootstrap.php"
4+
timeoutForSmallTests="900"
5+
timeoutForMediumTests="900"
6+
timeoutForLargeTests="900"
7+
failOnDeprecation="true"
8+
failOnIncomplete="true"
9+
failOnRisky="true"
10+
failOnWarning="true"
11+
failOnEmptyTestSuite="true"
12+
failOnNotice="true"
13+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
14+
cacheDirectory=".phpunit.result.cache"
15+
>
16+
<testsuite name="AppAPI Unit Tests">
17+
<directory suffix="Test.php">.</directory>
18+
</testsuite>
19+
<source>
20+
<include>
21+
<directory suffix=".php">../../lib</directory>
22+
</include>
23+
</source>
24+
</phpunit>

vendor-bin/phpunit/composer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"config": {
3+
"platform": {
4+
"php": "8.2"
5+
},
6+
"sort-packages": true
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "^11.5"
10+
}
11+
}

0 commit comments

Comments
 (0)