|
21 | 21 | validate_encryption_source, validate_source_uri, |
22 | 22 | validate_source_url, |
23 | 23 | validate_encryption_services, as_user_validator, |
24 | | - get_not_none_validator, validate_upload_blob) |
| 24 | + get_not_none_validator, validate_upload_blob, |
| 25 | + validate_client_parameters) |
25 | 26 |
|
26 | 27 |
|
27 | 28 | class MockCLI(CLI): |
@@ -254,6 +255,62 @@ def test_validate_upload_blob(self): |
254 | 255 | validate_upload_blob(Namespace(file_path=None, data=None)) |
255 | 256 |
|
256 | 257 |
|
| 258 | +class TestClientParametersValidator(unittest.TestCase): |
| 259 | + def setUp(self): |
| 260 | + self.cli = MockCLI() |
| 261 | + self.local_url = 'http://localhost:10000/devstoreaccount1' |
| 262 | + self.legacy_url = 'http://legacy:10000/devstoreaccount1' |
| 263 | + |
| 264 | + def test_service_endpoint_env_var_used(self): |
| 265 | + # AZURE_STORAGE_SERVICE_ENDPOINT is the documented env var, should set account_url |
| 266 | + ns = self._create_namespace(auth_mode='key') |
| 267 | + self._validate(ns, AZURE_STORAGE_SERVICE_ENDPOINT=self.local_url) |
| 268 | + self.assertEqual(ns.account_url, self.local_url) |
| 269 | + |
| 270 | + def test_account_url_env_var_still_used(self): |
| 271 | + # AZURE_STORAGE_ACCOUNT_URL is the legacy env var, should keep working |
| 272 | + ns = self._create_namespace(auth_mode='key') |
| 273 | + self._validate(ns, AZURE_STORAGE_ACCOUNT_URL=self.legacy_url) |
| 274 | + self.assertEqual(ns.account_url, self.legacy_url) |
| 275 | + |
| 276 | + def test_service_endpoint_env_var_wins_over_account_url(self): |
| 277 | + # both env vars set, documented one takes precedence |
| 278 | + ns = self._create_namespace(auth_mode='key') |
| 279 | + self._validate(ns, AZURE_STORAGE_SERVICE_ENDPOINT=self.local_url, |
| 280 | + AZURE_STORAGE_ACCOUNT_URL=self.legacy_url) |
| 281 | + self.assertEqual(ns.account_url, self.local_url) |
| 282 | + |
| 283 | + def test_no_endpoint_env_var(self): |
| 284 | + # no endpoint env var, account_url stays empty so client factory falls back to public cloud |
| 285 | + ns = self._create_namespace(auth_mode='key') |
| 286 | + self._validate(ns) |
| 287 | + self.assertIsNone(ns.account_url) |
| 288 | + |
| 289 | + def test_endpoint_argument_wins_over_env_var(self): |
| 290 | + # --blob-endpoint given explicitly, env var is ignored |
| 291 | + ns = self._create_namespace(auth_mode='key', account_url='https://flag.example.com') |
| 292 | + self._validate(ns, AZURE_STORAGE_SERVICE_ENDPOINT=self.local_url) |
| 293 | + self.assertEqual(ns.account_url, 'https://flag.example.com') |
| 294 | + |
| 295 | + def test_service_endpoint_env_var_used_without_auth_mode(self): |
| 296 | + # commands without --auth-mode go through the second lookup block in the validator |
| 297 | + ns = self._create_namespace() |
| 298 | + self._validate(ns, AZURE_STORAGE_SERVICE_ENDPOINT=self.local_url) |
| 299 | + self.assertEqual(ns.account_url, self.local_url) |
| 300 | + |
| 301 | + def _create_namespace(self, **kwargs): |
| 302 | + ns = Namespace(account_name=None, account_url=None, account_key=None, sas_token=None, |
| 303 | + connection_string=None) |
| 304 | + for key in kwargs: |
| 305 | + setattr(ns, key, kwargs[key]) |
| 306 | + return ns |
| 307 | + |
| 308 | + def _validate(self, ns, **env): |
| 309 | + env.update(AZURE_STORAGE_ACCOUNT='devstoreaccount1', AZURE_STORAGE_KEY='fake_key') |
| 310 | + with mock.patch.dict('os.environ', env, clear=True): |
| 311 | + validate_client_parameters(MockCmd(self.cli), ns) |
| 312 | + |
| 313 | + |
257 | 314 | class TestEncryptionValidators(unittest.TestCase): |
258 | 315 | def setUp(self): |
259 | 316 | self.cli = MockCLI() |
|
0 commit comments