From b34f80aea1266b5f8235cf4bc528aec5248a1fd1 Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Tue, 8 Sep 2015 19:39:24 -0400 Subject: [PATCH 1/1] Add tests for the "lttng list" command Should cover https://bugs.lttng.org/issues/933 Signed-off-by: Alexandre Montplaisir --- .../.settings/org.eclipse.jdt.core.prefs | 4 +- .../java/org/lttng/tools/ILttngSession.java | 9 ++ .../lttng/tools/LttngCommandLineSession.java | 11 ++ .../.settings/org.eclipse.jdt.core.prefs | 4 +- .../integration/events/ListEventsITBase.java | 140 ++++++++++++++++++ .../events/jul/JulListEventsIT.java | 110 ++++++++++++++ .../events/log4j/Log4jListEventsIT.java | 110 ++++++++++++++ 7 files changed, 384 insertions(+), 4 deletions(-) create mode 100644 lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/ListEventsITBase.java create mode 100644 lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/jul/JulListEventsIT.java create mode 100644 lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/log4j/Log4jListEventsIT.java diff --git a/lttng-tools-java/.settings/org.eclipse.jdt.core.prefs b/lttng-tools-java/.settings/org.eclipse.jdt.core.prefs index 3ef0c30..f1acd28 100644 --- a/lttng-tools-java/.settings/org.eclipse.jdt.core.prefs +++ b/lttng-tools-java/.settings/org.eclipse.jdt.core.prefs @@ -107,7 +107,7 @@ org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.source=1.8 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 -org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16 @@ -371,7 +371,7 @@ org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constan org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert org.eclipse.jdt.core.formatter.join_lines_in_comments=true -org.eclipse.jdt.core.formatter.join_wrapped_lines=true +org.eclipse.jdt.core.formatter.join_wrapped_lines=false org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false diff --git a/lttng-tools-java/src/main/java/org/lttng/tools/ILttngSession.java b/lttng-tools-java/src/main/java/org/lttng/tools/ILttngSession.java index b6d0aed..a28a4aa 100644 --- a/lttng-tools-java/src/main/java/org/lttng/tools/ILttngSession.java +++ b/lttng-tools-java/src/main/java/org/lttng/tools/ILttngSession.java @@ -19,6 +19,7 @@ package org.lttng.tools; import java.util.List; +import java.util.Set; /** * Java representation of a LTTng tracing session. @@ -149,6 +150,14 @@ public interface ILttngSession extends AutoCloseable { */ boolean disableAllEvents(); + /** + * Get a list of events currently available (exposed by applications) in the + * session's domain. + * + * @return The list of available events + */ + Set listEvents(); + /** * Start tracing * diff --git a/lttng-tools-java/src/main/java/org/lttng/tools/LttngCommandLineSession.java b/lttng-tools-java/src/main/java/org/lttng/tools/LttngCommandLineSession.java index e2b1ff6..171cffb 100644 --- a/lttng-tools-java/src/main/java/org/lttng/tools/LttngCommandLineSession.java +++ b/lttng-tools-java/src/main/java/org/lttng/tools/LttngCommandLineSession.java @@ -23,6 +23,7 @@ import static org.lttng.tools.utils.ShellUtils.executeCommand; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; @@ -136,6 +137,16 @@ class LttngCommandLineSession implements ILttngSession { "lttng", "disable-event", domain.flag(), "-a", "-s", sessionName)); } + @Override + public Set listEvents() { + List output = ShellUtils.getOutputFromCommand(true, Arrays.asList("lttng", "list", domain.flag())); + return output.stream() + .map(e -> e.trim()) + .filter(e -> e.startsWith("- ")) + .map(e -> e.substring(2)) + .collect(Collectors.toSet()); + } + @Override public boolean start() { /* diff --git a/lttng-ust-java-tests/.settings/org.eclipse.jdt.core.prefs b/lttng-ust-java-tests/.settings/org.eclipse.jdt.core.prefs index 3ef0c30..f1acd28 100644 --- a/lttng-ust-java-tests/.settings/org.eclipse.jdt.core.prefs +++ b/lttng-ust-java-tests/.settings/org.eclipse.jdt.core.prefs @@ -107,7 +107,7 @@ org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.source=1.8 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 -org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0 +org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16 @@ -371,7 +371,7 @@ org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constan org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert org.eclipse.jdt.core.formatter.join_lines_in_comments=true -org.eclipse.jdt.core.formatter.join_wrapped_lines=true +org.eclipse.jdt.core.formatter.join_wrapped_lines=false org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/ListEventsITBase.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/ListEventsITBase.java new file mode 100644 index 0000000..ec4e0c1 --- /dev/null +++ b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/ListEventsITBase.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.lttng.ust.agent.integration.events; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.lttng.tools.ILttngSession; + +/** + * Base class for the list events command tests + */ +public abstract class ListEventsITBase { + + protected static final String LOGGER_NAME_1 = "org.lttng"; + protected static final String LOGGER_NAME_2 = "org.lttng.mycomponent"; + protected static final String LOGGER_NAME_3 = "org.lttng.myothercomponent"; + + private ILttngSession session; + + /** + * Common test setup + */ + @Before + public void testSetup() { + session = ILttngSession.createSession(null, getDomain()); + } + + /** + * Common test teardown + */ + @After + public void testTeardown() { + session.close(); + } + + protected abstract ILttngSession.Domain getDomain(); + + protected abstract void attachHandlerToLogger(int handlerIndex, int loggerIndex); + + protected abstract void detachHandlerFromLogger(int handlerIndex, int loggerIndex); + + /** + * Test with many loggers existing, but none of them having a LTTng handler + * attached. + */ + @Test + public void testManyLoggersNoneAttached() { + /* Don't attach anything */ + Set actualEvents = session.listEvents(); + assertTrue(actualEvents.isEmpty()); + } + + /** + * Test with many loggers existing, but only a subset of them has a LTTng + * handler attached. + */ + @Test + public void testManyLoggersSomeAttached() { + attachHandlerToLogger(1, 1); + + Set expectedEvents = new HashSet<>(Arrays.asList(LOGGER_NAME_1)); + Set actualEvents = session.listEvents(); + + assertEquals(expectedEvents, actualEvents); + } + + /** + * Test with many loggers existing, and all of them having a LTTng handler + * attached. + */ + @Test + public void testManyLoggersAllAttached() { + attachHandlerToLogger(1, 1); + attachHandlerToLogger(2, 2); + attachHandlerToLogger(2, 3); + + Set expectedEvents = new HashSet<>(Arrays.asList(LOGGER_NAME_1, LOGGER_NAME_2, LOGGER_NAME_3)); + Set actualEvents = session.listEvents(); + + assertEquals(expectedEvents, actualEvents); + } + + /** + * Test with some loggers having had handlers attached but then detached. + */ + @Test + public void testLoggersSomeDetached() { + attachHandlerToLogger(1, 1); + attachHandlerToLogger(2, 2); + + attachHandlerToLogger(2, 3); + detachHandlerFromLogger(2, 3); + /* Only loggers 1 and 2 will be attached */ + + Set expectedEvents = new HashSet<>(Arrays.asList(LOGGER_NAME_1, LOGGER_NAME_2)); + Set actualEvents = session.listEvents(); + + assertEquals(expectedEvents, actualEvents); + } + + /** + * Test with all loggers having had handlers attached and then detached. + */ + @Test + public void testLoggersAllDetached() { + attachHandlerToLogger(1, 1); + attachHandlerToLogger(2, 2); + attachHandlerToLogger(2, 3); + detachHandlerFromLogger(1, 1); + detachHandlerFromLogger(2, 2); + detachHandlerFromLogger(2, 3); + + Set actualEvents = session.listEvents(); + assertTrue(actualEvents.isEmpty()); + } +} diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/jul/JulListEventsIT.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/jul/JulListEventsIT.java new file mode 100644 index 0000000..2025a97 --- /dev/null +++ b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/jul/JulListEventsIT.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.lttng.ust.agent.integration.events.jul; + +import static org.junit.Assume.assumeTrue; + +import java.io.IOException; +import java.util.logging.Handler; +import java.util.logging.Logger; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.lttng.tools.ILttngSession.Domain; +import org.lttng.tools.ILttngSession; +import org.lttng.tools.LttngToolsHelper; +import org.lttng.ust.agent.integration.events.ListEventsITBase; +import org.lttng.ust.agent.jul.LttngLogHandler; +import org.lttng.ust.agent.utils.LttngUtils; + +/** + * Test suite for the list events command for the JUL domain + */ +public class JulListEventsIT extends ListEventsITBase { + + private Logger[] loggers; + private Handler[] handlers; + + /** + * Test class setup + */ + @BeforeClass + public static void testClassSetup() { + /* Skip tests if we can't find the JNI library or lttng-tools */ + assumeTrue(LttngUtils.checkForJulLibrary()); + assumeTrue(LttngUtils.checkForLttngTools(Domain.JUL)); + + LttngToolsHelper.destroyAllSessions(); + } + + /** + * Test setup + * + * @throws SecurityException + * @throws IOException + */ + @Before + public void julSetup() throws SecurityException, IOException { + loggers = new Logger[] { + Logger.getLogger(LOGGER_NAME_1), + Logger.getLogger(LOGGER_NAME_2), + Logger.getLogger(LOGGER_NAME_3) + }; + + handlers = new Handler[] { + new LttngLogHandler(), + new LttngLogHandler() + }; + } + + /** + * Test teardown. Detach and close all log handlers. + */ + @After + public void julTeardown() { + for (Logger logger : loggers) { + for (Handler handler : handlers) { + logger.removeHandler(handler); + } + } + + for (Handler handler : handlers) { + handler.close(); + } + handlers = null; + loggers = null; + } + + @Override + protected ILttngSession.Domain getDomain() { + return ILttngSession.Domain.JUL; + } + + @Override + protected void attachHandlerToLogger(int handlerIndex, int loggerIndex) { + loggers[loggerIndex - 1].addHandler(handlers[handlerIndex - 1]); + } + + @Override + protected void detachHandlerFromLogger(int handlerIndex, int loggerIndex) { + loggers[loggerIndex - 1].removeHandler(handlers[handlerIndex - 1]); + } + +} diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/log4j/Log4jListEventsIT.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/log4j/Log4jListEventsIT.java new file mode 100644 index 0000000..2dc566e --- /dev/null +++ b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/events/log4j/Log4jListEventsIT.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +package org.lttng.ust.agent.integration.events.log4j; + +import static org.junit.Assume.assumeTrue; + +import java.io.IOException; + +import org.apache.log4j.Appender; +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.lttng.tools.ILttngSession.Domain; +import org.lttng.tools.ILttngSession; +import org.lttng.tools.LttngToolsHelper; +import org.lttng.ust.agent.integration.events.ListEventsITBase; +import org.lttng.ust.agent.log4j.LttngLogAppender; +import org.lttng.ust.agent.utils.LttngUtils; + +/** + * Test suite for the list events command for the log4j domain + */ +public class Log4jListEventsIT extends ListEventsITBase { + + private Logger[] loggers; + private Appender[] appenders; + + /** + * Test class setup + */ + @BeforeClass + public static void testClassSetup() { + /* Skip tests if we can't find the JNI library or lttng-tools */ + assumeTrue(LttngUtils.checkForLog4jLibrary()); + assumeTrue(LttngUtils.checkForLttngTools(Domain.LOG4J)); + + LttngToolsHelper.destroyAllSessions(); + } + + /** + * Test setup + * + * @throws SecurityException + * @throws IOException + */ + @Before + public void log4jSetup() throws SecurityException, IOException { + loggers = new Logger[] { + Logger.getLogger(LOGGER_NAME_1), + Logger.getLogger(LOGGER_NAME_2), + Logger.getLogger(LOGGER_NAME_3) + }; + + appenders = new Appender[] { + new LttngLogAppender(), + new LttngLogAppender() + }; + } + + /** + * Test teardown. Detach and close all log handlers. + */ + @After + public void log4jTeardown() { + for (Logger logger : loggers) { + for (Appender appender : appenders) { + logger.removeAppender(appender); + } + } + + for (Appender appender : appenders) { + appender.close(); + } + appenders = null; + loggers = null; + } + + @Override + protected ILttngSession.Domain getDomain() { + return ILttngSession.Domain.LOG4J; + } + + @Override + protected void attachHandlerToLogger(int handlerIndex, int loggerIndex) { + loggers[loggerIndex - 1].addAppender(appenders[handlerIndex - 1]); + } + + @Override + protected void detachHandlerFromLogger(int handlerIndex, int loggerIndex) { + loggers[loggerIndex - 1].removeAppender(appenders[handlerIndex - 1]); + } + +} -- 2.34.1