Add 'log4j2' domain tests to the Log4j 2.x agent
[lttng-ust-java-tests.git] / lttng-ust-java-tests-common / src / test / java / org / lttng / ust / agent / integration / client / TcpClientIT.java
index 2794dfa58411f96afcc98496756b1c9d6f82230e..6313a6fd34134d1be05fe67023b8132acb6ab8cf 100644 (file)
 
 package org.lttng.ust.agent.integration.client;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assume.assumeTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
 import org.lttng.tools.ILttngSession;
 import org.lttng.tools.LttngToolsHelper;
 import org.lttng.ust.agent.ILttngAgent;
@@ -42,7 +42,7 @@ 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;
+import org.lttng.ust.agent.utils.TestPrintExtension;
 
 /**
  * Tests for the TCP client only, without using an agent.
@@ -53,7 +53,7 @@ import org.lttng.ust.agent.utils.TestPrintRunner;
  *
  * @author Alexandre Montplaisir
  */
-@RunWith(TestPrintRunner.class)
+@ExtendWith(TestPrintExtension.class)
 public class TcpClientIT {
 
     // ------------------------------------------------------------------------
@@ -64,15 +64,21 @@ public class TcpClientIT {
     private static final String EVENT_NAME_B = "eventB";
     private static final String EVENT_NAME_C = "eventC";
 
+    private static final String CONTEXT_RETRIEVER_NAME_A = "retrieverA";
+    private static final String CONTEXT_RETRIEVER_NAME_B = "retrieverB";
+    private static final String CONTEXT_NAME_A = "contextA";
+    private static final String CONTEXT_NAME_B = "contextB";
+
     /* Test configuration */
     private static final int DOMAIN_VALUE = ILttngAgent.Domain.JUL.value();
     private static final ILttngSession.Domain SESSION_DOMAIN = ILttngSession.Domain.JUL;
-    private static final boolean ROOT_SESSIOND = true;
 
     private static TcpClientDebugListener clientListener;
     private static LttngTcpSessiondClient client;
     private static Thread clientThread;
 
+    private static EventRuleFactory eventRuleFactory = new EventRuleFactory(SESSION_DOMAIN);
+
     private ILttngSession session;
 
     // ------------------------------------------------------------------------
@@ -82,23 +88,40 @@ public class TcpClientIT {
     /**
      * Class setup
      */
-    @BeforeClass
+    @BeforeAll
     public static void setupClass() {
         LttngToolsHelper.destroyAllSessions();
 
         clientListener = new TcpClientDebugListener();
-        client = new LttngTcpSessiondClient(clientListener, DOMAIN_VALUE, ROOT_SESSIOND);
 
+        /* Try connecting to a root sessiond first */
+        client = new LttngTcpSessiondClient(clientListener, DOMAIN_VALUE, true);
+        clientThread = new Thread(client);
+        clientThread.start();
+
+        if (client.waitForConnection(5)) {
+            return;
+        }
+
+        /* Connection was not established, try a user sessiond instead */
+        client.close();
+        try {
+            clientThread.join();
+        } catch (InterruptedException e) {
+            fail(e.getMessage());
+        }
+
+        client = new LttngTcpSessiondClient(clientListener, DOMAIN_VALUE, false);
         clientThread = new Thread(client);
         clientThread.start();
 
-        assumeTrue("Timed out waiting for root sessiond", client.waitForConnection(5));
+        assertTrue(client.waitForConnection(5), "Timed out waiting for a sessiond");
     }
 
     /**
      * Class teardown
      */
-    @AfterClass
+    @AfterAll
     public static void teardownClass() {
         if (client != null) {
             client.close();
@@ -114,7 +137,7 @@ public class TcpClientIT {
     /**
      * Test setup
      */
-    @Before
+    @BeforeEach
     public void setup() {
         session = ILttngSession.createSession(null, SESSION_DOMAIN);
         clientListener.clearAllCommands();
@@ -123,7 +146,7 @@ public class TcpClientIT {
     /**
      * Test teardown
      */
-    @After
+    @AfterEach
     public void teardown() {
         session.close();
     }
@@ -147,7 +170,7 @@ public class TcpClientIT {
     }
 
     // ------------------------------------------------------------------------
-    // Test cases
+    // Event enabling/disabling test cases
     // ------------------------------------------------------------------------
 
     /**
@@ -158,7 +181,7 @@ public class TcpClientIT {
         session.enableEvent(EVENT_NAME_A, null, false, null);
 
         List<EventRule> expectedCommands = Collections.singletonList(
-                EventRuleFactory.createRule(EVENT_NAME_A));
+                eventRuleFactory.createRule(EVENT_NAME_A));
 
         List<EventRule> actualCommands = clientListener.getEnabledEventCommands();
         assertEquals(expectedCommands, actualCommands);
@@ -172,7 +195,7 @@ public class TcpClientIT {
         session.enableAllEvents();
 
         List<EventRule> expectedCommands = Collections.singletonList(
-                EventRuleFactory.createRuleAllEvents());
+                eventRuleFactory.createRuleAllEvents());
         List<EventRule> actualCommands = clientListener.getEnabledEventCommands();
 
         assertEquals(expectedCommands, actualCommands);
@@ -187,7 +210,7 @@ public class TcpClientIT {
         session.disableEvents(EVENT_NAME_A);
 
         List<EventRule> expectedEnableCommands = Collections.singletonList(
-                EventRuleFactory.createRule(EVENT_NAME_A));
+                eventRuleFactory.createRule(EVENT_NAME_A));
         List<String> expectedDisableCommands = Collections.singletonList(EVENT_NAME_A);
 
         assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands());
@@ -205,9 +228,9 @@ public class TcpClientIT {
         session.disableAllEvents();
 
         List<EventRule> expectedEnableCommands = Arrays.asList(
-                EventRuleFactory.createRule(EVENT_NAME_A),
-                EventRuleFactory.createRule(EVENT_NAME_B),
-                EventRuleFactory.createRule(EVENT_NAME_C));
+                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,13 +250,32 @@ public class TcpClientIT {
         session.enableAllEvents();
         session.disableAllEvents();
 
-        List<EventRule> expectedEnableCommands = Arrays.asList(EventRuleFactory.createRuleAllEvents());
+        List<EventRule> expectedEnableCommands = Arrays.asList(eventRuleFactory.createRuleAllEvents());
         List<String> expectedDisableCommands = Arrays.asList(EventRuleFactory.EVENT_NAME_ALL);
 
         assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands());
         assertTrue(containSameElements(expectedDisableCommands, clientListener.getDisabledEventCommands()));
     }
 
+    /**
+     * Test enabling then destroying the session (should send corresponding
+     * disable event messages).
+     */
+    @SuppressWarnings("static-method")
+    @Test
+    public void testEnableEventThenDestroy() {
+        try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) {
+            session2.enableEvent(EVENT_NAME_A, null, false, null);
+            session2.enableEvent(EVENT_NAME_B, null, false, null);
+        } // close(), aka destroy the session, sending "disable event" messages
+
+        List<EventRule> expectedEnabledCommands = Arrays.asList(eventRuleFactory.createRule(EVENT_NAME_A), eventRuleFactory.createRule(EVENT_NAME_B));
+        List<String> expectedDisabledCommands = Arrays.asList(EVENT_NAME_A, EVENT_NAME_B);
+
+        assertEquals(expectedEnabledCommands, clientListener.getEnabledEventCommands());
+        assertTrue(clientListener.getDisabledEventCommands().containsAll(expectedDisabledCommands));
+    }
+
     /**
      * Test specifying an event with a --loglevel option.
      */
@@ -244,7 +286,7 @@ public class TcpClientIT {
         session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null);
 
         List<EventRule> expectedCommands = Collections.singletonList(
-                EventRuleFactory.createRule(EVENT_NAME_A, lls));
+                eventRuleFactory.createRule(EVENT_NAME_A, lls));
         List<EventRule> actualCommands = clientListener.getEnabledEventCommands();
 
         assertEquals(expectedCommands, actualCommands);
@@ -260,7 +302,7 @@ public class TcpClientIT {
         session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null);
 
         List<EventRule> expectedCommands = Collections.singletonList(
-                EventRuleFactory.createRule(EVENT_NAME_A, lls));
+                eventRuleFactory.createRule(EVENT_NAME_A, lls));
         List<EventRule> actualCommands = clientListener.getEnabledEventCommands();
 
         assertEquals(expectedCommands, actualCommands);
@@ -278,8 +320,8 @@ public class TcpClientIT {
         session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null);
 
         List<EventRule> expectedCommands = Arrays.asList(
-                EventRuleFactory.createRule(EVENT_NAME_A, lls1),
-                EventRuleFactory.createRule(EVENT_NAME_A, lls2)
+                eventRuleFactory.createRule(EVENT_NAME_A, lls1),
+                eventRuleFactory.createRule(EVENT_NAME_A, lls2)
                 );
         List<EventRule> actualCommands = clientListener.getEnabledEventCommands();
 
@@ -298,8 +340,8 @@ public class TcpClientIT {
         session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null);
 
         List<EventRule> expectedCommands = Arrays.asList(
-                EventRuleFactory.createRule(EVENT_NAME_A, lls1),
-                EventRuleFactory.createRule(EVENT_NAME_A, lls2)
+                eventRuleFactory.createRule(EVENT_NAME_A, lls1),
+                eventRuleFactory.createRule(EVENT_NAME_A, lls2)
                 );
         List<EventRule> actualCommands = clientListener.getEnabledEventCommands();
 
@@ -321,8 +363,8 @@ public class TcpClientIT {
             session2.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null);
 
             List<EventRule> expectedCommands = Arrays.asList(
-                    EventRuleFactory.createRule(EVENT_NAME_A, lls1),
-                    EventRuleFactory.createRule(EVENT_NAME_A, lls2));
+                    eventRuleFactory.createRule(EVENT_NAME_A, lls1),
+                    eventRuleFactory.createRule(EVENT_NAME_A, lls2));
             List<EventRule> actualCommands = clientListener.getEnabledEventCommands();
 
             assertEquals(expectedCommands, actualCommands);
@@ -342,9 +384,9 @@ public class TcpClientIT {
         session.enableEvent(EVENT_NAME_A, null, false, filter2);
 
         List<EventRule> 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));
+                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<EventRule> actualCommands = clientListener.getEnabledEventCommands();
 
         assertEquals(expectedCommands, actualCommands);
@@ -365,12 +407,186 @@ public class TcpClientIT {
         session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, filter);
 
         List<EventRule> 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));
+                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<EventRule> actualCommands = clientListener.getEnabledEventCommands();
 
         assertEquals(expectedCommands, actualCommands);
     }
