Skip to content

Commit 496a695

Browse files
committed
fix(registry): correct the cmd/entrypoint override contract doc and cover untested branches
1 parent 746cbfc commit 496a695

3 files changed

Lines changed: 67 additions & 10 deletions

File tree

‎src/main/java/io/github/intisy/docker/registry/ImageAssembler.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ public static Assembled append(String baseConfigJson, String baseManifestJson,
8383
/**
8484
* @implNote a docker-schema2 manifest and an OCI manifest use different layer media types, and
8585
* mixing them in one manifest is rejected. Taking the type from the layers already there keeps
86-
* the assembled manifest internally consistent whichever shape the base image used.
86+
* the assembled manifest internally consistent whichever shape the base image used. The
87+
* {@code OCI_LAYER_GZIP} fallback only applies when the base manifest has no layers at all; a
88+
* real base image always has at least one, so this never mixes a Docker manifest with an OCI
89+
* layer type in practice.
8790
*/
8891
private static String layerMediaTypeMatching(JsonObject manifest) {
8992
JsonArray layers = manifest.getAsJsonArray("layers");
@@ -96,6 +99,12 @@ private static String layerMediaTypeMatching(JsonObject manifest) {
9699
return RegistryClient.OCI_LAYER_GZIP;
97100
}
98101

102+
/**
103+
* @implNote no {@code created} timestamp is written deliberately: a wall-clock value would
104+
* change the config bytes, and therefore the config digest and manifest, on every build, so
105+
* two builds of identical inputs would push different images. Same determinism property as
106+
* why {@link LayerBuilder} pins mtime to zero.
107+
*/
99108
private static void addHistory(JsonObject config) {
100109
JsonArray history = config.getAsJsonArray("history");
101110
if (history == null) {

‎src/main/java/io/github/intisy/docker/registry/ImageConfigOverrides.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
/**
66
* What to stamp into the assembled image's config.
77
*
8-
* @implNote a null entrypoint or cmd means "leave the base value alone"; an empty list means
9-
* "clear it". The difference matters: a base JRE image ships a Cmd that must be cleared or the
10-
* container runs that instead of ours.
8+
* @implNote {@code cmd} is three-state: never calling {@code withCmd} leaves the base Cmd alone;
9+
* {@code withCmd(null)} removes it; {@code withCmd(list)}, empty list included, replaces it. This
10+
* matters because a base JRE image ships a Cmd that must be removed, via {@code withCmd(null)},
11+
* or the container runs that instead of ours. {@code entrypoint} is only two-state: never calling
12+
* {@code withEntrypoint} leaves the base value alone, any list (including empty) replaces it;
13+
* there is no way to clear it.
1114
*
1215
* @author Finn Birich
1316
*/

‎src/test/java/io/github/intisy/docker/unit/ImageAssemblerTest.java‎

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ public void entrypointAndWorkingDirAreStampedAndCmdIsCleared() throws IOExceptio
7878
"a base Cmd left in place would run jshell instead of the launcher");
7979
}
8080

81+
@Test
82+
public void notCallingWithCmdLeavesTheBaseCmdInPlace() throws IOException {
83+
ImageConfigOverrides overrides = new ImageConfigOverrides()
84+
.withEntrypoint(Arrays.asList("/opt/spisor/launcher/bin/launcher"))
85+
.withWorkingDir("/var/lib/spisor");
86+
87+
JsonObject config = GSON.fromJson(
88+
ImageAssembler.append(fixture("base-config.json"), fixture("base-manifest.json"),
89+
layer(), overrides).configJson(),
90+
JsonObject.class).getAsJsonObject("config");
91+
92+
assertEquals("jshell", config.getAsJsonArray("Cmd").get(0).getAsString());
93+
}
94+
8195
@Test
8296
public void theBaseEnvironmentSurvives() throws IOException {
8397
JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class)
@@ -141,15 +155,46 @@ public void aDockerSchema2BaseKeepsDockerMediaTypesThroughout() throws IOExcepti
141155
manifest.getAsJsonArray("layers").get(2).getAsJsonObject().get("mediaType").getAsString());
142156
}
143157

158+
@Test
159+
public void aBaseConfigWithNoHistoryArrayGetsOneCreated() throws IOException {
160+
String baseConfigWithoutHistory = "{\"architecture\":\"amd64\",\"os\":\"linux\","
161+
+ "\"config\":{\"Env\":[],\"Cmd\":[\"jshell\"]},"
162+
+ "\"rootfs\":{\"type\":\"layers\",\"diff_ids\":["
163+
+ "\"sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\","
164+
+ "\"sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"]}}";
165+
166+
JsonObject config = GSON.fromJson(
167+
ImageAssembler.append(baseConfigWithoutHistory, fixture("base-manifest.json"),
168+
layer(), overrides()).configJson(),
169+
JsonObject.class);
170+
171+
assertEquals(1, config.getAsJsonArray("history").size());
172+
}
173+
174+
@Test
175+
public void aBaseConfigWithNoInnerConfigObjectGetsOneCreated() throws IOException {
176+
String baseConfigWithoutInnerConfig = "{\"architecture\":\"amd64\",\"os\":\"linux\","
177+
+ "\"rootfs\":{\"type\":\"layers\",\"diff_ids\":["
178+
+ "\"sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\","
179+
+ "\"sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"]}}";
180+
181+
JsonObject config = GSON.fromJson(
182+
ImageAssembler.append(baseConfigWithoutInnerConfig, fixture("base-manifest.json"),
183+
layer(), overrides()).configJson(),
184+
JsonObject.class).getAsJsonObject("config");
185+
186+
assertEquals("/opt/spisor/launcher/bin/launcher", config.getAsJsonArray("Entrypoint").get(0).getAsString());
187+
}
188+
144189
private static String fixture(String name) throws IOException {
145-
InputStream in = ImageAssemblerTest.class.getResourceAsStream("/registry/" + name);
146190
ByteArrayOutputStream out = new ByteArrayOutputStream();
147-
byte[] buffer = new byte[8192];
148-
int read;
149-
while ((read = in.read(buffer)) != -1) {
150-
out.write(buffer, 0, read);
191+
try (InputStream in = ImageAssemblerTest.class.getResourceAsStream("/registry/" + name)) {
192+
byte[] buffer = new byte[8192];
193+
int read;
194+
while ((read = in.read(buffer)) != -1) {
195+
out.write(buffer, 0, read);
196+
}
151197
}
152-
in.close();
153198
return new String(out.toByteArray(), StandardCharsets.UTF_8);
154199
}
155200
}

0 commit comments

Comments
 (0)