Search in sources :

Example 1 with LoggerTestTool

use of org.apache.zookeeper.test.LoggerTestTool in project zookeeper by apache.

the class Slf4JAuditLoggerTest method setUpBeforeClass.

@BeforeAll
public static void setUpBeforeClass() throws Exception {
    System.setProperty(ZKAuditProvider.AUDIT_ENABLE, "true");
    // setup the logger to capture all logs
    LoggerTestTool loggerTestTool = new LoggerTestTool(Slf4jAuditLogger.class);
    os = loggerTestTool.getOutputStream();
    mt = startQuorum();
    zk = ClientBase.createZKClient("127.0.0.1:" + mt[0].getQuorumPeer().getClientPort());
    // Verify start audit log here itself
    String expectedAuditLog = getStartLog();
    List<String> logs = readAuditLog(os, SERVER_COUNT);
    verifyLogs(expectedAuditLog, logs);
}
Also used : LoggerTestTool(org.apache.zookeeper.test.LoggerTestTool) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with LoggerTestTool

use of org.apache.zookeeper.test.LoggerTestTool in project zookeeper by apache.

the class QuorumPeerMainTest method testInvalidMetricsProvider.

/**
 * Verify boot fails with a bad MetricsProvider
 */
@Test
public void testInvalidMetricsProvider() throws Exception {
    ClientBase.setupTestEnv();
    try (LoggerTestTool loggerTestTool = new LoggerTestTool("org.apache.zookeeper.server.quorum")) {
        ByteArrayOutputStream os = loggerTestTool.getOutputStream();
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + "metricsProvider.className=BadClass\n";
        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        q1.start();
        boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 5000);
        assertFalse(isup, "Server never came up");
        q1.shutdown();
        assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT), "waiting for server 1 down");
        LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
        String line;
        boolean found = false;
        Pattern p = Pattern.compile(".*BadClass.*");
        while ((line = r.readLine()) != null) {
            found = p.matcher(line).matches();
            if (found) {
                break;
            }
        }
        assertTrue(found, "complains about metrics provider");
    }
}
Also used : Pattern(java.util.regex.Pattern) LoggerTestTool(org.apache.zookeeper.test.LoggerTestTool) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LineNumberReader(java.io.LineNumberReader) Test(org.junit.jupiter.api.Test)

Example 3 with LoggerTestTool

use of org.apache.zookeeper.test.LoggerTestTool in project zookeeper by apache.

the class QuorumPeerMainTest method testFaultyMetricsProviderOnConfigure.

/**
 * Verify boot fails with a MetricsProvider with fails to start
 */
@Test
public void testFaultyMetricsProviderOnConfigure() throws Exception {
    ClientBase.setupTestEnv();
    try (LoggerTestTool loggerTestTool = new LoggerTestTool("org.apache.zookeeper.server.quorum")) {
        ByteArrayOutputStream os = loggerTestTool.getOutputStream();
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + "metricsProvider.className=" + BaseTestMetricsProvider.MetricsProviderWithErrorInConfigure.class.getName() + "\n";
        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        q1.start();
        boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 5000);
        assertFalse(isup, "Server never came up");
        q1.shutdown();
        assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT), "waiting for server 1 down");
        LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
        String line;
        boolean found = false;
        Pattern p = Pattern.compile(".*MetricsProviderLifeCycleException.*");
        while ((line = r.readLine()) != null) {
            found = p.matcher(line).matches();
            if (found) {
                break;
            }
        }
        assertTrue(found, "complains about metrics provider MetricsProviderLifeCycleException");
    }
}
Also used : Pattern(java.util.regex.Pattern) LoggerTestTool(org.apache.zookeeper.test.LoggerTestTool) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseTestMetricsProvider(org.apache.zookeeper.metrics.BaseTestMetricsProvider) LineNumberReader(java.io.LineNumberReader) Test(org.junit.jupiter.api.Test)

Example 4 with LoggerTestTool

use of org.apache.zookeeper.test.LoggerTestTool in project zookeeper by apache.

the class QuorumPeerMainTest method testFaultyMetricsProviderOnStop.

