| 1 | /* |
| 2 | * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.ust.agent.integration.client; |
| 20 | |
| 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | import static org.junit.Assume.assumeTrue; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import org.junit.After; |
| 31 | import org.junit.AfterClass; |
| 32 | import org.junit.Before; |
| 33 | import org.junit.BeforeClass; |
| 34 | import org.junit.Ignore; |
| 35 | import org.junit.Test; |
| 36 | import org.lttng.tools.ILttngSession; |
| 37 | import org.lttng.tools.LttngToolsHelper; |
| 38 | import org.lttng.ust.agent.ILttngAgent; |
| 39 | import org.lttng.ust.agent.client.LttngTcpSessiondClient; |
| 40 | import org.lttng.ust.agent.session.EventRule; |
| 41 | import org.lttng.ust.agent.session.LogLevelFilter; |
| 42 | import org.lttng.ust.agent.session.LogLevelFilter.LogLevelType; |
| 43 | import org.lttng.ust.agent.utils.ILogLevelStrings; |
| 44 | |
| 45 | /** |
| 46 | * Tests for the TCP client only, without using an agent. |
| 47 | * |
| 48 | * This test suite requires that a *root* session daemon is running on the |
| 49 | * system. Since we have to explicitly tell the TCP client which sessiond to |
| 50 | * connect to, we have to hard-code it in here. |
| 51 | * |
| 52 | * @author Alexandre Montplaisir |
| 53 | */ |
| 54 | public class TcpClientIT { |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Attributes |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
| 60 | private static final LogLevelFilter LOG_LEVEL_UNSPECIFIED = new LogLevelFilter(Integer.MIN_VALUE, 0); |
| 61 | |
| 62 | private static final String EVENT_NAME_A = "eventA"; |
| 63 | private static final String EVENT_NAME_B = "eventB"; |
| 64 | private static final String EVENT_NAME_C = "eventC"; |
| 65 | private static final String EVENT_NAME_ALL = "*"; |
| 66 | |
| 67 | /* Test configuration */ |
| 68 | private static final int DOMAIN_VALUE = ILttngAgent.Domain.JUL.value(); |
| 69 | private static final ILttngSession.Domain SESSION_DOMAIN = ILttngSession.Domain.JUL; |
| 70 | private static final boolean ROOT_SESSIOND = true; |
| 71 | |
| 72 | private static TcpClientDebugListener clientListener; |
| 73 | private static LttngTcpSessiondClient client; |
| 74 | private static Thread clientThread; |
| 75 | |
| 76 | private ILttngSession session; |
| 77 | |
| 78 | // ------------------------------------------------------------------------ |
| 79 | // Maintenance |
| 80 | // ------------------------------------------------------------------------ |
| 81 | |
| 82 | /** |
| 83 | * Class setup |
| 84 | */ |
| 85 | @BeforeClass |
| 86 | public static void setupClass() { |
| 87 | LttngToolsHelper.destroyAllSessions(); |
| 88 | |
| 89 | clientListener = new TcpClientDebugListener(); |
| 90 | client = new LttngTcpSessiondClient(clientListener, DOMAIN_VALUE, ROOT_SESSIOND); |
| 91 | |
| 92 | clientThread = new Thread(client); |
| 93 | clientThread.start(); |
| 94 | |
| 95 | assumeTrue("Timed out waiting for root sessiond", client.waitForConnection(5)); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Class teardown |
| 100 | */ |
| 101 | @AfterClass |
| 102 | public static void teardownClass() { |
| 103 | if (client != null) { |
| 104 | client.close(); |
| 105 | } |
| 106 | if (clientThread != null) { |
| 107 | try { |
| 108 | clientThread.join(); |
| 109 | } catch (InterruptedException e) { |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Test setup |
| 116 | */ |
| 117 | @Before |
| 118 | public void setup() { |
| 119 | session = ILttngSession.createSession(null, SESSION_DOMAIN); |
| 120 | clientListener.clearAllCommands(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Test teardown |
| 125 | */ |
| 126 | @After |
| 127 | public void teardown() { |
| 128 | session.close(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | private static ILogLevelStrings getLogLevelStrings() { |
| 133 | return ILogLevelStrings.JUL_LOGLEVEL_STRINGS; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Check that two lists contain the exact same element (including |
| 138 | * duplicates), but their order does not matter. |
| 139 | */ |
| 140 | private static <T extends Comparable<T>> boolean containSameElements(List<T> list1, List<T> list2) { |
| 141 | List<T> newlist1 = new ArrayList<>(list1); |
| 142 | List<T> newlist2 = new ArrayList<>(list2); |
| 143 | Collections.sort(newlist1); |
| 144 | Collections.sort(newlist2); |
| 145 | return (newlist1.equals(newlist2)); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | // ------------------------------------------------------------------------ |
| 150 | // Test cases |
| 151 | // ------------------------------------------------------------------------ |
| 152 | |
| 153 | /** |
| 154 | * Test enabling one event. |
| 155 | */ |
| 156 | @Test |
| 157 | public void testEnableEvent() { |
| 158 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 159 | |
| 160 | List<EventRule> expectedCommands = Collections.singletonList( |
| 161 | new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null)); |
| 162 | |
| 163 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 164 | assertEquals(expectedCommands, actualCommands); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Test an "enable-event -a" command. |
| 169 | */ |
| 170 | @Test |
| 171 | public void testEnableAllEvents() { |
| 172 | session.enableAllEvents(); |
| 173 | |
| 174 | List<EventRule> expectedCommands = Collections.singletonList( |
| 175 | new EventRule(EVENT_NAME_ALL, LOG_LEVEL_UNSPECIFIED, null)); |
| 176 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 177 | |
| 178 | assertEquals(expectedCommands, actualCommands); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Test enabling then disabling one event. |
| 183 | */ |
| 184 | @Test |
| 185 | public void testEnableThenDisableOneEvent() { |
| 186 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 187 | session.disableEvents(EVENT_NAME_A); |
| 188 | |
| 189 | List<EventRule> expectedEnableCommands = Collections.singletonList( |
| 190 | new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null)); |
| 191 | List<String> expectedDisableCommands = Collections.singletonList(EVENT_NAME_A); |
| 192 | |
| 193 | assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands()); |
| 194 | assertTrue(containSameElements(expectedDisableCommands, clientListener.getDisabledEventCommands())); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Test enabling some events manually, then disabling all events (-a). |
| 199 | */ |
| 200 | @Test |
| 201 | public void testEnableSomeThenDisableAll() { |
| 202 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 203 | session.enableEvent(EVENT_NAME_B, null, false, null); |
| 204 | session.enableEvent(EVENT_NAME_C, null, false, null); |
| 205 | session.disableAllEvents(); |
| 206 | |
| 207 | List<EventRule> expectedEnableCommands = Arrays.asList( |
| 208 | new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null), |
| 209 | new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null), |
| 210 | new EventRule(EVENT_NAME_C, LOG_LEVEL_UNSPECIFIED, null)); |
| 211 | /* |
| 212 | * A "disable-event -a" will send one command for each enabled event. |
| 213 | * The order may be different though. |
| 214 | */ |
| 215 | List<String> expectedDisableCommands = Arrays.asList( |
| 216 | EVENT_NAME_A, EVENT_NAME_B, EVENT_NAME_C); |
| 217 | |
| 218 | assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands()); |
| 219 | assertTrue(containSameElements(expectedDisableCommands, clientListener.getDisabledEventCommands())); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Test enabling then (enable-event -a) then disabling all (disable-event -a) events. |
| 224 | */ |
| 225 | @Test |
| 226 | public void testEnableAllThenDisableAll() { |
| 227 | session.enableAllEvents(); |
| 228 | session.disableAllEvents(); |
| 229 | |
| 230 | List<EventRule> expectedEnableCommands = Arrays.asList( |
| 231 | new EventRule(EVENT_NAME_ALL, LOG_LEVEL_UNSPECIFIED, null)); |
| 232 | List<String> expectedDisableCommands = Arrays.asList( |
| 233 | EVENT_NAME_ALL); |
| 234 | |
| 235 | assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands()); |
| 236 | assertTrue(containSameElements(expectedDisableCommands, clientListener.getDisabledEventCommands())); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Test specifying an event with a --loglevel option. |
| 241 | */ |
| 242 | @Test |
| 243 | public void testEnableEventLogLevelRange() { |
| 244 | LogLevelFilter llf = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 245 | |
| 246 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 247 | |
| 248 | List<EventRule> expectedCommands = Collections.singletonList( |
| 249 | new EventRule(EVENT_NAME_A, llf, null)); |
| 250 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 251 | |
| 252 | assertEquals(expectedCommands, actualCommands); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Test enabling an event with a --loglevel-only option. |
| 257 | */ |
| 258 | @Test |
| 259 | public void testEnableEventLogLevelSingle() { |
| 260 | LogLevelFilter llf = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); |
| 261 | |
| 262 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); |
| 263 | |
| 264 | List<EventRule> expectedCommands = Collections.singletonList( |
| 265 | new EventRule(EVENT_NAME_A, llf, null)); |
| 266 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 267 | |
| 268 | assertEquals(expectedCommands, actualCommands); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Test enabling an event twice, for the same loglevel, with --loglevel followed by --loglevel-only. |
| 273 | */ |
| 274 | @Ignore("See http://bugs.lttng.org/issues/913") |
| 275 | @Test |
| 276 | public void testEnableEventsLogLevelRangeAndSingle() { |
| 277 | LogLevelFilter llf1 = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 278 | LogLevelFilter llf2 = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); |
| 279 | |
| 280 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 281 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); |
| 282 | |
| 283 | List<EventRule> expectedCommands = Arrays.asList( |
| 284 | new EventRule(EVENT_NAME_A, llf1, null), |
| 285 | new EventRule(EVENT_NAME_A, llf2, null) |
| 286 | ); |
| 287 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 288 | |
| 289 | assertEquals(expectedCommands, actualCommands); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Test enabling an event twice, for the same loglevel, with --loglevel--only followed by --loglevel. |
| 294 | */ |
| 295 | @Ignore("See http://bugs.lttng.org/issues/913") |
| 296 | @Test |
| 297 | public void testEnableEventsLogLevelSingleAndRange() { |
| 298 | LogLevelFilter llf1 = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); |
| 299 | LogLevelFilter llf2 = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 300 | |
| 301 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); |
| 302 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 303 | |
| 304 | List<EventRule> expectedCommands = Arrays.asList( |
| 305 | new EventRule(EVENT_NAME_A, llf1, null), |
| 306 | new EventRule(EVENT_NAME_A, llf2, null) |
| 307 | ); |
| 308 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 309 | |
| 310 | assertEquals(expectedCommands, actualCommands); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Test enabling the same event, same loglevel, but different loglevel types |
| 315 | * (--loglevel vs --loglevel-only) in two separate sessions. |
| 316 | */ |
| 317 | @Test |
| 318 | public void testEnableEventsLogLevelRangeAndSingleDiffSessions() { |
| 319 | try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) { |
| 320 | |
| 321 | LogLevelFilter llf1 = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 322 | LogLevelFilter llf2 = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); |
| 323 | |
| 324 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 325 | session2.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); |
| 326 | |
| 327 | List<EventRule> expectedCommands = Arrays.asList(new EventRule(EVENT_NAME_A, llf1, null), |
| 328 | new EventRule(EVENT_NAME_A, llf2, null)); |
| 329 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 330 | |
| 331 | assertEquals(expectedCommands, actualCommands); |
| 332 | } |
| 333 | } |
| 334 | } |