Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nodece committed May 19, 2024
1 parent 201de6f commit 7dcc06f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/fabric8/maven/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ private void proceedWithDockerBuild(ServiceHub hub, BuildService.BuildContext bu
} else {
buildService.buildImage(imageConfig, pullManager, buildContext, buildArchiveFile);
if (!skipTag) {
buildService.tagImage(imageConfig);
if (!imageConfig.getBuildConfiguration().skipTag()) {
buildService.tagImage(imageConfig);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/fabric8/maven/docker/TagMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void executeInternal(ServiceHub hub) throws DockerAccessException, MojoEx
continue;
}
if (buildConfig.isBuildX()) {
// Tag happens the building stage.
// Tag happens at the building stage.
continue;
}

Expand Down
101 changes: 94 additions & 7 deletions src/test/java/io/fabric8/maven/docker/BuildMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.fabric8.maven.docker.service.BuildService;
import io.fabric8.maven.docker.service.BuildXService;
import io.fabric8.maven.docker.service.ImagePullManager;
import io.fabric8.maven.docker.util.ImageName;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -31,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ExtendWith(MockitoExtension.class)
class BuildMojoTest extends MojoTestBase {
Expand Down Expand Up @@ -241,6 +244,75 @@ void buildUsingBuildxWithMultipleAuth() throws IOException, MojoExecutionExcepti
thenAuthContainsRegistry("custom-registry.org");
}

void buildUsingBuildxWithTag(boolean skipTag) throws IOException, MojoExecutionException {
givenBuildXService();

List<String> tags = new ArrayList<>();
tags.add("tag-" + System.currentTimeMillis());
tags.add("tag-" + System.currentTimeMillis());

givenMavenProject(buildMojo);
ImageConfiguration imageConfiguration = singleImageConfiguration(builder -> {
builder.buildx(getBuildXPlatforms(TWO_BUILDX_PLATFORMS).build());
builder.tags(tags);
builder.skipTag(skipTag);
});
givenResolvedImages(buildMojo, Collections.singletonList(imageConfiguration));
givenPackaging("jar");

whenMojoExecutes();

List<String> fullTags = skipTag ? Collections.emptyList() : tags.stream()
.map(tag -> new ImageName(imageConfiguration.getName(), tag).getFullName())
.collect(Collectors.toList());
thenBuildxRun(null, null, true, null, fullTags);
}

@Test
void buildUsingBuildxWithTag() throws IOException, MojoExecutionException {
buildUsingBuildxWithTag(false);
}

@Test
void buildUsingBuildxWithSkipTag() throws IOException, MojoExecutionException {
buildUsingBuildxWithTag(true);
}

void buildWithTag(boolean skipTag) throws IOException, MojoExecutionException {
givenMavenProject(buildMojo);

List<String> tags = new ArrayList<>();
tags.add("tag-" + System.currentTimeMillis());
tags.add("tag-" + System.currentTimeMillis());

ImageConfiguration imageConfiguration = singleImageConfiguration(builder -> {
builder.skipTag(skipTag);
builder.tags(tags);
});
givenResolvedImages(buildMojo, Collections.singletonList(imageConfiguration));
givenPackaging("jar");
givenSkipPom(true);

whenMojoExecutes();

thenBuildRun();

if (!skipTag) {
Mockito.verify(buildService, Mockito.times(1))
.tagImage(Mockito.any(ImageConfiguration.class));
}
}

@Test
void buildWithTag() throws IOException, MojoExecutionException {
buildWithTag(false);
}

@Test
void buildWithSkipTag() throws IOException, MojoExecutionException {
buildWithTag(true);
}

private void givenBuildXService() {
BuildXService buildXService = new BuildXService(dockerAccess, dockerAssemblyManager, log, exec);

Expand Down Expand Up @@ -288,24 +360,39 @@ private void verifyBuild(int wantedNumberOfInvocations) throws DockerAccessExcep
.buildImage(Mockito.any(ImageConfiguration.class), Mockito.any(ImagePullManager.class), Mockito.any(BuildService.BuildContext.class), Mockito.any());
}

private void thenBuildxRun(String relativeConfigFile, String contextDir, boolean nativePlatformIncluded,
String attestation) throws MojoExecutionException {
thenBuildxRun(relativeConfigFile, contextDir, nativePlatformIncluded, attestation, Collections.emptyList());
}

private void thenBuildxRun(String relativeConfigFile, String contextDir,
boolean nativePlatformIncluded, String attestation) throws MojoExecutionException {
boolean nativePlatformIncluded, String attestation, List<String> tags)
throws MojoExecutionException {
Path buildPath = projectBaseDirectory.toPath().resolve("target/docker/example/latest");
String config = getOsDependentBuild(buildPath, "docker");
String configFile = relativeConfigFile != null ? getOsDependentBuild(projectBaseDirectory.toPath(), relativeConfigFile) : null;
String configFile =
relativeConfigFile != null ? getOsDependentBuild(projectBaseDirectory.toPath(), relativeConfigFile) :
null;

List<String> cmds =
BuildXService.append(new ArrayList<>(), "docker", "--config", config, "buildx",
"create", "--driver", "docker-container", "--name", "maven" , "--node", "maven0");
BuildXService.append(new ArrayList<>(), "docker", "--config", config, "buildx",
"create", "--driver", "docker-container", "--name", "maven", "--node", "maven0");
if (configFile != null) {
BuildXService.append(cmds, "--config", configFile.replace('/', File.separatorChar));
}
Mockito.verify(exec).process(cmds);

if (nativePlatformIncluded) {
List<String> buildXLine = BuildXService.append(new ArrayList<>(), "docker", "--config", config, "buildx",
List<String> buildXLine = BuildXService.append(new ArrayList<>(), "docker", "--config", config, "buildx",
"build", "--progress=plain", "--builder", "maven",
"--platform", NATIVE_PLATFORM, "--tag", "example:latest", "--build-arg", "foo=bar");
"--platform", NATIVE_PLATFORM, "--tag", "example:latest");

tags.forEach(tag -> {
buildXLine.add("--tag");
buildXLine.add(tag);
});
buildXLine.add("--build-arg");
buildXLine.add("foo=bar");

if (attestation != null) {
buildXLine.add(attestation);
Expand All @@ -315,7 +402,7 @@ private void thenBuildxRun(String relativeConfigFile, String contextDir,
buildXLine.add(getOsDependentBuild(buildPath, "build"));
} else {
Path contextPath = tmpDir.resolve("docker-build");
BuildXService.append(buildXLine, "--file=" + contextPath.resolve("Dockerfile"), contextPath.toString() );
BuildXService.append(buildXLine, "--file=" + contextPath.resolve("Dockerfile"), contextPath.toString());
}

buildXLine.add("--load");
Expand Down
26 changes: 17 additions & 9 deletions src/test/java/io/fabric8/maven/docker/MojoTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.fabric8.maven.docker.util.AnsiLogger;
import io.fabric8.maven.docker.util.AuthConfigFactory;
import io.fabric8.maven.docker.util.GavLabel;
import java.util.function.Consumer;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build;
import org.apache.maven.monitor.logging.DefaultLog;
Expand Down Expand Up @@ -113,18 +114,25 @@ protected ImageConfiguration singleImageWithBuild() {
}

ImageConfiguration singleImageConfiguration(BuildXConfiguration buildx, String contextDir) {
BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder()
.from("scratch")
.buildx(buildx)
.args(Collections.singletonMap("foo", "bar"))
.contextDir(contextDir)
.build();
return singleImageConfiguration(builder -> {
builder.buildx(buildx);
builder.contextDir(contextDir);
});
}

ImageConfiguration singleImageConfiguration(
Consumer<BuildImageConfiguration.Builder> buildImageConfigurationConsumer) {
BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder()
.from("scratch")
.args(Collections.singletonMap("foo", "bar"));
buildImageConfigurationConsumer.accept(builder);
BuildImageConfiguration buildImageConfiguration = builder.build();
buildImageConfiguration.initAndValidate(log);

return new Builder()
.name("example:latest")
.buildConfig(buildImageConfiguration)
.build();
.name("example:latest")
.buildConfig(buildImageConfiguration)
.build();
}

ImageConfiguration singleImageConfigurationWithBuildWithSquash(BuildXConfiguration buildx, String contextDir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.fabric8.maven.docker.service;

import io.fabric8.maven.docker.util.ImageName;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.File;
Expand Down Expand Up @@ -261,6 +263,40 @@ void useBuilder_whenDockerBuildXIncompatibleWithConfigOverride_thenCopyBuildXBin
}
}

void testBuildWithTag(boolean skipTag) throws Exception {
List<String> tags = new ArrayList<>();
tags.add("tag-" + System.currentTimeMillis());
tags.add("tag-" + System.currentTimeMillis());

//Given
buildConfigUsingBuildx(temporaryFolder, (buildX, buildImage) -> {
buildImage.skipTag(skipTag);
buildImage.tags(tags);
});

// When
buildx.build(projectPaths, imageConfig, configuredRegistry, authConfigList, buildArchive);

String[] fullTags = tags.stream()
.map(tag -> new ImageName(imageConfig.getName(), tag).getFullName(configuredRegistry))
.toArray(String[]::new);
if (skipTag) {
verifyBuildXArgumentNotPresentInExec(fullTags);
} else {
verifyBuildXArgumentPresentInExec(fullTags);
}
}

@Test
void testBuildWithSkipTag() throws Exception {
testBuildWithTag(true);
}

@Test
void testBuildWithTag() throws Exception {
testBuildWithTag(false);
}

private void givenAnImageConfiguration(String... platforms) {
final BuildXConfiguration buildxConfig = new BuildXConfiguration.Builder()
.platforms(Arrays.asList(platforms))
Expand Down

0 comments on commit 7dcc06f

Please sign in to comment.