]> source.dussan.org Git - sonarqube.git/blob
c7d55a92b57767847550c18386e93fd8bb3d76f0
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.application.process;
21
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Properties;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.rules.TemporaryFolder;
33 import org.sonar.process.AllProcessesCommands;
34 import org.sonar.process.ProcessId;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.data.MapEntry.entry;
38 import static org.mockito.Mockito.RETURNS_MOCKS;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41
42 public class ProcessLauncherImplTest {
43
44   @Rule
45   public TemporaryFolder temp = new TemporaryFolder();
46   @Rule
47   public ExpectedException expectedException = ExpectedException.none();
48
49   private AllProcessesCommands commands = mock(AllProcessesCommands.class, RETURNS_MOCKS);
50
51   @Test
52   public void launch_forks_a_new_process() throws Exception {
53     File tempDir = temp.newFolder();
54     TestProcessBuilder processBuilder = new TestProcessBuilder();
55     ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder);
56     JavaCommand command = new JavaCommand(ProcessId.ELASTICSEARCH);
57     command.addClasspath("lib/*.class");
58     command.addClasspath("lib/*.jar");
59     command.setArgument("foo", "bar");
60     command.setClassName("org.sonarqube.Main");
61     command.setEnvVariable("VAR1", "valueOfVar1");
62     command.setWorkDir(temp.newFolder());
63
64     ProcessMonitor monitor = underTest.launch(command);
65
66     assertThat(monitor).isNotNull();
67     assertThat(processBuilder.started).isTrue();
68     assertThat(processBuilder.commands.get(0)).endsWith("java");
69     assertThat(processBuilder.commands).containsSequence(
70       "-Djava.io.tmpdir=" + tempDir.getAbsolutePath(),
71       "-cp",
72       "lib/*.class" + System.getProperty("path.separator") + "lib/*.jar",
73       "org.sonarqube.Main");
74     assertThat(processBuilder.dir).isEqualTo(command.getWorkDir());
75     assertThat(processBuilder.redirectErrorStream).isTrue();
76     assertThat(processBuilder.environment)
77       .contains(entry("VAR1", "valueOfVar1"))
78       .containsAllEntriesOf(command.getEnvVariables());
79   }
80
81   @Test
82   public void properties_are_passed_to_command_via_a_temporary_properties_file() throws Exception {
83     File tempDir = temp.newFolder();
84     TestProcessBuilder processBuilder = new TestProcessBuilder();
85     ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder);
86     JavaCommand command = new JavaCommand(ProcessId.ELASTICSEARCH);
87     command.setArgument("foo", "bar");
88     command.setArgument("baz", "woo");
89
90     underTest.launch(command);
91
92     String propsFilePath = processBuilder.commands.get(processBuilder.commands.size() - 1);
93     File file = new File(propsFilePath);
94     assertThat(file).exists().isFile();
95     try (FileReader reader = new FileReader(file)) {
96       Properties props = new Properties();
97       props.load(reader);
98       assertThat(props).containsOnly(
99         entry("foo", "bar"),
100         entry("baz", "woo"),
101         entry("process.terminationTimeout", "60000"),
102         entry("process.key", ProcessId.ELASTICSEARCH.getKey()),
103         entry("process.index", String.valueOf(ProcessId.ELASTICSEARCH.getIpcIndex())),
104         entry("process.sharedDir", tempDir.getAbsolutePath()));
105     }
106   }
107
108   @Test
109   public void throw_ISE_if_command_fails() throws IOException {
110     File tempDir = temp.newFolder();
111     ProcessLauncherImpl.ProcessBuilder processBuilder = mock(ProcessLauncherImpl.ProcessBuilder.class, RETURNS_MOCKS);
112     when(processBuilder.start()).thenThrow(new IOException("error"));
113     ProcessLauncher underTest = new ProcessLauncherImpl(tempDir, commands, () -> processBuilder);
114
115     expectedException.expect(IllegalStateException.class);
116     expectedException.expectMessage("Fail to launch process [es]");
117
118     underTest.launch(new JavaCommand(ProcessId.ELASTICSEARCH));
119   }
120
121   private static class TestProcessBuilder implements ProcessLauncherImpl.ProcessBuilder {
122     private List<String> commands = null;
123     private File dir = null;
124     private Boolean redirectErrorStream = null;
125     private final Map<String, String> environment = new HashMap<>();
126     private boolean started = false;
127
128     @Override
129     public List<String> command() {
130       return commands;
131     }
132
133     @Override
134     public TestProcessBuilder command(List<String> commands) {
135       this.commands = commands;
136       return this;
137     }
138
139     @Override
140     public TestProcessBuilder directory(File dir) {
141       this.dir = dir;
142       return this;
143     }
144
145     @Override
146     public Map<String, String> environment() {
147       return environment;
148     }
149
150     @Override
151     public TestProcessBuilder redirectErrorStream(boolean b) {
152       this.redirectErrorStream = b;
153       return this;
154     }
155
156     @Override
157     public Process start() throws IOException {
158       this.started = true;
159       return mock(Process.class);
160     }
161   }
162 }