1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.sca;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.platform.Server;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.System2;
import org.sonar.core.util.ProcessWrapperFactory;
import org.sonar.scanner.config.DefaultConfiguration;
import org.sonar.scanner.repository.TelemetryCache;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.slf4j.event.Level.DEBUG;
import static org.slf4j.event.Level.INFO;
class CliServiceTest {
private TelemetryCache telemetryCache;
private DefaultInputModule rootInputModule;
private final Server server = mock(Server.class);
@RegisterExtension
private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
@TempDir
Path rootModuleDir;
private CliService underTest;
@BeforeEach
void setup() {
telemetryCache = new TelemetryCache();
rootInputModule = new DefaultInputModule(
ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(rootModuleDir.toFile()));
underTest = new CliService(new ProcessWrapperFactory(), telemetryCache, System2.INSTANCE, server);
}
@Test
void generateZip_shouldCallProcessCorrectly_andRegisterTelemetry() throws IOException, URISyntaxException {
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
// We need to set the logging level to debug in order to be able to view the shell script's output
logTester.setLevel(DEBUG);
List<String> args = List.of(
"projects",
"save-lockfiles",
"--zip",
"--zip-filename",
rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
rootInputModule.getBaseDir().toString(),
"--debug");
String argumentOutput = "Arguments Passed In: " + String.join(" ", args);
DefaultConfiguration configuration = mock(DefaultConfiguration.class);
when(configuration.getProperties()).thenReturn(Map.of("sonar.sca.recursiveManifestSearch", "true"));
when(configuration.get("sonar.sca.recursiveManifestSearch")).thenReturn(Optional.of("true"));
File producedZip = underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(producedZip).exists();
assertThat(logTester.logs(DEBUG))
.contains(argumentOutput)
.contains("TIDELIFT_SKIP_UPDATE_CHECK=1")
.contains("TIDELIFT_ALLOW_MANIFEST_FAILURES=1")
.contains("TIDELIFT_RECURSIVE_MANIFEST_SEARCH=true");
assertThat(logTester.logs(INFO)).contains("Generated manifests zip file: " + producedZip.getName());
assertThat(telemetryCache.getAll()).containsKey("scanner.sca.execution.cli.duration").isNotNull();
assertThat(telemetryCache.getAll()).containsEntry("scanner.sca.execution.cli.success", "true");
}
@Test
void generateZip_whenDebugLogLevel_shouldCallProcessCorrectly() throws IOException, URISyntaxException {
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
// We need to set the logging level to debug in order to be able to view the shell script's output
logTester.setLevel(DEBUG);
List<String> args = List.of(
"projects",
"save-lockfiles",
"--zip",
"--zip-filename",
rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
rootInputModule.getBaseDir().toString(),
"--debug");
String argumentOutput = "Arguments Passed In: " + String.join(" ", args);
DefaultConfiguration configuration = mock(DefaultConfiguration.class);
when(configuration.getProperties()).thenReturn(Map.of("sonar.sca.recursiveManifestSearch", "true"));
when(configuration.get("sonar.sca.recursiveManifestSearch")).thenReturn(Optional.of("true"));
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(logTester.logs(DEBUG))
.contains(argumentOutput);
}
@Test
void generateZip_whenScaDebugEnabled_shouldCallProcessCorrectly() throws IOException, URISyntaxException {
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
// Set the logging level to info so that we don't automatically set --debug flag
logTester.setLevel(INFO);
List<String> args = List.of(
"projects",
"save-lockfiles",
"--zip",
"--zip-filename",
rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
rootInputModule.getBaseDir().toString(),
"--debug");
String argumentOutput = "Arguments Passed In: " + String.join(" ", args);
DefaultConfiguration configuration = mock(DefaultConfiguration.class);
when(configuration.getProperties()).thenReturn(Map.of("sonar.sca.recursiveManifestSearch", "true"));
when(configuration.get("sonar.sca.recursiveManifestSearch")).thenReturn(Optional.of("true"));
when(configuration.getBoolean("sonar.sca.debug")).thenReturn(Optional.of(true));
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(logTester.logs(INFO))
.contains(argumentOutput);
}
@Test
void generateZip_shouldSendSQEnvVars() throws IOException, URISyntaxException {
// We need to set the logging level to debug in order to be able to view the shell script's output
logTester.setLevel(DEBUG);
var version = "1.0.0";
when(server.getVersion()).thenReturn(version);
DefaultConfiguration configuration = mock(DefaultConfiguration.class);
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(logTester.logs(DEBUG))
.contains("TIDELIFT_CLI_INSIDE_SCANNER_ENGINE=1")
.contains("TIDELIFT_CLI_SQ_SERVER_VERSION=" + version);
}
private URL scriptUrl() {
// There is a custom test Bash script available in src/test/resources/org/sonar/scanner/sca that
// will serve as our "CLI". This script will output some messages about what arguments were passed
// to it and will try to generate a zip file in the location the process specifies. This allows us
// to simulate a real CLI call without needing an OS specific CLI executable to run on a real project.
URL scriptUrl = CliServiceTest.class.getResource(SystemUtils.IS_OS_WINDOWS ? "echo_args.bat" : "echo_args.sh");
assertThat(scriptUrl).isNotNull();
return scriptUrl;
}
private File scriptDir() throws URISyntaxException {
return new File(scriptUrl().toURI());
}
}
|