Search in sources :

Example 6 with TestLogging

use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.

the class RemoteClusterClientTests method testEnsureWeReconnect.

@TestLogging(value = "org.opensearch.transport.SniffConnectionStrategy:TRACE,org.opensearch.transport.ClusterConnectionManager:TRACE", reason = "debug intermittent test failure")
public void testEnsureWeReconnect() throws Exception {
    Settings remoteSettings = Settings.builder().put(ClusterName.CLUSTER_NAME_SETTING.getKey(), "foo_bar_cluster").build();
    try (MockTransportService remoteTransport = startTransport("remote_node", Collections.emptyList(), Version.CURRENT, threadPool, remoteSettings)) {
        DiscoveryNode remoteNode = remoteTransport.getLocalDiscoNode();
        Settings localSettings = Settings.builder().put(onlyRole(DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE)).put("cluster.remote.test.seeds", remoteNode.getAddress().getAddress() + ":" + remoteNode.getAddress().getPort()).build();
        try (MockTransportService service = MockTransportService.createNewService(localSettings, Version.CURRENT, threadPool, null)) {
            service.start();
            // this test is not perfect since we might reconnect concurrently but it will fail most of the time if we don't have
            // the right calls in place in the RemoteAwareClient
            service.acceptIncomingRequests();
            RemoteClusterService remoteClusterService = service.getRemoteClusterService();
            assertBusy(() -> assertTrue(remoteClusterService.isRemoteNodeConnected("test", remoteNode)));
            for (int i = 0; i < 10; i++) {
                RemoteClusterConnection remoteClusterConnection = remoteClusterService.getRemoteClusterConnection("test");
                assertBusy(remoteClusterConnection::assertNoRunningConnections);
                ConnectionManager connectionManager = remoteClusterConnection.getConnectionManager();
                Transport.Connection connection = connectionManager.getConnection(remoteNode);
                PlainActionFuture<Void> closeFuture = PlainActionFuture.newFuture();
                connection.addCloseListener(closeFuture);
                connectionManager.disconnectFromNode(remoteNode);
                closeFuture.get();
                Client client = remoteClusterService.getRemoteClusterClient(threadPool, "test");
                ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().execute().get();
                assertNotNull(clusterStateResponse);
                assertEquals("foo_bar_cluster", clusterStateResponse.getState().getClusterName().value());
                assertTrue(remoteClusterConnection.isNodeConnected(remoteNode));
            }
        }
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) RemoteClusterConnectionTests.startTransport(org.opensearch.transport.RemoteClusterConnectionTests.startTransport) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) TestLogging(org.opensearch.test.junit.annotations.TestLogging)

Example 7 with TestLogging

use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.

the class AbstractSimpleTransportTestCase method testTracerLog.

