From: Sébastien Lesaint Date: Wed, 30 Nov 2016 10:31:24 +0000 (+0100) Subject: SONAR-8335 add IT on dynamic log level change X-Git-Tag: 6.2-RC2~6 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=82e3d57cf327c5bb8843bd54492bca884614d325;p=sonarqube.git SONAR-8335 add IT on dynamic log level change --- diff --git a/it/it-tests/src/test/java/it/serverSystem/LogsTest.java b/it/it-tests/src/test/java/it/serverSystem/LogsTest.java index dc976fc78d5..4252df014ba 100644 --- a/it/it-tests/src/test/java/it/serverSystem/LogsTest.java +++ b/it/it-tests/src/test/java/it/serverSystem/LogsTest.java @@ -20,20 +20,28 @@ package it.serverSystem; import com.sonar.orchestrator.Orchestrator; +import com.sonar.orchestrator.build.SonarScanner; import it.Category4Suite; import java.io.File; import java.io.IOException; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.apache.commons.io.input.ReversedLinesFileReader; +import org.assertj.core.util.Files; +import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.WsClient; +import org.sonarqube.ws.client.issue.SearchWsRequest; import util.ItUtils; import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; +import static util.ItUtils.projectDir; public class LogsTest { @@ -43,6 +51,11 @@ public class LogsTest { @ClassRule public static final Orchestrator orchestrator = Category4Suite.ORCHESTRATOR; + @Before + public void cleanDatabase() { + orchestrator.resetData(); + } + /** * SONAR-7581 */ @@ -75,6 +88,46 @@ public class LogsTest { assertThat(logs.get(logs.size() - 1)).describedAs("message is the last line of logs").contains(sqIsUpMessage); } + @Test + public void test_ws_change_log_level() throws IOException { + generateSqlAndEsLogsInWebAndCe(); + + assertThat(logLevelsOf(orchestrator.getServer().getWebLogs())).doesNotContain("DEBUG", "TRACE"); + assertThat(logLevelsOf(orchestrator.getServer().getCeLogs())).doesNotContain("DEBUG", "TRACE"); + + orchestrator.getServer().adminWsClient().post("api/system/change_log_level", "level", "TRACE"); + + generateSqlAndEsLogsInWebAndCe(); + + // there is hardly DEBUG logs, but we are sure there must be TRACE logs for SQL and ES requests + assertThat(logLevelsOf(orchestrator.getServer().getWebLogs())).contains("TRACE"); + assertThat(logLevelsOf(orchestrator.getServer().getCeLogs())).contains("TRACE"); + + // reset log files to empty and level to INFO + orchestrator.getServer().adminWsClient().post("api/system/change_log_level", "level", "INFO"); + FileUtils.write(orchestrator.getServer().getWebLogs(), ""); + FileUtils.write(orchestrator.getServer().getCeLogs(), ""); + + generateSqlAndEsLogsInWebAndCe(); + + assertThat(logLevelsOf(orchestrator.getServer().getWebLogs())).doesNotContain("DEBUG", "TRACE"); + assertThat(logLevelsOf(orchestrator.getServer().getCeLogs())).doesNotContain("DEBUG", "TRACE"); + } + + private void generateSqlAndEsLogsInWebAndCe() { + orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"))); + ItUtils.newAdminWsClient(orchestrator).issues().search(new SearchWsRequest() + .setProjectKeys(Collections.singletonList("sample"))); + } + + private Collection logLevelsOf(File webLogs) { + return Files.linesOf(webLogs, "UTF-8").stream() + .filter(str -> str.length() >= 25) + .map(str -> str.substring(20, 25)) + .map(String::trim) + .collect(Collectors.toSet()); + } + private void verifyLastAccessLogLine(String login, String path, int status) throws IOException { assertThat(readLastAccessLog()).endsWith(format("\"%s\" \"GET %s HTTP/1.1\" %d", login, path, status)); } diff --git a/server/sonar-process/src/main/java/org/sonar/process/logging/LogbackHelper.java b/server/sonar-process/src/main/java/org/sonar/process/logging/LogbackHelper.java index 5a55bade6d3..b9342e4f70f 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/logging/LogbackHelper.java +++ b/server/sonar-process/src/main/java/org/sonar/process/logging/LogbackHelper.java @@ -243,7 +243,7 @@ public class LogbackHelper { private static boolean isAllowed(Level level) { for (Level allowedRootLogLevel : ALLOWED_ROOT_LOG_LEVELS) { - if (level == allowedRootLogLevel) { + if (level.equals(allowedRootLogLevel)) { return true; } }