From d872edaed22cc4989ef4103737e35558f5711640 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 18 Jan 2016 09:39:22 +0100 Subject: [PATCH] SONAR-7168 add user login to log message when restart is requested --- .../java/it/serverSystem/RestartTest.java | 2 +- .../server/platform/ws/RestartAction.java | 2 +- .../server/platform/ws/RestartActionTest.java | 23 +++++++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/it/it-tests/src/test/java/it/serverSystem/RestartTest.java b/it/it-tests/src/test/java/it/serverSystem/RestartTest.java index 949f8f193a2..3101b9cc7ec 100644 --- a/it/it-tests/src/test/java/it/serverSystem/RestartTest.java +++ b/it/it-tests/src/test/java/it/serverSystem/RestartTest.java @@ -77,7 +77,7 @@ public class RestartTest { Thread.sleep(5000); assertThat(FileUtils.readFileToString(orchestrator.getServer().getLogs())) - .contains("Requesting SonarQube restart"); + .contains("SonarQube restart requested by admin"); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/RestartAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/RestartAction.java index e2fe1d5c117..97d84b0609e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/RestartAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/RestartAction.java @@ -72,8 +72,8 @@ public class RestartAction implements SystemWsAction { platform.restart(); LOGGER.info("WebServer restarted"); } else { - LOGGER.info("Requesting SonarQube restart"); userSession.checkPermission(UserRole.ADMIN); + LOGGER.info("SonarQube restart requested by {}", userSession.getLogin()); File shareDir = nonNullValueAsFile(PROPERTY_SHARED_PATH); int processNumber = nonNullAsInt(PROPERTY_PROCESS_INDEX); diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartActionTest.java index 2818364f53f..21b51b62ca3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartActionTest.java @@ -25,6 +25,8 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.config.Settings; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; import org.sonar.api.web.UserRole; import org.sonar.process.DefaultProcessCommands; import org.sonar.server.exceptions.ForbiddenException; @@ -38,15 +40,14 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; public class RestartActionTest { - private static final int PROCESS_NUMBER = 1; - @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); @Rule public ExpectedException expectedException = ExpectedException.none(); @Rule public TemporaryFolder temp = new TemporaryFolder(); - + @Rule + public LogTester logTester = new LogTester(); Settings settings = new Settings(); Platform platform = mock(Platform.class); @@ -102,10 +103,24 @@ public class RestartActionTest { settings.setProperty("process.index", processNumber); DefaultProcessCommands processCommands = new DefaultProcessCommands(tempFolder, processNumber); - + actionTester.newRequest().execute(); assertThat(processCommands.askedForRestart()).isTrue(); } + @Test + public void logs_login_of_authenticated_user_requesting_the_restart_in_production_mode() throws Exception { + String login = "BigBother"; + + userSessionRule.login(login).setGlobalPermissions(UserRole.ADMIN); + settings.setProperty("process.sharedDir", temp.newFolder().getAbsoluteFile().getAbsolutePath()); + settings.setProperty("process.index", 2); + + actionTester.newRequest().execute(); + + assertThat(logTester.logs(LoggerLevel.INFO)) + .contains("SonarQube restart requested by " + login); + } + } -- 2.39.5