@TestLogging(value = "org.opensearch.transport.TransportService.tracer:trace", reason = "to ensure we log network events on TRACE level")
public void testTracerLog() throws Exception {
    TransportRequestHandler<TransportRequest> handler = (request, channel, task) -> channel.sendResponse(new StringMessageResponse(""));
    TransportRequestHandler<StringMessageRequest> handlerWithError = (request, channel, task) -> {
        if (request.timeout() > 0) {
            Thread.sleep(request.timeout);
        }
        channel.sendResponse(new RuntimeException(""));
    };
    TransportResponseHandler<StringMessageResponse> noopResponseHandler = new TransportResponseHandler<StringMessageResponse>() {

        @Override
        public StringMessageResponse read(StreamInput in) throws IOException {
            return new StringMessageResponse(in);
        }

        @Override
        public void handleResponse(StringMessageResponse response) {
        }

        @Override
        public void handleException(TransportException exp) {
        }

        @Override
        public String executor() {
            return ThreadPool.Names.SAME;
        }
    };
    serviceA.registerRequestHandler("internal:test", ThreadPool.Names.SAME, StringMessageRequest::new, handler);
    serviceA.registerRequestHandler("internal:testNotSeen", ThreadPool.Names.SAME, StringMessageRequest::new, handler);
    serviceA.registerRequestHandler("internal:testError", ThreadPool.Names.SAME, StringMessageRequest::new, handlerWithError);
    serviceB.registerRequestHandler("internal:test", ThreadPool.Names.SAME, StringMessageRequest::new, handler);
    serviceB.registerRequestHandler("internal:testNotSeen", ThreadPool.Names.SAME, StringMessageRequest::new, handler);
    serviceB.registerRequestHandler("internal:testError", ThreadPool.Names.SAME, StringMessageRequest::new, handlerWithError);
    String includeSettings;
    String excludeSettings;
    if (randomBoolean()) {
        // sometimes leave include empty (default)
        includeSettings = randomBoolean() ? "*" : "";
        excludeSettings = "internal:testNotSeen";
    } else {
        includeSettings = "internal:test,internal:testError";
        excludeSettings = "DOESN'T_MATCH";
    }
    clusterSettingsA.applySettings(Settings.builder().put(TransportSettings.TRACE_LOG_INCLUDE_SETTING.getKey(), includeSettings).put(TransportSettings.TRACE_LOG_EXCLUDE_SETTING.getKey(), excludeSettings).build());
    final Logger logger = LogManager.getLogger("org.opensearch.transport.TransportService.tracer");
    try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
        final String requestSent = ".*\\[internal:test].*sent to.*\\{TS_B}.*";
        final MockLogAppender.LoggingExpectation requestSentExpectation = new MockLogAppender.PatternSeenEventExpectation("sent request", "org.opensearch.transport.TransportService.tracer", Level.TRACE, requestSent);
        final String requestReceived = ".*\\[internal:test].*received request.*";
        final MockLogAppender.LoggingExpectation requestReceivedExpectation = new MockLogAppender.PatternSeenEventExpectation("received request", "org.opensearch.transport.TransportService.tracer", Level.TRACE, requestReceived);
        final String responseSent = ".*\\[internal:test].*sent response.*";
        final MockLogAppender.LoggingExpectation responseSentExpectation = new MockLogAppender.PatternSeenEventExpectation("sent response", "org.opensearch.transport.TransportService.tracer", Level.TRACE, responseSent);
        final String responseReceived = ".*\\[internal:test].*received response from.*\\{TS_B}.*";
        final MockLogAppender.LoggingExpectation responseReceivedExpectation = new MockLogAppender.PatternSeenEventExpectation("received response", "org.opensearch.transport.TransportService.tracer", Level.TRACE, responseReceived);
        appender.addExpectation(requestSentExpectation);
        appender.addExpectation(requestReceivedExpectation);
        appender.addExpectation(responseSentExpectation);
        appender.addExpectation(responseReceivedExpectation);
        StringMessageRequest request = new StringMessageRequest("", 10);
        serviceA.sendRequest(nodeB, "internal:test", request, TransportRequestOptions.EMPTY, noopResponseHandler);
        assertBusy(appender::assertAllExpectationsMatched);
        final String errorResponseSent = ".*\\[internal:testError].*sent error response.*";
        final MockLogAppender.LoggingExpectation errorResponseSentExpectation = new MockLogAppender.PatternSeenEventExpectation("sent error response", "org.opensearch.transport.TransportService.tracer", Level.TRACE, errorResponseSent);
        final String errorResponseReceived = ".*\\[internal:testError].*received response from.*\\{TS_B}.*";
        final MockLogAppender.LoggingExpectation errorResponseReceivedExpectation = new MockLogAppender.PatternSeenEventExpectation("received error response", "org.opensearch.transport.TransportService.tracer", Level.TRACE, errorResponseReceived);
        appender.addExpectation(errorResponseSentExpectation);
        appender.addExpectation(errorResponseReceivedExpectation);
        serviceA.sendRequest(nodeB, "internal:testError", new StringMessageRequest(""), noopResponseHandler);
        assertBusy(appender::assertAllExpectationsMatched);
        final String notSeenSent = "*[internal:testNotSeen]*sent to*";
        final MockLogAppender.LoggingExpectation notSeenSentExpectation = new MockLogAppender.UnseenEventExpectation("not seen request sent", "org.opensearch.transport.TransportService.tracer", Level.TRACE, notSeenSent);
        final String notSeenReceived = ".*\\[internal:testNotSeen].*received request.*";
        final MockLogAppender.LoggingExpectation notSeenReceivedExpectation = new MockLogAppender.PatternSeenEventExpectation("not seen request received", "org.opensearch.transport.TransportService.tracer", Level.TRACE, notSeenReceived);
        appender.addExpectation(notSeenSentExpectation);
        appender.addExpectation(notSeenReceivedExpectation);
        PlainTransportFuture<StringMessageResponse> future = new PlainTransportFuture<>(noopResponseHandler);
        serviceA.sendRequest(nodeB, "internal:testNotSeen", new StringMessageRequest(""), future);
        future.txGet();
        assertBusy(appender::assertAllExpectationsMatched);
    }
}
Also used : Matchers.hasToString(org.hamcrest.Matchers.hasToString) Arrays(java.util.Arrays) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) StubbableTransport(org.opensearch.test.transport.StubbableTransport) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Matchers.not(org.hamcrest.Matchers.not) Level(org.apache.logging.log4j.Level) Version(org.opensearch.Version) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) OpenSearchException(org.opensearch.OpenSearchException) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) V_3_0_0(org.opensearch.transport.TransportHandshaker.V_3_0_0) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ActionListener(org.opensearch.action.ActionListener) SuppressForbidden(org.opensearch.common.SuppressForbidden) CyclicBarrier(java.util.concurrent.CyclicBarrier) TimeValue(org.opensearch.common.unit.TimeValue) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ExceptionsHelper(org.opensearch.ExceptionsHelper) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) Matchers.startsWith(org.hamcrest.Matchers.startsWith) TransportAddress(org.opensearch.common.transport.TransportAddress) UncheckedIOException(java.io.UncheckedIOException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CloseableChannel(org.opensearch.common.network.CloseableChannel) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Supplier(org.apache.logging.log4j.util.Supplier) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ActionListenerResponseHandler(org.opensearch.action.ActionListenerResponseHandler) Matchers.containsString(org.hamcrest.Matchers.containsString) NOOP_TRANSPORT_INTERCEPTOR(org.opensearch.transport.TransportService.NOOP_TRANSPORT_INTERCEPTOR) MockLogAppender(org.opensearch.test.MockLogAppender) Socket(java.net.Socket) ThreadPool(org.opensearch.threadpool.ThreadPool) NetworkUtils(org.opensearch.common.network.NetworkUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamOutput(org.opensearch.common.io.stream.StreamOutput) HashMap(java.util.HashMap) Node(org.opensearch.node.Node) MockTransportService(org.opensearch.test.transport.MockTransportService) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils(org.opensearch.test.VersionUtils) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) NetworkAddress(org.opensearch.common.network.NetworkAddress) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.empty(org.hamcrest.Matchers.empty) Setting(org.opensearch.common.settings.Setting) Collections.emptySet(java.util.Collections.emptySet) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) CollectionUtil(org.apache.lucene.util.CollectionUtil) IOUtils(org.opensearch.core.internal.io.IOUtils) TestLogging(org.opensearch.test.junit.annotations.TestLogging) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Inet6Address(java.net.Inet6Address) Constants(org.apache.lucene.util.Constants) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) MockLogAppender(org.opensearch.test.MockLogAppender) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) Logger(org.apache.logging.log4j.Logger) StreamInput(org.opensearch.common.io.stream.StreamInput) TestLogging(org.opensearch.test.junit.annotations.TestLogging)

