|
| 1 | +/*- |
| 2 | + * =LICENSE= |
| 3 | + * ORAS Java SDK |
| 4 | + * === |
| 5 | + * Copyright (C) 2024 - 2026 ORAS |
| 6 | + * === |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * =LICENSEEND= |
| 19 | + */ |
| 20 | + |
| 21 | +package land.oras; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 24 | + |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.nio.file.Paths; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Objects; |
| 30 | +import land.oras.utils.Const; |
| 31 | +import land.oras.utils.SupportedAlgorithm; |
| 32 | +import land.oras.utils.ZotUnsecureContainer; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.parallel.Execution; |
| 35 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 36 | +import org.testcontainers.junit.jupiter.Container; |
| 37 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 38 | + |
| 39 | +@Testcontainers |
| 40 | +@Execution(ExecutionMode.CONCURRENT) |
| 41 | +class OpenTofuITCase { |
| 42 | + |
| 43 | + @Container |
| 44 | + private final ZotUnsecureContainer unsecureRegistry = new ZotUnsecureContainer().withStartupAttempts(3); |
| 45 | + |
| 46 | + /** |
| 47 | + * This test demonstrate how to assemble a Flux CD OCI Artifact |
| 48 | + */ |
| 49 | + @Test |
| 50 | + void shouldAssembleProviderArtifact() { |
| 51 | + |
| 52 | + // The compressed manifests |
| 53 | + Path archive = |
| 54 | + Paths.get("src/test/resources/archives").resolve("terraform-provider-random_3.8.1_linux_amd64.zip"); |
| 55 | + |
| 56 | + ArtifactType indexArtifactType = ArtifactType.from("application/vnd.opentofu.provider"); |
| 57 | + ArtifactType manifestArtifactType = ArtifactType.from("application/vnd.opentofu.provider-target"); |
| 58 | + String contentMediaType = "archive/zip"; |
| 59 | + |
| 60 | + Platform linuxAmd64 = Platform.linuxAmd64(); |
| 61 | + |
| 62 | + // Create objects |
| 63 | + Config config = Config.empty(); |
| 64 | + Layer layer = Layer.fromFile(archive).withMediaType(contentMediaType); |
| 65 | + Manifest manifest = Manifest.empty() |
| 66 | + .withArtifactType(manifestArtifactType) |
| 67 | + .withConfig(config) |
| 68 | + .withLayers(List.of(layer)); |
| 69 | + |
| 70 | + // Index with given platform |
| 71 | + Objects.requireNonNull(manifest.getDescriptor()); |
| 72 | + String manifestJson = manifest.toJson(); |
| 73 | + String manifestDigest = SupportedAlgorithm.getDefault().digest(manifestJson.getBytes(StandardCharsets.UTF_8)); |
| 74 | + ManifestDescriptor manifestDescriptor = ManifestDescriptor.of( |
| 75 | + Const.DEFAULT_MANIFEST_MEDIA_TYPE, manifestDigest, manifestJson.length()) |
| 76 | + .withArtifactType(manifestArtifactType.getMediaType()); |
| 77 | + Index index = Index.fromManifests(List.of(manifestDescriptor.withPlatform(linuxAmd64))) |
| 78 | + .withArtifactType(indexArtifactType); |
| 79 | + |
| 80 | + // Push config, layers and manifest to registry |
| 81 | + Registry registry = Registry.builder() |
| 82 | + .defaults() |
| 83 | + .insecure() |
| 84 | + .withRegistry(unsecureRegistry.getRegistry()) |
| 85 | + .build(); |
| 86 | + ContainerRef containerRef = ContainerRef.parse("oras/opentofu-providers/terraform-provider-random:3.8.1"); |
| 87 | + |
| 88 | + registry.pushConfig(containerRef, config); |
| 89 | + registry.pushBlob(containerRef, archive); |
| 90 | + registry.pushManifest(containerRef.withDigest(manifestDigest), manifest); |
| 91 | + registry.pushIndex(containerRef, index); |
| 92 | + |
| 93 | + // Ensure we can pull |
| 94 | + Index createdIndex = registry.getIndex(containerRef); |
| 95 | + assertNotNull(createdIndex); |
| 96 | + } |
| 97 | +} |
0 commit comments