-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_environment_variables.py
More file actions
161 lines (148 loc) · 6.88 KB
/
test_environment_variables.py
File metadata and controls
161 lines (148 loc) · 6.88 KB
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
#
# Sonar Scanner Python
# Copyright (C) 2011-2026 SonarSource Sàrl
# 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.
#
from json import JSONDecodeError
import unittest
from unittest.mock import MagicMock, patch
from pysonar_scanner.configuration import environment_variables
from pysonar_scanner.configuration.properties import (
SONAR_HOST_URL,
SONAR_REGION,
SONAR_SCANNER_ARCH,
SONAR_SCANNER_JAVA_OPTS,
SONAR_SCANNER_OS,
SONAR_TOKEN,
SONAR_USER_HOME,
SONAR_PROJECT_KEY,
SONAR_SCANNER_OPTS,
)
class TestEnvironmentVariables(unittest.TestCase):
def setUp(self):
self.maxDiff = None
def test_empty_environment(self):
with patch.dict("os.environ", {}, clear=True):
properties = environment_variables.load()
self.assertEqual(len(properties), 0)
self.assertDictEqual(properties, {})
def test__environment_variables(self):
env = {
"SONAR_TOKEN": "my-token",
"SONAR_HOST_URL": "https://sonarqube.example.com",
"SONAR_USER_HOME": "/custom/sonar/home",
"SONAR_SCANNER_JAVA_OPTS": "-Xmx1024m -XX:MaxPermSize=256m",
"SONAR_REGION": "us",
}
with patch.dict("os.environ", env, clear=True):
properties = environment_variables.load()
expected_properties = {
SONAR_TOKEN: "my-token",
SONAR_HOST_URL: "https://sonarqube.example.com",
SONAR_USER_HOME: "/custom/sonar/home",
SONAR_SCANNER_JAVA_OPTS: "-Xmx1024m -XX:MaxPermSize=256m",
SONAR_REGION: "us",
}
self.assertEqual(len(properties), 5)
self.assertDictEqual(properties, expected_properties)
def test_irrelevant_environment_variables(self):
env = {"UNRELATED_VAR": "some-value", "PATH": "/usr/bin:/bin", "HOME": "/home/user"}
with patch.dict("os.environ", env, clear=True):
properties = environment_variables.load()
self.assertEqual(len(properties), 0)
self.assertDictEqual(properties, {})
def test_mixed_environment_variables(self):
env = {
"SONAR_TOKEN": "my-token",
"SONAR_HOST_URL": "https://sonarqube.example.com",
"SONAR_PROJECT_KEY": "MyProjectKey",
"UNRELATED_VAR": "some-value",
"SONAR_SCANNER_OS": "linux",
"SONAR_SCANNER_ARCH": "x64",
"PATH": "/usr/bin:/bin",
}
with patch.dict("os.environ", env, clear=True):
properties = environment_variables.load()
expected_properties = {
SONAR_TOKEN: "my-token",
SONAR_HOST_URL: "https://sonarqube.example.com",
SONAR_PROJECT_KEY: "MyProjectKey",
SONAR_SCANNER_OS: "linux",
SONAR_SCANNER_ARCH: "x64",
}
self.assertEqual(len(properties), 5)
self.assertDictEqual(properties, expected_properties)
def test_environment_variables_from_json_params(self):
env = {
"SONAR_SCANNER_JSON_PARAMS": '{"sonar.token": "json-token", "sonar.host.url": "https://json.example.com"}'
}
with patch.dict("os.environ", env, clear=True):
properties = environment_variables.load()
expected_properties = {
SONAR_TOKEN: "json-token",
SONAR_HOST_URL: "https://json.example.com",
}
self.assertEqual(len(properties), 2)
self.assertDictEqual(properties, expected_properties)
@patch("pysonar_scanner.configuration.environment_variables.logging")
def test_invalid_json_params(self, mock_logging):
env = {"SONAR_SCANNER_JSON_PARAMS": '{"sonar.token": "json-token"'}
with patch.dict("os.environ", env, clear=True):
properties = environment_variables.load()
self.assertEqual(len(properties), 0)
self.assertDictEqual(properties, {})
mock_logging.warning.assert_called_once_with(
"The JSON in SONAR_SCANNER_JSON_PARAMS environment variable is invalid. The other environment variables will still be loaded. Error : Expecting ',' delimiter: line 1 column 29 (char 28)",
)
def test_environment_variables_priority_over_json_params(self):
env = {
"SONAR_TOKEN": "regular-token", # This should take priority
"SONAR_HOST_URL": "https://regular.example.com", # This should take priority
"SONAR_SCANNER_JSON_PARAMS": '{"sonar.token": "json-token", "sonar.host.url": "https://json.example.com", "sonar.region": "eu"}',
}
with patch.dict("os.environ", env, clear=True):
properties = environment_variables.load()
expected_properties = {
SONAR_TOKEN: "regular-token", # Regular env var takes priority
SONAR_HOST_URL: "https://regular.example.com", # Regular env var takes priority
SONAR_REGION: "eu", # Only in JSON, so this value is used
}
self.assertEqual(len(properties), 3)
self.assertDictEqual(properties, expected_properties)
@patch("pysonar_scanner.configuration.environment_variables.logging")
def test_SONAR_SCANNER_OPTS(self, mock_logging):
env = {
"SONAR_TOKEN": "my-token",
"SONAR_HOST_URL": "https://sonarqube.example.com",
"SONAR_USER_HOME": "/custom/sonar/home",
"SONAR_SCANNER_OPTS": "-Xmx1024m -XX:MaxPermSize=256m",
"SONAR_REGION": "us",
}
with patch.dict("os.environ", env, clear=True):
properties = environment_variables.load()
expected_properties = {
SONAR_TOKEN: "my-token",
SONAR_HOST_URL: "https://sonarqube.example.com",
SONAR_USER_HOME: "/custom/sonar/home",
SONAR_SCANNER_OPTS: "-Xmx1024m -XX:MaxPermSize=256m",
SONAR_REGION: "us",
}
self.assertEqual(len(properties), 5)
self.assertDictEqual(properties, expected_properties)
mock_logging.warning.assert_called_once_with(
"SONAR_SCANNER_OPTS is deprecated, please use SONAR_SCANNER_JAVA_OPTS instead.",
)