|
31 | 31 | SONAR_SCANNER_SONARCLOUD_URL,
|
32 | 32 | )
|
33 | 33 | from pysonar_scanner.api import SQVersion
|
| 34 | +from pysonar_scanner.exceptions import InconsistentConfiguration |
34 | 35 | from tests.unit import sq_api_utils
|
35 | 36 | from tests.unit.sq_api_utils import sq_api_mocker
|
36 | 37 |
|
@@ -139,33 +140,29 @@ class TestCaseDict(TypedDict):
|
139 | 140 | base_url="https://sonarcloud.io", api_base_url="https://api.sonarcloud.io", is_sonar_qube_cloud=True
|
140 | 141 | ),
|
141 | 142 | },
|
142 |
| - # SQ:Cloud region tests |
| 143 | + # Region parameter |
143 | 144 | {
|
144 |
| - "name": "When region is set, use region in base urls", |
| 145 | + "name": "When region is set to US, use US base urls", |
145 | 146 | "config": {
|
146 |
| - SONAR_HOST_URL: "https://sonarcloud.io", |
| 147 | + SONAR_HOST_URL: "/", |
147 | 148 | SONAR_SCANNER_SONARCLOUD_URL: "",
|
148 | 149 | SONAR_SCANNER_API_BASE_URL: "",
|
149 | 150 | SONAR_REGION: "us",
|
150 | 151 | },
|
151 | 152 | "expected": BaseUrls(
|
152 |
| - base_url="https://us.sonarcloud.io", |
153 |
| - api_base_url="https://api.us.sonarcloud.io", |
154 |
| - is_sonar_qube_cloud=True, |
| 153 | + base_url="https://sonarqube.us", api_base_url="https://api.sonarqube.us", is_sonar_qube_cloud=True |
155 | 154 | ),
|
156 | 155 | },
|
157 | 156 | {
|
158 |
| - "name": "Ignore region when sonarcloud_url and api_base_url is set", |
| 157 | + "name": "US region and US base url is fine", |
159 | 158 | "config": {
|
160 |
| - SONAR_HOST_URL: "https://custom-sq-cloud.io", |
161 |
| - SONAR_SCANNER_SONARCLOUD_URL: "https://custom-sq-cloud.io", |
162 |
| - SONAR_SCANNER_API_BASE_URL: "https://other-api.custom-sq-cloud.io", |
| 159 | + SONAR_HOST_URL: "https://sonarqube.us", |
| 160 | + SONAR_SCANNER_SONARCLOUD_URL: "", |
| 161 | + SONAR_SCANNER_API_BASE_URL: "", |
163 | 162 | SONAR_REGION: "us",
|
164 | 163 | },
|
165 | 164 | "expected": BaseUrls(
|
166 |
| - base_url="https://custom-sq-cloud.io", |
167 |
| - api_base_url="https://other-api.custom-sq-cloud.io", |
168 |
| - is_sonar_qube_cloud=True, |
| 165 | + base_url="https://sonarqube.us", api_base_url="https://api.sonarqube.us", is_sonar_qube_cloud=True |
169 | 166 | ),
|
170 | 167 | },
|
171 | 168 | # SQ:Server tests
|
@@ -204,6 +201,56 @@ class TestCaseDict(TypedDict):
|
204 | 201 | self.assertEqual(base_urls.is_sonar_qube_cloud, expected.is_sonar_qube_cloud)
|
205 | 202 | self.assertEqual(base_urls, expected)
|
206 | 203 |
|
| 204 | + def test_inconsistent_urls_raises_exception(self): |
| 205 | + class TestCaseDict(TypedDict): |
| 206 | + name: str |
| 207 | + config: dict[Key, any] |
| 208 | + expected: str |
| 209 | + |
| 210 | + cases: list[TestCaseDict] = [ |
| 211 | + { |
| 212 | + "name": "US region and global SQCloud URL", |
| 213 | + "config": { |
| 214 | + SONAR_HOST_URL: "https://sonarcloud.io", |
| 215 | + SONAR_SCANNER_SONARCLOUD_URL: "", |
| 216 | + SONAR_SCANNER_API_BASE_URL: "", |
| 217 | + SONAR_REGION: "us", |
| 218 | + }, |
| 219 | + "expected": "Inconsistent values for properties 'sonar.region' and 'sonar.host.url'. " |
| 220 | + "Please only specify one of the two properties.", |
| 221 | + }, |
| 222 | + { |
| 223 | + "name": "Region set with unknown SQCloud URL", |
| 224 | + "config": { |
| 225 | + SONAR_HOST_URL: "https://custom-sq-cloud.io", |
| 226 | + SONAR_SCANNER_SONARCLOUD_URL: "https://custom-sq-cloud.io", |
| 227 | + SONAR_SCANNER_API_BASE_URL: "https://other-api.custom-sq-cloud.io", |
| 228 | + SONAR_REGION: "us", |
| 229 | + }, |
| 230 | + "expected": "Inconsistent values for properties 'sonar.region' and 'sonar.host.url'. " |
| 231 | + "Please only specify one of the two properties.", |
| 232 | + }, |
| 233 | + { |
| 234 | + "name": "Unsupported region parameter", |
| 235 | + "config": { |
| 236 | + SONAR_HOST_URL: "", |
| 237 | + SONAR_SCANNER_SONARCLOUD_URL: "", |
| 238 | + SONAR_SCANNER_API_BASE_URL: "", |
| 239 | + SONAR_REGION: "fr", |
| 240 | + }, |
| 241 | + "expected": "Invalid region 'fr'. Valid regions are: 'us'. " |
| 242 | + "Please check the 'sonar.region' property " |
| 243 | + "or the 'SONAR_REGION' environment variable.", |
| 244 | + }, |
| 245 | + ] |
| 246 | + |
| 247 | + for case in cases: |
| 248 | + config = case["config"] |
| 249 | + expected = case["expected"] |
| 250 | + with self.subTest(case["name"], config=config, expected=expected): |
| 251 | + with self.assertRaises(InconsistentConfiguration, msg=expected): |
| 252 | + get_base_urls(config) |
| 253 | + |
207 | 254 |
|
208 | 255 | class TestSonarQubeApiWithUnreachableSQServer(unittest.TestCase):
|
209 | 256 | def setUp(self):
|
|
0 commit comments