|
29 | 29 | import java.nio.file.Path; |
30 | 30 | import java.util.List; |
31 | 31 | import java.util.Map; |
| 32 | +import java.util.Set; |
32 | 33 | import land.oras.exception.OrasException; |
33 | 34 | import land.oras.utils.Const; |
34 | 35 | import land.oras.utils.SupportedAlgorithm; |
@@ -780,6 +781,85 @@ void testShouldCopyArtifactFromRegistryIntoOciLayout() throws IOException { |
780 | 781 | assertBlobAbsent(layoutPath, SupportedAlgorithm.SHA256.digest(file2)); |
781 | 782 | } |
782 | 783 |
|
| 784 | + @Test |
| 785 | + void testShouldCopyIndexWithPlatformFilterFromRegistryIntoOciLayout() throws IOException { |
| 786 | + |
| 787 | + Registry registry = Registry.Builder.builder() |
| 788 | + .defaults("myuser", "mypass") |
| 789 | + .withInsecure(true) |
| 790 | + .build(); |
| 791 | + |
| 792 | + Path ociLayoutPath = layoutPath.resolve("testShouldCopyIndexWithPlatformFilterFromRegistryIntoOciLayout"); |
| 793 | + OCILayout ociLayout = |
| 794 | + OCILayout.Builder.builder().defaults(ociLayoutPath).build(); |
| 795 | + LayoutRef layoutRef = LayoutRef.parse("%s:latest".formatted(ociLayoutPath.toString())); |
| 796 | + |
| 797 | + ContainerRef containerRef = |
| 798 | + ContainerRef.parse("%s/library/platform-filter-source".formatted(this.registry.getRegistry())); |
| 799 | + |
| 800 | + // Push two manifests with different content, one per platform |
| 801 | + Path fileAmd64 = blobDir.resolve("platform-filter-amd64.txt"); |
| 802 | + Path fileArm64 = blobDir.resolve("platform-filter-arm64.txt"); |
| 803 | + Files.writeString(fileAmd64, "content-amd64"); |
| 804 | + Files.writeString(fileArm64, "content-arm64"); |
| 805 | + |
| 806 | + Manifest manifestAmd64 = registry.pushArtifact(containerRef.withTag("amd64"), LocalPath.of(fileAmd64)); |
| 807 | + Manifest manifestArm64 = registry.pushArtifact(containerRef.withTag("arm64"), LocalPath.of(fileArm64)); |
| 808 | + |
| 809 | + assertNotNull(manifestAmd64.getDescriptor()); |
| 810 | + assertNotNull(manifestArm64.getDescriptor()); |
| 811 | + |
| 812 | + // Build a multi-platform index and push it |
| 813 | + ManifestDescriptor descAmd64 = manifestAmd64.getDescriptor().withPlatform(Platform.linuxAmd64()); |
| 814 | + ManifestDescriptor descArm64 = manifestArm64.getDescriptor().withPlatform(Platform.linuxArm64V8()); |
| 815 | + Index sourceIndex = Index.fromManifests(List.of(descAmd64, descArm64)); |
| 816 | + registry.pushIndex(containerRef.withTag("latest"), sourceIndex); |
| 817 | + |
| 818 | + // Copy only linux/amd64 into the OCI layout |
| 819 | + CopyUtils.copy( |
| 820 | + registry, |
| 821 | + containerRef.withTag("latest"), |
| 822 | + ociLayout, |
| 823 | + layoutRef, |
| 824 | + CopyUtils.CopyOptions.shallow().withPlatformFilter(Set.of(Platform.linuxAmd64()))); |
| 825 | + |
| 826 | + // The layout must be a valid OCI layout |
| 827 | + assertOciLayout(ociLayoutPath); |
| 828 | + |
| 829 | + // The OCI layout root index.json has two entries: |
| 830 | + // 1. the amd64 manifest blob (pushed individually, no tag) |
| 831 | + // 2. the filtered index blob (tagged "latest") |
| 832 | + Index ociIndex = Index.fromPath(ociLayoutPath.resolve(Const.OCI_LAYOUT_INDEX)); |
| 833 | + assertEquals(2, ociIndex.getManifests().size(), "OCI layout index.json must have two entries"); |
| 834 | + |
| 835 | + // Find the filtered-index entry by its tag annotation |
| 836 | + ManifestDescriptor filteredIndexDescriptor = ociIndex.getManifests().stream() |
| 837 | + .filter(d -> d.getAnnotations() != null |
| 838 | + && "latest".equals(d.getAnnotations().get(Const.ANNOTATION_REF))) |
| 839 | + .findFirst() |
| 840 | + .orElseThrow(() -> new AssertionError("No entry with tag 'latest' found in OCI layout index.json")); |
| 841 | + assertBlobExists(ociLayoutPath, filteredIndexDescriptor.getDigest()); |
| 842 | + |
| 843 | + // The filtered index blob itself contains only the amd64 manifest descriptor |
| 844 | + Index filteredIndex = Index.fromJson(Files.readString(ociLayoutPath |
| 845 | + .resolve("blobs") |
| 846 | + .resolve("sha256") |
| 847 | + .resolve(SupportedAlgorithm.getDigest(filteredIndexDescriptor.getDigest())))); |
| 848 | + assertEquals(1, filteredIndex.getManifests().size(), "Filtered index must contain exactly one manifest"); |
| 849 | + assertEquals( |
| 850 | + Platform.linuxAmd64(), |
| 851 | + filteredIndex.getManifests().get(0).getPlatform(), |
| 852 | + "The single manifest in the filtered index must be linux/amd64"); |
| 853 | + |
| 854 | + // The amd64 layer blob and its config must be present |
| 855 | + assertBlobExists(ociLayoutPath, SupportedAlgorithm.SHA256.digest(fileAmd64)); |
| 856 | + assertBlobContent(ociLayoutPath, SupportedAlgorithm.SHA256.digest(fileAmd64), "content-amd64"); |
| 857 | + assertBlobContent(ociLayoutPath, Config.empty().getDigest(), "{}"); |
| 858 | + |
| 859 | + // The arm64 layer blob must be absent — it was filtered out |
| 860 | + assertBlobAbsent(ociLayoutPath, SupportedAlgorithm.SHA256.digest(fileArm64)); |
| 861 | + } |
| 862 | + |
783 | 863 | @Test |
784 | 864 | void testShouldCopyFromOciLayoutIntoOciLayoutRecursive() throws IOException { |
785 | 865 |
|
|
0 commit comments