|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +import os |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from smithy_aws_core.config.sources import EnvironmentSource |
| 7 | + |
| 8 | + |
| 9 | +class TestEnvironmentSource: |
| 10 | + def test_source_name(self): |
| 11 | + source = EnvironmentSource() |
| 12 | + assert source.name == "environment" |
| 13 | + |
| 14 | + def test_get_region_from_aws_region(self): |
| 15 | + with patch.dict(os.environ, {"AWS_REGION": "us-west-2"}, clear=True): |
| 16 | + source = EnvironmentSource() |
| 17 | + value = source.get("region") |
| 18 | + assert value == "us-west-2" |
| 19 | + |
| 20 | + def test_get_returns_none_when_env_var_not_set(self): |
| 21 | + with patch.dict(os.environ, {}, clear=True): |
| 22 | + source = EnvironmentSource() |
| 23 | + value = source.get("region") |
| 24 | + assert value is None |
| 25 | + |
| 26 | + def test_get_returns_none_for_unknown_key(self): |
| 27 | + source = EnvironmentSource() |
| 28 | + value = source.get("unknown_config_key") |
| 29 | + assert value is None |
| 30 | + |
| 31 | + def test_get_handles_empty_string_env_var(self): |
| 32 | + with patch.dict(os.environ, {"AWS_REGION": ""}, clear=True): |
| 33 | + source = EnvironmentSource() |
| 34 | + value = source.get("region") |
| 35 | + assert value == "" |
| 36 | + |
| 37 | + def test_get_handles_whitespace_env_var(self): |
| 38 | + with patch.dict(os.environ, {"AWS_REGION": " us-west-2 "}, clear=True): |
| 39 | + source = EnvironmentSource() |
| 40 | + value = source.get("region") |
| 41 | + # Whitespaces should be stripped |
| 42 | + assert value == "us-west-2" |
| 43 | + |
| 44 | + def test_get_handles_whole_whitespace_env_var(self): |
| 45 | + with patch.dict(os.environ, {"AWS_REGION": " "}, clear=True): |
| 46 | + source = EnvironmentSource() |
| 47 | + value = source.get("region") |
| 48 | + # Whitespaces should be stripped |
| 49 | + assert value == "" |
| 50 | + |
| 51 | + def test_multiple_keys_with_different_env_vars(self): |
| 52 | + env_vars = {"AWS_REGION": "eu-west-1", "AWS_RETRY_MODE": "standard"} |
| 53 | + with patch.dict(os.environ, env_vars, clear=True): |
| 54 | + source = EnvironmentSource() |
| 55 | + |
| 56 | + region = source.get("region") |
| 57 | + retry_mode = source.get("retry_mode") |
| 58 | + |
| 59 | + assert region == "eu-west-1" |
| 60 | + assert retry_mode == "standard" |
| 61 | + |
| 62 | + def test_get_is_idempotent(self): |
| 63 | + with patch.dict(os.environ, {"AWS_REGION": "ap-south-1"}, clear=True): |
| 64 | + source = EnvironmentSource() |
| 65 | + # Calling get on source multiple times should return the same value |
| 66 | + value1 = source.get("region") |
| 67 | + value2 = source.get("region") |
| 68 | + value3 = source.get("region") |
| 69 | + |
| 70 | + assert value1 == value2 == value3 == "ap-south-1" |
| 71 | + |
| 72 | + def test_source_does_not_cache_env_vars(self): |
| 73 | + source = EnvironmentSource() |
| 74 | + |
| 75 | + # First read |
| 76 | + with patch.dict(os.environ, {"AWS_REGION": "us-east-1"}, clear=True): |
| 77 | + value1 = source.get("region") |
| 78 | + assert value1 == "us-east-1" |
| 79 | + |
| 80 | + # Environment changes |
| 81 | + with patch.dict(os.environ, {"AWS_REGION": "us-west-2"}, clear=False): |
| 82 | + value2 = source.get("region") |
| 83 | + assert value2 == "us-west-2" |
| 84 | + |
| 85 | + # Source reads from os.environ and not from cache |
| 86 | + assert value1 != value2 |
0 commit comments