Example 8 with TestLogging

use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.

the class LoggingListenerTests method runTestCustomLevelPerMethod.

private void runTestCustomLevelPerMethod(final Class<?> clazz) throws Exception {
    LoggingListener loggingListener = new LoggingListener();
    Description suiteDescription = Description.createSuiteDescription(clazz);
    Logger xyzLogger = LogManager.getLogger("xyz");
    Logger abcLogger = LogManager.getLogger("abc");
    final Level level = LogManager.getRootLogger().getLevel();
    assertThat(xyzLogger.getLevel(), equalTo(level));
    assertThat(abcLogger.getLevel(), equalTo(level));
    loggingListener.testRunStarted(suiteDescription);
    assertThat(xyzLogger.getLevel(), equalTo(level));
    assertThat(abcLogger.getLevel(), equalTo(level));
    Method method = clazz.getMethod("annotatedTestMethod");
    TestLogging testLogging = method.getAnnotation(TestLogging.class);
    TestIssueLogging testIssueLogging = method.getAnnotation(TestIssueLogging.class);
    Annotation[] annotations = Stream.of(testLogging, testIssueLogging).filter(Objects::nonNull).toArray(Annotation[]::new);
    Description testDescription = Description.createTestDescription(LoggingListenerTests.class, "annotatedTestMethod", annotations);
    loggingListener.testStarted(testDescription);
    assertThat(xyzLogger.getLevel(), equalTo(Level.TRACE));
    assertThat(abcLogger.getLevel(), equalTo(level));
    loggingListener.testFinished(testDescription);
    assertThat(xyzLogger.getLevel(), equalTo(level));
    assertThat(abcLogger.getLevel(), equalTo(level));
    loggingListener.testRunFinished(new Result());
    assertThat(xyzLogger.getLevel(), equalTo(level));
    assertThat(abcLogger.getLevel(), equalTo(level));
}
Also used : Description(org.junit.runner.Description) TestLogging(org.opensearch.test.junit.annotations.TestLogging) TestIssueLogging(org.opensearch.test.junit.annotations.TestIssueLogging) LoggingListener(org.opensearch.test.junit.listeners.LoggingListener) Level(org.apache.logging.log4j.Level) Method(java.lang.reflect.Method) Logger(org.apache.logging.log4j.Logger) Annotation(java.lang.annotation.Annotation) Result(org.junit.runner.Result)

Example 9 with TestLogging

use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.

the class LoggingListener method testStarted.

