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
|
/*
* 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.bootstrap;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
class EnvironmentConfigTest {
@RegisterExtension
private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
@Test
void shouldProcessGenericEnvVariables() {
var inputProperties = new HashMap<String, String>();
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of("SONAR_SCANNER", "ignored",
"SONAR_SCANNER_", "ignored as well",
"SONAR_SCANNER_FOO", "bar",
"SONAR_SCANNER_FOO_BAZ", "bar",
"SONAR_SCANNER_fuZz_bAz", "env vars are case insensitive"));
assertThat(inputProperties).containsOnly(
entry("sonar.scanner.foo", "bar"),
entry("sonar.scanner.fooBaz", "bar"),
entry("sonar.scanner.fuzzBaz", "env vars are case insensitive"));
}
@Test
void genericEnvVarShouldNotOverrideInputProperties() {
var inputProperties = new HashMap<String, String>(Map.of("sonar.scanner.foo", "foo", "sonar.scanner.bar", "same value"));
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of(
"SONAR_SCANNER_FOO", "should not override",
"SONAR_SCANNER_BAR", "same value",
"SONAR_SCANNER_BAZ", "baz"));
assertThat(inputProperties).containsOnly(
entry("sonar.scanner.foo", "foo"),
entry("sonar.scanner.bar", "same value"),
entry("sonar.scanner.baz", "baz"));
assertThat(logTester.logs(Level.WARN)).containsOnly("Ignoring environment variable 'SONAR_SCANNER_FOO' because it is already defined in the properties");
}
@Test
void shouldProcessJsonEnvVariables() {
var inputProperties = new HashMap<String, String>();
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of("SONAR_SCANNER_JSON_PARAMS",
"{\"key1\":\"value1\", \"key2\":\"value2\"}"));
assertThat(inputProperties).containsOnly(
entry("key1", "value1"),
entry("key2", "value2"));
}
@Test
void ignoreEmptyValueForJsonEnv() {
var inputProperties = new HashMap<String, String>();
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of("SONAR_SCANNER_JSON_PARAMS", ""));
assertThat(inputProperties).isEmpty();
}
@Test
void throwIfInvalidFormat() {
var inputProperties = new HashMap<String, String>();
var env = Map.of("SONAR_SCANNER_JSON_PARAMS", "{garbage");
var thrown = assertThrows(IllegalArgumentException.class, () -> EnvironmentConfig.processEnvVariables(inputProperties, env));
assertThat(thrown).hasMessage("Failed to parse JSON properties from environment variable 'SONAR_SCANNER_JSON_PARAMS'");
}
@Test
void jsonEnvVariablesShouldNotOverrideInputProperties() {
var inputProperties = new HashMap<String, String>(Map.of("key1", "value1", "key3", "value3"));
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of("SONAR_SCANNER_JSON_PARAMS",
"{\"key1\":\"should not override\", \"key2\":\"value2\"}"));
assertThat(inputProperties).containsOnly(
entry("key1", "value1"),
entry("key2", "value2"),
entry("key3", "value3"));
assertThat(logTester.logs(Level.WARN)).containsOnly("Ignoring property 'key1' from env variable 'SONAR_SCANNER_JSON_PARAMS' because it is already defined");
}
@Test
void jsonEnvVariablesShouldNotOverrideGenericEnv() {
var inputProperties = new HashMap<String, String>();
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of("SONAR_SCANNER_FOO", "value1",
"SONAR_SCANNER_JSON_PARAMS", "{\"sonar.scanner.foo\":\"should not override\", \"key2\":\"value2\"}"));
assertThat(inputProperties).containsOnly(
entry("sonar.scanner.foo", "value1"),
entry("key2", "value2"));
assertThat(logTester.logs(Level.WARN)).containsOnly("Ignoring property 'sonar.scanner.foo' from env variable 'SONAR_SCANNER_JSON_PARAMS' because it is already defined");
}
@Test
void shouldProcessOldJsonEnvVariables() {
var inputProperties = new HashMap<String, String>();
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of("SONARQUBE_SCANNER_PARAMS",
"{\"key1\":\"value1\", \"key2\":\"value2\"}"));
assertThat(inputProperties).containsOnly(
entry("key1", "value1"),
entry("key2", "value2"));
}
@Test
void oldJsonEnvVariablesIsIgnoredIfNewIsDefinedAndLogAWarning() {
var inputProperties = new HashMap<String, String>();
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of("SONARQUBE_SCANNER_PARAMS", "{\"key1\":\"should not override\", \"key3\":\"value3\"}",
"SONAR_SCANNER_JSON_PARAMS", "{\"key1\":\"value1\", \"key2\":\"value2\"}"));
assertThat(inputProperties).containsOnly(
entry("key1", "value1"),
entry("key2", "value2"));
assertThat(logTester.logs(Level.WARN)).containsOnly("Ignoring environment variable 'SONARQUBE_SCANNER_PARAMS' because 'SONAR_SCANNER_JSON_PARAMS' is set");
}
@Test
void oldJsonEnvVariablesIsIgnoredIfNewIsDefinedButDontLogIfSameValue() {
var inputProperties = new HashMap<String, String>();
EnvironmentConfig.processEnvVariables(inputProperties,
Map.of("SONARQUBE_SCANNER_PARAMS", "{\"key1\":\"value1\", \"key2\":\"value2\"}",
"SONAR_SCANNER_JSON_PARAMS", "{\"key1\":\"value1\", \"key2\":\"value2\"}"));
assertThat(inputProperties).containsOnly(
entry("key1", "value1"),
entry("key2", "value2"));
assertThat(logTester.logs()).isEmpty();
}
}
|