forked from connelldave/botocove
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix effect on target regions of assuming_session
Change the host account init to pass these tests: * `test_when_no_default_region_then_cove_uses_assuming_session_region` * `test_cove_prefers_assuming_session_region` A side effect of the change is even if the caller doesn't specify a target region then the cove result includes a `Region` key. (PR connelldave#79 shows how to avoid that side effect, but it's simpler to accept it.) Set the default region for all tests to allow me to test for that side effect. A real user will always set the region from a profile or an environment variable if one isn't passed explicitly to botocove. The default region forces me to update the `test_regions` module. Without setting a default region, the old test still passed! * Old: `test_when_region_is_unspecified_then_result_has_no_region_key` * New: `test_when_region_is_unspecified_then_result_has_default_region_key` The default region makes a `Region` key appear in tests that check the whole cove output instead of a single feature. Later I'll refactor these tests because it's not clear what feature they cover. The default region is `eu-west-1` and all region arguments use some other value such as `eu-central-1` or `us-east-1`. This convention makes the tests easier to read. Use the `_no_default_region` fixture when you really need to test how cove behaves without the default region. Using the `_org_with_one_member` fixture and the `raise_exception=True` parameter together make the tests easier to write because you can assume the cove output has a single result and no exceptions. If any exception occurs, use `pytest.raises` to detect it. Later I'll refactor more tests to use this calling style. We can use dedicated test modules to cover the behavior of `raise_exception=False` and of an organization with multiple accounts. This paves the way to querying the opt-in regions in connelldave#77.
- Loading branch information
Showing
9 changed files
with
82 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pytest | ||
from boto3.session import Session | ||
from botocore.exceptions import NoRegionError | ||
from moto import mock_ec2 | ||
|
||
from botocove import cove | ||
|
||
# Query the region with different configurations of assuming session and | ||
# environment variables. To make the assertions easier all `cove` calls set | ||
# `raise_exception`. If set the assuming session region and the default region | ||
# are always distinct to be able to assert the source of the query result. | ||
|
||
def _query_region(session: Session) -> str: | ||
with mock_ec2(): | ||
response = session.client("ec2").describe_availability_zones() | ||
return response["AvailabilityZones"][0]["RegionName"] | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def _org_with_one_member(mock_session: Session) -> None: | ||
org_client = mock_session.client("organizations") | ||
org_client.create_organization(FeatureSet="ALL") | ||
org_client.create_account(Email="[email protected]", AccountName="Account 1") | ||
|
||
|
||
@pytest.mark.usefixtures("_no_default_region") | ||
def test_when_no_assuming_session_and_no_default_region_then_cove_raises_error() -> None: # noqa: 501 | ||
with pytest.raises(NoRegionError, match=r"^You must specify a region\.$"): | ||
cove(_query_region, raise_exception=True)() | ||
|
||
|
||
def test_when_no_assuming_session_then_cove_uses_default_region() -> None: | ||
output = cove(_query_region, raise_exception=True)() | ||
assert output["Results"][0]["Result"] == "eu-west-1" | ||
|
||
|
||
@pytest.mark.usefixtures("_no_default_region") | ||
def test_when_no_default_region_then_cove_uses_assuming_session_region() -> None: | ||
output = cove( | ||
_query_region, | ||
assuming_session=Session(region_name="eu-central-1"), | ||
raise_exception=True, | ||
)() | ||
assert output["Results"][0]["Result"] == "eu-central-1" | ||
|
||
|
||
def test_cove_prefers_assuming_session_region() -> None: | ||
output = cove( | ||
_query_region, | ||
assuming_session=Session(region_name="eu-central-1"), | ||
raise_exception=True, | ||
)() | ||
assert output["Results"][0]["Result"] == "eu-central-1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters