Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions Tests/Docker/Image/ReplicatedRegistryImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Keboola\DockerBundle\Docker\Image\ReplicatedRegistry;
use Keboola\DockerBundle\Docker\Image\ReplicatedRegistryImage;
use Keboola\DockerBundle\Exception\ApplicationException;
use Keboola\JobQueue\JobConfiguration\JobDefinition\Component\ComponentSpecification;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class ReplicatedRegistryImageTest extends TestCase
Expand Down Expand Up @@ -93,9 +93,10 @@ public function testGetFullImageIdIncludesTag(): void
);
}

public function testGetImageIdFallsBackToUriTransformationWhenDefinitionNameMissing(): void
public function testGetImageIdThrowsWhenDefinitionNameMissing(): void
{
$imageConfig = new ComponentSpecification([
'id' => 'keboola.test-component',
'data' => [
'definition' => [
'type' => 'aws-ecr',
Expand All @@ -111,22 +112,18 @@ public function testGetImageIdFallsBackToUriTransformationWhenDefinitionNameMiss
'testpass',
);

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::once())
->method('warning')
->with(self::stringContains($imageConfig->getId()));

$image = new ReplicatedRegistryImage(
$imageConfig,
$logger,
new NullLogger(),
$service,
);

$result = $image->getImageId();

self::assertSame(
'us-docker.pkg.dev/my-project/my-repo/keboola/test-component',
$result,
$this->expectException(ApplicationException::class);
$this->expectExceptionMessage(
'Component "keboola.test-component" is missing definition.name, '
. 'cannot resolve replicated registry image id.',
);

$image->getImageId();
}
}
84 changes: 0 additions & 84 deletions Tests/Docker/Image/ReplicatedRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,90 +116,6 @@ public static function composeImageUrlWhenDisabledDataProvider(): iterable
];
}

/** @dataProvider transformImageUrlDataProvider */
public function testTransformImageUrl(
string $replicatedRegistryUrl,
string $originalUrl,
string $expectedUrl,
): void {
$service = new ReplicatedRegistry(
true,
$replicatedRegistryUrl,
'testuser',
'testpass',
);

self::assertSame($expectedUrl, $service->transformImageUrl($originalUrl));
}

public static function transformImageUrlDataProvider(): iterable
{
yield 'ECR to GAR with tag' => [
'replicatedRegistryUrl' => 'registry.example.com/keboola',
'originalUrl' => '147946154733.dkr.ecr.us-east-1.amazonaws.com/developer-portal-v2/component:1.0.0',
'expectedUrl' => 'registry.example.com/keboola/developer-portal-v2/component:1.0.0',
];
yield 'ECR to GAR without tag' => [
'replicatedRegistryUrl' => 'registry.example.com/keboola',
'originalUrl' => '147946154733.dkr.ecr.us-east-1.amazonaws.com/developer-portal-v2/component',
'expectedUrl' => 'registry.example.com/keboola/developer-portal-v2/component',
];
yield 'ECR to GAR with digest' => [
'replicatedRegistryUrl' => 'registry.example.com/keboola',
'originalUrl' => '147946154733.dkr.ecr.us-east-1.amazonaws.com/component@sha256:abcd1234',
'expectedUrl' => 'registry.example.com/keboola/component@sha256:abcd1234',
];
yield 'non-ECR image unchanged' => [
'replicatedRegistryUrl' => 'registry.example.com/keboola',
'originalUrl' => 'docker.io/library/nginx:latest',
'expectedUrl' => 'docker.io/library/nginx:latest',
];
yield 'ECR to ACR' => [
'replicatedRegistryUrl' => 'myregistry.azurecr.io',
'originalUrl' => '147946154733.dkr.ecr.us-east-1.amazonaws.com/component:v1',
'expectedUrl' => 'myregistry.azurecr.io/component:v1',
];
yield 'ECR to GAR with port' => [
'replicatedRegistryUrl' => 'registry.example.com:5000/keboola',
'originalUrl' => '147946154733.dkr.ecr.us-east-1.amazonaws.com/component:latest',
'expectedUrl' => 'registry.example.com:5000/keboola/component:latest',
];
yield 'ECR to GAR with multiple path segments' => [
'replicatedRegistryUrl' => 'registry.example.com/org/team/keboola',
'originalUrl' => '147946154733.dkr.ecr.us-east-1.amazonaws.com/component:v1.0.0',
'expectedUrl' => 'registry.example.com/org/team/keboola/component:v1.0.0',
];
}

/** @dataProvider transformImageUrlWhenDisabledDataProvider */
public function testTransformImageUrlWhenDisabledThrowsException(
bool $useReplicatedRegistry,
string $replicatedRegistryUrl,
): void {
$service = new ReplicatedRegistry($useReplicatedRegistry, $replicatedRegistryUrl, 'testuser', 'testpass');

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Replicated registry is not enabled');

$service->transformImageUrl('147946154733.dkr.ecr.us-east-1.amazonaws.com/component:v1');
}

public static function transformImageUrlWhenDisabledDataProvider(): iterable
{
yield 'disabled with empty URL' => [
'useReplicatedRegistry' => false,
'replicatedRegistryUrl' => '',
];
yield 'disabled with non-empty URL' => [
'useReplicatedRegistry' => false,
'replicatedRegistryUrl' => 'registry.example.com/keboola',
];
yield 'enabled with empty URL' => [
'useReplicatedRegistry' => true,
'replicatedRegistryUrl' => '',
];
}

/** @dataProvider loginParamsDataProvider */
public function testGetLoginParams(
string $registryUrl,
Expand Down
10 changes: 0 additions & 10 deletions src/Docker/Image/ReplicatedRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

class ReplicatedRegistry
{
private const ECR_REGISTRY_URL = '147946154733.dkr.ecr.us-east-1.amazonaws.com';

public function __construct(
private readonly bool $useReplicatedRegistry,
private string $replicatedRegistryUrl,
Expand All @@ -34,14 +32,6 @@ public function composeImageUrl(string $imageName): string
return $this->replicatedRegistryUrl . '/' . $imageName;
}

public function transformImageUrl(string $originalImageId): string
{
if (!$this->isEnabled()) {
throw new LogicException('Replicated registry is not enabled');
}
return str_replace(self::ECR_REGISTRY_URL, $this->replicatedRegistryUrl, $originalImageId);
}

public function getLoginParams(): string
{
$registryHost = $this->getRegistryHost();
Expand Down
13 changes: 5 additions & 8 deletions src/Docker/Image/ReplicatedRegistryImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ public function __construct(

public function getImageId(): string
{
$imageName = $this->getSourceComponent()->getImageName();
if ($imageName === null) {
$this->logger->warning(sprintf(
'Component "%s" is missing definition.name, falling back to URI-based registry transformation.',
$this->getSourceComponent()->getId(),
));
return $this->replicatedRegistry->transformImageUrl($this->imageId);
}
$imageName = $this->getSourceComponent()->getImageName() ?? throw new ApplicationException(sprintf(
'Component "%s" is missing definition.name, cannot resolve replicated registry image id.',
$this->getSourceComponent()->getId(),
));

return $this->replicatedRegistry->composeImageUrl($imageName);
}

Expand Down
Loading