/**
 * Test verifies that the server shouldn't be affected but runtime errors on stop()
 */
@Test
public void testFaultyMetricsProviderOnStop() throws Exception {
    ClientBase.setupTestEnv();
    BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.reset();
    try (LoggerTestTool loggerTestTool = new LoggerTestTool("org.apache.zookeeper.server.quorum")) {
        ByteArrayOutputStream os = loggerTestTool.getOutputStream();
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        final int CLIENT_PORT_QP2 = PortAssignment.unique();
        String quorumCfgSectionServer = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2 + "\n";
        // server 1 boots with a MetricsProvider
        String quorumCfgSectionServer1 = quorumCfgSectionServer + "metricsProvider.className=" + BaseTestMetricsProvider.MetricsProviderWithErrorInStop.class.getName() + "\n";
        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSectionServer1);
        MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSectionServer);
        q1.start();
        q2.start();
        boolean isup1 = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 30000);
        boolean isup2 = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, 30000);
        assertTrue(isup1, "Server 1 never came up");
        assertTrue(isup2, "Server 2 never came up");
        q1.shutdown();
        q2.shutdown();
        assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT), "waiting for server 1 down");
        assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT), "waiting for server 2 down");
        assertTrue(BaseTestMetricsProvider.MetricsProviderWithErrorInStop.stopCalled.get(), "metrics provider lifecycle error");
        LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
        String line;
        boolean found = false;
        Pattern p = Pattern.compile(".*Error while stopping metrics.*");
        while ((line = r.readLine()) != null) {
            found = p.matcher(line).matches();
            if (found) {
                break;
            }
        }
        assertTrue(found, "complains about metrics provider");
    }
}
Also used : Pattern(java.util.regex.Pattern) LoggerTestTool(org.apache.zookeeper.test.LoggerTestTool) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseTestMetricsProvider(org.apache.zookeeper.metrics.BaseTestMetricsProvider) LineNumberReader(java.io.LineNumberReader) Test(org.junit.jupiter.api.Test)

Example 5 with LoggerTestTool

use of org.apache.zookeeper.test.LoggerTestTool in project zookeeper by apache.

the class QuorumPeerMainTest method testFaultyMetricsProviderOnStart.

/**
 * Verify boot fails with a MetricsProvider with fails to start
 */
@Test
public void testFaultyMetricsProviderOnStart() throws Exception {
    ClientBase.setupTestEnv();
    try (LoggerTestTool loggerTestTool = new LoggerTestTool("org.apache.zookeeper.server.quorum")) {
        ByteArrayOutputStream os = loggerTestTool.getOutputStream();
        final int CLIENT_PORT_QP1 = PortAssignment.unique();
        String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + "metricsProvider.className=" + BaseTestMetricsProvider.MetricsProviderWithErrorInStart.class.getName() + "\n";
        MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
        q1.start();
        boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 5000);
        assertFalse(isup, "Server never came up");
        q1.shutdown();
        assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT), "waiting for server 1 down");
        LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
        String line;
        boolean found = false;
        Pattern p = Pattern.compile(".*MetricsProviderLifeCycleException.*");
        while ((line = r.readLine()) != null) {
            found = p.matcher(line).matches();
            if (found) {
                break;
            }
        }
        assertTrue(found, "complains about metrics provider MetricsProviderLifeCycleException");
    }
}
Also used : Pattern(java.util.regex.Pattern) LoggerTestTool(org.apache.zookeeper.test.LoggerTestTool) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseTestMetricsProvider(org.apache.zookeeper.metrics.BaseTestMetricsProvider) LineNumberReader(java.io.LineNumberReader) Test(org.junit.jupiter.api.Test)

Aggregations

LoggerTestTool (org.apache.zookeeper.test.LoggerTestTool)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 LineNumberReader (java.io.LineNumberReader)9 StringReader (java.io.StringReader)9 Pattern (java.util.regex.Pattern)9 Test (org.junit.jupiter.api.Test)9 BaseTestMetricsProvider (org.apache.zookeeper.metrics.BaseTestMetricsProvider)3 BeforeAll (org.junit.jupiter.api.BeforeAll)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)1