From 8576633f94f26d8fb955d7c3a29a03cd09e99ed4 Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Fri, 24 Jul 2015 03:43:03 -0400 Subject: [PATCH] Turn LttngSession(Control) into a non-static class By managing session objects, the session management becomes much less verbose in the tests. Additionally, it becomes clear that the @Before method has to create the session, and the @After has to close it. And AutoCloseable rocks, as usual. Signed-off-by: Alexandre Montplaisir --- .settings/org.eclipse.jdt.core.prefs | 58 ++-- ...tngJulHandlerTracingDisabledBenchmark.java | 14 +- ...ttngJulHandlerTracingEnabledBenchmark.java | 14 +- ...tngJulHandlerTracingDisabledBenchmark.java | 18 +- ...ttngJulHandlerTracingEnabledBenchmark.java | 16 +- .../integration/common/EnabledEventsTest.java | 98 +++--- .../integration/jul/JulEnabledEventsTest.java | 49 +-- .../integration/jul/JulLegacyApiTest.java | 73 ++-- .../agent/integration/jul/JulTestUtils.java | 27 ++ .../log4j/Log4jEnabledEventsTest.java | 46 +-- .../integration/log4j/Log4jLegacyApiTest.java | 73 ++-- .../integration/log4j/Log4jTestUtils.java | 26 ++ .../lttng/ust/agent/utils/LttngSession.java | 201 +++++++++++ .../ust/agent/utils/LttngSessionControl.java | 323 ------------------ src/org/lttng/ust/agent/utils/TestUtils.java | 127 +++++++ 15 files changed, 567 insertions(+), 596 deletions(-) create mode 100644 src/org/lttng/ust/agent/integration/jul/JulTestUtils.java create mode 100644 src/org/lttng/ust/agent/integration/log4j/Log4jTestUtils.java create mode 100644 src/org/lttng/ust/agent/utils/LttngSession.java delete mode 100644 src/org/lttng/ust/agent/utils/LttngSessionControl.java create mode 100644 src/org/lttng/ust/agent/utils/TestUtils.java diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 3b5cbc1..b632271 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -15,7 +15,7 @@ org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.autoboxing=ignore +org.eclipse.jdt.core.compiler.problem.autoboxing=warning org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning org.eclipse.jdt.core.compiler.problem.deadCode=warning org.eclipse.jdt.core.compiler.problem.deprecation=warning @@ -24,10 +24,10 @@ org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod= org.eclipse.jdt.core.compiler.problem.discouragedReference=warning org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore -org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore +org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning +org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled -org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore +org.eclipse.jdt.core.compiler.problem.fieldHiding=warning org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=error @@ -35,17 +35,17 @@ org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning -org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore -org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore +org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=warning +org.eclipse.jdt.core.compiler.problem.localVariableHiding=warning org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning -org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore -org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingDefaultCase=warning +org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=warning org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled -org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore +org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=warning org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning -org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore +org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=warning org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore @@ -55,47 +55,47 @@ org.eclipse.jdt.core.compiler.problem.nullReference=warning org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning -org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore -org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore -org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore -org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore +org.eclipse.jdt.core.compiler.problem.parameterAssignment=warning +org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=warning +org.eclipse.jdt.core.compiler.problem.potentialNullReference=warning +org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=warning org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning -org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore -org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore -org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore -org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore +org.eclipse.jdt.core.compiler.problem.redundantNullCheck=warning +org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=warning +org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=warning +org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=warning +org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=warning org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled -org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore +org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=warning org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore -org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=warning +org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=warning org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=warning +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=disabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=disabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=enabled org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore org.eclipse.jdt.core.compiler.problem.unusedImport=warning org.eclipse.jdt.core.compiler.problem.unusedLabel=warning org.eclipse.jdt.core.compiler.problem.unusedLocal=warning -org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=warning +org.eclipse.jdt.core.compiler.problem.unusedParameter=warning org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning -org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=warning org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java index 562965d..18f84d3 100644 --- a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingDisabledBenchmark.java @@ -8,21 +8,25 @@ import org.junit.After; import org.junit.Before; import org.lttng.ust.agent.benchmarks.jul.handler.AbstractJulBenchmark; import org.lttng.ust.agent.jul.LttngLogHandler; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; public class LttngJulHandlerTracingDisabledBenchmark extends AbstractJulBenchmark { + private LttngSession session; + @Before public void testSetup() throws IOException { handler = new LttngLogHandler(); - assertTrue(LttngSessionControl.setupSession(null, Domain.JUL)); + session = new LttngSession(null, Domain.JUL); + assertTrue(session.enableEvents("non-event")); + assertTrue(session.start()); } @After public void testTeardown() { - assertTrue(LttngSessionControl.stopSession(null)); - assertTrue(LttngSessionControl.destroySession(null)); + assertTrue(session.stop()); + session.close(); } } diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java index 38b6aa6..02c7b05 100644 --- a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/LttngJulHandlerTracingEnabledBenchmark.java @@ -8,21 +8,25 @@ import org.junit.After; import org.junit.Before; import org.lttng.ust.agent.benchmarks.jul.handler.AbstractJulBenchmark; import org.lttng.ust.agent.jul.LttngLogHandler; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; public class LttngJulHandlerTracingEnabledBenchmark extends AbstractJulBenchmark { + private LttngSession session; + @Before public void testSetup() throws IOException { handler = new LttngLogHandler(); - assertTrue(LttngSessionControl.setupSessionAllEvents(null, Domain.JUL)); + session = new LttngSession(null, Domain.JUL); + assertTrue(session.enableAllEvents()); + assertTrue(session.start()); } @After public void testTeardown() { - assertTrue(LttngSessionControl.stopSession(null)); - assertTrue(LttngSessionControl.destroySession(null)); + assertTrue(session.stop()); + session.close(); } } diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java index 21ab953..2978fe0 100644 --- a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingDisabledBenchmark.java @@ -2,29 +2,31 @@ package org.lttng.ust.agent.benchmarks.jul.handler.lttng.old; import static org.junit.Assert.assertTrue; -import java.io.IOException; - import org.junit.After; import org.junit.Before; import org.lttng.ust.agent.LTTngAgent; import org.lttng.ust.agent.benchmarks.jul.handler.AbstractJulBenchmark; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; @SuppressWarnings("deprecation") public class OldLttngJulHandlerTracingDisabledBenchmark extends AbstractJulBenchmark { + private LttngSession session; + @Before - public void testSetup() throws IOException { + public void testSetup() { LTTngAgent.getLTTngAgent(); - assertTrue(LttngSessionControl.setupSession(null, Domain.JUL)); + session = new LttngSession(null, Domain.JUL); + assertTrue(session.enableEvents("non-event")); + assertTrue(session.start()); } @After public void testTeardown() { - assertTrue(LttngSessionControl.stopSession(null)); - assertTrue(LttngSessionControl.destroySession(null)); + assertTrue(session.stop()); + session.close(); LTTngAgent.dispose(); } diff --git a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java index 29439a0..c6cc49c 100644 --- a/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java +++ b/src/org/lttng/ust/agent/benchmarks/jul/handler/lttng/old/OldLttngJulHandlerTracingEnabledBenchmark.java @@ -3,7 +3,6 @@ package org.lttng.ust.agent.benchmarks.jul.handler.lttng.old; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.io.IOException; import java.lang.reflect.Field; import org.junit.After; @@ -11,16 +10,17 @@ import org.junit.Before; import org.lttng.ust.agent.LTTngAgent; import org.lttng.ust.agent.benchmarks.jul.handler.AbstractJulBenchmark; import org.lttng.ust.agent.jul.LttngLogHandler; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; @SuppressWarnings("deprecation") public class OldLttngJulHandlerTracingEnabledBenchmark extends AbstractJulBenchmark { + private LttngSession session; private LttngLogHandler agentHandler; @Before - public void testSetup() throws IOException { + public void testSetup() { LTTngAgent agentInstance = LTTngAgent.getLTTngAgent(); /* @@ -43,13 +43,15 @@ public class OldLttngJulHandlerTracingEnabledBenchmark extends AbstractJulBenchm fail(); } - assertTrue(LttngSessionControl.setupSessionAllEvents(null, Domain.JUL)); + session = new LttngSession(null, Domain.JUL); + assertTrue(session.enableAllEvents()); + assertTrue(session.start()); } @After public void testTeardown() { - assertTrue(LttngSessionControl.stopSession(null)); - assertTrue(LttngSessionControl.destroySession(null)); + assertTrue(session.stop()); + session.close(); logger.removeHandler(agentHandler); LTTngAgent.dispose(); diff --git a/src/org/lttng/ust/agent/integration/common/EnabledEventsTest.java b/src/org/lttng/ust/agent/integration/common/EnabledEventsTest.java index 28389be..5a8a8b5 100644 --- a/src/org/lttng/ust/agent/integration/common/EnabledEventsTest.java +++ b/src/org/lttng/ust/agent/integration/common/EnabledEventsTest.java @@ -10,8 +10,8 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.lttng.ust.agent.ILttngHandler; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; public abstract class EnabledEventsTest { @@ -20,6 +20,8 @@ public abstract class EnabledEventsTest { protected static final String EVENT_NAME_C = "EventABC"; protected static final String EVENT_NAME_D = "EventABCD"; + private LttngSession session; + /* Fields defined by the sub-class */ protected ILttngHandler handlerA; protected ILttngHandler handlerB; @@ -31,13 +33,12 @@ public abstract class EnabledEventsTest { @Before public void testSetup() { - + session = new LttngSession(null, getDomain()); } @After public void testTeardown() { - /* In case the test fails before destroying the session */ - LttngSessionControl.tryDestroySession(null); + session.close(); handlerA.close(); handlerB.close(); @@ -55,18 +56,16 @@ public abstract class EnabledEventsTest { */ @Test public void testNoEvents() { - assertTrue(LttngSessionControl.setupSession(null, getDomain())); + assertTrue(session.start()); sendEventsToLoggers(); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertTrue(output.isEmpty()); - assertTrue(LttngSessionControl.destroySession(null)); - assertEquals(0, handlerA.getEventCount()); assertEquals(0, handlerB.getEventCount()); assertEquals(0, handlerC.getEventCount()); @@ -78,18 +77,17 @@ public abstract class EnabledEventsTest { */ @Test public void testAllEvents() { - assertTrue(LttngSessionControl.setupSessionAllEvents(null, getDomain())); + assertTrue(session.enableAllEvents()); + assertTrue(session.start()); sendEventsToLoggers(); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(30, output.size()); // loggerD has no handler attached - assertTrue(LttngSessionControl.destroySession(null)); - assertEquals(10, handlerA.getEventCount()); assertEquals(10, handlerB.getEventCount()); assertEquals(10, handlerC.getEventCount()); @@ -101,19 +99,17 @@ public abstract class EnabledEventsTest { */ @Test public void testSomeEvents() { - assertTrue(LttngSessionControl.setupSession(null, getDomain(), - EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); + assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); + assertTrue(session.start()); sendEventsToLoggers(); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(20, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - assertEquals(10, handlerA.getEventCount()); assertEquals(0, handlerB.getEventCount()); assertEquals(10, handlerC.getEventCount()); @@ -125,20 +121,18 @@ public abstract class EnabledEventsTest { */ @Test public void testAllEventsAndSome() { - assertTrue(LttngSessionControl.setupSessionAllEvents(null, getDomain())); - assertTrue(LttngSessionControl.enableEvents(null, getDomain(), - EVENT_NAME_A, EVENT_NAME_B)); + assertTrue(session.enableAllEvents()); + assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_B)); + assertTrue(session.start()); sendEventsToLoggers(); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(30, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - assertEquals(10, handlerA.getEventCount()); assertEquals(10, handlerB.getEventCount()); assertEquals(10, handlerC.getEventCount()); @@ -150,22 +144,18 @@ public abstract class EnabledEventsTest { */ @Test public void testSomeEventsAfterDisabling() { - assertTrue(LttngSessionControl.setupSession(null, getDomain(), - EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); - - assertTrue(LttngSessionControl.disableEvents(null, getDomain(), - EVENT_NAME_C)); + assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); + assertTrue(session.disableEvents(EVENT_NAME_C)); + assertTrue(session.start()); sendEventsToLoggers(); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(10, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - assertEquals(10, handlerA.getEventCount()); assertEquals(0, handlerB.getEventCount()); assertEquals(0, handlerC.getEventCount()); @@ -177,19 +167,18 @@ public abstract class EnabledEventsTest { */ @Test public void testEventPrefix() { - assertTrue(LttngSessionControl.setupSession(null, getDomain(), - "EventAB*")); // should match event/loggers B and C, but not A. + // should match event/loggers B and C, but not A. + assertTrue(session.enableEvents("EventAB*")); + assertTrue(session.start()); sendEventsToLoggers(); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(20, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - assertEquals(0, handlerA.getEventCount()); assertEquals(10, handlerB.getEventCount()); assertEquals(10, handlerC.getEventCount()); @@ -202,19 +191,18 @@ public abstract class EnabledEventsTest { */ @Test public void testEventPrefixOverlapping() { - assertTrue(LttngSessionControl.setupSession(null, getDomain(), - "EventAB*", "EventABC*")); // should still match B and C + // should still match B and C + assertTrue(session.enableEvents("EventAB*", "EventABC*")); + assertTrue(session.start()); sendEventsToLoggers(); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(20, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - assertEquals(0, handlerA.getEventCount()); assertEquals(10, handlerB.getEventCount()); assertEquals(10, handlerC.getEventCount()); @@ -226,20 +214,18 @@ public abstract class EnabledEventsTest { */ @Test public void testAllEventsAndPrefix() { - assertTrue(LttngSessionControl.setupSessionAllEvents(null, getDomain())); - assertTrue(LttngSessionControl.enableEvents(null, getDomain(), - "EventAB*")); // should match B and C + assertTrue(session.enableAllEvents()); + assertTrue(session.enableEvents("EventAB*")); + assertTrue(session.start()); sendEventsToLoggers(); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(30, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - assertEquals(10, handlerA.getEventCount()); assertEquals(10, handlerB.getEventCount()); assertEquals(10, handlerC.getEventCount()); diff --git a/src/org/lttng/ust/agent/integration/jul/JulEnabledEventsTest.java b/src/org/lttng/ust/agent/integration/jul/JulEnabledEventsTest.java index 9493379..cbb1413 100644 --- a/src/org/lttng/ust/agent/integration/jul/JulEnabledEventsTest.java +++ b/src/org/lttng/ust/agent/integration/jul/JulEnabledEventsTest.java @@ -3,7 +3,6 @@ package org.lttng.ust.agent.integration.jul; import static org.junit.Assume.assumeTrue; import java.io.IOException; -import java.util.List; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; @@ -14,8 +13,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.lttng.ust.agent.integration.common.EnabledEventsTest; import org.lttng.ust.agent.jul.LttngLogHandler; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; +import org.lttng.ust.agent.utils.TestUtils; public class JulEnabledEventsTest extends EnabledEventsTest { @@ -29,25 +29,13 @@ public class JulEnabledEventsTest extends EnabledEventsTest { @BeforeClass public static void julClassSetup() { /* Skip tests if we can't find the JNI library or lttng-tools */ - try { - LttngLogHandler testHandler = new LttngLogHandler(); - testHandler.close(); - } catch (SecurityException | IOException e) { - assumeTrue(false); - } - - boolean ret1 = LttngSessionControl.setupSession(null, DOMAIN); - boolean ret2 = LttngSessionControl.stopSession(null); - /* "lttng view" also tests that Babeltrace is installed and working */ - List contents = LttngSessionControl.viewSession(null); - boolean ret3 = LttngSessionControl.destroySession(null); - assumeTrue(ret1 && ret2 && ret3); - assumeTrue(contents.isEmpty()); + assumeTrue(TestUtils.checkForJulLibrary()); + assumeTrue(TestUtils.checkForLttngTools(Domain.JUL)); } @AfterClass public static void julClassCleanup() { - LttngSessionControl.deleteAllTracee(); + LttngSession.deleteAllTracee(); } @Before @@ -92,26 +80,9 @@ public class JulEnabledEventsTest extends EnabledEventsTest { @Override protected void sendEventsToLoggers() { - send10Events(loggerA); - send10Events(loggerB); - send10Events(loggerC); - send10Events(loggerD); - } - - static void send10Events(Logger logger) { - String a = new String("a"); - Object[] params = { a, new String("b"), new Object() }; - - // Levels are FINE, FINER, FINEST, INFO, SEVERE, WARNING - logger.fine("A fine level message"); - logger.finer("A finer level message"); - logger.finest("A finest level message"); - logger.info("A info level message"); - logger.severe("A severe level message"); - logger.warning("A warning level message"); - logger.warning("Another warning level message"); - logger.log(Level.WARNING, "A warning message using Logger.log()"); - logger.log(Level.INFO, "A message with one parameter", a); - logger.log(Level.INFO, "A message with parameters", params); + JulTestUtils.send10EventsTo(loggerA); + JulTestUtils.send10EventsTo(loggerB); + JulTestUtils.send10EventsTo(loggerC); + JulTestUtils.send10EventsTo(loggerD); } } diff --git a/src/org/lttng/ust/agent/integration/jul/JulLegacyApiTest.java b/src/org/lttng/ust/agent/integration/jul/JulLegacyApiTest.java index 808cf27..d66479e 100644 --- a/src/org/lttng/ust/agent/integration/jul/JulLegacyApiTest.java +++ b/src/org/lttng/ust/agent/integration/jul/JulLegacyApiTest.java @@ -6,7 +6,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; -import java.io.IOException; import java.lang.reflect.Field; import java.util.List; import java.util.logging.Level; @@ -19,9 +18,9 @@ import org.junit.BeforeClass; import org.junit.Test; import org.lttng.ust.agent.ILttngHandler; import org.lttng.ust.agent.LTTngAgent; -import org.lttng.ust.agent.jul.LttngLogHandler; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; +import org.lttng.ust.agent.utils.TestUtils; @SuppressWarnings("deprecation") public class JulLegacyApiTest { @@ -31,31 +30,21 @@ public class JulLegacyApiTest { private static final String EVENT_NAME_A = "EventA"; private static final String EVENT_NAME_B = "EventB"; + private LttngSession session; + private Logger loggerA; private Logger loggerB; @BeforeClass public static void julClassSetup() { /* Skip tests if we can't find the JNI library or lttng-tools */ - try { - LttngLogHandler testHandler = new LttngLogHandler(); - testHandler.close(); - } catch (SecurityException | IOException e) { - assumeTrue(false); - } - - boolean ret1 = LttngSessionControl.setupSession(null, DOMAIN); - boolean ret2 = LttngSessionControl.stopSession(null); - /* "lttng view" also tests that Babeltrace is installed and working */ - List contents = LttngSessionControl.viewSession(null); - boolean ret3 = LttngSessionControl.destroySession(null); - assumeTrue(ret1 && ret2 && ret3); - assumeTrue(contents.isEmpty()); + assumeTrue(TestUtils.checkForJulLibrary()); + assumeTrue(TestUtils.checkForLttngTools(Domain.JUL)); } @AfterClass public static void julClassCleanup() { - LttngSessionControl.deleteAllTracee(); + LttngSession.deleteAllTracee(); } @Before @@ -66,12 +55,13 @@ public class JulLegacyApiTest { loggerA.setLevel(Level.ALL); loggerB.setLevel(Level.ALL); + + session = new LttngSession(null, DOMAIN); } @After public void tearDown() { - /* In case the test fails before destroying the session */ - LttngSessionControl.tryDestroySession(null); + session.close(); LTTngAgent.dispose(); @@ -81,58 +71,53 @@ public class JulLegacyApiTest { @Test public void testNoEvents() { - assertTrue(LttngSessionControl.setupSession(null, DOMAIN)); + assertTrue(session.start()); - JulEnabledEventsTest.send10Events(loggerA); - JulEnabledEventsTest.send10Events(loggerB); + JulTestUtils.send10EventsTo(loggerA); + JulTestUtils.send10EventsTo(loggerB); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertTrue(output.isEmpty()); - assertTrue(LttngSessionControl.destroySession(null)); - ILttngHandler handler = getAgentHandler(); assertEquals(0, handler.getEventCount()); } @Test public void testAllEvents() { - assertTrue(LttngSessionControl.setupSessionAllEvents(null, DOMAIN)); + assertTrue(session.enableAllEvents()); + assertTrue(session.start()); - JulEnabledEventsTest.send10Events(loggerA); - JulEnabledEventsTest.send10Events(loggerB); + JulTestUtils.send10EventsTo(loggerA); + JulTestUtils.send10EventsTo(loggerB); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(20, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - ILttngHandler handler = getAgentHandler(); assertEquals(20, handler.getEventCount()); } @Test public void testSomeEvents() { - assertTrue(LttngSessionControl.setupSession(null, DOMAIN, - EVENT_NAME_A)); + assertTrue(session.enableEvents(EVENT_NAME_A)); + assertTrue(session.start()); - JulEnabledEventsTest.send10Events(loggerA); - JulEnabledEventsTest.send10Events(loggerB); + JulTestUtils.send10EventsTo(loggerA); + JulTestUtils.send10EventsTo(loggerB); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(10, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - ILttngHandler handler = getAgentHandler(); assertEquals(10, handler.getEventCount()); } @@ -143,7 +128,7 @@ public class JulLegacyApiTest { * * @return The agent's JUL handler */ - private ILttngHandler getAgentHandler() { + private static ILttngHandler getAgentHandler() { try { Field julHandlerField = LTTngAgent.class.getDeclaredField("julHandler"); julHandlerField.setAccessible(true); diff --git a/src/org/lttng/ust/agent/integration/jul/JulTestUtils.java b/src/org/lttng/ust/agent/integration/jul/JulTestUtils.java new file mode 100644 index 0000000..223f323 --- /dev/null +++ b/src/org/lttng/ust/agent/integration/jul/JulTestUtils.java @@ -0,0 +1,27 @@ +package org.lttng.ust.agent.integration.jul; + +import java.util.logging.Level; +import java.util.logging.Logger; + +final class JulTestUtils { + + JulTestUtils() { + } + + static void send10EventsTo(Logger logger) { + String a = new String("a"); + Object[] params = { a, new String("b"), new Object() }; + + // Levels are FINE, FINER, FINEST, INFO, SEVERE, WARNING + logger.fine("A fine level message"); + logger.finer("A finer level message"); + logger.finest("A finest level message"); + logger.info("A info level message"); + logger.severe("A severe level message"); + logger.warning("A warning level message"); + logger.warning("Another warning level message"); + logger.log(Level.WARNING, "A warning message using Logger.log()"); + logger.log(Level.INFO, "A message with one parameter", a); + logger.log(Level.INFO, "A message with parameters", params); + } +} diff --git a/src/org/lttng/ust/agent/integration/log4j/Log4jEnabledEventsTest.java b/src/org/lttng/ust/agent/integration/log4j/Log4jEnabledEventsTest.java index 8118069..a38d2e3 100644 --- a/src/org/lttng/ust/agent/integration/log4j/Log4jEnabledEventsTest.java +++ b/src/org/lttng/ust/agent/integration/log4j/Log4jEnabledEventsTest.java @@ -3,7 +3,6 @@ package org.lttng.ust.agent.integration.log4j; import static org.junit.Assume.assumeTrue; import java.io.IOException; -import java.util.List; import org.apache.log4j.Appender; import org.apache.log4j.Level; @@ -14,8 +13,9 @@ import org.junit.Before; import org.junit.BeforeClass; import org.lttng.ust.agent.integration.common.EnabledEventsTest; import org.lttng.ust.agent.log4j.LttngLogAppender; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; +import org.lttng.ust.agent.utils.TestUtils; public class Log4jEnabledEventsTest extends EnabledEventsTest { @@ -29,25 +29,13 @@ public class Log4jEnabledEventsTest extends EnabledEventsTest { @BeforeClass public static void julClassSetup() { /* Skip tests if we can't find the JNI library or lttng-tools */ - try { - LttngLogAppender testHandler = new LttngLogAppender(); - testHandler.close(); - } catch (SecurityException | IOException e) { - assumeTrue(false); - } - - boolean ret1 = LttngSessionControl.setupSession(null, DOMAIN); - boolean ret2 = LttngSessionControl.stopSession(null); - /* "lttng view" also tests that Babeltrace is installed and working */ - List contents = LttngSessionControl.viewSession(null); - boolean ret3 = LttngSessionControl.destroySession(null); - assumeTrue(ret1 && ret2 && ret3); - assumeTrue(contents.isEmpty()); + assumeTrue(TestUtils.checkForLog4jLibrary()); + assumeTrue(TestUtils.checkForLttngTools(Domain.LOG4J)); } @AfterClass public static void julClassCleanup() { - LttngSessionControl.deleteAllTracee(); + LttngSession.deleteAllTracee(); } @Before @@ -92,23 +80,9 @@ public class Log4jEnabledEventsTest extends EnabledEventsTest { @Override protected void sendEventsToLoggers() { - send10Events(loggerA); - send10Events(loggerB); - send10Events(loggerC); - send10Events(loggerD); - } - - static void send10Events(Logger logger) { - // Levels/priorities are DEBUG, ERROR, FATAL, INFO, TRACE, WARN - logger.debug("Debug message. Lost among so many."); - logger.debug("Debug message with a throwable", new IOException()); - logger.error("Error messsage. This might be bad."); - logger.error("Error message with a throwable", new IOException()); - logger.fatal("A fatal message. You are already dead."); - logger.info("A info message. Lol, who cares."); - logger.trace("A trace message. No, no *that* trace"); - logger.warn("A warn message. Yellow underline."); - logger.log(Level.DEBUG, "A debug message using .log()"); - logger.log(Level.ERROR, "A error message using .log()"); + Log4jTestUtils.send10Events(loggerA); + Log4jTestUtils.send10Events(loggerB); + Log4jTestUtils.send10Events(loggerC); + Log4jTestUtils.send10Events(loggerD); } } diff --git a/src/org/lttng/ust/agent/integration/log4j/Log4jLegacyApiTest.java b/src/org/lttng/ust/agent/integration/log4j/Log4jLegacyApiTest.java index 98b6318..1a240b0 100644 --- a/src/org/lttng/ust/agent/integration/log4j/Log4jLegacyApiTest.java +++ b/src/org/lttng/ust/agent/integration/log4j/Log4jLegacyApiTest.java @@ -6,7 +6,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; -import java.io.IOException; import java.lang.reflect.Field; import java.util.List; @@ -19,9 +18,9 @@ import org.junit.BeforeClass; import org.junit.Test; import org.lttng.ust.agent.ILttngHandler; import org.lttng.ust.agent.LTTngAgent; -import org.lttng.ust.agent.log4j.LttngLogAppender; -import org.lttng.ust.agent.utils.LttngSessionControl; -import org.lttng.ust.agent.utils.LttngSessionControl.Domain; +import org.lttng.ust.agent.utils.LttngSession; +import org.lttng.ust.agent.utils.LttngSession.Domain; +import org.lttng.ust.agent.utils.TestUtils; @SuppressWarnings("deprecation") public class Log4jLegacyApiTest { @@ -31,31 +30,21 @@ public class Log4jLegacyApiTest { private static final String EVENT_NAME_A = "EventA"; private static final String EVENT_NAME_B = "EventB"; + private LttngSession session; + private Logger loggerA; private Logger loggerB; @BeforeClass public static void classSetup() { /* Skip tests if we can't find the JNI library or lttng-tools */ - try { - LttngLogAppender testAppender = new LttngLogAppender(); - testAppender.close(); - } catch (SecurityException | IOException e) { - assumeTrue(false); - } - - boolean ret1 = LttngSessionControl.setupSession(null, DOMAIN); - boolean ret2 = LttngSessionControl.stopSession(null); - /* "lttng view" also tests that Babeltrace is installed and working */ - List contents = LttngSessionControl.viewSession(null); - boolean ret3 = LttngSessionControl.destroySession(null); - assumeTrue(ret1 && ret2 && ret3); - assumeTrue(contents.isEmpty()); + assumeTrue(TestUtils.checkForLog4jLibrary()); + assumeTrue(TestUtils.checkForLttngTools(Domain.LOG4J)); } @AfterClass public static void classCleanup() { - LttngSessionControl.deleteAllTracee(); + LttngSession.deleteAllTracee(); } @Before @@ -66,12 +55,13 @@ public class Log4jLegacyApiTest { loggerA.setLevel(Level.ALL); loggerB.setLevel(Level.ALL); + + session = new LttngSession(null, DOMAIN); } @After public void tearDown() { - /* In case the test fails before destroying the session */ - LttngSessionControl.tryDestroySession(null); + session.close(); LTTngAgent.dispose(); @@ -81,58 +71,53 @@ public class Log4jLegacyApiTest { @Test public void testNoEvents() { - assertTrue(LttngSessionControl.setupSession(null, DOMAIN)); + assertTrue(session.start()); - Log4jEnabledEventsTest.send10Events(loggerA); - Log4jEnabledEventsTest.send10Events(loggerB); + Log4jTestUtils.send10Events(loggerA); + Log4jTestUtils.send10Events(loggerB); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertTrue(output.isEmpty()); - assertTrue(LttngSessionControl.destroySession(null)); - ILttngHandler handler = getAgentHandler(); assertEquals(0, handler.getEventCount()); } @Test public void testAllEvents() { - assertTrue(LttngSessionControl.setupSessionAllEvents(null, DOMAIN)); + assertTrue(session.enableAllEvents()); + assertTrue(session.start()); - Log4jEnabledEventsTest.send10Events(loggerA); - Log4jEnabledEventsTest.send10Events(loggerB); + Log4jTestUtils.send10Events(loggerA); + Log4jTestUtils.send10Events(loggerB); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(20, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - ILttngHandler handler = getAgentHandler(); assertEquals(20, handler.getEventCount()); } @Test public void testSomeEvents() { - assertTrue(LttngSessionControl.setupSession(null, DOMAIN, - EVENT_NAME_A)); + assertTrue(session.enableEvents(EVENT_NAME_A)); + assertTrue(session.start()); - Log4jEnabledEventsTest.send10Events(loggerA); - Log4jEnabledEventsTest.send10Events(loggerB); + Log4jTestUtils.send10Events(loggerA); + Log4jTestUtils.send10Events(loggerB); - assertTrue(LttngSessionControl.stopSession(null)); + assertTrue(session.stop()); - List output = LttngSessionControl.viewSession(null); + List output = session.view(); assertNotNull(output); assertEquals(10, output.size()); - assertTrue(LttngSessionControl.destroySession(null)); - ILttngHandler handler = getAgentHandler(); assertEquals(10, handler.getEventCount()); } @@ -143,7 +128,7 @@ public class Log4jLegacyApiTest { * * @return The agent's Log4j handler */ - private ILttngHandler getAgentHandler() { + private static ILttngHandler getAgentHandler() { try { Field log4jAppenderField = LTTngAgent.class.getDeclaredField("log4jAppender"); log4jAppenderField.setAccessible(true); diff --git a/src/org/lttng/ust/agent/integration/log4j/Log4jTestUtils.java b/src/org/lttng/ust/agent/integration/log4j/Log4jTestUtils.java new file mode 100644 index 0000000..d10ad3d --- /dev/null +++ b/src/org/lttng/ust/agent/integration/log4j/Log4jTestUtils.java @@ -0,0 +1,26 @@ +package org.lttng.ust.agent.integration.log4j; + +import java.io.IOException; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +final class Log4jTestUtils { + + private Log4jTestUtils() { + } + + static void send10Events(Logger logger) { + // Levels/priorities are DEBUG, ERROR, FATAL, INFO, TRACE, WARN + logger.debug("Debug message. Lost among so many."); + logger.debug("Debug message with a throwable", new IOException()); + logger.error("Error messsage. This might be bad."); + logger.error("Error message with a throwable", new IOException()); + logger.fatal("A fatal message. You are already dead."); + logger.info("A info message. Lol, who cares."); + logger.trace("A trace message. No, no *that* trace"); + logger.warn("A warn message. Yellow underline."); + logger.log(Level.DEBUG, "A debug message using .log()"); + logger.log(Level.ERROR, "A error message using .log()"); + } +} diff --git a/src/org/lttng/ust/agent/utils/LttngSession.java b/src/org/lttng/ust/agent/utils/LttngSession.java new file mode 100644 index 0000000..d13e2fb --- /dev/null +++ b/src/org/lttng/ust/agent/utils/LttngSession.java @@ -0,0 +1,201 @@ +package org.lttng.ust.agent.utils; + +import java.io.IOException; +import java.lang.ProcessBuilder.Redirect; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +public class LttngSession implements AutoCloseable { + + public enum Domain { + JUL("-j"), + LOG4J("-l"); + + private final String flag; + + private Domain(String flag) { + this.flag = flag; + } + + public String flag() { + return flag; + } + } + + private final String sessionName; + private final Domain domain; + + private volatile boolean channelCreated = false; + + public LttngSession(String sessionName, Domain domain) { + if (sessionName != null) { + this.sessionName = sessionName; + } else { + this.sessionName = UUID.randomUUID().toString(); + } + this.domain = domain; + + /* Create the session in LTTng */ + executeCommand(Arrays.asList("lttng", "create", this.sessionName)); + } + + @Override + public void close() { + /* Destroy the session */ + executeCommand(Arrays.asList("lttng", "destroy", sessionName)); + // FIXME also delete the trace we generated ? + } + + // ------------------------------------------------------------------------ + // Public methods + // ------------------------------------------------------------------------ + + /** + * Enable all events in the given session (enable-event -a) + * + * @return If the command executed successfully (return code = 0). + */ + public boolean enableAllEvents() { + channelCreated = true; + return executeCommand(Arrays.asList( + "lttng", "enable-event", domain.flag(), "-a", "-s", sessionName)); + } + + /** + * Enable individual event(s). + * + * @param enabledEvents + * The list of events to enable. Should not be null or empty + * @return If the command executed successfully (return code = 0). + */ + public boolean enableEvents(String... enabledEvents) { + if (enabledEvents == null || enabledEvents.length == 0) { + throw new IllegalArgumentException(); + } + channelCreated = true; + return executeCommand(Arrays.asList( + "lttng", "enable-event", domain.flag(), + Arrays.stream(enabledEvents).collect(Collectors.joining(",")), + "-s", sessionName)); + } + + /** + * Send a disable-event command. Used to disable events that were previously + * enabled. + * + * @param disabledEvents + * The list of disabled events. Should not be null or empty + * @return If the command executed successfully (return code = 0). + */ + public boolean disableEvents(String... disabledEvents) { + if (disabledEvents == null || disabledEvents.length == 0) { + throw new IllegalArgumentException(); + } + return executeCommand(Arrays.asList( + "lttng", "disable-event", domain.flag(), + Arrays.stream(disabledEvents).collect(Collectors.joining(",")), + "-s", sessionName)); + } + + public boolean start() { + /* + * We have to enable a channel for 'lttng start' to work. However, we + * cannot enable a channel directly, see + * https://bugs.lttng.org/issues/894 . Instead we will enable an event + * we know does not exist + */ + if (!channelCreated) { + enableEvents("non-event"); + } + return executeCommand(Arrays.asList("lttng", "start", sessionName)); + } + + /** + * Stop the tracing session + * + * @return If the command executed successfully (return code = 0). + */ + public boolean stop() { + return executeCommand(Arrays.asList("lttng", "stop", sessionName)); + } + + /** + * Issue a "lttng view" command on the session, and returns its output. This + * effectively returns the current content of the trace in text form. + * + * @return The output of Babeltrace on the session's current trace + */ + public List view() { + return TestUtils.getOutputFromCommand(Arrays.asList("lttng", "view", sessionName)); + } + + /** + * Outside of the scope of lttng-tools, but this utility method can be used + * to delete all traces currently under ~/lttng-traces/. This can be used by + * tests to cleanup a trace they have created. + * + * @return True if the command completes successfully, false if there was an + * error. + */ + public static boolean deleteAllTracee() { + String tracesDir = new String(System.getProperty("user.home") + "/lttng-traces/"); + return deleteDirectory(Paths.get(tracesDir)); + } + + // ------------------------------------------------------------------------ + // Private helper methods + // ------------------------------------------------------------------------ + + private static boolean deleteDirectory(Path directory) { + try { + Files.walkFileTree(directory, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + /* At least we tried... */ + return false; + } + return true; + } + + /** + * Just to test the environment / stdout are working correctly + */ + public static void main(String[] args) { + List command = Arrays.asList("ls", "-l"); + executeCommand(command); + } + + private static boolean executeCommand(List command) { + try { + ProcessBuilder builder = new ProcessBuilder(command); + builder.redirectErrorStream(true); + builder.redirectOutput(Redirect.INHERIT); + + Process p = builder.start(); + int ret = p.waitFor(); + return (ret == 0); + + } catch (IOException | InterruptedException e) { + return false; + } + } +} diff --git a/src/org/lttng/ust/agent/utils/LttngSessionControl.java b/src/org/lttng/ust/agent/utils/LttngSessionControl.java deleted file mode 100644 index 3e10bd5..0000000 --- a/src/org/lttng/ust/agent/utils/LttngSessionControl.java +++ /dev/null @@ -1,323 +0,0 @@ -package org.lttng.ust.agent.utils; - -import java.io.IOException; -import java.lang.ProcessBuilder.Redirect; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -public final class LttngSessionControl { - - private LttngSessionControl() {} - - public enum Domain { - JUL("-j"), - LOG4J("-l"); - - private final String flag; - - private Domain(String flag) { - this.flag = flag; - } - - public String flag() { - return flag; - } - } - - // ------------------------------------------------------------------------ - // Public utility methods - // ------------------------------------------------------------------------ - - /** - * Setup a LTTng session by enabling certain events (or none). - * - * @param sessionName - * The name of the session to create. May be null to use the - * default one from lttng-tools. - * @param domain - * The tracing domain - * @param enabledEvents - * The list of events to enable. May be null or empty, to not - * enable any events. - * @return If the command executed successfully (return code = 0). - */ - public static boolean setupSession(String sessionName, Domain domain, String... enabledEvents) { - String[] createCommand = (sessionName == null ? - new String[] { "lttng", "create" } : - new String[] { "lttng", "create", sessionName} - ); - - String eventsToEnable = (enabledEvents == null || enabledEvents.length == 0 ? - /* - * We have to enable a channel for 'lttng start' to work. - * However, we cannot enable a channel directly, see - * https://bugs.lttng.org/issues/894 . Instead we will enable an - * event we know does not exist - */ - "non-event" : - Arrays.stream(enabledEvents).collect(Collectors.joining(",")) - ); - - return executeCommands(new String[][] { - createCommand, - { "lttng", "enable-event", domain.flag(), eventsToEnable}, - { "lttng", "start" } - }); - } - - /** - * Setup a LTTng session with all events enabled (lttng enable-event -a). - * - * @param sessionName - * The name of the session to create. May be null to use the - * default one from lttng-tools. - * @param domain - * The tracing domain - * @return If the command executed successfully (return code = 0). - */ - public static boolean setupSessionAllEvents(String sessionName, Domain domain) { - String[] createCommand = (sessionName == null ? - new String[] { "lttng", "create" } : - new String[] { "lttng", "create", sessionName} - ); - - return executeCommands(new String[][] { - createCommand, - { "lttng", "enable-event", domain.flag(), "-a" }, - { "lttng", "start" } - }); - } - - /** - * Send a separate enable-event command. - * - * @param sessionName - * Name of the session in which to enable events. Use null for - * current session. - * @param domain - * The tracing domain - * @param enabledEvents - * The list of events to enable. Should not be null or empty - * @return If the command executed successfully (return code = 0). - */ - public static boolean enableEvents(String sessionName, Domain domain, String... enabledEvents) { - if (enabledEvents == null || enabledEvents.length == 0) { - throw new IllegalArgumentException(); - } - List command = new ArrayList(); - command.add("lttng"); - command.add("enable-event"); - command.add(domain.flag()); - command.add(Arrays.stream(enabledEvents).collect(Collectors.joining(","))); - if (sessionName != null) { - command.add("-s"); - command.add(sessionName); - } - return executeCommand(command.toArray(new String[0])); - } - - /** - * Send a disable-event command. Used to disable events that were previously - * enabled. - * - * @param sessionName - * Name of the session in which to disable events. Use null for - * current session. - * @param domain - * The tracing domain - * @param disabledEvents - * The list of disabled events. Should not be null or empty - * @return If the command executed successfully (return code = 0). - */ - public static boolean disableEvents(String sessionName, Domain domain, String... disabledEvents) { - if (disabledEvents == null || disabledEvents.length == 0) { - throw new IllegalArgumentException(); - } - List command = new ArrayList(); - command.add("lttng"); - command.add("disable-event"); - command.add(domain.flag()); - command.add(Arrays.stream(disabledEvents).collect(Collectors.joining(","))); - if (sessionName != null) { - command.add("-s"); - command.add(sessionName); - } - return executeCommand(command.toArray(new String[0])); - } - - /** - * Stop the current tracing session - * - * @param sessionName - * The name of the session to stop. Use null for the current - * session. - * @return If the command executed successfully (return code = 0). - */ - public static boolean stopSession(String sessionName) { - List command = new ArrayList(); - command.add("lttng"); - command.add("stop"); - if (sessionName != null) { - command.add(sessionName); - } - return executeCommand(command.toArray(new String[0])); - } - - /** - * Issue a "lttng view" command on the provided session, and returns its - * output. This effectively returns the current content of the trace in text - * form. - * - * @param sessionName - * The name of the session to print. Use null for the current - * session. - * @return The output of Babeltrace on the session's current trace - */ - public static List viewSession(String sessionName) { - List command = new ArrayList(); - command.add("lttng"); - command.add("view"); - if (sessionName != null) { - command.add(sessionName); - } - return getOutputFromCommand(command.toArray(new String[0])); - } - - /** - * Destroy the current tracing session - * - * @param sessionName - * The name of the session to destroy. Use null for the current - * session. - * @return If the command executed successfully (return code = 0). - */ - public static boolean destroySession(String sessionName) { - List command = new ArrayList(); - command.add("lttng"); - command.add("destroy"); - if (sessionName != null) { - command.add(sessionName); - } - return executeCommand(command.toArray(new String[0])); - } - - /** - * Try destroying the given tracing session, fail silently if there is no - * session. - * - * @param sessionName - * The name of the session to destroy. Use null for the current - * session. - */ - public static void tryDestroySession(String sessionName) { - getOutputFromCommand(false, new String[] { "lttng", "destroy" }); - } - - /** - * Outside of the scope of lttng-tools, but this utility method can be used - * to delete all traces currently under ~/lttng-traces/. This can be used by - * tests to cleanup a trace they have created. - * - * @return True if the command completes successfully, false if there was an - * error. - */ - public static boolean deleteAllTracee() { - String tracesDir = new String(System.getProperty("user.home") + "/lttng-traces/"); - return deleteDirectory(Paths.get(tracesDir)); - } - - private static boolean deleteDirectory(Path directory) { - try { - Files.walkFileTree(directory, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } catch (IOException e) { - /* At least we tried... */ - return false; - } - return true; - } - - // ------------------------------------------------------------------------ - // Private helper methods - // ------------------------------------------------------------------------ - - private static boolean executeCommands(String [][] commands) { - for (String[] command : commands) { - if (executeCommand(command) == false) { - return false; - } - } - return true; - } - - /** - * Just to test the environment / stdout are working correctly - */ - public static void main(String[] args) { - executeCommand(new String[] {"ls", "-l"}); - } - - private static boolean executeCommand(String[] command) { - try { - ProcessBuilder builder = new ProcessBuilder(command); - builder.redirectErrorStream(true); - builder.redirectOutput(Redirect.INHERIT); - - Process p = builder.start(); - int ret = p.waitFor(); - return (ret == 0); - - } catch (IOException | InterruptedException e) { - return false; - } - } - - private static List getOutputFromCommand(String[] command) { - return getOutputFromCommand(true, command); - } - - private static List getOutputFromCommand(boolean print, String[] command) { - try { - Path tempFile = Files.createTempFile("test-output", null); - - ProcessBuilder builder = new ProcessBuilder(command); - builder.redirectErrorStream(true); - builder.redirectOutput(Redirect.to(tempFile.toFile())); - - Process p = builder.start(); - p.waitFor(); - - List lines = Files.readAllLines(tempFile); - Files.delete(tempFile); - - if (print) { - /* Also print the output to the console */ - lines.stream().forEach(s -> System.out.println(s)); - } - - return lines; - - } catch (IOException | InterruptedException e) { - return null; - } - } -} diff --git a/src/org/lttng/ust/agent/utils/TestUtils.java b/src/org/lttng/ust/agent/utils/TestUtils.java new file mode 100644 index 0000000..7302e02 --- /dev/null +++ b/src/org/lttng/ust/agent/utils/TestUtils.java @@ -0,0 +1,127 @@ +package org.lttng.ust.agent.utils; + +import java.io.IOException; +import java.lang.ProcessBuilder.Redirect; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; + +import org.lttng.ust.agent.jul.LttngLogHandler; +import org.lttng.ust.agent.log4j.LttngLogAppender; +import org.lttng.ust.agent.utils.LttngSession.Domain; + +/** + * Utility methods to help with UST-Java tests + */ +public final class TestUtils { + + private TestUtils() {} + + /** + * Check the the JUL native library is available, effectively allowing LTTng + * JUL handlers to be used. + * + * @return True if JUL works fine, false if it does not. + */ + public static boolean checkForJulLibrary() { + try { + LttngLogHandler testHandler = new LttngLogHandler(); + testHandler.close(); + } catch (SecurityException | IOException e) { + return false; + } + return true; + } + + /** + * Check the the Log4j native library is available, effectively allowing + * LTTng Log4j appenders to be used. + * + * @return True if Log4j works fine, false if it does not. + */ + public static boolean checkForLog4jLibrary() { + try { + LttngLogAppender testAppender = new LttngLogAppender(); + testAppender.close(); + } catch (SecurityException | IOException e) { + return false; + } + return true; + } + + /** + * Check that lttng-tools and babeltrace are installed on the system and + * working. + * + * @param domain + * The tracing domain to test for (we will try to setup a session + * with this domain) + * @return True if the environment should allow tracing fine, false if there + * was an error + */ + public static boolean checkForLttngTools(Domain domain) { + try (LttngSession session = new LttngSession(null, domain)) { + boolean ret1 = session.enableAllEvents(); + boolean ret2 = session.start(); + boolean ret3 = session.stop(); + /* + * "lttng view" also tests that Babeltrace is installed and working + */ + List contents = session.view(); + return (ret1 && ret2 && ret3 && contents.isEmpty()); + } + } + + public static boolean checkForUserSessiond() { + String userName = System.getProperty("user.name"); + + /* The user name is truncated to 7 characters in "ps" */ + String shortUserName = userName.substring(0, Math.min(userName.length(), 7)); + + List command = Arrays.asList("ps", "-e", "u"); + List output = getOutputFromCommand(false, command); + return output.stream() + .filter(s -> s.contains("lttng-sessiond")) + .anyMatch(s -> s.startsWith(shortUserName)); + } + + public static boolean checkForRootSessiond() { + List command = Arrays.asList("ps", "-e", "u"); + List output = getOutputFromCommand(false, command); + return output.stream() + .filter(s -> s.contains("lttng-sessiond")) + .anyMatch(s -> s.startsWith("root")); + } + + + static List getOutputFromCommand(List command) { + return TestUtils.getOutputFromCommand(true, command); + } + + static List getOutputFromCommand(boolean print, List command) { + try { + Path tempFile = Files.createTempFile("test-output", null); + + ProcessBuilder builder = new ProcessBuilder(command); + builder.redirectErrorStream(true); + builder.redirectOutput(Redirect.to(tempFile.toFile())); + + Process p = builder.start(); + p.waitFor(); + + List lines = Files.readAllLines(tempFile); + Files.delete(tempFile); + + if (print) { + /* Also print the output to the console */ + lines.stream().forEach(s -> System.out.println(s)); + } + + return lines; + + } catch (IOException | InterruptedException e) { + return null; + } + } +} -- 2.34.1