From 51f0474be31fa69eb4ad64d9417d088ca9ded884 Mon Sep 17 00:00:00 2001 From: Alex Rudyy Date: Fri, 2 Aug 2013 17:03:34 +0000 Subject: QPID-5037: Move log viewer into a separate tab and add abilities to download logs and filter log entries in the logs table git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1509778 13f79535-47bb-0310-9956-ffa450edef68 --- .../qpid/systest/rest/LogRecordsRestTest.java | 21 +++++ .../apache/qpid/systest/rest/LogViewerTest.java | 105 +++++++++++++++++++++ .../apache/qpid/systest/rest/RestTestHelper.java | 7 ++ .../qpid/systest/rest/acl/LogViewerACLTest.java | 100 ++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/LogViewerTest.java create mode 100644 qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/acl/LogViewerACLTest.java (limited to 'qpid/java/systests/src') diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/LogRecordsRestTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/LogRecordsRestTest.java index a2f9d3189c..8f2c138869 100644 --- a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/LogRecordsRestTest.java +++ b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/LogRecordsRestTest.java @@ -39,4 +39,25 @@ public class LogRecordsRestTest extends QpidRestTestCase assertEquals("Unexpected thread", "main", record.get("thread")); assertEquals("Unexpected logger", "qpid.message.broker.ready", record.get("logger")); } + + public void testGetLogsFromGivenId() throws Exception + { + List> logs = getRestTestHelper().getJsonAsList("/rest/logrecords"); + assertNotNull("Logs data cannot be null", logs); + assertTrue("Logs are not found", logs.size() > 0); + + Map lastLog = logs.get(logs.size() -1); + Object lastId = lastLog.get("id"); + + //make sure that new logs are created + getConnection(); + + List> newLogs = getRestTestHelper().getJsonAsList("/rest/logrecords?lastLogId=" + lastId); + assertNotNull("Logs data cannot be null", newLogs); + assertTrue("Logs are not found", newLogs.size() > 0); + + Object nextId = newLogs.get(0).get("id"); + + assertEquals("Unexpected next log id", ((Number)lastId).longValue() + 1, ((Number)nextId).longValue()); + } } diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/LogViewerTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/LogViewerTest.java new file mode 100644 index 0000000000..6166e8afc1 --- /dev/null +++ b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/LogViewerTest.java @@ -0,0 +1,105 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.systest.rest; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.util.List; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import org.apache.qpid.server.BrokerOptions; + +public class LogViewerTest extends QpidRestTestCase +{ + public static final String DEFAULT_FILE_APPENDER_NAME = "FileAppender"; + private String _expectedLogFileName; + + public void setUp() throws Exception + { + setSystemProperty("logsuffix", "-" + getTestQueueName()); + _expectedLogFileName = System.getProperty("logprefix", "") + "qpid" + System.getProperty("logsuffix", "") + ".log"; + + // use real broker log file + File brokerLogFile = new File(System.getProperty(QPID_HOME), BrokerOptions.DEFAULT_LOG_CONFIG_FILE); + setBrokerCommandLog4JFile(brokerLogFile); + + super.setUp(); + } + + public void testGetLogFiles() throws Exception + { + List> logFiles = getRestTestHelper().getJsonAsList("/rest/logfiles"); + assertNotNull("Log files data cannot be null", logFiles); + + // 1 file appender is configured in QPID default log4j xml: + assertEquals("Unexpected number of log files", 1, logFiles.size()); + + Map logFileDetails = logFiles.get(0); + assertEquals("Unexpected log file name", _expectedLogFileName, logFileDetails.get("name")); + assertEquals("Unexpected log file mime type", "text/plain", logFileDetails.get("mimeType")); + assertEquals("Unexpected log file appender",DEFAULT_FILE_APPENDER_NAME, logFileDetails.get("appenderName")); + assertTrue("Unexpected log file size", ((Number)logFileDetails.get("size")).longValue()>0); + assertTrue("Unexpected log file modification time", ((Number)logFileDetails.get("lastModified")).longValue()>0); + } + + public void testDownloadExistingLogFiles() throws Exception + { + byte[] bytes = getRestTestHelper().getBytes("/rest/logfile?l=" + DEFAULT_FILE_APPENDER_NAME + "%2F" + _expectedLogFileName); + + ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bytes)); + try + { + ZipEntry entry = zis.getNextEntry(); + assertEquals("Unexpected broker log file name", DEFAULT_FILE_APPENDER_NAME + "/" + _expectedLogFileName, entry.getName()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int len; + while ((len = zis.read(buffer)) > 0) + { + baos.write(buffer, 0, len); + } + baos.close(); + assertTrue("Unexpected broker log file content", new String(baos.toByteArray()).contains("BRK-1004")); + assertNull("Unexpepected log file entry", zis.getNextEntry()); + } + finally + { + zis.close(); + } + } + + public void testDownloadNonExistingLogFiles() throws Exception + { + int responseCode = getRestTestHelper().submitRequest("/rest/logfile?l=" + DEFAULT_FILE_APPENDER_NAME + "%2F" + + _expectedLogFileName + "_" + System.currentTimeMillis(), "GET", null); + + assertEquals("Unexpected response code", 404, responseCode); + } + + public void testDownloadNonLogFiles() throws Exception + { + int responseCode = getRestTestHelper().submitRequest("/rest/logfile?l=config.json", "GET", null); + assertEquals("Unexpected response code", 400, responseCode); + } +} diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/RestTestHelper.java b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/RestTestHelper.java index c15e5d7285..7d99b30049 100644 --- a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/RestTestHelper.java +++ b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/RestTestHelper.java @@ -468,4 +468,11 @@ public class RestTestHelper connection.disconnect(); return responseCode; } + + public byte[] getBytes(String path) throws IOException + { + HttpURLConnection connection = openManagementConnection(path, "GET"); + connection.connect(); + return readConnectionInputStream(connection); + } } diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/acl/LogViewerACLTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/acl/LogViewerACLTest.java new file mode 100644 index 0000000000..5a2ebe3e8e --- /dev/null +++ b/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/acl/LogViewerACLTest.java @@ -0,0 +1,100 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.systest.rest.acl; + +import java.io.IOException; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.qpid.server.management.plugin.HttpManagement; +import org.apache.qpid.server.security.acl.AbstractACLTestCase; +import org.apache.qpid.systest.rest.QpidRestTestCase; +import org.apache.qpid.test.utils.TestBrokerConfiguration; + +public class LogViewerACLTest extends QpidRestTestCase +{ + private static final String ALLOWED_USER = "user1"; + private static final String DENIED_USER = "user2"; + + @Override + protected void customizeConfiguration() throws ConfigurationException, IOException + { + super.customizeConfiguration(); + getRestTestHelper().configureTemporaryPasswordFile(this, ALLOWED_USER, DENIED_USER); + + AbstractACLTestCase.writeACLFileUtil(this, null, + "ACL ALLOW-LOG ALL ACCESS MANAGEMENT", + "ACL ALLOW-LOG " + ALLOWED_USER + " ACCESS_LOGS BROKER", + "ACL DENY-LOG " + DENIED_USER + " ACCESS_LOGS BROKER", + "ACL DENY-LOG ALL ALL"); + + getBrokerConfiguration().setObjectAttribute(TestBrokerConfiguration.ENTRY_NAME_HTTP_MANAGEMENT, + HttpManagement.HTTP_BASIC_AUTHENTICATION_ENABLED, true); + } + + public void testGetLogRecordsAllowed() throws Exception + { + getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER); + + int responseCode = getRestTestHelper().submitRequest("/rest/logrecords", "GET", null); + assertEquals("Access to log records should be allowed", 200, responseCode); + } + + public void testGetLogRecordsDenied() throws Exception + { + getRestTestHelper().setUsernameAndPassword(DENIED_USER, DENIED_USER); + + int responseCode = getRestTestHelper().submitRequest("/rest/logrecords", "GET", null); + assertEquals("Access to log records should be denied", 403, responseCode); + } + + public void testGetLogFilesAllowed() throws Exception + { + getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER); + + int responseCode = getRestTestHelper().submitRequest("/rest/logfiles", "GET", null); + assertEquals("Access to log files should be allowed", 200, responseCode); + } + + public void testGetLogFilesDenied() throws Exception + { + getRestTestHelper().setUsernameAndPassword(DENIED_USER, DENIED_USER); + + int responseCode = getRestTestHelper().submitRequest("/rest/logfiles", "GET", null); + assertEquals("Access to log files should be denied", 403, responseCode); + } + + public void testDownloadLogFileAllowed() throws Exception + { + getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER); + + int responseCode = getRestTestHelper().submitRequest("/rest/logfile?l=appender%2fqpid.log", "GET", null); + assertEquals("Access to log files should be allowed", 404, responseCode); + } + + public void testDownloadLogFileDenied() throws Exception + { + getRestTestHelper().setUsernameAndPassword(DENIED_USER, DENIED_USER); + + int responseCode = getRestTestHelper().submitRequest("/rest/logfile?l=appender%2fqpid.log", "GET", null); + assertEquals("Access to log files should be denied", 403, responseCode); + } + +} -- cgit v1.2.1