+
+    // ------------------------------------------------------------------------
+    // Application context enabling/disabling test cases
+    // ------------------------------------------------------------------------
+
+    /**
+     * Test enabling one application context.
+     */
+    @Test
+    public void testEnableAppContext() {
+        session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A);
+
+        List<String> expectedCommands = Collections.singletonList(
+                CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+
+        List<String> actualCommands = clientListener.getEnabledAppContextCommands();
+        assertEquals(expectedCommands, actualCommands);
+    }
+
+    /**
+     * Test enabling two application contexts sharing the same retriever name.
+     */
+    @Test
+    public void testEnableAppContextsSameRetriever() {
+        session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A);
+        session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_B);
+
+        List<String> expectedCommands = Arrays.asList(
+                CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A,
+                CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_B);
+
+        List<String> actualCommands = clientListener.getEnabledAppContextCommands();
+        assertEquals(expectedCommands, actualCommands);
+    }
+
+    /**
+     * Test enabling two application contexts sharing the same context name, but
+     * with different retrievers. Unusual, but they should still be recognized
+     * separately.
+     */
+    @Test
+    public void testEnableAppContextsSameContext() {
+        session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A);
+        session.enableAppContext(CONTEXT_RETRIEVER_NAME_B, CONTEXT_NAME_A);
+
+        List<String> expectedCommands = Arrays.asList(
+                CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A,
+                CONTEXT_RETRIEVER_NAME_B + ':' + CONTEXT_NAME_A);
+
+        List<String> actualCommands = clientListener.getEnabledAppContextCommands();
+        assertEquals(expectedCommands, actualCommands);
+    }
+
+    /**
+     * Test enabling one application context, then destroying the session. We
+     * should receive the corresponding "context removed" message.
+     */
+    @Test
+    @SuppressWarnings("static-method")
+    public void testEnableAppContextThenDestroy() {
+        try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) {
+            session2.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A);
+        } // close(), aka destroy the session, sending "disable context" messages
+
+        List<String> expectedEnabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+        List<String> expectedDisabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+        List<String> actualEnabledCommands = clientListener.getEnabledAppContextCommands();
+        List<String> actualDisabledCommands = clientListener.getDisabledAppContextCommands();
+
+        assertEquals(expectedEnabledCommands, actualEnabledCommands);
+        assertEquals(expectedDisabledCommands, actualDisabledCommands);
+    }
+
+    /**
+     * Test enabling the same application context in two different sessions.
+     * Upon destroying one, we should only receive one "destroyed" message.
+     */
+    @Test
+    public void testEnableSameAppContextTwoSessions() {
+        List<String> expectedEnabledCommands = Arrays.asList(
+                CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A,
+                CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+        List<String> actualEnabledCommands;
+
+        try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) {
+            session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A);
+            session2.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A);
+
+            actualEnabledCommands = clientListener.getEnabledAppContextCommands();
+            assertEquals(expectedEnabledCommands, actualEnabledCommands);
+        } // close/destroy session2
+
+        actualEnabledCommands = clientListener.getEnabledAppContextCommands();
+        assertEquals(expectedEnabledCommands, actualEnabledCommands);
+
+        List<String> expectedDisabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+        List<String> actualDisabledCommands = clientListener.getDisabledAppContextCommands();
+
+        assertEquals(expectedDisabledCommands, actualDisabledCommands);
+    }
+
+    /**
+     * Test enabling two different application context in two different
+     * sessions. Upon destroying one, we should receive the correct "destroyed"
+     * message.
+     */
+    @Test
+    public void testEnableDiffAppContextTwoSessions() {
+        List<String> expectedEnabledCommands = Arrays.asList(
+                CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A,
+                CONTEXT_RETRIEVER_NAME_B + ':' + CONTEXT_NAME_B);
+        List<String> actualEnabledCommands;
+
+        try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) {
+            session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A);
+            session2.enableAppContext(CONTEXT_RETRIEVER_NAME_B, CONTEXT_NAME_B);
+
+            actualEnabledCommands = clientListener.getEnabledAppContextCommands();
+            assertEquals(expectedEnabledCommands, actualEnabledCommands);
+        } // close/destroy session2
+
+        actualEnabledCommands = clientListener.getEnabledAppContextCommands();
+        assertEquals(expectedEnabledCommands, actualEnabledCommands);
+
+        List<String> expectedDisabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_B + ':' + CONTEXT_NAME_B);
+        List<String> actualDisabledCommands = clientListener.getDisabledAppContextCommands();
+
+        assertEquals(expectedDisabledCommands, actualDisabledCommands);
+    }
+
+    // ------------------------------------------------------------------------
+    // Application context filtering
+    // ------------------------------------------------------------------------
+
+    /**
+     * Test that enabling an event with a filter string referring to a context
+     * should send an agent message about this context now being "enabled".
+     *
+     * This is because we will pass the context information to UST for the
+     * filtering step, even if the actual context won't be present in the trace.
+     */
+    @SuppressWarnings("static-method")
+    @Test
+    public void testContextInFilterString() {
+        try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) {
+            session2.enableEvent(EVENT_NAME_A, null, false, "$app." + CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A + "==\"bozo\"");
+
+            List<String> expectedEnabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+            assertEquals(expectedEnabledCommands, clientListener.getEnabledAppContextCommands());
+        } // close(), aka destroy the session, sending "disable context" messages
+
+        List<String> expectedDisabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+        assertEquals(expectedDisabledCommands, clientListener.getDisabledAppContextCommands());
+    }
+
+    /**
+     * Test that if we the context is both referred to by a filter string *and*
+     * enabled directly, we receive *2* messages about this context being
+     * enabled (and disabled on session teardown).
+     */
+    @SuppressWarnings("static-method")
+    @Test
+    public void testContextEnabledAndInFilterString() {
+        try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) {
+            session2.enableEvent(EVENT_NAME_A, null, false, "$app." + CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A + "==\"bozo\"");
+            session2.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A);
+
+            List<String> expectedEnabledCommands = Collections.nCopies(2, CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+            assertEquals(expectedEnabledCommands, clientListener.getEnabledAppContextCommands());
+        } // close(), aka destroy the session, sending "disable context" messages
+
+        List<String> expectedDisabledCommands = Collections.nCopies(2, CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A);
+        assertEquals(expectedDisabledCommands, clientListener.getDisabledAppContextCommands());
+    }
 }
This page took 0.02964 seconds and 4 git commands to generate.