Properly test the --install feature

This commit is contained in:
Peter Palaga
2020-06-17 12:37:22 +02:00
parent 3c23c154d7
commit aeb13b7fcf
18 changed files with 774 additions and 197 deletions

View File

@@ -0,0 +1,76 @@
package org.jboss.fuse.mvnd.it;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Objects;
import java.util.Set;
import javax.inject.Inject;
import org.assertj.core.api.Assertions;
import org.jboss.fuse.mvnd.client.Client;
import org.jboss.fuse.mvnd.client.ClientOutput;
import org.jboss.fuse.mvnd.client.DefaultClient;
import org.jboss.fuse.mvnd.client.Environment;
import org.jboss.fuse.mvnd.junit.MvndNativeTest;
import org.jboss.fuse.mvnd.junit.TestLayout;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@MvndNativeTest(projectDir = "src/test/projects/single-module")
public class InstallDaemonNativeIT {
@Inject
Client client;
@Inject
TestLayout layout;
@Test
void installDaemon() throws IOException, InterruptedException {
final Path testDir = layout.getTestDir();
final Path mvndPropertiesPath = testDir.resolve("installation-mvnd.properties");
final Path mavenHome = testDir.resolve("installation-maven-home");
Assertions.assertThat(mvndPropertiesPath).doesNotExist();
Assertions.assertThat(mavenHome).doesNotExist();
final ClientOutput o = Mockito.mock(ClientOutput.class);
final Path mvndDistPath = Paths.get(Objects.requireNonNull(System.getProperty("mvnd.dist.path")))
.toAbsolutePath()
.normalize();
Assertions.assertThat(mvndDistPath).exists();
final String mvndDistUri = mvndDistPath.toUri().toString();
client.execute(o,
"--install",
Environment.MVND_DIST_URI.asCommandLineProperty(mvndDistUri),
Environment.MVND_PROPERTIES_PATH.asCommandLineProperty(mvndPropertiesPath.toString()),
Environment.JAVA_HOME.asCommandLineProperty(layout.javaHome().toString()),
Environment.MAVEN_HOME.asCommandLineProperty(mavenHome.toString()))
.assertSuccess();
Assertions.assertThat(mvndPropertiesPath).exists();
Assertions.assertThat(mavenHome).exists();
final Path mvndShPath = mavenHome.resolve("bin/mvnd");
Assertions.assertThat(mvndShPath).exists();
if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
final PosixFileAttributeView attributes = Files.getFileAttributeView(mvndShPath, PosixFileAttributeView.class);
Assertions.assertThat(attributes).isNotNull();
final Set<PosixFilePermission> permissions = attributes.readAttributes().permissions();
Assertions.assertThat(permissions).contains(PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OWNER_EXECUTE,
PosixFilePermission.OTHERS_EXECUTE);
}
final String version = DefaultClient.loadBuildProperties().getProperty("version");
Assertions.assertThat(mavenHome.resolve("lib/ext/mvnd-client-" + version + ".jar")).exists();
Assertions.assertThat(mavenHome.resolve("lib/ext/mvnd-daemon-" + version + ".jar")).exists();
}
}

View File

@@ -0,0 +1,8 @@
package org.jboss.fuse.mvnd.it;
import org.jboss.fuse.mvnd.junit.MvndTest;
@MvndTest(projectDir = "src/test/projects/single-module")
public class InstallDaemonTest extends InstallDaemonNativeIT {
}

View File

