3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.monitoring;
22 import com.google.common.base.Optional;
23 import okhttp3.mockwebserver.MockResponse;
24 import okhttp3.mockwebserver.MockWebServer;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.rules.TemporaryFolder;
32 import org.sonar.api.config.Settings;
33 import org.sonar.process.DefaultProcessCommands;
34 import org.sonar.process.ProcessEntryPoint;
35 import org.sonar.process.ProcessId;
36 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
38 import static java.lang.String.format;
39 import static org.assertj.core.api.Assertions.assertThat;
41 public class ProcessSystemInfoClientTest {
43 public TemporaryFolder temp = new TemporaryFolder();
46 public ExpectedException expectedException = ExpectedException.none();
49 public MockWebServer server = new MockWebServer();
52 ProcessSystemInfoClient underTest;
55 public void setUp() throws Exception {
56 ipcSharedDir = temp.newFolder();
57 Settings settings = new Settings();
58 settings.setProperty(ProcessEntryPoint.PROPERTY_SHARED_PATH, ipcSharedDir.getAbsolutePath());
59 underTest = new ProcessSystemInfoClient(settings);
63 public void connect_returns_absent_if_process_is_down() throws Exception {
64 Optional<ProtobufSystemInfo.SystemInfo> info = underTest.connect(ProcessId.COMPUTE_ENGINE);
66 assertThat(info.isPresent()).isFalse();
70 public void get_information_if_process_is_up() throws Exception {
71 Buffer response = new Buffer();
72 response.read(ProtobufSystemInfo.Section.newBuilder().build().toByteArray());
73 server.enqueue(new MockResponse().setBody(response));
75 // initialize registration of process
76 try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
77 processCommands.setUp();
78 processCommands.setSystemInfoUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
81 Optional<ProtobufSystemInfo.SystemInfo> info = underTest.connect(ProcessId.COMPUTE_ENGINE);
82 assertThat(info.get().getSectionsCount()).isEqualTo(0);
86 public void throws_ISE_if_http_error() throws Exception {
87 server.enqueue(new MockResponse().setResponseCode(500));
89 // initialize registration of process
90 try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
91 processCommands.setUp();
92 processCommands.setSystemInfoUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
95 expectedException.expect(IllegalStateException.class);
96 expectedException.expectMessage("Can not get system info of process " + ProcessId.COMPUTE_ENGINE);
97 underTest.connect(ProcessId.COMPUTE_ENGINE);