Skip to content

Commit

Permalink
Merge branch 'master' into 1576
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia authored Jul 9, 2022
2 parents 07d928c + db7a69b commit 46eb0a4
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public String toJson() {
JsonObject creds = new JsonObject();
creds.addProperty("auth", encodeBase64(username + ":" + password));
JsonObject auths = new JsonObject();
auths.add(registry != null ? registry : "docker.io", creds);
auths.add(registry != null ? registry : "https://index.docker.io/v1/", creds);
JsonObject root = new JsonObject();
root.add("auths", auths);
return root.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public File createDockerTarArchive(String imageName, final MojoParameters params
throws MojoExecutionException {

final BuildDirs buildDirs = createBuildDirs(imageName, params);
final List<AssemblyConfiguration> assemblyConfigurations = buildConfig.getAssemblyConfigurations();
final List<AssemblyConfiguration> assemblyConfigurations = buildConfig.getAllAssemblyConfigurations();

final List<ArchiverCustomizer> archiveCustomizers = new ArrayList<>();

Expand Down Expand Up @@ -241,7 +241,7 @@ private void interpolateDockerfile(File dockerFile, BuildDirs params, FixedStrin

// visible for testing
void verifyGivenDockerfile(File dockerFile, BuildImageConfiguration buildConfig, FixedStringSearchInterpolator interpolator, Logger log) throws IOException {
List<AssemblyConfiguration> assemblyConfigs = buildConfig.getAssemblyConfigurations();
List<AssemblyConfiguration> assemblyConfigs = buildConfig.getAllAssemblyConfigurations();
if (assemblyConfigs.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import org.apache.maven.plugins.annotations.Parameter;

import io.fabric8.maven.docker.util.DeepCopy;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.MojoParameters;
import org.apache.maven.plugins.annotations.Parameter;

/**
* @author roland
Expand Down Expand Up @@ -295,26 +297,40 @@ public String getWorkdir() {
}

/**
* @deprecated Use {@link #getAssemblyConfigurations()} instead.
* @deprecated Use {@link #getAllAssemblyConfigurations()} instead.
*/
@Deprecated
public AssemblyConfiguration getAssemblyConfiguration() {
return assembly;
}


/**
* Use {@link #getAllAssemblyConfigurations()} unless you specifically want the configuration defined only by <code>assemblies</code>.
*/
@Nonnull
public List<AssemblyConfiguration> getAssemblyConfigurations() {
public List<AssemblyConfiguration> getAssembliesConfiguration() {
final List<AssemblyConfiguration> assemblyConfigurations = new ArrayList<>();

if (assemblies != null) {
for (AssemblyConfiguration config : assemblies) {
if (config != null) {
assemblyConfigurations.add(config);
}
}
}

return assemblyConfigurations;
}

@Nonnull
public List<AssemblyConfiguration> getAllAssemblyConfigurations() {
final List<AssemblyConfiguration> assemblyConfigurations = getAssembliesConfiguration();

if (assembly != null) {
assemblyConfigurations.add(assembly);
}

return assemblyConfigurations;
}

Expand Down Expand Up @@ -762,7 +778,7 @@ public String initAndValidate(Logger log) throws IllegalArgumentException {
}

private void ensureUniqueAssemblyNames(Logger log) {
List<AssemblyConfiguration> assemblyConfigurations = getAssemblyConfigurations();
List<AssemblyConfiguration> assemblyConfigurations = getAllAssemblyConfigurations();
Set<String> assemblyNames = new HashSet<>();
for (AssemblyConfiguration config : assemblyConfigurations) {
String assemblyName = config.getName();
Expand Down Expand Up @@ -822,7 +838,7 @@ private File findDockerFileFile(Logger log) {
// TODO: Remove the following deprecated handling section
if (dockerArchive == null) {
Optional<String> deprecatedDockerFileDir =
getAssemblyConfigurations().stream()
getAllAssemblyConfigurations().stream()
.map(AssemblyConfiguration::getDockerFileDir)
.filter(Objects::nonNull)
.findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum ConfigKey {
ALIAS,
ARGS(ValueCombinePolicy.Merge),
ASSEMBLIES,
ASSEMBLY,
ASSEMBLY_BASEDIR("assembly.baseDir"),
ASSEMBLY_DESCRIPTOR("assembly.descriptor"),
ASSEMBLY_DESCRIPTOR_REF("assembly.descriptorRef"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private BuildImageConfiguration extractBuildConfiguration(ImageConfiguration fro
.optimise(valueProvider.getBoolean(OPTIMISE, config.getOptimise()))
.entryPoint(extractArguments(valueProvider, ENTRYPOINT, config.getEntryPoint()))
.assembly(extractAssembly(config.getAssemblyConfiguration(), valueProvider))
.assemblies(extractAssemblies(config.getAssemblyConfigurations(), valueProvider))
.assemblies(extractAssemblies(config.getAssembliesConfiguration(), valueProvider))
.env(CollectionUtils.mergeMaps(
valueProvider.getMap(ENV_BUILD, config.getEnv()),
valueProvider.getMap(ENV, Collections.emptyMap())
Expand Down Expand Up @@ -275,6 +275,12 @@ private List<AssemblyConfiguration> extractAssemblies(List<AssemblyConfiguration

@SuppressWarnings("deprecation")
private AssemblyConfiguration extractAssembly(AssemblyConfiguration config, ValueProvider valueProvider) {
Map<String, String> assemblyProperties = valueProvider.getMap(ASSEMBLY, Collections.emptyMap());

if (assemblyProperties == null || assemblyProperties.isEmpty()) {
return config;
}

if (config == null) {
config = new AssemblyConfiguration();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public AssemblyFiles getAssemblyFiles(ImageConfiguration imageConfig, String ass

String name = imageConfig.getName();
try {
List<AssemblyConfiguration> assemblyConfigurations = imageConfig.getBuildConfiguration().getAssemblyConfigurations();
List<AssemblyConfiguration> assemblyConfigurations = imageConfig.getBuildConfiguration().getAllAssemblyConfigurations();
AssemblyConfiguration assemblyConfig = assemblyConfigurations.stream().filter(a -> a.getName().equals(assemblyName)).findFirst().orElse(null);

if (assemblyConfig == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ private String extractBaseFromConfiguration(BuildImageConfiguration buildConfig)
String fromImage;
fromImage = buildConfig.getFrom();
if (fromImage == null) {
List<AssemblyConfiguration> assemblyConfig = buildConfig.getAssemblyConfigurations();
List<AssemblyConfiguration> assemblyConfig = buildConfig.getAllAssemblyConfigurations();
if (assemblyConfig.isEmpty()) {
fromImage = DockerAssemblyManager.DEFAULT_DATA_BASE_IMAGE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void build(String jibImageFormat, ImageConfiguration imageConfig, Registr

File dockerTarArchive = getAssemblyTarArchive(imageConfig, serviceHub, mojoParameters, log);

for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAssemblyConfigurations()) {
for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAllAssemblyConfigurations()) {
// TODO: Improve Assembly Manager so that the effective assemblyFileEntries computed can be properly shared
// the call to DockerAssemblyManager.getInstance().createDockerTarArchive should not be necessary,
// files should be added using the AssemblyFileEntry list. DockerAssemblyManager, should provide
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public synchronized void watch(WatchContext context, BuildService.BuildContext b
ArrayList<String> tasks = new ArrayList<>();

if (imageConfig.getBuildConfiguration() != null) {
for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAssemblyConfigurations()) {
for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAllAssemblyConfigurations()) {
if (watcher.isCopy()) {
String containerBaseDir = assemblyConfiguration.getTargetDir();
schedule(executor, createCopyWatchTask(watcher, assemblyConfiguration.getName(), context.getMojoParameters(), containerBaseDir), interval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
import org.codehaus.plexus.util.ReflectionUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -76,7 +75,7 @@ class DockerAssemblyManagerTest {
@Test
void testNoAssembly() {
BuildImageConfiguration buildConfig = new BuildImageConfiguration();
List<AssemblyConfiguration> assemblyConfig = buildConfig.getAssemblyConfigurations();
List<AssemblyConfiguration> assemblyConfig = buildConfig.getAllAssemblyConfigurations();

String content =
assemblyManager.createDockerFileBuilder(
Expand All @@ -94,7 +93,7 @@ void testShellIsSet() {
.build();

DockerFileBuilder builder =
assemblyManager.createDockerFileBuilder(buildConfig, buildConfig.getAssemblyConfigurations());
assemblyManager.createDockerFileBuilder(buildConfig, buildConfig.getAllAssemblyConfigurations());
String content = builder.content();

Assertions.assertTrue(content.contains("SHELL [\"/bin/sh\",\"echo\",\"hello\"]"));
Expand All @@ -111,7 +110,7 @@ void assemblyFiles(@TempDir Path tmpDir,

BuildImageConfiguration buildConfig = createBuildConfig();

assemblyManager.getAssemblyFiles("testImage", buildConfig.getAssemblyConfigurations().get(0), mojoParams, new AnsiLogger(new SystemStreamLog(), true, "build"));
assemblyManager.getAssemblyFiles("testImage", buildConfig.getAllAssemblyConfigurations().get(0), mojoParams, new AnsiLogger(new SystemStreamLog(), true, "build"));
Mockito.verify(assemblyArchiver).createArchive(Mockito.eq(assembly), Mockito.eq("maven"), Mockito.eq("track"), Mockito.any(DockerAssemblyConfigurationSource.class),
Mockito.eq(false), Mockito.any());
}
Expand All @@ -128,10 +127,10 @@ void multipleAssemblyFiles(@TempDir Path tmpDir,

BuildImageConfiguration buildConfig = createBuildConfigMultiAssembly();

AssemblyFiles files = assemblyManager.getAssemblyFiles("testImage", buildConfig.getAssemblyConfigurations().get(0), mojoParams,
AssemblyFiles files = assemblyManager.getAssemblyFiles("testImage", buildConfig.getAllAssemblyConfigurations().get(0), mojoParams,
new AnsiLogger(new SystemStreamLog(), true, "build"));
Assertions.assertNotNull(files);
files = assemblyManager.getAssemblyFiles("testImage", buildConfig.getAssemblyConfigurations().get(1), mojoParams, new AnsiLogger(new SystemStreamLog(), true, "build"));
files = assemblyManager.getAssemblyFiles("testImage", buildConfig.getAllAssemblyConfigurations().get(1), mojoParams, new AnsiLogger(new SystemStreamLog(), true, "build"));
Assertions.assertNotNull(files);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import io.fabric8.maven.docker.config.UlimitConfig;
import io.fabric8.maven.docker.config.WaitConfiguration;
import io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest;
import io.fabric8.maven.docker.util.Logger;

import org.apache.maven.plugins.assembly.model.Assembly;
import org.apache.maven.plugins.assembly.model.DependencySet;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -658,8 +660,9 @@ imageConfiguration, props(
void testAssembly() {
List<ImageConfiguration> configs = resolveImage(imageConfiguration, props(getTestAssemblyData()));
Assertions.assertEquals(1, configs.size());
configs.get(0).initAndValidate(ConfigHelper.NameFormatter.IDENTITY, null);

List<AssemblyConfiguration> assemblies = configs.get(0).getBuildConfiguration().getAssemblyConfigurations();
List<AssemblyConfiguration> assemblies = configs.get(0).getBuildConfiguration().getAllAssemblyConfigurations();
Assertions.assertEquals(1, assemblies.size());

AssemblyConfiguration config = assemblies.get(0);
Expand All @@ -673,8 +676,9 @@ void testAssembly() {
void testMultipleAssemblies() {
List<ImageConfiguration> configs = resolveImage(imageConfiguration, props(getTestMultipleAssemblyData()));
Assertions.assertEquals(1, configs.size());
configs.get(0).initAndValidate(ConfigHelper.NameFormatter.IDENTITY, null);

List<AssemblyConfiguration> assemblies = configs.get(0).getBuildConfiguration().getAssemblyConfigurations();
List<AssemblyConfiguration> assemblies = configs.get(0).getBuildConfiguration().getAllAssemblyConfigurations();
Assertions.assertEquals(2, assemblies.size());

AssemblyConfiguration config = assemblies.get(0);
Expand Down Expand Up @@ -707,10 +711,13 @@ void testAssemblyInline() {

List<ImageConfiguration> configs = resolveImage(imageConfiguration, props(getTestAssemblyData()));
Assertions.assertEquals(1, configs.size());
configs.get(0).initAndValidate(ConfigHelper.NameFormatter.IDENTITY, null);

AssemblyConfiguration config = configs.get(0).getBuildConfiguration().getAssemblyConfiguration();
Assertions.assertNotNull(config.getInline());
Assertions.assertEquals(1, config.getInline().getDependencySets().size());
List<AssemblyConfiguration> assemblyConfigurations = configs.get(0).getBuildConfiguration().getAllAssemblyConfigurations();
Assertions.assertEquals(1, assemblyConfigurations.size());
AssemblyConfiguration assemblyConfiguration = assemblyConfigurations.get(0);
Assertions.assertNotNull(assemblyConfiguration.getInline());
Assertions.assertEquals(1, assemblyConfiguration.getInline().getDependencySets().size());
}

@Test
Expand Down Expand Up @@ -1023,7 +1030,7 @@ private void validateBuildConfiguration(BuildImageConfiguration buildConfig) {
* validate only the descriptor is required and defaults are all used, 'testAssembly' validates
* all options can be set
*/
List<AssemblyConfiguration> assemblyConfigurations = buildConfig.getAssemblyConfigurations();
List<AssemblyConfiguration> assemblyConfigurations = buildConfig.getAllAssemblyConfigurations();
Assertions.assertEquals(1, assemblyConfigurations.size());

AssemblyConfiguration assemblyConfig = assemblyConfigurations.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.fabric8.maven.docker.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.maven.plugins.assembly.model.Assembly;
import org.apache.maven.plugins.assembly.model.FileItem;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import io.fabric8.maven.docker.assembly.AssemblyFiles;
import io.fabric8.maven.docker.assembly.DockerAssemblyManager;
import io.fabric8.maven.docker.config.AssemblyConfiguration;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.MojoParameters;

@ExtendWith(MockitoExtension.class)
class ArchiveServiceTest {

@Mock
private DockerAssemblyManager dockerAssemblyManager;

@Mock
private Logger log;

private ArchiveService archiveService;

@BeforeEach
void setUp() {
archiveService = new ArchiveService(dockerAssemblyManager, null);
}

@Test
void testGetAssemblyFiles() throws Exception {
// ARRANGE
when(dockerAssemblyManager.getAssemblyFiles(any(), any(), any(), any())).thenReturn(mock(AssemblyFiles.class));
AssemblyConfiguration assemblyConfiguration = new AssemblyConfiguration();
BuildImageConfiguration build = new BuildImageConfiguration.Builder().assembly(assemblyConfiguration).build();
ImageConfiguration imageConfiguration = new ImageConfiguration.Builder().buildConfig(build).build();

// ACT
AssemblyFiles assemblyFiles = archiveService.getAssemblyFiles(imageConfiguration, assemblyConfiguration.getName(), mock(MojoParameters.class));

// ASSERT
Assertions.assertNotNull(assemblyFiles);
}
}
Loading

0 comments on commit 46eb0a4

Please sign in to comment.