Repository: karaf Updated Branches: refs/heads/karaf-3.0.x f4f8d6476 -> 10ceb12d9
KARAF-1897 - Use dynamic allocated network ports in itest and fix couple of itests Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/10ceb12d Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/10ceb12d Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/10ceb12d Branch: refs/heads/karaf-3.0.x Commit: 10ceb12d91fd43ed0de9cc188c26c1721c32d012 Parents: f4f8d64 Author: Jean-Baptiste Onofré <[email protected]> Authored: Wed Jan 27 08:50:38 2016 +0100 Committer: Jean-Baptiste Onofré <[email protected]> Committed: Wed Jan 27 08:50:38 2016 +0100 ---------------------------------------------------------------------- .../karaf/itests/ConditionalFeaturesTest.java | 2 +- .../apache/karaf/itests/KarafTestSupport.java | 60 +++++++++++++++++--- .../apache/karaf/itests/SshCommandTestBase.java | 2 +- 3 files changed, 55 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/10ceb12d/itests/src/test/java/org/apache/karaf/itests/ConditionalFeaturesTest.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/karaf/itests/ConditionalFeaturesTest.java b/itests/src/test/java/org/apache/karaf/itests/ConditionalFeaturesTest.java index 46908d1..a641362 100644 --- a/itests/src/test/java/org/apache/karaf/itests/ConditionalFeaturesTest.java +++ b/itests/src/test/java/org/apache/karaf/itests/ConditionalFeaturesTest.java @@ -50,7 +50,7 @@ public class ConditionalFeaturesTest extends KarafTestSupport { MavenArtifactUrlReference karafUrl = maven().groupId("org.apache.karaf").artifactId("apache-karaf").type("zip").versionAsInProject(); return new Option[]{ karafDistributionConfiguration().frameworkUrl(karafUrl), - KarafDistributionOption.editConfigurationFilePut("etc/org.ops4j.pax.web.cfg", "org.osgi.service.http.port", KarafTestSupport.HTTP_PORT) + KarafDistributionOption.editConfigurationFilePut("etc/org.ops4j.pax.web.cfg", "org.osgi.service.http.port", KarafTestSupport.MIN_HTTP_PORT) }; } */ http://git-wip-us.apache.org/repos/asf/karaf/blob/10ceb12d/itests/src/test/java/org/apache/karaf/itests/KarafTestSupport.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/karaf/itests/KarafTestSupport.java b/itests/src/test/java/org/apache/karaf/itests/KarafTestSupport.java index 2aba68a..a4f1b6d 100644 --- a/itests/src/test/java/org/apache/karaf/itests/KarafTestSupport.java +++ b/itests/src/test/java/org/apache/karaf/itests/KarafTestSupport.java @@ -22,6 +22,7 @@ import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.PrintStream; +import java.net.ServerSocket; import java.net.URL; import java.security.Principal; import java.security.PrivilegedExceptionAction; @@ -60,15 +61,21 @@ import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.util.tracker.ServiceTracker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class KarafTestSupport { - public static final String RMI_SERVER_PORT = "44445"; - public static final String HTTP_PORT = "9081"; - public static final String RMI_REG_PORT = "1100"; + public static final String MIN_RMI_SERVER_PORT = "44444"; + public static final String MAX_RMI_SERVER_PORT = "66666"; + public static final String MIN_HTTP_PORT = "9080"; + public static final String MAX_HTTP_PORT = "9999"; + public static final String MIN_RMI_REG_PORT = "1099"; + public static final String MAX_RMI_REG_PORT = "9999"; + public static final String MIN_SSH_PORT = "8101"; + public static final String MAX_SSH_PORT = "8888"; static final Long COMMAND_TIMEOUT = 30000L; static final Long SERVICE_TIMEOUT = 30000L; @@ -86,6 +93,9 @@ public class KarafTestSupport { @Inject protected FeaturesService featureService; + @Inject + protected ConfigurationAdmin configurationAdmin; + /** * To make sure the tests run only when the boot features are fully installed */ @@ -109,6 +119,12 @@ public class KarafTestSupport { @Configuration public Option[] config() { MavenArtifactUrlReference karafUrl = maven().groupId("org.apache.karaf").artifactId("apache-karaf").versionAsInProject().type("tar.gz"); + + String httpPort = Integer.toString(getAvailablePort(Integer.parseInt(MIN_HTTP_PORT), Integer.parseInt(MAX_HTTP_PORT))); + String rmiRegistryPort = Integer.toString(getAvailablePort(Integer.parseInt(MIN_RMI_REG_PORT), Integer.parseInt(MAX_RMI_REG_PORT))); + String rmiServerPort = Integer.toString(getAvailablePort(Integer.parseInt(MIN_RMI_SERVER_PORT), Integer.parseInt(MIN_RMI_SERVER_PORT))); + String sshPort = Integer.toString(getAvailablePort(Integer.parseInt(MIN_SSH_PORT), Integer.parseInt(MAX_SSH_PORT))); + return new Option[]{ // KarafDistributionOption.debugConfiguration("8889", true), karafDistributionConfiguration().frameworkUrl(karafUrl).name("Apache Karaf").unpackDirectory(new File("target/exam")), @@ -118,9 +134,10 @@ public class KarafTestSupport { logLevel(LogLevel.INFO), replaceConfigurationFile("etc/org.ops4j.pax.logging.cfg", getConfigFile("/etc/org.ops4j.pax.logging.cfg")), editConfigurationFilePut("etc/org.apache.karaf.features.cfg", "featuresBoot", "config,standard,region,package,kar,management"), - editConfigurationFilePut("etc/org.ops4j.pax.web.cfg", "org.osgi.service.http.port", HTTP_PORT), - editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiRegistryPort", RMI_REG_PORT), - editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiServerPort", RMI_SERVER_PORT), + editConfigurationFilePut("etc/org.ops4j.pax.web.cfg", "org.osgi.service.http.port", httpPort), + editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiRegistryPort", rmiRegistryPort), + editConfigurationFilePut("etc/org.apache.karaf.management.cfg", "rmiServerPort", rmiServerPort), + editConfigurationFilePut("etc/org.apache.karaf.shell.cfg", "sshPort", sshPort), editConfigurationFilePut("etc/system.properties", "spring31.version", System.getProperty("spring31.version")), editConfigurationFilePut("etc/system.properties", "spring32.version", System.getProperty("spring32.version")), editConfigurationFilePut("etc/system.properties", "spring40.version", System.getProperty("spring40.version")), @@ -129,6 +146,19 @@ public class KarafTestSupport { }; } + private int getAvailablePort(int min, int max) { + for (int i = min; i <= max; i++) { + try { + ServerSocket serverSocket = new ServerSocket(i); + return serverSocket.getLocalPort(); + } catch (Exception e) { + System.err.println("Port " + i + " not available, trying next one"); + continue; // try next port + } + } + throw new IllegalStateException("Can't find available network ports"); + } + /** * Executes a shell command and returns output as a String. * Commands have a default timeout of 10 seconds. @@ -330,7 +360,7 @@ public class KarafTestSupport { } public JMXConnector getJMXConnector(String userName, String passWord) throws Exception { - JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + RMI_REG_PORT+ "/karaf-root"); + JMXServiceURL url = new JMXServiceURL(getJmxServiceUrl()); Hashtable<String, Object> env = new Hashtable<String, Object>(); String[] credentials = new String[]{ userName, passWord }; env.put("jmx.remote.credentials", credentials); @@ -338,6 +368,22 @@ public class KarafTestSupport { return connector; } + public String getJmxServiceUrl() throws Exception { + org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration("org.apache.karaf.management", null); + if (configuration != null) { + return configuration.getProperties().get("serviceUrl").toString(); + } + return "service:jmx:rmi:///jndi/rmi://localhost:" + MIN_RMI_SERVER_PORT + "/karaf-root"; + } + + public String getSshPort() throws Exception { + org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration("org.apache.karaf.shell", null); + if (configuration != null) { + return configuration.getProperties().get("sshPort").toString(); + } + return "8101"; + } + public void assertFeatureInstalled(String featureName) { Feature[] features = featureService.listInstalledFeatures(); for (Feature feature : features) { http://git-wip-us.apache.org/repos/asf/karaf/blob/10ceb12d/itests/src/test/java/org/apache/karaf/itests/SshCommandTestBase.java ---------------------------------------------------------------------- diff --git a/itests/src/test/java/org/apache/karaf/itests/SshCommandTestBase.java b/itests/src/test/java/org/apache/karaf/itests/SshCommandTestBase.java index eaf5cda..f9dc7f6 100644 --- a/itests/src/test/java/org/apache/karaf/itests/SshCommandTestBase.java +++ b/itests/src/test/java/org/apache/karaf/itests/SshCommandTestBase.java @@ -116,7 +116,7 @@ public class SshCommandTestBase extends KarafTestSupport { private OutputStream openSshChannel(String username, String password, OutputStream ... outputs) throws Exception { client = SshClient.setUpDefaultClient(); client.start(); - ConnectFuture future = client.connect(username, "localhost", 8101).await(); + ConnectFuture future = client.connect(username, "localhost", Integer.parseInt(getSshPort())).await(); session = future.getSession(); int ret = ClientSession.WAIT_AUTH;
