]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8335 add IT on dynamic log level change
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 30 Nov 2016 10:31:24 +0000 (11:31 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 1 Dec 2016 11:29:40 +0000 (12:29 +0100)
it/it-tests/src/test/java/it/serverSystem/LogsTest.java
server/sonar-process/src/main/java/org/sonar/process/logging/LogbackHelper.java

index dc976fc78d587b46d73f3570afe8b838f2bea306..4252df014ba7b347012f326110dbe8c6e410a16a 100644 (file)
 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<String> 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));
   }
index 5a55bade6d3fda625ef439a6340f89d1828f3d86..b9342e4f70f48ec65724d5de99b6f856e5667ff3 100644 (file)
@@ -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;
       }
     }