|
18 | 18 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 | 19 | #
|
20 | 20 | import unittest
|
| 21 | +import json |
| 22 | +import time |
21 | 23 |
|
22 | 24 | from unittest.mock import patch
|
23 | 25 | from io import StringIO
|
24 | 26 |
|
25 |
| -from pysonar_scanner.configuration import ConfigurationLoader, Configuration, Scanner, Internal, Sonar |
| 27 | +from pysonar_scanner.configuration import ConfigurationLoader, Configuration, JRECacheStatus, Scanner, Internal, Sonar |
26 | 28 |
|
27 | 29 |
|
28 | 30 | class TestMain(unittest.TestCase):
|
@@ -161,3 +163,120 @@ def test_all_cli_args(self):
|
161 | 163 |
|
162 | 164 | expected_configuration = Configuration(sonar=expected_sonar)
|
163 | 165 | self.assertEqual(configuration, expected_configuration)
|
| 166 | + |
| 167 | + def test_minimal_json(self): |
| 168 | + minimal_json = Configuration(sonar=Sonar(token="MyToken")).to_json() |
| 169 | + |
| 170 | + minimal_dict = json.loads(minimal_json) |
| 171 | + self.assertIn("scannerProperties", minimal_dict) |
| 172 | + self.assertIn("sonar.scanner.bootstrapStartTime", [prop["key"] for prop in minimal_dict["scannerProperties"]]) |
| 173 | + bootstrap_start_time = [ |
| 174 | + prop["value"] |
| 175 | + for prop in minimal_dict["scannerProperties"] |
| 176 | + if prop["key"] == "sonar.scanner.bootstrapStartTime" |
| 177 | + ][0] |
| 178 | + self.assertAlmostEqual( |
| 179 | + bootstrap_start_time, int(time.time() * 1000), delta=1000 * 60 |
| 180 | + ) # 1 minute delta between the configuration creation and this test |
| 181 | + |
| 182 | + expected_json = json.dumps( |
| 183 | + { |
| 184 | + "scannerProperties": [ |
| 185 | + {"key": "sonar.scanner.app", "value": "python"}, |
| 186 | + {"key": "sonar.scanner.appVersion", "value": "1.0"}, |
| 187 | + {"key": "sonar.token", "value": "MyToken"}, |
| 188 | + {"key": "sonar.verbose", "value": False}, |
| 189 | + {"key": "sonar.scanner.bootstrapStartTime", "value": bootstrap_start_time}, |
| 190 | + ] |
| 191 | + } |
| 192 | + ) |
| 193 | + expected_dict = json.loads(expected_json) |
| 194 | + self.assertDictEqual(minimal_dict, expected_dict) |
| 195 | + |
| 196 | + def test_full_json(self): |
| 197 | + internal = Internal( |
| 198 | + dump_to_file="mySonarScannerInternalDumpToFile", sq_version="mySonarScannerInternalSqVersion" |
| 199 | + ) |
| 200 | + |
| 201 | + scanner = Scanner( |
| 202 | + internal=internal, |
| 203 | + os="windows", |
| 204 | + arch="x64", |
| 205 | + connect_timeout=42, |
| 206 | + socket_timeout=43, |
| 207 | + response_timeout=44, |
| 208 | + truststore_path="mySonarScannerTruststorePath", |
| 209 | + truststore_password="mySonarScannerTruststorePassword", |
| 210 | + keystore_path="mySonarScannerKeystorePath", |
| 211 | + keystore_password="mySonarScannerKeystorePassword", |
| 212 | + proxy_host="mySonarScannerProxyHost", |
| 213 | + proxy_port=45, |
| 214 | + proxy_user="mySonarScannerProxyUser", |
| 215 | + proxy_password="mySonarScannerProxyPassword", |
| 216 | + skip_jre_provisioning=True, |
| 217 | + java_exe_path="mySonarScannerJavaExePath", |
| 218 | + java_opts="mySonarScannerJavaOpts", |
| 219 | + sonarcloud_url="mySonarScannerCloudUrl", |
| 220 | + api_base_url="mySonarScannerApiUrl", |
| 221 | + was_engine_cache_hit=True, |
| 222 | + was_jre_cache_hit=JRECacheStatus.HIT, |
| 223 | + ) |
| 224 | + |
| 225 | + sonar = Sonar( |
| 226 | + scanner=scanner, |
| 227 | + token="MyToken", |
| 228 | + verbose=True, |
| 229 | + host_url="mySonarHostUrl", |
| 230 | + region="us", |
| 231 | + user_home="mySonarUserHome", |
| 232 | + project_base_dir="mySonarProjectBaseDir", |
| 233 | + ) |
| 234 | + |
| 235 | + full_configuration = Configuration(sonar=sonar) |
| 236 | + |
| 237 | + full_json = full_configuration.to_json() |
| 238 | + full_dict = json.loads(full_json) |
| 239 | + self.assertIn("scannerProperties", full_dict) |
| 240 | + self.assertIn("sonar.scanner.bootstrapStartTime", [prop["key"] for prop in full_dict["scannerProperties"]]) |
| 241 | + bootstrap_start_time = [ |
| 242 | + prop["value"] |
| 243 | + for prop in full_dict["scannerProperties"] |
| 244 | + if prop["key"] == "sonar.scanner.bootstrapStartTime" |
| 245 | + ][0] |
| 246 | + self.assertAlmostEqual( |
| 247 | + bootstrap_start_time, int(time.time() * 1000), delta=1000 * 60 |
| 248 | + ) # 1 minute delta between the configuration creation and this test |
| 249 | + |
| 250 | + expected_json = json.dumps( |
| 251 | + { |
| 252 | + "scannerProperties": [ |
| 253 | + {"key": "sonar.scanner.app", "value": "python"}, |
| 254 | + {"key": "sonar.scanner.appVersion", "value": "1.0"}, |
| 255 | + {"key": "sonar.token", "value": "MyToken"}, |
| 256 | + {"key": "sonar.region", "value": "us"}, |
| 257 | + {"key": "sonar.host.url", "value": "mySonarHostUrl"}, |
| 258 | + {"key": "sonar.projectBaseDir", "value": "mySonarProjectBaseDir"}, |
| 259 | + {"key": "sonar.verbose", "value": True}, |
| 260 | + {"key": "sonar.userHome", "value": "mySonarUserHome"}, |
| 261 | + {"key": "sonar.scanner.apiBaseUrl", "value": "mySonarScannerApiUrl"}, |
| 262 | + {"key": "sonar.scanner.bootstrapStartTime", "value": bootstrap_start_time}, |
| 263 | + {"key": "sonar.scanner.os", "value": "windows"}, |
| 264 | + {"key": "sonar.scanner.arch", "value": "x64"}, |
| 265 | + {"key": "sonar.scanner.connectTimeout", "value": 42}, |
| 266 | + {"key": "sonar.scanner.socketTimeout", "value": 43}, |
| 267 | + {"key": "sonar.scanner.responseTimeout", "value": 44}, |
| 268 | + {"key": "sonar.scanner.truststorePath", "value": "mySonarScannerTruststorePath"}, |
| 269 | + {"key": "sonar.scanner.truststorePassword", "value": "mySonarScannerTruststorePassword"}, |
| 270 | + {"key": "sonar.scanner.keystorePath", "value": "mySonarScannerKeystorePath"}, |
| 271 | + {"key": "sonar.scanner.keystorePassword", "value": "mySonarScannerKeystorePassword"}, |
| 272 | + {"key": "sonar.scanner.proxyHost", "value": "mySonarScannerProxyHost"}, |
| 273 | + {"key": "sonar.scanner.proxyPort", "value": 45}, |
| 274 | + {"key": "sonar.scanner.proxyUser", "value": "mySonarScannerProxyUser"}, |
| 275 | + {"key": "sonar.scanner.proxyPassword", "value": "mySonarScannerProxyPassword"}, |
| 276 | + {"key": "sonar.scanner.wasJreCacheHit", "value": "HIT"}, |
| 277 | + {"key": "sonar.scanner.wasEngineCacheHit", "value": True}, |
| 278 | + ] |
| 279 | + } |
| 280 | + ) |
| 281 | + expected_dict = json.loads(expected_json) |
| 282 | + self.assertDictEqual(full_dict, expected_dict) |
0 commit comments