@Override
public void testStarted(final Description description) throws Exception {
    final TestLogging testLogging = description.getAnnotation(TestLogging.class);
    final TestIssueLogging testIssueLogging = description.getAnnotation(TestIssueLogging.class);
    previousLoggingMap = processTestLogging(testLogging, testIssueLogging);
}
Also used : TestLogging(org.opensearch.test.junit.annotations.TestLogging) TestIssueLogging(org.opensearch.test.junit.annotations.TestIssueLogging)

Example 10 with TestLogging

use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.

the class MasterServiceTests method testClusterStateUpdateLogging.

@TestLogging(value = "org.opensearch.cluster.service:TRACE", reason = "to ensure that we log cluster state events on TRACE level")
public void testClusterStateUpdateLogging() throws Exception {
    try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(MasterService.class))) {
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1 start", MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test1]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1 computation", MasterService.class.getCanonicalName(), Level.DEBUG, "took [1s] to compute cluster state update for [test1]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1 notification", MasterService.class.getCanonicalName(), Level.DEBUG, "took [0s] to notify listeners on unchanged cluster state for [test1]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2 start", MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test2]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2 failure", MasterService.class.getCanonicalName(), Level.TRACE, "failed to execute cluster state update (on version: [*], uuid: [*]) for [test2]*"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2 computation", MasterService.class.getCanonicalName(), Level.DEBUG, "took [2s] to compute cluster state update for [test2]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2 notification", MasterService.class.getCanonicalName(), Level.DEBUG, "took [0s] to notify listeners on unchanged cluster state for [test2]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3 start", MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test3]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3 computation", MasterService.class.getCanonicalName(), Level.DEBUG, "took [3s] to compute cluster state update for [test3]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3 notification", MasterService.class.getCanonicalName(), Level.DEBUG, "took [4s] to notify listeners on successful publication of cluster state (version: *, uuid: *) for [test3]"));
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test4", MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test4]"));
        try (MasterService masterService = createMasterService(true)) {
            masterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() {

                @Override
                public ClusterState execute(ClusterState currentState) {
                    relativeTimeInMillis += TimeValue.timeValueSeconds(1).millis();
                    return currentState;
                }

                @Override
                public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                }

                @Override
                public void onFailure(String source, Exception e) {
                    fail();
                }
            });
            masterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() {

                @Override
                public ClusterState execute(ClusterState currentState) {
                    relativeTimeInMillis += TimeValue.timeValueSeconds(2).millis();
                    throw new IllegalArgumentException("Testing handling of exceptions in the cluster state task");
                }

                @Override
                public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                    fail();
                }

                @Override
                public void onFailure(String source, Exception e) {
                }
            });
            masterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() {

                @Override
                public ClusterState execute(ClusterState currentState) {
                    relativeTimeInMillis += TimeValue.timeValueSeconds(3).millis();
                    return ClusterState.builder(currentState).incrementVersion().build();
                }

                @Override
                public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                    relativeTimeInMillis += TimeValue.timeValueSeconds(4).millis();
                }

                @Override
                public void onFailure(String source, Exception e) {
                    fail();
                }
            });
            masterService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() {

                @Override
                public ClusterState execute(ClusterState currentState) {
                    return currentState;
                }

                @Override
                public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                }

                @Override
                public void onFailure(String source, Exception e) {
                    fail();
                }
            });
            assertBusy(mockAppender::assertAllExpectationsMatched);
        }
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) MockLogAppender(org.opensearch.test.MockLogAppender) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) AckedClusterStateUpdateTask(org.opensearch.cluster.AckedClusterStateUpdateTask) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchException(org.opensearch.OpenSearchException) FailedToCommitClusterStateException(org.opensearch.cluster.coordination.FailedToCommitClusterStateException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) TestLogging(org.opensearch.test.junit.annotations.TestLogging)

Aggregations

TestLogging (org.opensearch.test.junit.annotations.TestLogging)19 MockLogAppender (org.opensearch.test.MockLogAppender)13 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)7 ClusterSettings (org.opensearch.common.settings.ClusterSettings)7 Logger (org.apache.logging.log4j.Logger)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 OpenSearchException (org.opensearch.OpenSearchException)5 Settings (org.opensearch.common.settings.Settings)5 ClusterState (org.opensearch.cluster.ClusterState)4 TestIssueLogging (org.opensearch.test.junit.annotations.TestIssueLogging)4 Annotation (java.lang.annotation.Annotation)3 Method (java.lang.reflect.Method)3 HashSet (java.util.HashSet)3 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Level (org.apache.logging.log4j.Level)3 InetSocketAddress (java.net.InetSocketAddress)2 UnknownHostException (java.net.UnknownHostException)2 Set (java.util.Set)2