Skip to content

Commit

Permalink
Isolate the integration tests from the local environment #97
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Oct 17, 2020
1 parent 90b4c35 commit e9b92ae
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public static ClientLayout getEnvInstance() {
final Path mvndPropertiesPath = Environment.findMvndPropertiesPath();
final Supplier<Properties> mvndProperties = lazyMvndProperties(mvndPropertiesPath);
final Path pwd = Paths.get(".").toAbsolutePath().normalize();
final Path mvndHome = Environment.findBasicMavenHome()
.orLocalProperty(mvndProperties, mvndPropertiesPath)
.or(new ValueSource(
final Path mvndHome = Environment.MVND_HOME
.fromValueSource(new ValueSource(
description -> description.append("path relative to the mvnd executable"),
() -> {
Optional<String> cmd = ProcessHandle.current().info().command();
Expand All @@ -63,6 +62,9 @@ public static ClientLayout getEnvInstance() {
}
return null;
}))
.orSystemProperty()
.orEnvironmentVariable()
.orLocalProperty(mvndProperties, mvndPropertiesPath)
.orFail()
.asPath()
.toAbsolutePath().normalize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.jboss.fuse.mvnd.client;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SocketChannel;
Expand All @@ -30,7 +32,9 @@
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jboss.fuse.mvnd.common.BuildProperties;
import org.jboss.fuse.mvnd.common.DaemonCompatibilitySpec;
import org.jboss.fuse.mvnd.common.DaemonCompatibilitySpec.Result;
Expand Down Expand Up @@ -252,7 +256,11 @@ private String startDaemon() {
final Path workingDir = layout.userDir();
String command = "";
try {
String classpath = findCommonJar(mavenHome).toString();
String classpath = findJars(
mavenHome,
p -> p.getFileName().toString().equals("mvnd-common-" + buildProperties.getVersion() + ".jar"),
p -> p.getFileName().toString().startsWith("slf4j-api-"),
p -> p.getFileName().toString().startsWith("logback-"));
final String java = IS_WINDOWS ? "bin\\java.exe" : "bin/java";
List<String> args = new ArrayList<>();
args.add(layout.javaHome().resolve(java).toString());
Expand All @@ -275,7 +283,7 @@ private String startDaemon() {
command = String.join(" ", args);

LOGGER.debug("Starting daemon process: uid = {}, workingDir = {}, daemonArgs: {}", uid, workingDir, command);
ProcessBuilder.Redirect redirect = ProcessBuilder.Redirect.appendTo(layout.daemonLog("output").toFile());
ProcessBuilder.Redirect redirect = ProcessBuilder.Redirect.appendTo(layout.daemonLog(uid + ".out").toFile());
new ProcessBuilder()
.directory(workingDir.toFile())
.command(args)
Expand All @@ -291,16 +299,21 @@ private String startDaemon() {
}
}

private Path findCommonJar(Path mavenHome) {
final Path result = mavenHome.resolve("mvn/lib/ext/mvnd-common-" + buildProperties.getVersion() + ".jar");
if (!Files.isRegularFile(result)) {
throw new RuntimeException("File must exist and must be a regular file: " + result);
private String findJars(Path mavenHome, Predicate<Path>... filters) {
final Path libExtDir = mavenHome.resolve("mvn/lib/ext");
try (Stream<Path> jars = Files.list(libExtDir)) {
return jars
.filter(Stream.of(filters).reduce((previous, current) -> previous.or(current)).get())
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));
} catch (IOException e) {
throw new RuntimeException("Could not list " + libExtDir);
}
return result;
}

private DaemonClientConnection connectToDaemonWithId(String daemon) throws DaemonException.ConnectException {
// Look for 'our' daemon among the busy daemons - a daemon will start in busy state so that nobody else will grab it.
// Look for 'our' daemon among the busy daemons - a daemon will start in busy state so that nobody else will
// grab it.
DaemonInfo daemonInfo = registry.get(daemon);
if (daemonInfo != null) {
try {
Expand Down
31 changes: 21 additions & 10 deletions common/src/main/java/org/jboss/fuse/mvnd/common/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Properties;
import java.util.function.Function;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Collects system properties and environment variables used by mvnd client or server.
Expand All @@ -41,6 +43,7 @@ public enum Environment {
DAEMON_IDLE_TIMEOUT("daemon.idleTimeout", null),
DAEMON_UID("daemon.uid", null);

private static final Logger LOG = LoggerFactory.getLogger(Environment.class);
static Properties properties = System.getProperties();
static Map<String, String> env = System.getenv();

Expand All @@ -66,6 +69,10 @@ public Environment.EnvValue environmentVariable() {
return new EnvValue(this, environmentVariableSource());
}

public EnvValue fromValueSource(ValueSource valueSource) {
return new EnvValue(this, valueSource);
}

public String asCommandLineProperty(String value) {
return "-D" + property + "=" + value;
}
Expand Down Expand Up @@ -99,14 +106,6 @@ public static Path findMvndPropertiesPath() {
.toAbsolutePath().normalize();
}

public static Path findMavenHome(Supplier<Properties> mvndProperties, Path mvndPropertiesPath) {
return findBasicMavenHome()
.orLocalProperty(mvndProperties, mvndPropertiesPath)
.orFail()
.asPath()
.toAbsolutePath().normalize();
}

public static EnvValue findBasicMavenHome() {
return MVND_HOME
.environmentVariable()
Expand Down Expand Up @@ -231,7 +230,7 @@ public EnvValue or(ValueSource source) {

public Environment.EnvValue orDefault(Supplier<String> defaultSupplier) {
return new EnvValue(this, envKey,
new ValueSource(sb -> sb.append("default").append(defaultSupplier.get()), defaultSupplier));
new ValueSource(sb -> sb.append("default: ").append(defaultSupplier.get()), defaultSupplier));
}

public Environment.EnvValue orFail() {
Expand Down Expand Up @@ -265,7 +264,18 @@ String get() {
return result;
}
}
return valueSource.valueSupplier.get();
final String result = valueSource.valueSupplier.get();
if (result != null && LOG.isDebugEnabled()) {
StringBuilder sb = new StringBuilder("Loaded environment value for key [")
.append(envKey.name())
.append("] from ");
valueSource.descriptionFunction.apply(sb);
sb.append(": [")
.append(result)
.append(']');
LOG.debug(sb.toString());
}
return result;
}

public String asString() {
Expand All @@ -290,4 +300,5 @@ public int asInt() {
}

}

}
4 changes: 2 additions & 2 deletions common/src/main/java/org/jboss/fuse/mvnd/common/Layout.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public static Layout getEnvInstance() {

ENV_INSTANCE = new Layout(
mvndPropertiesPath,
Environment.findBasicMavenHome()
.orLocalProperty(mvndProperties, mvndPropertiesPath)
Environment.MVND_HOME
.systemProperty()
.orFail()
.asPath()
.toAbsolutePath().normalize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
Thread.currentThread().setContextClassLoader(loader);
Class<?> clazz = loader.loadClass("org.jboss.fuse.mvnd.daemon.Server");
try (AutoCloseable server = (AutoCloseable) clazz.getConstructor(String.class).newInstance(uidStr)) {
System.out.println("server = " + server);
((Runnable) server).run();
}
}
Expand Down

0 comments on commit e9b92ae

Please sign in to comment.