Allow image Created date to be configurable
A `createdDate` option on the Maven `spring-boot:build-image` goal and the Gradle `bootBuildImage` task can be used to set the `Created` metadata field on a generated OCI image to a specified date or to the current date. Closes gh-28798
This commit is contained in:
@@ -21,6 +21,9 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -258,6 +261,37 @@ class BuildRequestTests {
|
||||
.withMessage("LaunchCache must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCreatedDateSetsCreatedDate() throws Exception {
|
||||
Instant createDate = Instant.now();
|
||||
BuildRequest request = BuildRequest.forJarFile(writeTestJarFile("my-app-0.0.1.jar"));
|
||||
BuildRequest withCreatedDate = request.withCreatedDate(createDate.toString());
|
||||
assertThat(withCreatedDate.getCreatedDate()).isEqualTo(createDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCreatedDateNowSetsCreatedDate() throws Exception {
|
||||
OffsetDateTime now = OffsetDateTime.now();
|
||||
BuildRequest request = BuildRequest.forJarFile(writeTestJarFile("my-app-0.0.1.jar"));
|
||||
BuildRequest withCreatedDate = request.withCreatedDate("now");
|
||||
OffsetDateTime createdDate = OffsetDateTime.ofInstant(withCreatedDate.getCreatedDate(), ZoneId.of("UTC"));
|
||||
assertThat(createdDate.getYear()).isEqualTo(now.getYear());
|
||||
assertThat(createdDate.getMonth()).isEqualTo(now.getMonth());
|
||||
assertThat(createdDate.getDayOfMonth()).isEqualTo(now.getDayOfMonth());
|
||||
withCreatedDate = request.withCreatedDate("NOW");
|
||||
createdDate = OffsetDateTime.ofInstant(withCreatedDate.getCreatedDate(), ZoneId.of("UTC"));
|
||||
assertThat(createdDate.getYear()).isEqualTo(now.getYear());
|
||||
assertThat(createdDate.getMonth()).isEqualTo(now.getMonth());
|
||||
assertThat(createdDate.getDayOfMonth()).isEqualTo(now.getDayOfMonth());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCreatedDateAndInvalidDateThrowsException() throws Exception {
|
||||
BuildRequest request = BuildRequest.forJarFile(writeTestJarFile("my-app-0.0.1.jar"));
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> request.withCreatedDate("not a date"))
|
||||
.withMessageContaining("'not a date'");
|
||||
}
|
||||
|
||||
private void hasExpectedJarContent(TarArchive archive) {
|
||||
try {
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
|
||||
@@ -116,7 +116,7 @@ class EphemeralBuilderTests extends AbstractJsonTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getArchiveHasFixedCreateDate() throws Exception {
|
||||
void getArchiveHasFixedCreatedDate() throws Exception {
|
||||
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
|
||||
this.creator, this.env, this.buildpacks);
|
||||
Instant createInstant = builder.getArchive().getCreateDate();
|
||||
|
||||
@@ -218,6 +218,17 @@ class LifecycleTests {
|
||||
assertThat(this.out.toString()).contains("Successfully built image 'docker.io/library/my-application:latest'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeWithCreatedDateExecutesPhases() throws Exception {
|
||||
given(this.docker.container().create(any())).willAnswer(answerWithGeneratedContainerId());
|
||||
given(this.docker.container().create(any(), any())).willAnswer(answerWithGeneratedContainerId());
|
||||
given(this.docker.container().wait(any())).willReturn(ContainerStatus.of(0, null));
|
||||
BuildRequest request = getTestRequest().withCreatedDate("2020-07-01T12:34:56Z");
|
||||
createLifecycle(request).execute();
|
||||
assertPhaseWasRun("creator", withExpectedConfig("lifecycle-creator-created-date.json"));
|
||||
assertThat(this.out.toString()).contains("Successfully built image 'docker.io/library/my-application:latest'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void executeWithDockerHostAndRemoteAddressExecutesPhases() throws Exception {
|
||||
given(this.docker.container().create(any())).willAnswer(answerWithGeneratedContainerId());
|
||||
|
||||
@@ -67,6 +67,12 @@ class ImageTests extends AbstractJsonTests {
|
||||
assertThat(image.getOs()).isEqualTo("linux");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCreatedReturnsDate() throws Exception {
|
||||
Image image = getImage();
|
||||
assertThat(image.getCreated()).isEqualTo("2019-10-30T19:34:56.296666503Z");
|
||||
}
|
||||
|
||||
private Image getImage() throws IOException {
|
||||
return Image.of(getContent("image.json"));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"User": "root",
|
||||
"Image": "pack.local/ephemeral-builder",
|
||||
"Cmd": [
|
||||
"/cnb/lifecycle/creator",
|
||||
"-app",
|
||||
"/workspace",
|
||||
"-platform",
|
||||
"/platform",
|
||||
"-run-image",
|
||||
"docker.io/cloudfoundry/run:latest",
|
||||
"-layers",
|
||||
"/layers",
|
||||
"-cache-dir",
|
||||
"/cache",
|
||||
"-launch-cache",
|
||||
"/launch-cache",
|
||||
"-daemon",
|
||||
"docker.io/library/my-application:latest"
|
||||
],
|
||||
"Env": [
|
||||
"CNB_PLATFORM_API=0.8",
|
||||
"SOURCE_DATE_EPOCH=1593606896"
|
||||
],
|
||||
"Labels": {
|
||||
"author": "spring-boot"
|
||||
},
|
||||
"HostConfig": {
|
||||
"Binds": [
|
||||
"/var/run/docker.sock:/var/run/docker.sock",
|
||||
"pack-layers-aaaaaaaaaa:/layers",
|
||||
"pack-app-aaaaaaaaaa:/workspace",
|
||||
"pack-cache-b35197ac41ea.build:/cache",
|
||||
"pack-cache-b35197ac41ea.launch:/launch-cache"
|
||||
],
|
||||
"SecurityOpt" : [
|
||||
"label=disable"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user