From 1df8e5d7716e0f095b434e2190f4054edb837f42 Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Mon, 14 Sep 2015 16:16:48 -0400 Subject: [PATCH] Handle filter strings being passed by the sessiond Signed-off-by: Alexandre Montplaisir --- .../agent/integration/client/TcpClientIT.java | 92 ++++++++---- .../filter/FilterListenerITBase.java | 62 +++----- .../ust/agent/utils/EventRuleFactory.java | 135 ++++++++++++++++++ 3 files changed, 225 insertions(+), 64 deletions(-) create mode 100644 lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/utils/EventRuleFactory.java diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/client/TcpClientIT.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/client/TcpClientIT.java index ac9c99e..2794dfa 100644 --- a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/client/TcpClientIT.java +++ b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/client/TcpClientIT.java @@ -31,8 +31,8 @@ import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; +import org.junit.runner.RunWith; import org.lttng.tools.ILttngSession; import org.lttng.tools.LttngToolsHelper; import org.lttng.ust.agent.ILttngAgent; @@ -40,7 +40,9 @@ import org.lttng.ust.agent.client.LttngTcpSessiondClient; import org.lttng.ust.agent.session.EventRule; import org.lttng.ust.agent.session.LogLevelSelector; import org.lttng.ust.agent.session.LogLevelSelector.LogLevelType; +import org.lttng.ust.agent.utils.EventRuleFactory; import org.lttng.ust.agent.utils.ILogLevelStrings; +import org.lttng.ust.agent.utils.TestPrintRunner; /** * Tests for the TCP client only, without using an agent. @@ -51,18 +53,16 @@ import org.lttng.ust.agent.utils.ILogLevelStrings; * * @author Alexandre Montplaisir */ +@RunWith(TestPrintRunner.class) public class TcpClientIT { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private static final LogLevelSelector LOG_LEVEL_UNSPECIFIED = new LogLevelSelector(Integer.MIN_VALUE, 0); - private static final String EVENT_NAME_A = "eventA"; private static final String EVENT_NAME_B = "eventB"; private static final String EVENT_NAME_C = "eventC"; - private static final String EVENT_NAME_ALL = "*"; /* Test configuration */ private static final int DOMAIN_VALUE = ILttngAgent.Domain.JUL.value(); @@ -158,7 +158,7 @@ public class TcpClientIT { session.enableEvent(EVENT_NAME_A, null, false, null); List expectedCommands = Collections.singletonList( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null)); + EventRuleFactory.createRule(EVENT_NAME_A)); List actualCommands = clientListener.getEnabledEventCommands(); assertEquals(expectedCommands, actualCommands); @@ -172,7 +172,7 @@ public class TcpClientIT { session.enableAllEvents(); List expectedCommands = Collections.singletonList( - new EventRule(EVENT_NAME_ALL, LOG_LEVEL_UNSPECIFIED, null)); + EventRuleFactory.createRuleAllEvents()); List actualCommands = clientListener.getEnabledEventCommands(); assertEquals(expectedCommands, actualCommands); @@ -187,7 +187,7 @@ public class TcpClientIT { session.disableEvents(EVENT_NAME_A); List expectedEnableCommands = Collections.singletonList( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null)); + EventRuleFactory.createRule(EVENT_NAME_A)); List expectedDisableCommands = Collections.singletonList(EVENT_NAME_A); assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands()); @@ -205,9 +205,9 @@ public class TcpClientIT { session.disableAllEvents(); List expectedEnableCommands = Arrays.asList( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_C, LOG_LEVEL_UNSPECIFIED, null)); + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_B), + EventRuleFactory.createRule(EVENT_NAME_C)); /* * A "disable-event -a" will send one command for each enabled event. * The order may be different though. @@ -227,10 +227,8 @@ public class TcpClientIT { session.enableAllEvents(); session.disableAllEvents(); - List expectedEnableCommands = Arrays.asList( - new EventRule(EVENT_NAME_ALL, LOG_LEVEL_UNSPECIFIED, null)); - List expectedDisableCommands = Arrays.asList( - EVENT_NAME_ALL); + List expectedEnableCommands = Arrays.asList(EventRuleFactory.createRuleAllEvents()); + List expectedDisableCommands = Arrays.asList(EventRuleFactory.EVENT_NAME_ALL); assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands()); assertTrue(containSameElements(expectedDisableCommands, clientListener.getDisabledEventCommands())); @@ -246,7 +244,7 @@ public class TcpClientIT { session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); List expectedCommands = Collections.singletonList( - new EventRule(EVENT_NAME_A, lls, null)); + EventRuleFactory.createRule(EVENT_NAME_A, lls)); List actualCommands = clientListener.getEnabledEventCommands(); assertEquals(expectedCommands, actualCommands); @@ -262,7 +260,7 @@ public class TcpClientIT { session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); List expectedCommands = Collections.singletonList( - new EventRule(EVENT_NAME_A, lls, null)); + EventRuleFactory.createRule(EVENT_NAME_A, lls)); List actualCommands = clientListener.getEnabledEventCommands(); assertEquals(expectedCommands, actualCommands); @@ -271,7 +269,6 @@ public class TcpClientIT { /** * Test enabling an event twice, for the same loglevel, with --loglevel followed by --loglevel-only. */ - @Ignore("See http://bugs.lttng.org/issues/913") @Test public void testEnableEventsLogLevelRangeAndSingle() { LogLevelSelector lls1 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); @@ -281,8 +278,8 @@ public class TcpClientIT { session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); List expectedCommands = Arrays.asList( - new EventRule(EVENT_NAME_A, lls1, null), - new EventRule(EVENT_NAME_A, lls2, null) + EventRuleFactory.createRule(EVENT_NAME_A, lls1), + EventRuleFactory.createRule(EVENT_NAME_A, lls2) ); List actualCommands = clientListener.getEnabledEventCommands(); @@ -290,9 +287,8 @@ public class TcpClientIT { } /** - * Test enabling an event twice, for the same loglevel, with --loglevel--only followed by --loglevel. + * Test enabling an event twice, for the same loglevel, with --loglevel-only followed by --loglevel. */ - @Ignore("See http://bugs.lttng.org/issues/913") @Test public void testEnableEventsLogLevelSingleAndRange() { LogLevelSelector lls1 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); @@ -302,8 +298,8 @@ public class TcpClientIT { session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); List expectedCommands = Arrays.asList( - new EventRule(EVENT_NAME_A, lls1, null), - new EventRule(EVENT_NAME_A, lls2, null) + EventRuleFactory.createRule(EVENT_NAME_A, lls1), + EventRuleFactory.createRule(EVENT_NAME_A, lls2) ); List actualCommands = clientListener.getEnabledEventCommands(); @@ -324,11 +320,57 @@ public class TcpClientIT { session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); session2.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); - List expectedCommands = Arrays.asList(new EventRule(EVENT_NAME_A, lls1, null), - new EventRule(EVENT_NAME_A, lls2, null)); + List expectedCommands = Arrays.asList( + EventRuleFactory.createRule(EVENT_NAME_A, lls1), + EventRuleFactory.createRule(EVENT_NAME_A, lls2)); List actualCommands = clientListener.getEnabledEventCommands(); assertEquals(expectedCommands, actualCommands); } } + + /** + * Enable the same event multiple times with different filter strings. + */ + @Test + public void testEnableEventsDiffFilters() { + final String filter1 = "filter1"; + final String filter2 = "filter2"; + + session.enableEvent(EVENT_NAME_A, null, false, null); + session.enableEvent(EVENT_NAME_A, null, false, filter1); + session.enableEvent(EVENT_NAME_A, null, false, filter2); + + List expectedCommands = Arrays.asList( + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_A, EventRuleFactory.LOG_LEVEL_UNSPECIFIED, filter1), + EventRuleFactory.createRule(EVENT_NAME_A, EventRuleFactory.LOG_LEVEL_UNSPECIFIED, filter2)); + List actualCommands = clientListener.getEnabledEventCommands(); + + assertEquals(expectedCommands, actualCommands); + } + + /** + * Enable the same event multiple times with different log levels and + * filters. + */ + @Test + public void testEnableEventsLogLevelAndFilters() { + final LogLevelSelector lls = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); + final String filter = "filter1"; + + session.enableEvent(EVENT_NAME_A, null, false, null); + session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); + session.enableEvent(EVENT_NAME_A, null, false, filter); + session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, filter); + + List expectedCommands = Arrays.asList( + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_A, lls), + EventRuleFactory.createRule(EVENT_NAME_A, EventRuleFactory.LOG_LEVEL_UNSPECIFIED, filter), + EventRuleFactory.createRule(EVENT_NAME_A, lls, filter)); + List actualCommands = clientListener.getEnabledEventCommands(); + + assertEquals(expectedCommands, actualCommands); + } } diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/filter/FilterListenerITBase.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/filter/FilterListenerITBase.java index 409ec56..020ad36 100644 --- a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/filter/FilterListenerITBase.java +++ b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/integration/filter/FilterListenerITBase.java @@ -29,7 +29,6 @@ import java.util.stream.Stream; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.lttng.tools.ILttngSession; @@ -39,6 +38,7 @@ import org.lttng.ust.agent.filter.IFilterChangeListener; import org.lttng.ust.agent.session.EventRule; import org.lttng.ust.agent.session.LogLevelSelector; import org.lttng.ust.agent.session.LogLevelSelector.LogLevelType; +import org.lttng.ust.agent.utils.EventRuleFactory; import org.lttng.ust.agent.utils.ILogLevelStrings; import org.lttng.ust.agent.utils.TestPrintRunner; @@ -50,8 +50,6 @@ import org.lttng.ust.agent.utils.TestPrintRunner; @RunWith(TestPrintRunner.class) public abstract class FilterListenerITBase { - protected static final LogLevelSelector LOG_LEVEL_UNSPECIFIED = new LogLevelSelector(Integer.MIN_VALUE, 0); - private static final String EVENT_NAME_A = "eventA"; private static final String EVENT_NAME_B = "eventB"; private static final String EVENT_NAME_C = "eventC"; @@ -103,7 +101,7 @@ public abstract class FilterListenerITBase { @Test public void testOneRule() { Set rules = Collections.singleton( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null)); + EventRuleFactory.createRule(EVENT_NAME_A)); session.enableEvent(EVENT_NAME_A, null, false, null); @@ -116,10 +114,10 @@ public abstract class FilterListenerITBase { */ @Test public void testManyRules() { - Set rules = Stream - .of(new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_C, LOG_LEVEL_UNSPECIFIED, null)) + Set rules = Stream.of( + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_B), + EventRuleFactory.createRule(EVENT_NAME_C)) .collect(Collectors.toSet()); session.enableEvent(EVENT_NAME_A, null, false, null); @@ -136,7 +134,7 @@ public abstract class FilterListenerITBase { @Test public void testManyRulesDisableSome() { Set rules = Collections.singleton( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null)); + EventRuleFactory.createRule(EVENT_NAME_A)); session.enableEvent(EVENT_NAME_A, null, false, null); session.enableEvent(EVENT_NAME_B, null, false, null); @@ -171,7 +169,6 @@ public abstract class FilterListenerITBase { /** * Test enabling the same event name with various values of loglevels. */ - @Ignore("Does not work as expected atm, see http://bugs.lttng.org/issues/913") @Test public void testSameEventsDiffLogLevels() { LogLevelSelector lls1 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); @@ -179,9 +176,9 @@ public abstract class FilterListenerITBase { LogLevelSelector lls3 = new LogLevelSelector(getLogLevelStrings().infoInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); Set rules = Stream.of( - new EventRule(EVENT_NAME_A, lls1, null), - new EventRule(EVENT_NAME_A, lls2, null), - new EventRule(EVENT_NAME_A, lls3, null)) + EventRuleFactory.createRule(EVENT_NAME_A, lls1), + EventRuleFactory.createRule(EVENT_NAME_A, lls2), + EventRuleFactory.createRule(EVENT_NAME_A, lls3)) .collect(Collectors.toSet()); session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); @@ -195,16 +192,15 @@ public abstract class FilterListenerITBase { /** * Test enabling the same event name with various filters. */ - @Ignore("Filters are not tracked yet") @Test public void testSameEventsDiffFilters() { String filterA = "filterA"; String filterB = "filterB"; Set rules = Stream.of( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, filterA), - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, filterB)) + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_B, EventRuleFactory.LOG_LEVEL_UNSPECIFIED, filterA), + EventRuleFactory.createRule(EVENT_NAME_C, EventRuleFactory.LOG_LEVEL_UNSPECIFIED, filterB)) .collect(Collectors.toSet()); session.enableEvent(EVENT_NAME_A, null, false, null); @@ -222,8 +218,8 @@ public abstract class FilterListenerITBase { @Test public void testDetachingListener() { Set rules = Stream.of( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null)) + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_B)) .collect(Collectors.toSet()); session.enableEvent(EVENT_NAME_A, null, false, null); @@ -248,8 +244,8 @@ public abstract class FilterListenerITBase { fcn.registerListener(listener3); Set rules = Stream.of( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null)) + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_B)) .collect(Collectors.toSet()); session.enableEvent(EVENT_NAME_A, null, false, null); @@ -285,8 +281,8 @@ public abstract class FilterListenerITBase { fcn.unregisterListener(listener2); Set rules = Stream.of( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null)) + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_B)) .collect(Collectors.toSet()); session.enableEvent(EVENT_NAME_A, null, false, null); @@ -312,12 +308,12 @@ public abstract class FilterListenerITBase { TestFilterListener listener2 = new TestFilterListener(); Set rules1 = Stream.of( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null)) + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_B)) .collect(Collectors.toSet()); Set rules2 = Stream.of( - new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), - new EventRule(EVENT_NAME_C, LOG_LEVEL_UNSPECIFIED, null)) + EventRuleFactory.createRule(EVENT_NAME_A), + EventRuleFactory.createRule(EVENT_NAME_C)) .collect(Collectors.toSet()); session.enableEvent(EVENT_NAME_A, null, false, null); @@ -340,18 +336,6 @@ public abstract class FilterListenerITBase { /** * The filter listener used for tests. - * - *

- * Usage: - *

    - *
  • Specify the expected number of notifications and end rules with - * {@link #setParameters}.
  • - *
  • Send the commands to LTTng (using {@link ILttngSession} for example. - *
  • - *
  • Call {@link #waitForAllNotifications()}.
  • - *
  • Verify that {@link #checkRules()} returns true.
  • - *
- *

*/ private static class TestFilterListener implements IFilterChangeListener { diff --git a/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/utils/EventRuleFactory.java b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/utils/EventRuleFactory.java new file mode 100644 index 0000000..fd1a46b --- /dev/null +++ b/lttng-ust-java-tests/src/test/java/org/lttng/ust/agent/utils/EventRuleFactory.java @@ -0,0 +1,135 @@ +/* + * 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.utils; + +import java.util.StringJoiner; + +import org.lttng.ust.agent.session.EventRule; +import org.lttng.ust.agent.session.LogLevelSelector; + +/** + * Factory class for {@link EventRule} objects, offering convenience methods + * that mimic the results of passing the given arguments on the command-line. + * + * @author Alexandre Montplaisir + */ +public final class EventRuleFactory { + + /** Name of the "all" (-a) event */ + public static final String EVENT_NAME_ALL = "*"; + + /** Log level set by default when it is not specified */ + public static final LogLevelSelector LOG_LEVEL_UNSPECIFIED = new LogLevelSelector(Integer.MIN_VALUE, 0); + + private EventRuleFactory() {} + + /** + * Construct an event by only passing the event name on the command-line. + * + * @param eventName + * The event name + * @return The corresponding event rule + */ + public static EventRule createRule(String eventName) { + return new EventRule(eventName, LOG_LEVEL_UNSPECIFIED, filterStringFromEventName(eventName)); + } + + /** + * Construct and event rule by specifying the event name and log level. + * + * @param eventName + * The event name + * @param logLevelSelector + * The log level + * @return The corresponding event rule + */ + public static EventRule createRule(String eventName, LogLevelSelector logLevelSelector) { + StringJoiner sj = new StringJoiner(") && (", "(", ")"); + String filterStr = sj.add(filterStringFromEventName(eventName)) + .add(filterStringFromLogLevel(logLevelSelector)) + .toString(); + return new EventRule(eventName, logLevelSelector, filterStr); + } + + /** + * Construct and event rule by specifying the event name, log level, and a + * filter string. + * + * @param eventName + * The event name + * @param logLevelSelector + * The log level + * @param extraFilter + * The filter string passed on the command-line + * @return The corresponding event rule + */ + public static EventRule createRule(String eventName, LogLevelSelector logLevelSelector, String extraFilter) { + StringJoiner sj1 = new StringJoiner(") && (", "(", ")"); + sj1.add(extraFilter); + sj1.add(filterStringFromEventName(eventName)); + String firstPart = sj1.toString(); + + if (logLevelSelector.equals(LOG_LEVEL_UNSPECIFIED)) { + return new EventRule(eventName, logLevelSelector, firstPart); + } + + /* + * If there is both a filter and a log level, the filter + event name is + * "resolved" first. + */ + StringJoiner sj2 = new StringJoiner(") && (", "(", ")"); + sj2.add(firstPart); + sj2.add(filterStringFromLogLevel(logLevelSelector)); + return new EventRule(eventName, logLevelSelector, sj2.toString()); + } + + /** + * Construct an event rule corresponding to enabling all (-a) events. + * + * @return The corresponding event rule + */ + public static EventRule createRuleAllEvents() { + return new EventRule(EVENT_NAME_ALL, LOG_LEVEL_UNSPECIFIED, ""); + } + + private static String filterStringFromEventName(String eventName) { + return "logger_name == \"" + eventName + "\""; + } + + private static String filterStringFromLogLevel(LogLevelSelector logLevelSelector) { + StringBuilder sb = new StringBuilder(); + sb.append("int_loglevel "); + + switch (logLevelSelector.getLogLevelType()) { + case LTTNG_EVENT_LOGLEVEL_RANGE: + sb.append(">="); + break; + case LTTNG_EVENT_LOGLEVEL_SINGLE: + sb.append("=="); + break; + case LTTNG_EVENT_LOGLEVEL_ALL: + default: + throw new IllegalArgumentException(); + } + + sb.append(" " + logLevelSelector.getLogLevel()); + return sb.toString(); + } + +} -- 2.39.5