diff --git a/Tests/Docker/Image/ReplicatedRegistryImageTest.php b/Tests/Docker/Image/ReplicatedRegistryImageTest.php index 9447ece2..1df959d3 100644 --- a/Tests/Docker/Image/ReplicatedRegistryImageTest.php +++ b/Tests/Docker/Image/ReplicatedRegistryImageTest.php @@ -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 @@ -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', @@ -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(); } } diff --git a/Tests/Docker/Image/ReplicatedRegistryTest.php b/Tests/Docker/Image/ReplicatedRegistryTest.php index 8309e180..df2b2139 100644 --- a/Tests/Docker/Image/ReplicatedRegistryTest.php +++ b/Tests/Docker/Image/ReplicatedRegistryTest.php @@ -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, diff --git a/src/Docker/Image/ReplicatedRegistry.php b/src/Docker/Image/ReplicatedRegistry.php index cfb927af..4f41ee7a 100644 --- a/src/Docker/Image/ReplicatedRegistry.php +++ b/src/Docker/Image/ReplicatedRegistry.php @@ -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, @@ -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(); diff --git a/src/Docker/Image/ReplicatedRegistryImage.php b/src/Docker/Image/ReplicatedRegistryImage.php index 14c940e2..2a3800c8 100644 --- a/src/Docker/Image/ReplicatedRegistryImage.php +++ b/src/Docker/Image/ReplicatedRegistryImage.php @@ -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); }