UT: Fix names in test_aws_metadata.py

This commit is contained in:
Mike Salvatore 2022-05-06 04:31:10 -04:00
parent 8d24b913df
commit 73e7e31a63
1 changed files with 40 additions and 58 deletions

View File

@ -38,7 +38,7 @@ EXPECTED_REGION = "us-west-2"
EXPECTED_ACCOUNT_ID = "123456789012" EXPECTED_ACCOUNT_ID = "123456789012"
def get_test_aws_instance( def patch_and_call_fetch_aws_instance_metadata(
text=MappingProxyType({"instance_id": None, "region": None, "account_id": None}), text=MappingProxyType({"instance_id": None, "region": None, "account_id": None}),
exception=MappingProxyType({"instance_id": None, "region": None, "account_id": None}), exception=MappingProxyType({"instance_id": None, "region": None, "account_id": None}),
): ):
@ -64,86 +64,80 @@ def get_test_aws_instance(
# all good data # all good data
@pytest.fixture @pytest.fixture
def good_data_mock_instance(): def good_metadata():
instance = get_test_aws_instance( return patch_and_call_fetch_aws_instance_metadata(
text={ text={
"instance_id": INSTANCE_ID_RESPONSE, "instance_id": INSTANCE_ID_RESPONSE,
"region": AVAILABILITY_ZONE_RESPONSE, "region": AVAILABILITY_ZONE_RESPONSE,
"account_id": INSTANCE_IDENTITY_DOCUMENT_RESPONSE, "account_id": INSTANCE_IDENTITY_DOCUMENT_RESPONSE,
} }
) )
yield instance
del instance
def test_instance_id_good_data(good_data_mock_instance): def test_instance_id_good_data(good_metadata):
assert good_data_mock_instance[0] == EXPECTED_INSTANCE_ID assert good_metadata[0] == EXPECTED_INSTANCE_ID
def test_region_good_data(good_data_mock_instance): def test_region_good_data(good_metadata):
assert good_data_mock_instance[1] == EXPECTED_REGION assert good_metadata[1] == EXPECTED_REGION
def test_account_id_good_data(good_data_mock_instance): def test_account_id_good_data(good_metadata):
assert good_data_mock_instance[2] == EXPECTED_ACCOUNT_ID assert good_metadata[2] == EXPECTED_ACCOUNT_ID
# 'region' bad data # 'region' bad data
@pytest.fixture @pytest.fixture
def bad_region_data_mock_instance(): def bad_region_metadata():
instance = get_test_aws_instance( return patch_and_call_fetch_aws_instance_metadata(
text={ text={
"instance_id": INSTANCE_ID_RESPONSE, "instance_id": INSTANCE_ID_RESPONSE,
"region": "in-a-different-world", "region": "in-a-different-world",
"account_id": INSTANCE_IDENTITY_DOCUMENT_RESPONSE, "account_id": INSTANCE_IDENTITY_DOCUMENT_RESPONSE,
} }
) )
yield instance
del instance
def test_instance_id_bad_region_data(bad_region_data_mock_instance): def test_instance_id_bad_region_data(bad_region_metadata):
assert bad_region_data_mock_instance[0] == EXPECTED_INSTANCE_ID assert bad_region_metadata[0] == EXPECTED_INSTANCE_ID
def test_region_bad_region_data(bad_region_data_mock_instance): def test_region_bad_region_data(bad_region_metadata):
assert bad_region_data_mock_instance[1] is None assert bad_region_metadata[1] is None
def test_account_id_bad_region_data(bad_region_data_mock_instance): def test_account_id_bad_region_data(bad_region_metadata):
assert bad_region_data_mock_instance[2] == EXPECTED_ACCOUNT_ID assert bad_region_metadata[2] == EXPECTED_ACCOUNT_ID
# 'account_id' bad data # 'account_id' bad data
@pytest.fixture @pytest.fixture
def bad_account_id_data_mock_instance(): def bad_account_id_metadata():
instance = get_test_aws_instance( return patch_and_call_fetch_aws_instance_metadata(
text={ text={
"instance_id": INSTANCE_ID_RESPONSE, "instance_id": INSTANCE_ID_RESPONSE,
"region": AVAILABILITY_ZONE_RESPONSE, "region": AVAILABILITY_ZONE_RESPONSE,
"account_id": "who-am-i", "account_id": "who-am-i",
} }
) )
yield instance
del instance
def test_instance_id_bad_account_id_data(bad_account_id_data_mock_instance): def test_instance_id_bad_account_id_data(bad_account_id_metadata):
assert bad_account_id_data_mock_instance[0] is None assert bad_account_id_metadata[0] is None
def test_region_bad_account_id_data(bad_account_id_data_mock_instance): def test_region_bad_account_id_data(bad_account_id_metadata):
assert bad_account_id_data_mock_instance[1] is None assert bad_account_id_metadata[1] is None
def test_account_id_data_bad_account_id_data(bad_account_id_data_mock_instance): def test_account_id_data_bad_account_id_data(bad_account_id_metadata):
assert bad_account_id_data_mock_instance[2] is None assert bad_account_id_metadata[2] is None
# 'region' bad requests # 'region' bad requests
@pytest.fixture @pytest.fixture
def bad_region_request_mock_instance(region_exception): def region_request_failure_metadata(region_exception):
instance = get_test_aws_instance( return patch_and_call_fetch_aws_instance_metadata(
text={ text={
"instance_id": INSTANCE_ID_RESPONSE, "instance_id": INSTANCE_ID_RESPONSE,
"region": None, "region": None,
@ -151,28 +145,26 @@ def bad_region_request_mock_instance(region_exception):
}, },
exception={"instance_id": None, "region": region_exception, "account_id": None}, exception={"instance_id": None, "region": region_exception, "account_id": None},
) )
yield instance
del instance
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError]) @pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_instance_id_bad_region_request(bad_region_request_mock_instance): def test_instance_id_bad_region_request(region_request_failure_metadata):
assert bad_region_request_mock_instance[0] is None assert region_request_failure_metadata[0] is None
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError]) @pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_region_bad_region_request(bad_region_request_mock_instance): def test_region_bad_region_request(region_request_failure_metadata):
assert bad_region_request_mock_instance[1] is None assert region_request_failure_metadata[1] is None
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError]) @pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_account_id_bad_region_request(bad_region_request_mock_instance): def test_account_id_bad_region_request(region_request_failure_metadata):
assert bad_region_request_mock_instance[2] is None assert region_request_failure_metadata[2] is None
# not found request # not found request
@pytest.fixture @pytest.fixture
def not_found_request_mock_instance(): def not_found_metadata():
with requests_mock.Mocker() as m: with requests_mock.Mocker() as m:
# request made to get instance_id # request made to get instance_id
url = f"{AWS_LATEST_METADATA_URI_PREFIX}meta-data/instance-id" url = f"{AWS_LATEST_METADATA_URI_PREFIX}meta-data/instance-id"
@ -189,23 +181,13 @@ def not_found_request_mock_instance():
return fetch_aws_instance_metadata() return fetch_aws_instance_metadata()
def test_instance_id_not_found_request(not_found_request_mock_instance): def test_instance_id_not_found_request(not_found_metadata):
assert not_found_request_mock_instance[0] is None assert not_found_metadata[0] is None
def test_region_not_found_request(not_found_request_mock_instance): def test_region_not_found_request(not_found_metadata):
assert not_found_request_mock_instance[1] is None assert not_found_metadata[1] is None
def test_account_id_not_found_request(not_found_request_mock_instance): def test_account_id_not_found_request(not_found_metadata):
assert not_found_request_mock_instance[2] is None assert not_found_metadata[2] is None
def test_instance_id_4xx_error_code():
with requests_mock.Mocker() as m:
# request made to get instance_id
url = f"{AWS_LATEST_METADATA_URI_PREFIX}meta-data/instance-id"
m.get(url, text="1234", status_code=404)
result = fetch_aws_instance_metadata()
assert result[0] is None