UT: Fix AWS instance unit tests

This commit is contained in:
vakarisz 2022-04-28 17:58:20 +03:00
parent e51b760af0
commit c5a126ff13
1 changed files with 27 additions and 79 deletions

View File

@ -37,9 +37,14 @@ EXPECTED_ACCOUNT_ID = "123456789012"
def get_test_aws_instance(
text={"instance_id": None, "region": None, "account_id": None},
exception={"instance_id": None, "region": None, "account_id": None},
text=None,
exception=None,
):
if text is None:
text = {"instance_id": None, "region": None, "account_id": None}
if exception is None:
exception = {"instance_id": None, "region": None, "account_id": None}
with requests_mock.Mocker() as m:
# request made to get instance_id
url = f"{AWS_LATEST_METADATA_URI_PREFIX}meta-data/instance-id"
@ -64,13 +69,15 @@ def get_test_aws_instance(
# all good data
@pytest.fixture
def good_data_mock_instance():
return get_test_aws_instance(
instance = get_test_aws_instance(
text={
"instance_id": INSTANCE_ID_RESPONSE,
"region": AVAILABILITY_ZONE_RESPONSE,
"account_id": INSTANCE_IDENTITY_DOCUMENT_RESPONSE,
}
)
yield instance
del instance
def test_is_instance_good_data(good_data_mock_instance):
@ -92,13 +99,15 @@ def test_account_id_good_data(good_data_mock_instance):
# 'region' bad data
@pytest.fixture
def bad_region_data_mock_instance():
return get_test_aws_instance(
instance = get_test_aws_instance(
text={
"instance_id": INSTANCE_ID_RESPONSE,
"region": "in-a-different-world",
"account_id": INSTANCE_IDENTITY_DOCUMENT_RESPONSE,
}
)
yield instance
del instance
def test_is_instance_bad_region_data(bad_region_data_mock_instance):
@ -120,68 +129,37 @@ def test_account_id_bad_region_data(bad_region_data_mock_instance):
# 'account_id' bad data
@pytest.fixture
def bad_account_id_data_mock_instance():
return get_test_aws_instance(
instance = get_test_aws_instance(
text={
"instance_id": INSTANCE_ID_RESPONSE,
"region": AVAILABILITY_ZONE_RESPONSE,
"account_id": "who-am-i",
}
)
yield instance
del instance
def test_is_instance_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.is_instance
assert not bad_account_id_data_mock_instance.is_instance
def test_instance_id_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.instance_id == EXPECTED_INSTANCE_ID
assert bad_account_id_data_mock_instance.instance_id is None
def test_region_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.region == EXPECTED_REGION
assert bad_account_id_data_mock_instance.region is None
def test_account_id_data_bad_account_id_data(bad_account_id_data_mock_instance):
assert bad_account_id_data_mock_instance.account_id is None
# 'instance_id' bad requests
@pytest.fixture
def bad_instance_id_request_mock_instance(instance_id_exception):
return get_test_aws_instance(
text={
"instance_id": None,
"region": AVAILABILITY_ZONE_RESPONSE,
"account_id": INSTANCE_IDENTITY_DOCUMENT_RESPONSE,
},
exception={"instance_id": instance_id_exception, "region": None, "account_id": None},
)
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_is_instance_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.is_instance is False
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_instance_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.instance_id is None
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_region_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.region is None
@pytest.mark.parametrize("instance_id_exception", [requests.RequestException, IOError])
def test_account_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
assert bad_instance_id_request_mock_instance.account_id == EXPECTED_ACCOUNT_ID
# 'region' bad requests
@pytest.fixture
def bad_region_request_mock_instance(region_exception):
return get_test_aws_instance(
instance = get_test_aws_instance(
text={
"instance_id": INSTANCE_ID_RESPONSE,
"region": None,
@ -189,16 +167,18 @@ def bad_region_request_mock_instance(region_exception):
},
exception={"instance_id": None, "region": region_exception, "account_id": None},
)
yield instance
del instance
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_is_instance_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.is_instance
assert not bad_region_request_mock_instance.is_instance
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_instance_id_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.instance_id == EXPECTED_INSTANCE_ID
assert bad_region_request_mock_instance.instance_id is None
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
@ -208,40 +188,7 @@ def test_region_bad_region_request(bad_region_request_mock_instance):
@pytest.mark.parametrize("region_exception", [requests.RequestException, IOError])
def test_account_id_bad_region_request(bad_region_request_mock_instance):
assert bad_region_request_mock_instance.account_id == EXPECTED_ACCOUNT_ID
# 'account_id' bad requests
@pytest.fixture
def bad_account_id_request_mock_instance(account_id_exception):
return get_test_aws_instance(
text={
"instance_id": INSTANCE_ID_RESPONSE,
"region": AVAILABILITY_ZONE_RESPONSE,
"account_id": None,
},
exception={"instance_id": None, "region": None, "account_id": account_id_exception},
)
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_is_instance_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.is_instance
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_instance_id_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.instance_id == EXPECTED_INSTANCE_ID
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_region_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.region == EXPECTED_REGION
@pytest.mark.parametrize("account_id_exception", [requests.RequestException, IOError])
def test_account_id_bad_account_id_request(bad_account_id_request_mock_instance):
assert bad_account_id_request_mock_instance.account_id is None
assert bad_region_request_mock_instance.account_id is None
# not found request
@ -261,7 +208,8 @@ def not_found_request_mock_instance():
m.get(url)
not_found_aws_instance_object = AwsInstance()
return not_found_aws_instance_object
yield not_found_aws_instance_object
del not_found_aws_instance_object
def test_is_instance_not_found_request(not_found_request_mock_instance):