package org.sonar.ce.http;
import java.io.File;
+import java.io.IOException;
import java.net.URI;
import java.util.Optional;
import okhttp3.OkHttpClient;
import org.apache.commons.io.IOUtils;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.api.utils.log.Loggers;
import org.sonar.process.DefaultProcessCommands;
import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
-import org.sonar.server.platform.ws.ChangeLogLevelAction;
+import static java.util.Objects.requireNonNull;
import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
import static org.sonar.process.ProcessId.COMPUTE_ENGINE;
}
public void changeLogLevel(LoggerLevel level) {
+ requireNonNull(level, "level can't be null");
call(new ChangeLogLevelActionClient(level));
}
.build();
okhttp3.Response response = new OkHttpClient().newCall(request).execute();
if (response.code() != 200) {
- Loggers.get(ChangeLogLevelAction.class).error(
- "Failed to change log level in Compute Engine. Code was '{}' and response was '{}'",
- response.code(),
- response.body().string());
+ throw new IOException(
+ String.format(
+ "Failed to change log level in Compute Engine. Code was '%s' and response was '%s' for url '%s'",
+ response.code(),
+ response.body().string(),
+ url));
}
return null;
}
}
return actionClient.getDefault();
} catch (Exception e) {
- throw new IllegalStateException("Can not get system info of process " + COMPUTE_ENGINE, e);
+ throw new IllegalStateException("Failed to call HTTP server of process " + COMPUTE_ENGINE, e);
}
}
package org.sonar.ce.http;
import java.io.File;
+import java.io.IOException;
import java.util.Optional;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.config.Settings;
+import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.process.DefaultProcessCommands;
import org.sonar.process.ProcessEntryPoint;
import org.sonar.process.ProcessId;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.test.ExceptionCauseMatcher.hasType;
public class CeHttpClientTest {
@Rule
server.enqueue(new MockResponse().setBody(response));
// initialize registration of process
- try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
- processCommands.setUp();
- processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
- }
+ setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE);
Optional<ProtobufSystemInfo.SystemInfo> info = underTest.retrieveSystemInfo();
assertThat(info.get().getSectionsCount()).isEqualTo(0);
server.enqueue(new MockResponse().setResponseCode(500));
// initialize registration of process
- try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
- processCommands.setUp();
- processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
- }
+ setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE);
expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Can not get system info of process " + ProcessId.COMPUTE_ENGINE);
+ expectedException.expectMessage("Failed to call HTTP server of process " + ProcessId.COMPUTE_ENGINE);
+ expectedException.expectCause(hasType(IOException.class)
+ .andMessage(format("Server returned HTTP response code: 500 for URL: http://%s:%d/systemInfo", server.getHostName(), server.getPort())));
underTest.retrieveSystemInfo();
}
+
+ @Test
+ public void changeLogLevel_throws_NPE_if_level_argument_is_null() {
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage("level can't be null");
+
+ underTest.changeLogLevel(null);
+ }
+
+ @Test
+ public void changeLogLevel_throws_ISE_if_http_error() {
+ String message = "blah";
+ server.enqueue(new MockResponse().setResponseCode(500).setBody(message));
+
+ // initialize registration of process
+ setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE);
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Failed to call HTTP server of process " + ProcessId.COMPUTE_ENGINE);
+ expectedException.expectCause(hasType(IOException.class)
+ .andMessage(format("Failed to change log level in Compute Engine. Code was '500' and response was 'blah' for url " +
+ "'http://%s:%s/changeLogLevel'", server.getHostName(), server.getPort())));
+
+ underTest.changeLogLevel(LoggerLevel.DEBUG);
+ }
+
+ @Test
+ public void changeLogLevel_does_not_fail_when_http_code_is_200() {
+ server.enqueue(new MockResponse().setResponseCode(200));
+
+ setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE);
+
+ underTest.changeLogLevel(LoggerLevel.TRACE);
+ }
+
+ @Test
+ public void changelogLevel_does_not_fail_if_process_is_down() {
+ underTest.changeLogLevel(LoggerLevel.INFO);
+ }
+
+ private void setUpWithHttpUrl(ProcessId processId) {
+ try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, processId.getIpcIndex())) {
+ processCommands.setUp();
+ processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
+ }
+ }
}