Skip to content

Commit 1f7918c

Browse files
committed
Add test that assemble OpenTofu provider
Signed-off-by: Valentin Delaye <jonesbusy@users.noreply.github.com>
1 parent e56e3ed commit 1f7918c

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

src/main/java/land/oras/Registry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public Manifest pushManifest(ContainerRef containerRef, Manifest manifest) {
262262
}
263263
ContainerRef ref = containerRef.forRegistry(this).checkBlocked(this);
264264
if (ref.isInsecure(this) && !this.isInsecure()) {
265-
return asInsecure().pushManifest(containerRef, manifest);
265+
return asInsecure().pushManifest(ref, manifest);
266266
}
267267
URI uri = URI.create("%s://%s".formatted(getScheme(), ref.getManifestsPath(this)));
268268
byte[] manifestData = manifest.getJson() != null
@@ -284,7 +284,7 @@ public Manifest pushManifest(ContainerRef containerRef, Manifest manifest) {
284284
"Subject was set on manifest but not OCI subject header was returned. Legacy flow not implemented");
285285
}
286286
}
287-
return getManifest(containerRef);
287+
return getManifest(ref);
288288
}
289289

290290
@Override
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)