You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2021/06/08 13:02:48 UTC
[karaf] branch main updated: [KARAF-6877] Allow executing aliases
in itests
This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/karaf.git
The following commit(s) were added to refs/heads/main by this push:
new 95b2bc4 [KARAF-6877] Allow executing aliases in itests
new 063b3c6 Merge pull request #1378 from vassilz/KARAF-6877
95b2bc4 is described below
commit 95b2bc47e5d968d5d38efb458dcb682c3ddd7e0d
Author: vassilz <va...@gmail.com>
AuthorDate: Thu May 20 00:01:46 2021 +0300
[KARAF-6877] Allow executing aliases in itests
---
.../org/apache/karaf/itests/KarafTestSupport.java | 74 ++++++++++++++++++++--
.../java/org/apache/karaf/itests/BundleTest.java | 7 ++
.../java/org/apache/karaf/itests/ConfigTest.java | 7 ++
.../java/org/apache/karaf/itests/FeatureTest.java | 8 +++
.../test/java/org/apache/karaf/itests/LogTest.java | 17 +++++
.../java/org/apache/karaf/itests/ServiceTest.java | 7 ++
.../apache/karaf/itests/SystemShutdownTest.java | 5 ++
7 files changed, 120 insertions(+), 5 deletions(-)
diff --git a/itests/common/src/main/java/org/apache/karaf/itests/KarafTestSupport.java b/itests/common/src/main/java/org/apache/karaf/itests/KarafTestSupport.java
index b267e03..8b92041 100644
--- a/itests/common/src/main/java/org/apache/karaf/itests/KarafTestSupport.java
+++ b/itests/common/src/main/java/org/apache/karaf/itests/KarafTestSupport.java
@@ -21,6 +21,9 @@ import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.URI;
import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.security.Principal;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
@@ -104,6 +107,12 @@ public class KarafTestSupport {
static final Long SERVICE_TIMEOUT = 360000L;
static final long BUNDLE_TIMEOUT = 360000L;
+ // Commands provided by boot features will be available to the test at all times
+ // Only additionally installed features may require a proper custom timeout
+ // keep the default command timeout to ensure existing tests don't break, but use this
+ // as a default timeout for aliases
+ static final Long ALIAS_SERVICE_TIMEOUT = 1L;
+
private static Logger LOG = LoggerFactory.getLogger(KarafTestSupport.class);
@Rule
@@ -328,7 +337,7 @@ public class KarafTestSupport {
*
* @param command The command to execute
* @param principals The principals (e.g. RolePrincipal objects) to run the command under
- * @return
+ * @return the result of executing the command
*/
public String executeCommand(final String command, Principal ... principals) {
return executeCommand(command, COMMAND_TIMEOUT, false, principals);
@@ -341,10 +350,48 @@ public class KarafTestSupport {
* @param timeout The amount of time in millis to wait for the command to execute.
* @param silent Specifies if the command should be displayed in the screen.
* @param principals The principals (e.g. RolePrincipal objects) to run the command under
- * @return
+ * @return the result of executing the command
*/
public String executeCommand(final String command, final Long timeout, final Boolean silent, final Principal ... principals) {
- waitForCommandService(command);
+ return executeCommand(command, timeout, SERVICE_TIMEOUT, silent, principals);
+ }
+
+ /**
+ * Executes a shell alias representing a command and returns output as a String.
+ *
+ * @param commandAlias The command alias to execute
+ * @param principals The principals (e.g. RolePrincipal objects) to run the command alias under
+ * @return the result of executing the alias
+ */
+ public String executeAlias(final String commandAlias, Principal... principals) {
+ return executeAlias(commandAlias, COMMAND_TIMEOUT, ALIAS_SERVICE_TIMEOUT, false, principals);
+ }
+
+ /**
+ * Executes a shell alias representing a command and returns output as a String.
+ *
+ * @param commandAlias The command alias to execute.
+ * @param timeout The amount of time in millis to wait for the alias to execute.
+ * @param commandServiceTimeout The amount of time in millis to wait for the command service to be available in the OSGi environment.
+ * @param silent Specifies if the alias should be displayed in the screen.
+ * @param principals The principals (e.g. RolePrincipal objects) to run the command alias under
+ * @return the result of executing the alias
+ */
+ public String executeAlias(final String commandAlias, final Long timeout, final Long commandServiceTimeout, final Boolean silent, final Principal... principals) {
+ return executeCommand(commandAlias, timeout, commandServiceTimeout, silent, principals);
+ }
+
+ /**
+ * Executes a shell command or alias and returns output as a String.
+ *
+ * @param command The command to execute.
+ * @param timeout The amount of time in millis to wait for the command to execute.
+ * @param silent Specifies if the command should be displayed in the screen.
+ * @param principals The principals (e.g. RolePrincipal objects) to run the command under
+ * @return the result of executing the command/alias
+ */
+ private String executeCommand(final String command, final Long timeout, final Long commandServiceTimeout, final Boolean silent, final Principal ... principals) {
+ waitForCommandService(command, commandServiceTimeout);
String response;
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
@@ -357,6 +404,10 @@ public class KarafTestSupport {
if (!silent) {
System.err.println(command);
}
+
+ // load all aliases defined in the init script in the session
+ executeInitScript(session);
+
Object result = session.execute(command);
if (result != null) {
session.getConsole().println(result.toString());
@@ -395,6 +446,19 @@ public class KarafTestSupport {
return response;
}
+ private void executeInitScript(Session session) {
+ try {
+ // possibly all scripts in karaf.shell.init.script property ?
+ final Path initScript = Paths.get(System.getProperty("karaf.etc") + "/shell.init.script");
+ String script = String.join("\n",
+ Files.readAllLines(initScript));
+ session.execute(script);
+ } catch (Exception e) {
+ LOG.debug("Error in initialization script", e);
+ System.err.println("Error in initialization script: " + e.getMessage());
+ }
+ }
+
public void assertServiceAvailable(String type) {
Assert.assertNotNull(getOsgiService(type));
}
@@ -507,7 +571,7 @@ public class KarafTestSupport {
}
}
- private void waitForCommandService(String command) {
+ private void waitForCommandService(String command, Long timeout) {
// the commands are represented by services. Due to the asynchronous nature of services they may not be
// immediately available. This code waits the services to be available, in their secured form. It
// means that the code waits for the command service to appear with the roles defined.
@@ -526,7 +590,7 @@ public class KarafTestSupport {
try {
long start = System.currentTimeMillis();
long cur = start;
- while (cur - start < SERVICE_TIMEOUT) {
+ while (cur - start < timeout) {
if (sessionFactory.getRegistry().getCommand(scope, name) != null) {
return;
}
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/BundleTest.java b/itests/test/src/test/java/org/apache/karaf/itests/BundleTest.java
index 8168988..529efc6 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/BundleTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/BundleTest.java
@@ -51,6 +51,13 @@ public class BundleTest extends BaseTest {
}
@Test
+ public void laAlias() throws Exception {
+ String laOutput = executeAlias("la", ADMIN_ROLES);
+ System.out.println(laOutput);
+ assertFalse(laOutput.isEmpty());
+ }
+
+ @Test
public void listViaMBean() throws Exception {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("org.apache.karaf:type=bundle,name=root");
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/ConfigTest.java b/itests/test/src/test/java/org/apache/karaf/itests/ConfigTest.java
index 1a6a455..20a6d0f 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/ConfigTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/ConfigTest.java
@@ -46,6 +46,13 @@ public class ConfigTest extends BaseTest {
}
@Test
+ public void clAlias() throws Exception {
+ String configListOutput = executeAlias("cl org.apache.karaf.features");
+ System.out.println(configListOutput);
+ assertFalse(configListOutput.isEmpty());
+ }
+
+ @Test
public void listShortCommand() throws Exception {
String configListOutput = executeCommand("config:list -s");
System.out.println(configListOutput);
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java b/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
index e2b80bb..977433c 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
@@ -90,6 +90,14 @@ public class FeatureTest extends BaseTest {
}
@Test
+ public void upgradeUninstallCommand() throws Exception {
+ System.out.println(executeAlias("feature:upgrade -v -r wrapper", new RolePrincipal("admin")));
+ assertFeatureInstalled("wrapper");
+ System.out.println(executeCommand("feature:uninstall -r wrapper", new RolePrincipal("admin")));
+ assertFeatureNotInstalled("wrapper");
+ }
+
+ @Test
public void installUninstallViaMBean() throws Exception {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("org.apache.karaf:type=feature,name=root");
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/LogTest.java b/itests/test/src/test/java/org/apache/karaf/itests/LogTest.java
index 0c73b65..877bf47 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/LogTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/LogTest.java
@@ -44,6 +44,13 @@ public class LogTest extends BaseTest {
}
@Test
+ public void setDebugAndDisplayAlias() throws Exception {
+ assertSetLevel("DEBUG");
+ LOGGER.debug("Making sure there is DEBUG level output");
+ assertContains("DEBUG", executeAlias("ld -n 200"));
+ }
+
+ @Test
public void setDebugViaMBean() throws Exception {
assertSetLevel("INFO");
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
@@ -62,10 +69,20 @@ public class LogTest extends BaseTest {
String displayOutput = executeCommand("log:display").trim();
assertTrue("Should be empty but was: " + displayOutput, displayOutput.trim().isEmpty());
}
+
+ @Test
+ public void setGetDebugAndClearAlias() throws Exception {
+ assertSetLevel("DEBUG");
+ assertSetLevel("INFO");
+ System.out.println(executeCommand("log:clear"));
+ String displayOutput = executeAlias("ld").trim();
+ assertTrue("Should be empty but was: " + displayOutput, displayOutput.trim().isEmpty());
+ }
public void assertSetLevel(String level) throws InterruptedException {
System.out.println(executeCommand("log:set " + level));
assertContains(level, executeCommand("log:get"));
+ assertContains(level, executeAlias("log:list"));
Thread.sleep(100);
}
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/ServiceTest.java b/itests/test/src/test/java/org/apache/karaf/itests/ServiceTest.java
index bbc9b90..9bc63fa 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/ServiceTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/ServiceTest.java
@@ -40,6 +40,13 @@ public class ServiceTest extends BaseTest {
}
@Test
+ public void lsAlias() throws Exception {
+ String listOutput = executeAlias("ls");
+ System.out.println(listOutput);
+ assertFalse(listOutput.isEmpty());
+ }
+
+ @Test
public void listViaMBean() throws Exception {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("org.apache.karaf:type=service,name=root");
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/SystemShutdownTest.java b/itests/test/src/test/java/org/apache/karaf/itests/SystemShutdownTest.java
index f857056..b34abac 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/SystemShutdownTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/SystemShutdownTest.java
@@ -35,6 +35,11 @@ public class SystemShutdownTest extends BaseTest {
}
@Test
+ public void haltAlias() throws Exception {
+ System.out.println(executeAlias("halt", new RolePrincipal("admin")));
+ }
+
+ @Test
public void shutdownViaMBean() throws Exception {
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("org.apache.karaf:type=system,name=root");