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

BuildX failing for Docker CLI on Windows fixed #1819

Merged
merged 4 commits into from
Sep 25, 2024
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
34 changes: 8 additions & 26 deletions src/main/java/io/fabric8/maven/docker/service/BuildXService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,17 @@
import io.fabric8.maven.docker.access.util.ExternalCommand;
import io.fabric8.maven.docker.assembly.BuildDirs;
import io.fabric8.maven.docker.assembly.DockerAssemblyManager;
import io.fabric8.maven.docker.config.AttestationConfiguration;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.BuildXConfiguration;
import io.fabric8.maven.docker.config.ConfigHelper;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.config.SecretConfiguration;
import io.fabric8.maven.docker.config.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we need to address #1819 (comment) here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok tried to revert that aswell

import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.ProjectPaths;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand Down Expand Up @@ -74,7 +55,6 @@ protected <C> void useBuilder(ProjectPaths projectPaths, ImageConfiguration imag
List<String> buildX = new ArrayList<>(Arrays.asList(DOCKER, "--config", configPath.toString(), "buildx"));
if (!isDockerBuildXWorkingWithOverriddenConfig(configPath)) {
logger.debug("Detected current version of BuildX not working with --config override");
logger.debug("Copying BuildX binary to " + configPath);
copyBuildXToConfigPathIfBuildXBinaryInDefaultDockerConfig(configPath);
}

Expand All @@ -90,10 +70,12 @@ protected <C> void useBuilder(ProjectPaths projectPaths, ImageConfiguration imag

private void copyBuildXToConfigPathIfBuildXBinaryInDefaultDockerConfig(Path configPath) {
try {
File buildXInUserHomeDockerConfig = Paths.get(EnvUtil.getUserHome(), ".docker/cli-plugins/docker-buildx").toFile();
String dockerBuildxExecutableName = "docker-buildx" + (EnvUtil.isWindows()?".exe":"");
File buildXInUserHomeDockerConfig = Paths.get(EnvUtil.getUserHome(), ".docker/cli-plugins/" + dockerBuildxExecutableName).toFile();
Files.createDirectory(configPath.resolve("cli-plugins"));
if (buildXInUserHomeDockerConfig.exists() && buildXInUserHomeDockerConfig.isFile()) {
Files.copy(buildXInUserHomeDockerConfig.toPath(), configPath.resolve("cli-plugins").resolve("docker-buildx"), StandardCopyOption.COPY_ATTRIBUTES);
Files.copy(buildXInUserHomeDockerConfig.toPath(), configPath.resolve("cli-plugins").resolve(dockerBuildxExecutableName), StandardCopyOption.COPY_ATTRIBUTES);
logger.debug("Copying BuildX binary to " + configPath);
}
} catch (IOException exception) {
logger.debug(exception.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
package io.fabric8.maven.docker.service;

import io.fabric8.maven.docker.util.ImageName;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.File;
import java.nio.file.Paths;

import io.fabric8.maven.docker.access.AuthConfig;
import java.util.function.BiConsumer;

import io.fabric8.maven.docker.util.EnvUtil;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.AfterEach;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.Spy;
import org.mockito.Mockito;
import org.mockito.quality.Strictness;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import io.fabric8.maven.docker.access.DockerAccess;
import io.fabric8.maven.docker.access.AuthConfig;
import io.fabric8.maven.docker.access.AuthConfigList;
import io.fabric8.maven.docker.access.DockerAccess;
import io.fabric8.maven.docker.assembly.DockerAssemblyManager;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.ProjectPaths;
import io.fabric8.maven.docker.config.BuildXConfiguration;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.BuildXConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.ProjectPaths;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
Expand Down Expand Up @@ -238,28 +235,32 @@ void useBuilder_whenConfiguredRegistryAbsentInDockerRegistry_thenAddConfigOption
assertEquals(Arrays.asList("docker", "--config", temporaryFolder.getAbsolutePath(), "buildx"), buildXArgCaptor.getValue());
}

@Test
void useBuilder_whenDockerBuildXIncompatibleWithConfigOverride_thenCopyBuildXBinaryToTemporaryConfig() throws IOException, MojoExecutionException {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void useBuilder_whenDockerBuildXIncompatibleWithConfigOverride_thenCopyBuildXBinaryToTemporaryConfig(boolean isWindows) throws IOException, MojoExecutionException {
try (MockedStatic<EnvUtil> envUtilMockedStatic = mockStatic(EnvUtil.class);
MockedConstruction<BuildXService.BuildXListWithConfigCommand> ignore = mockConstruction(BuildXService.BuildXListWithConfigCommand.class, (mock, ctx) -> {
MockedConstruction<BuildXService.BuildXListWithConfigCommand> ignore = mockConstruction(BuildXService.BuildXListWithConfigCommand.class, (mock, ctx) -> {
when(mock.isSuccessFul()).thenReturn(false);
})) {
// Given
Path configDirPath = temporaryFolder.toPath().resolve("docker-state-dir");
Files.createDirectory(configDirPath);
Files.createDirectory(temporaryFolder.toPath().resolve(".docker"));
Files.createDirectory(temporaryFolder.toPath().resolve(".docker").resolve("cli-plugins"));
Files.createFile(temporaryFolder.toPath().resolve(".docker").resolve("cli-plugins").resolve("docker-buildx"));
final String buildxExecutableFilename = "docker-buildx" + (isWindows ? ".exe" : "");
Files.createFile(temporaryFolder.toPath().resolve(".docker").resolve("cli-plugins").resolve(buildxExecutableFilename));
envUtilMockedStatic.when(EnvUtil::getUserHome).thenReturn(temporaryFolder.getAbsolutePath());
envUtilMockedStatic.when(EnvUtil::isWindows).thenReturn(isWindows);
BuildXService.Builder<File> builder = Mockito.mock(BuildXService.Builder.class);

givenAnImageConfiguration("linux/arm46", "linux/amd64");

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

// Then
assertTrue(configDirPath.resolve("cli-plugins").toFile().exists());
assertTrue(configDirPath.resolve("cli-plugins").resolve("docker-buildx").toFile().exists());
assertTrue(configDirPath.resolve("cli-plugins").resolve(buildxExecutableFilename).toFile().exists());
verify(logger).debug("Detected current version of BuildX not working with --config override");
verify(logger).debug("Copying BuildX binary to " + temporaryFolder.toPath().resolve("docker-state-dir"));
}
Expand Down
Loading