-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_api.py
More file actions
397 lines (352 loc) · 16.3 KB
/
test_api.py
File metadata and controls
397 lines (352 loc) · 16.3 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#
# Sonar Scanner Python
# Copyright (C) 2011-2024 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.
#
from typing import TypedDict
import io
from pysonar_scanner import api
from pysonar_scanner.api import JRE, BaseUrls, EngineInfo, SonarQubeApi, SonarQubeApiException, get_base_urls
from pysonar_scanner.configuration.properties import (
Key,
SONAR_HOST_URL,
SONAR_REGION,
SONAR_SCANNER_API_BASE_URL,
SONAR_SCANNER_SONARCLOUD_URL,
)
from pysonar_scanner.api import SQVersion
from tests import sq_api_utils
from tests.sq_api_utils import sq_api_mocker
import unittest
class TestSQVersion(unittest.TestCase):
def test_does_support_bootstrapping(self):
self.assertTrue(SQVersion.from_str("10.6").does_support_bootstrapping())
self.assertTrue(SQVersion.from_str("10.6.5").does_support_bootstrapping())
self.assertTrue(SQVersion.from_str("10.6.5a").does_support_bootstrapping())
self.assertTrue(SQVersion.from_str("10.7").does_support_bootstrapping())
self.assertTrue(SQVersion.from_str("11.1").does_support_bootstrapping())
self.assertTrue(SQVersion.from_str("11.1.1").does_support_bootstrapping())
self.assertTrue(SQVersion.from_str("11").does_support_bootstrapping())
self.assertFalse(SQVersion.from_str("9.9.9").does_support_bootstrapping())
self.assertFalse(SQVersion.from_str("9.9.9a").does_support_bootstrapping())
self.assertFalse(SQVersion.from_str("9.9").does_support_bootstrapping())
self.assertFalse(SQVersion.from_str("9").does_support_bootstrapping())
self.assertFalse(SQVersion.from_str("10.5").does_support_bootstrapping())
self.assertFalse(SQVersion.from_str("10").does_support_bootstrapping())
self.assertFalse(SQVersion.from_str("a").does_support_bootstrapping())
self.assertFalse(SQVersion.from_str("1.a").does_support_bootstrapping())
def test_str(self):
self.assertEqual(str(SQVersion.from_str("10.6")), "10.6")
self.assertEqual(str(SQVersion.from_str("9.9.9aa")), "9.9.9aa")
class TestApi(unittest.TestCase):
def test_BaseUrls_normalization(self):
self.assertEqual(
BaseUrls("test1/", "test2/", is_sonar_qube_cloud=True),
BaseUrls("test1", "test2", is_sonar_qube_cloud=True),
)
def test_get_base_urls(self):
class TestCaseDict(TypedDict):
name: str
config: dict[Key, any]
expected: BaseUrls
cases: list[TestCaseDict] = [
# SQ:Cloud tests
{
"name": "default configuration defaults to SQ:cloud base urls",
"config": {
SONAR_HOST_URL: "",
SONAR_SCANNER_SONARCLOUD_URL: "",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "",
},
"expected": BaseUrls(
base_url="https://sonarcloud.io", api_base_url="https://api.sonarcloud.io", is_sonar_qube_cloud=True
),
},
{
"name": "sonar.host.url with whitespaces uses the SQ:cloud base urls",
"config": {
SONAR_HOST_URL: " ",
SONAR_SCANNER_SONARCLOUD_URL: "",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "",
},
"expected": BaseUrls(
base_url="https://sonarcloud.io", api_base_url="https://api.sonarcloud.io", is_sonar_qube_cloud=True
),
},
{
"name": "when host_url is set to SQ:cloud, use SQ:cloud base urls",
"config": {
SONAR_HOST_URL: "https://sonarcloud.io",
SONAR_SCANNER_SONARCLOUD_URL: "",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "",
},
"expected": BaseUrls(
base_url="https://sonarcloud.io", api_base_url="https://api.sonarcloud.io", is_sonar_qube_cloud=True
),
},
{
"name": "When both host_url and sonarcloud_url are set, use sonarcloud_url to check if host is SQ:cloud",
"config": {
SONAR_HOST_URL: "https://sonarcloud.io",
SONAR_SCANNER_SONARCLOUD_URL: "https://custom-sq-cloud.io",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "",
},
"expected": BaseUrls(
base_url="https://sonarcloud.io",
api_base_url="https://sonarcloud.io/api/v2",
is_sonar_qube_cloud=False,
),
},
{
"name": "when host_url with trailing slash is set to SQ:cloud, use SQ:cloud base urls",
"config": {
SONAR_HOST_URL: "https://sonarcloud.io/",
SONAR_SCANNER_SONARCLOUD_URL: "",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "",
},
"expected": BaseUrls(
base_url="https://sonarcloud.io", api_base_url="https://api.sonarcloud.io", is_sonar_qube_cloud=True
),
},
# SQ:Cloud region tests
{
"name": "When region is set, use region in base urls",
"config": {
SONAR_HOST_URL: "https://sonarcloud.io",
SONAR_SCANNER_SONARCLOUD_URL: "",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "us",
},
"expected": BaseUrls(
base_url="https://us.sonarcloud.io",
api_base_url="https://api.us.sonarcloud.io",
is_sonar_qube_cloud=True,
),
},
{
"name": "Ignore region when sonarcloud_url and api_base_url is set",
"config": {
SONAR_HOST_URL: "https://custom-sq-cloud.io",
SONAR_SCANNER_SONARCLOUD_URL: "https://custom-sq-cloud.io",
SONAR_SCANNER_API_BASE_URL: "https://other-api.custom-sq-cloud.io",
SONAR_REGION: "us",
},
"expected": BaseUrls(
base_url="https://custom-sq-cloud.io",
api_base_url="https://other-api.custom-sq-cloud.io",
is_sonar_qube_cloud=True,
),
},
# SQ:Server tests
{
"name": "When host_url is set to SQ:server, use SQ:server base urls",
"config": {
SONAR_HOST_URL: "https://sq.home",
SONAR_SCANNER_SONARCLOUD_URL: "",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "",
},
"expected": BaseUrls(
base_url="https://sq.home", api_base_url="https://sq.home/api/v2", is_sonar_qube_cloud=False
),
},
{
"name": "When host_url with trailing slash is set to SQ:server, use SQ:server base urls",
"config": {
SONAR_HOST_URL: "https://sq.home/",
SONAR_SCANNER_SONARCLOUD_URL: "",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "",
},
"expected": BaseUrls(
base_url="https://sq.home", api_base_url="https://sq.home/api/v2", is_sonar_qube_cloud=False
),
},
]
for case in cases:
expected = case["expected"]
with self.subTest(case["name"], config=case["config"], expected=expected):
base_urls = get_base_urls(case["config"])
self.assertEqual(base_urls.base_url, expected.base_url)
self.assertEqual(base_urls.api_base_url, expected.api_base_url)
self.assertEqual(base_urls.is_sonar_qube_cloud, expected.is_sonar_qube_cloud)
self.assertEqual(base_urls, expected)
class TestSonarQubeApiWithUnreachableSQServer(unittest.TestCase):
def setUp(self):
self.sq = SonarQubeApi(
base_urls=BaseUrls("https://localhost:1000", "https://localhost:1000/api", is_sonar_qube_cloud=True),
token="<invalid_token>",
)
def test_get_analysis_version(self):
with self.assertRaises(SonarQubeApiException):
self.sq.get_analysis_version()
class TestSonarQubeApi(unittest.TestCase):
def setUp(self):
self.sq = sq_api_utils.get_sq_server()
def test_get_analysis_version(self):
with self.subTest("/analysis/version returns 200"), sq_api_mocker() as mocker:
mocker.mock_analysis_version("10.7")
self.assertEqual(self.sq.get_analysis_version(), SQVersion.from_str("10.7"))
with self.subTest("/analysis/version returns error"), sq_api_mocker() as mocker:
mocker.mock_analysis_version(status=404)
mocker.mock_server_version("10.8")
self.assertEqual(self.sq.get_analysis_version(), SQVersion.from_str("10.8"))
with (
self.subTest("both version endpoints return error"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
mocker.mock_analysis_version(status=404)
mocker.mock_server_version(status=404)
self.sq.get_analysis_version()
with (
self.subTest("request raises an exception"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
self.sq.get_analysis_version()
def test_get_analysis_engine(self):
with self.subTest("get_analysis_engine works"), sq_api_mocker() as mocker:
engine_info = EngineInfo(filename="sonar-scanner-engine-shaded-8.9.0.43852-all.jar", sha256="1234567890")
mocker.mock_analysis_engine(filename=engine_info.filename, sha256=engine_info.sha256)
self.assertEqual(self.sq.get_analysis_engine(), engine_info)
with (
self.subTest("get_analysis_engine returns error"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
mocker.mock_analysis_engine(status=404)
self.sq.get_analysis_engine()
with (
self.subTest("get_analysis_engine response misses sha256"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
mocker.mock_analysis_engine(filename="sonar-scanner-engine-shaded-8.9.0.43852-all.jar")
self.sq.get_analysis_engine()
def test_download_analysis_engine(self):
with self.subTest("download_analysis_engine works"), sq_api_mocker() as mocker:
file_content = b"fake_engine_binary"
mocker.mock_analysis_engine_download(body=file_content)
fake_file = io.BytesIO()
self.sq.download_analysis_engine(fake_file)
self.assertEqual(fake_file.getvalue(), file_content)
with (
self.subTest("download_analysis_engine returns 404"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
mocker.mock_analysis_engine_download(status=404)
self.sq.download_analysis_engine(io.BytesIO())
with (
self.subTest("download_analysis_engine: requests throws exception"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
# since the api is not mocked, requests will throw an exception
self.sq.download_analysis_engine(io.BytesIO())
def test_get_analysis_jres(self):
expected_jres: list[JRE] = [
JRE(
id="jre1",
filename="jre1.tar.gz",
sha256="dummysha256value1",
java_path="/path/to/jre1/bin/java",
os="linux",
arch="x64",
download_url="https://example.com/jre1.tar.gz",
),
JRE(
id="jre2",
filename="jre2.tar.gz",
sha256="dummysha256value2",
java_path="/path/to/jre2/bin/java",
os="windows",
arch="x64",
download_url=None,
),
]
with self.subTest("get_analysis_jres works"), sq_api_mocker() as mocker:
mocker.mock_analysis_jres([sq_api_utils.jre_to_dict(jre) for jre in expected_jres])
actual_jres = self.sq.get_analysis_jres(os="linux", arch="x86_64")
self.assertEqual(actual_jres, expected_jres)
with (
self.subTest("get_analysis_jres returns error"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
mocker.mock_analysis_jres(status=404)
self.sq.get_analysis_jres(os="linux", arch="x86_64")
with (
self.subTest("get_analysis_jres returns error when keys are missing"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
mocker.mock_analysis_jres([{"id": "jre1"}])
self.sq.get_analysis_jres(os="linux", arch="x86_64")
def test_download_analysis_jre(self):
jre_id = "jre1"
jre_file_content = b"fake_jre_binary"
with self.subTest("download_analysis_jre works"), sq_api_mocker() as mocker:
mocker.mock_analysis_jre_download(id=jre_id, body=jre_file_content)
fake_file = io.BytesIO()
self.sq.download_analysis_jre(jre_id, fake_file)
self.assertEqual(fake_file.getvalue(), jre_file_content)
with self.subTest("download_analysis_jre works with redirect"), sq_api_mocker() as mocker:
mocker.mock_analysis_jre_download(
id=jre_id, status=302, redirect_url="/api/v2/analysis/jres/redirected-jre"
)
mocker.mock_analysis_jre_download(id="redirected-jre", body=jre_file_content)
fake_file = io.BytesIO()
self.sq.download_analysis_jre(jre_id, fake_file)
self.assertEqual(fake_file.getvalue(), jre_file_content)
with (
self.subTest("download_analysis_jre returns 404"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
mocker.mock_analysis_jre_download(id=jre_id, status=404)
self.sq.download_analysis_jre(jre_id, io.BytesIO())
with (
self.subTest("download_analysis_jre: requests throws exception"),
sq_api_mocker() as mocker,
self.assertRaises(SonarQubeApiException),
):
# since the api is not mocked, requests will throw an exception
self.sq.download_analysis_jre(jre_id, io.BytesIO())
def test_to_api_configuration(self):
with self.subTest("Missing keys"):
expected = {
SONAR_HOST_URL: "",
SONAR_SCANNER_SONARCLOUD_URL: "",
SONAR_SCANNER_API_BASE_URL: "",
SONAR_REGION: "",
}
self.assertEqual(expected, api.to_api_configuration({}))
with self.subTest("All keys"):
expected = {
SONAR_HOST_URL: "https://sonarcloud.io",
SONAR_SCANNER_SONARCLOUD_URL: "https://sonarcloud.io",
SONAR_SCANNER_API_BASE_URL: "https://api.sonarcloud.io",
SONAR_REGION: "us",
}
self.assertEqual(expected, api.to_api_configuration(expected))