Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add layer description history #1198

Merged
merged 5 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public ListenableFuture<CachedLayer> getFuture() {
return listenableFuture;
}

public String getLayerType() {
return layerType;
}

@Override
public CachedLayer call() throws IOException, CacheCorruptedException {
String description = "Building " + layerType + " layer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ private Image<Layer> afterCachedLayerSteps()
HistoryEntry.builder()
.setCreationTimestamp(layerCreationTime)
.setAuthor("Jib")
.setCreatedBy(buildConfiguration.getToolName() + ":" + ProjectInfo.VERSION)
.setCreatedBy(
capitalizeFirstLetter(buildAndCacheApplicationLayerStep.getLayerType())
+ " created by "
+ buildConfiguration.getToolName()
+ ":"
+ ProjectInfo.VERSION)
.build());
}
if (containerConfiguration != null) {
Expand All @@ -162,6 +167,13 @@ private Image<Layer> afterCachedLayerSteps()
}
}

private static String capitalizeFirstLetter(String string) {
paivagustavo marked this conversation as resolved.
Show resolved Hide resolved
if (string.length() == 0) {
return string;
}
return Character.toUpperCase(string.charAt(0)) + string.substring(1);
}

/**
* Computes the image entrypoint. If {@link ContainerConfiguration#getEntrypoint()} is null, the
* entrypoint is inherited from the base image. Otherwise {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.time.Instant;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -53,7 +54,10 @@ public class BuildImageStepTest {
@Mock private PullBaseImageStep mockPullBaseImageStep;
@Mock private PullAndCacheBaseImageLayersStep mockPullAndCacheBaseImageLayersStep;
@Mock private PullAndCacheBaseImageLayerStep mockPullAndCacheBaseImageLayerStep;
@Mock private BuildAndCacheApplicationLayerStep mockBuildAndCacheApplicationLayerStep;
@Mock private BuildAndCacheApplicationLayerStep mockBuildAndCacheApplicationLayerStepDependencies;
@Mock private BuildAndCacheApplicationLayerStep mockBuildAndCacheApplicationLayerStepResources;
@Mock private BuildAndCacheApplicationLayerStep mockBuildAndCacheApplicationLayerStepClasses;
@Mock private BuildAndCacheApplicationLayerStep mockBuildAndCacheApplicationLayerStepExtraFiles;

private DescriptorDigest testDescriptorDigest;
private HistoryEntry nonEmptyLayerHistory;
Expand Down Expand Up @@ -141,8 +145,24 @@ public BlobDescriptor getBlobDescriptor() {
.thenReturn(
Futures.immediateFuture(
new PullBaseImageStep.BaseImageWithAuthorization(baseImage, null)));
Mockito.when(mockBuildAndCacheApplicationLayerStep.getFuture())
.thenReturn(Futures.immediateFuture(testCachedLayer));

Stream.of(
mockBuildAndCacheApplicationLayerStepClasses,
mockBuildAndCacheApplicationLayerStepDependencies,
mockBuildAndCacheApplicationLayerStepExtraFiles,
mockBuildAndCacheApplicationLayerStepResources)
.forEach(
layerStep ->
Mockito.when(layerStep.getFuture())
.thenReturn(Futures.immediateFuture(testCachedLayer)));

Mockito.when(mockBuildAndCacheApplicationLayerStepClasses.getLayerType()).thenReturn("classes");
Mockito.when(mockBuildAndCacheApplicationLayerStepDependencies.getLayerType())
.thenReturn("dependencies");
Mockito.when(mockBuildAndCacheApplicationLayerStepExtraFiles.getLayerType())
.thenReturn("extra files");
Mockito.when(mockBuildAndCacheApplicationLayerStepResources.getLayerType())
.thenReturn("resources");
}

@Test
Expand All @@ -154,9 +174,9 @@ public void test_validateAsyncDependencies() throws ExecutionException, Interrup
mockPullBaseImageStep,
mockPullAndCacheBaseImageLayersStep,
ImmutableList.of(
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep));
mockBuildAndCacheApplicationLayerStepDependencies,
mockBuildAndCacheApplicationLayerStepResources,
mockBuildAndCacheApplicationLayerStepClasses));
Image<Layer> image = buildImageStep.getFuture().get().getFuture().get();
Assert.assertEquals(
testDescriptorDigest, image.getLayers().asList().get(0).getBlobDescriptor().getDigest());
Expand All @@ -176,9 +196,9 @@ public void test_propagateBaseImageConfiguration()
mockPullBaseImageStep,
mockPullAndCacheBaseImageLayersStep,
ImmutableList.of(
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep));
mockBuildAndCacheApplicationLayerStepDependencies,
mockBuildAndCacheApplicationLayerStepResources,
mockBuildAndCacheApplicationLayerStepClasses));
Image<Layer> image = buildImageStep.getFuture().get().getFuture().get();
Assert.assertEquals(
ImmutableMap.of("BASE_ENV", "BASE_ENV_VALUE", "MY_ENV", "MY_ENV_VALUE"),
Expand Down Expand Up @@ -209,9 +229,9 @@ public void test_inheritedEntrypoint() throws ExecutionException, InterruptedExc
mockPullBaseImageStep,
mockPullAndCacheBaseImageLayersStep,
ImmutableList.of(
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep));
mockBuildAndCacheApplicationLayerStepDependencies,
mockBuildAndCacheApplicationLayerStepResources,
mockBuildAndCacheApplicationLayerStepClasses));
Image<Layer> image = buildImageStep.getFuture().get().getFuture().get();

Assert.assertEquals(ImmutableList.of("baseImageEntrypoint"), image.getEntrypoint());
Expand All @@ -231,9 +251,9 @@ public void test_inheritedEntrypointAndProgramArguments()
mockPullBaseImageStep,
mockPullAndCacheBaseImageLayersStep,
ImmutableList.of(
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep));
mockBuildAndCacheApplicationLayerStepDependencies,
mockBuildAndCacheApplicationLayerStepResources,
mockBuildAndCacheApplicationLayerStepClasses));
Image<Layer> image = buildImageStep.getFuture().get().getFuture().get();

Assert.assertEquals(ImmutableList.of("baseImageEntrypoint"), image.getEntrypoint());
Expand All @@ -253,9 +273,9 @@ public void test_notInheritedProgramArguments() throws ExecutionException, Inter
mockPullBaseImageStep,
mockPullAndCacheBaseImageLayersStep,
ImmutableList.of(
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep));
mockBuildAndCacheApplicationLayerStepDependencies,
mockBuildAndCacheApplicationLayerStepResources,
mockBuildAndCacheApplicationLayerStepClasses));
Image<Layer> image = buildImageStep.getFuture().get().getFuture().get();

Assert.assertEquals(ImmutableList.of("myEntrypoint"), image.getEntrypoint());
Expand All @@ -271,9 +291,10 @@ public void test_generateHistoryObjects() throws ExecutionException, Interrupted
mockPullBaseImageStep,
mockPullAndCacheBaseImageLayersStep,
ImmutableList.of(
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep,
mockBuildAndCacheApplicationLayerStep));
mockBuildAndCacheApplicationLayerStepDependencies,
mockBuildAndCacheApplicationLayerStepResources,
mockBuildAndCacheApplicationLayerStepClasses,
mockBuildAndCacheApplicationLayerStepExtraFiles));
Image<Layer> image = buildImageStep.getFuture().get().getFuture().get();

// Make sure history is as expected
Expand All @@ -282,11 +303,33 @@ public void test_generateHistoryObjects() throws ExecutionException, Interrupted
.setCreationTimestamp(Instant.EPOCH)
.setComment("auto-generated by Jib")
.build();
HistoryEntry expectedApplicationLayerHistory =

HistoryEntry expectedApplicationLayerHistoryDependencies =
HistoryEntry.builder()
.setCreationTimestamp(Instant.EPOCH)
.setAuthor("Jib")
.setCreatedBy("Dependencies created by jib:null")
.build();

HistoryEntry expectedApplicationLayerHistoryResources =
HistoryEntry.builder()
.setCreationTimestamp(Instant.EPOCH)
.setAuthor("Jib")
.setCreatedBy("Resources created by jib:null")
.build();

HistoryEntry expectedApplicationLayerHistoryClasses =
HistoryEntry.builder()
.setCreationTimestamp(Instant.EPOCH)
.setAuthor("Jib")
.setCreatedBy("Classes created by jib:null")
.build();

HistoryEntry expectedApplicationLayerHistoryExtrafiles =
HistoryEntry.builder()
.setCreationTimestamp(Instant.EPOCH)
.setAuthor("Jib")
.setCreatedBy("jib:null")
.setCreatedBy("Extra files created by jib:null")
.build();

// Base layers (1 non-empty propagated, 2 empty propagated, 2 non-empty generated)
Expand All @@ -297,11 +340,12 @@ public void test_generateHistoryObjects() throws ExecutionException, Interrupted
Assert.assertEquals(expectedAddedBaseLayerHistory, image.getHistory().get(4));

// Application layers (3 generated)
paivagustavo marked this conversation as resolved.
Show resolved Hide resolved
Assert.assertEquals(expectedApplicationLayerHistory, image.getHistory().get(5));
Assert.assertEquals(expectedApplicationLayerHistory, image.getHistory().get(6));
Assert.assertEquals(expectedApplicationLayerHistory, image.getHistory().get(7));
Assert.assertEquals(expectedApplicationLayerHistoryDependencies, image.getHistory().get(5));
Assert.assertEquals(expectedApplicationLayerHistoryResources, image.getHistory().get(6));
Assert.assertEquals(expectedApplicationLayerHistoryClasses, image.getHistory().get(7));
Assert.assertEquals(expectedApplicationLayerHistoryExtrafiles, image.getHistory().get(8));

// Should be exactly 8 total
paivagustavo marked this conversation as resolved.
Show resolved Hide resolved
Assert.assertEquals(8, image.getHistory().size());
Assert.assertEquals(9, image.getHistory().size());
}
}