@@ -13,7 +13,6 @@ import org.jboss.fuse.mvnd.client.ClientLayout;
import org.jboss.fuse.mvnd.client.ClientOutput;
import org.jboss.fuse.mvnd.junit.MvndNativeTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.mockito.InOrder;
import org.mockito.Mockito;
@@ -27,7 +26,7 @@ public class SingleModuleNativeIT {
ClientLayout layout;
@Test
void cleanInstall(TestInfo testInfo) throws IOException, InterruptedException {
void cleanInstall() throws IOException, InterruptedException {
final Path helloFilePath = layout.multiModuleProjectDirectory().resolve("target/hello.txt");
if (Files.exists(helloFilePath)) {
Files.delete(helloFilePath);

View File

@@ -68,13 +68,11 @@ public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback,
f.setAccessible(true);
if (f.getType() == DaemonRegistry.class) {
f.set(testInstance, resource.registry);
} else if (f.getType() == ClientLayout.class) {
f.set(testInstance, resource.layout);
} else if (f.getType() == Layout.class) {
} else if (Layout.class.isAssignableFrom(f.getType())) {
f.set(testInstance, resource.layout);
} else if (f.getType() == Client.class) {
if (resource.isNative) {
final Path mvndNativeExecutablePath = Paths.get(System.getProperty("mvnd.native.executable"));
final Path mvndNativeExecutablePath = Paths.get(System.getProperty("mvnd.native.executable")).toAbsolutePath().normalize();
if (!Files.isRegularFile(mvndNativeExecutablePath)) {
throw new IllegalStateException("mvnd executable does not exist: " + mvndNativeExecutablePath);
}
@@ -101,7 +99,7 @@ public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback,
static class MvndResource implements ExtensionContext.Store.CloseableResource {
private final ClientLayout layout;
private final TestLayout layout;
private final DaemonRegistry registry;
private final boolean isNative;
private final long timeoutMs;
@@ -141,9 +139,12 @@ public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback,
throw new IllegalStateException(
"The value of mvnd.home system property points at a path that does not exist or is not a directory");
}
final Path mvndPropertiesPath = testDir.resolve("mvnd.properties");
final Path localMavenRepository = deleteDir(testDir.resolve("local-maven-repo"));
final Path settingsPath = createSettings(testDir.resolve("settings.xml"));
final ClientLayout layout = new ClientLayout(
final TestLayout layout = new TestLayout(
testDir,
mvndPropertiesPath,
mvndHome,
testExecutionDir,
testExecutionDir,
@@ -188,7 +189,7 @@ public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback,
return settingsPath;
}
public MvndResource(ClientLayout layout, DaemonRegistry registry, boolean isNative, long timeoutMs) {
public MvndResource(TestLayout layout, DaemonRegistry registry, boolean isNative, long timeoutMs) {
super();
this.layout = layout;
this.registry = registry;

View File

@@ -16,6 +16,7 @@ import java.util.stream.Collectors;
import org.jboss.fuse.mvnd.client.Client;
import org.jboss.fuse.mvnd.client.ClientLayout;
import org.jboss.fuse.mvnd.client.ClientOutput;
import org.jboss.fuse.mvnd.client.Environment;
import org.jboss.fuse.mvnd.client.ExecutionResult;
/**
@@ -43,14 +44,23 @@ public class NativeTestClient implements Client {
final List<String> cmd = new ArrayList<String>(args.size() + 1);
cmd.add(mvndNativeExecutablePath.toString());
args.stream().forEach(cmd::add);
cmd.add("-Dmaven.repo.local=" + layout.getLocalMavenRepository().toString());
ProcessBuilder builder = new ProcessBuilder(cmd.toArray(new String[0]))
if (!Environment.MVND_PROPERTIES_PATH.hasCommandLineProperty(args)) {
cmd.add(Environment.MVND_PROPERTIES_PATH.asCommandLineProperty(layout.getMvndPropertiesPath().toString()));
}
if (!Environment.MAVEN_REPO_LOCAL.hasCommandLineProperty(args)) {
cmd.add(Environment.MAVEN_REPO_LOCAL.asCommandLineProperty(layout.getLocalMavenRepository().toString()));
}
final ProcessBuilder builder = new ProcessBuilder(cmd.toArray(new String[0]))
.directory(layout.userDir().toFile()) //
.redirectErrorStream(false);
.redirectErrorStream(true);
Map<String, String> env = builder.environment();
env.put("MAVEN_HOME", System.getProperty("mvnd.home"));
env.put("JAVA_HOME", System.getProperty("java.home"));
final Map<String, String> env = builder.environment();
if (!Environment.MAVEN_HOME.hasCommandLineProperty(args)) {
env.put("MAVEN_HOME", System.getProperty("mvnd.home"));
}
if (!Environment.JAVA_HOME.hasCommandLineProperty(args)) {
env.put("JAVA_HOME", System.getProperty("java.home"));
}
final String cmdString = cmd.stream().collect(Collectors.joining(" "));
output.accept("Executing " + cmdString);
try (CommandProcess process = new CommandProcess(builder.start(), cmd, output)) {
@@ -64,15 +74,16 @@ public class NativeTestClient implements Client {
private final int exitCode;
private final List<String> args;
private final List<String> log;
public Result(List<String> args, int exitCode) {
public Result(List<String> args, int exitCode, List<String> log) {
super();
this.args = new ArrayList<>(args);
this.exitCode = exitCode;
this.log = log;
}
StringBuilder appendCommand(StringBuilder sb) {
sb.append("mvnd");
for (String arg : args) {
sb.append(" \"").append(arg).append('"');
}
@@ -90,7 +101,16 @@ public class NativeTestClient implements Client {
public Result assertSuccess() {
if (exitCode != 0) {
throw new AssertionError(appendCommand(new StringBuilder("mvnd returned ").append(exitCode).append(": ")));
final StringBuilder sb = appendCommand(new StringBuilder("mvnd returned ").append(exitCode));
if (exitCode == TIMEOUT_EXIT_CODE) {
sb.append(" (timeout)");
}
sb.append("--- stderr+stdout start ---");
synchronized (log) {
log.stream().forEach(s -> sb.append('\n').append(s));
}
sb.append("--- stderr+stdout end ---");
throw new AssertionError(sb);
}
return this;
}
@@ -114,13 +134,19 @@ public class NativeTestClient implements Client {
private final Process process;
private final Thread shutDownHook;
private final StreamGobbler stdOut;
private List<String> args;
private final List<String> args;
private final List<String> log = new ArrayList<>();
public CommandProcess(Process process, List<String> args, Consumer<String> outputConsumer) {
super();
this.process = process;
this.args = args;
this.stdOut = new StreamGobbler(process.getInputStream(), outputConsumer);
final Consumer<String> loggingConsumer = s -> {
synchronized (log) {
log.add(s);
}
};
this.stdOut = new StreamGobbler(process.getInputStream(), loggingConsumer.andThen(outputConsumer));
stdOut.start();
this.shutDownHook = new Thread(new Runnable() {
@@ -148,7 +174,8 @@ public class NativeTestClient implements Client {
Runtime.getRuntime().removeShutdownHook(shutDownHook);
} catch (Exception ignored) {
}
return new Result(args, timeouted ? TIMEOUT_EXIT_CODE : process.exitValue());
final int exitCode = timeouted ? TIMEOUT_EXIT_CODE : process.exitValue();
return new Result(args, exitCode, log);
}
}

View File

@@ -0,0 +1,21 @@
package org.jboss.fuse.mvnd.junit;
import java.nio.file.Path;
import org.jboss.fuse.mvnd.client.ClientLayout;
public class TestLayout extends ClientLayout {
private final Path testDir;
public TestLayout(Path testDir, Path mvndPropertiesPath, Path mavenHome, Path userDir, Path multiModuleProjectDirectory,
Path javaHome,
Path localMavenRepository, Path settings) {
super(mvndPropertiesPath, mavenHome, userDir, multiModuleProjectDirectory, javaHome, localMavenRepository, settings);
this.testDir = testDir;
}
public Path getTestDir() {
return testDir;
}
}