Skip to content

Commit

Permalink
feat(maven-plugin): log informative message when maven jkube goals ar…
Browse files Browse the repository at this point in the history
…e skipped #1142

Signed-off-by: Sun Tan <sutan@redhat.com>
  • Loading branch information
sunix committed Jan 6, 2022
1 parent b662134 commit f98a290
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 32 deletions.
4 changes: 4 additions & 0 deletions kubernetes-maven-plugin/plugin/pom.xml
Expand Up @@ -157,6 +157,10 @@
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -71,6 +71,7 @@
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
Expand Down Expand Up @@ -119,6 +120,9 @@ public abstract class AbstractDockerMojo extends AbstractMojo
@Parameter(defaultValue = "${session}", readonly = true)
protected MavenSession session;

@Parameter(defaultValue = "${mojoExecution}", readonly = true)
protected MojoExecution mojoExecution;

@Parameter(property = "jkube.docker.apiVersion")
protected String apiVersion;

Expand Down Expand Up @@ -419,40 +423,46 @@ protected boolean canExecute() {
@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
init();
if (canExecute()) {
final boolean ansiRestore = Ansi.isEnabled();
if (!canExecute()) {
log.info("`%s` goal is skipped.", mojoExecution.getMojoDescriptor().getFullGoalName());
return;
}
doExecute();
}

protected void doExecute() throws MojoExecutionException, MojoFailureException {
final boolean ansiRestore = Ansi.isEnabled();
try {
DockerAccess dockerAccess = null;
try {
DockerAccess dockerAccess = null;
try {
javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
// The 'real' images configuration to use (configured images + externally resolved images)
if (isDockerAccessRequired()) {
DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
dockerAccess = dockerAccessFactory.createDockerAccess(dockerAccessContext);
}
jkubeServiceHub = JKubeServiceHub.builder()
.log(log)
.configuration(initJKubeConfiguration())
.clusterAccess(clusterAccess)
.platformMode(getConfiguredRuntimeMode())
.dockerServiceHub(DockerServiceHub.newInstance(log, dockerAccess, logOutputSpecFactory))
.buildServiceConfig(buildServiceConfigBuilder().build())
.offline(offline)
.build();
resolvedImages = ConfigHelper.initImageConfiguration(apiVersion, getBuildTimestamp(getPluginContext(), CONTEXT_KEY_BUILD_TIMESTAMP, project.getBuild().getDirectory(), DOCKER_BUILD_TIMESTAMP), javaProject, images, imageConfigResolver, log, filter, this);
executeInternal();
} catch (IOException | DependencyResolutionRequiredException exp) {
logException(exp);
throw new MojoExecutionException(exp.getMessage());
} catch (MojoExecutionException exp) {
logException(exp);
throw exp;
} finally {
Optional.ofNullable(jkubeServiceHub).ifPresent(JKubeServiceHub::close);
javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
// The 'real' images configuration to use (configured images + externally resolved images)
if (isDockerAccessRequired()) {
DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
dockerAccess = dockerAccessFactory.createDockerAccess(dockerAccessContext);
}
jkubeServiceHub = JKubeServiceHub.builder()
.log(log)
.configuration(initJKubeConfiguration())
.clusterAccess(clusterAccess)
.platformMode(getConfiguredRuntimeMode())
.dockerServiceHub(DockerServiceHub.newInstance(log, dockerAccess, logOutputSpecFactory))
.buildServiceConfig(buildServiceConfigBuilder().build())
.offline(offline)
.build();
resolvedImages = ConfigHelper.initImageConfiguration(apiVersion, getBuildTimestamp(getPluginContext(), CONTEXT_KEY_BUILD_TIMESTAMP, project.getBuild().getDirectory(), DOCKER_BUILD_TIMESTAMP), javaProject, images, imageConfigResolver, log, filter, this);
executeInternal();
} catch (IOException | DependencyResolutionRequiredException exp) {
logException(exp);
throw new MojoExecutionException(exp.getMessage());
} catch (MojoExecutionException exp) {
logException(exp);
throw exp;
} finally {
Ansi.setEnabled(ansiRestore);
Optional.ofNullable(jkubeServiceHub).ifPresent(JKubeServiceHub::close);
}
} finally {
Ansi.setEnabled(ansiRestore);
}
}

Expand Down
Expand Up @@ -26,6 +26,7 @@

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -57,6 +58,9 @@ public abstract class AbstractJKubeMojo extends AbstractMojo implements KitLogge
@Parameter(defaultValue = "${session}", readonly = true)
protected MavenSession session;

@Parameter(defaultValue = "${mojoExecution}", readonly = true)
protected MojoExecution mojoExecution;

// Whether to use color
@Parameter(property = "jkube.useColor", defaultValue = "true")
protected boolean useColor;
Expand Down Expand Up @@ -106,9 +110,12 @@ protected boolean canExecute() {
public void execute() throws MojoExecutionException, MojoFailureException {
try {
init();
if (canExecute()) {
executeInternal();
if (!canExecute()) {
log.info("`%s` goal is skipped.", mojoExecution.getMojoDescriptor().getFullGoalName());
return;
}
executeInternal();

} catch (DependencyResolutionRequiredException e) {
throw new MojoFailureException(e.getMessage());
}
Expand Down
Expand Up @@ -28,7 +28,9 @@
import mockit.Mocked;
import mockit.Verifications;
import org.apache.maven.model.Build;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
Expand All @@ -50,6 +52,11 @@ public class HelmPushMojoTest {
HelmService helmService;
@Mocked
SecDispatcher secDispatcher;
@Mocked
MojoExecution mojoExecution;
@Mocked
MojoDescriptor mojoDescriptor;


private Properties mavenProperties;

Expand All @@ -63,12 +70,15 @@ public void setUp() throws Exception {
helmPushMojo.project = mavenProject;
helmPushMojo.settings = new Settings();
helmPushMojo.securityDispatcher = secDispatcher;
helmPushMojo.mojoExecution = mojoExecution;
// @formatter:off
new Expectations(helmPushMojo) {{
mavenProject.getProperties(); result = mavenProperties; minTimes = 0;
mavenProject.getBuild(); result = mavenBuild; minTimes = 0;
mavenBuild.getOutputDirectory(); result = "target/classes"; minTimes = 0;
mavenBuild.getDirectory(); result = "target"; minTimes = 0;
mojoExecution.getMojoDescriptor(); result = mojoDescriptor; minTimes = 0;
mojoDescriptor.getFullGoalName(); result = "k8s:helm-push"; minTimes = 0;
secDispatcher.decrypt(anyString);
result = new Delegate<String>() {String delegate(String arg) {return arg;}}; minTimes = 0;
}};
Expand Down
@@ -0,0 +1,106 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.maven.plugin.mojo.build;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.maven.plugin.MojoExecution;
import org.eclipse.jkube.kit.common.KitLogger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class SkipGoalsTest {

@Mock
private KitLogger log;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private MojoExecution mojoExecution;

@Spy
@InjectMocks
BuildMojo buildMojo;

public void setupBuildGoal() throws Exception {
doNothing().when(buildMojo).init();
doNothing().when(buildMojo).doExecute();
when(mojoExecution.getMojoDescriptor().getFullGoalName()).thenReturn("k8s:build");
}

@Test
public void should_execute_build_goal_if_skip_false() throws Exception {
setupBuildGoal();
// given
buildMojo.skip = false;
// when
buildMojo.execute();
// then
verify(buildMojo).doExecute();
}

@Test
public void should_log_informative_message_when_build_goal_is_skipped() throws Exception {
setupBuildGoal();
// given
buildMojo.skip = true;
// when
buildMojo.execute();
// then
verify(buildMojo, never()).doExecute();
verify(log).info("`%s` goal is skipped.", "k8s:build");
}

@Spy
@InjectMocks
ApplyMojo applyMojo;

public void setupApplyGoal() throws Exception {
doNothing().when(applyMojo).init();
doNothing().when(applyMojo).executeInternal();
when(mojoExecution.getMojoDescriptor().getFullGoalName()).thenReturn("k8s:apply");
}

@Test
public void should_execute_apply_goal_if_skip_false() throws Exception {
setupApplyGoal();
// given
applyMojo.skip = false;
// when
applyMojo.execute();
// then
verify(applyMojo).executeInternal();
}

@Test
public void should_log_informative_message_when_apply_goal_is_skipped() throws Exception {
setupApplyGoal();
// given
applyMojo.skip = true;
// when
applyMojo.execute();
// then
verify(applyMojo, never()).executeInternal();
verify(log).info("`%s` goal is skipped.", "k8s:apply");
}

}

0 comments on commit f98a290

Please sign in to comment.