Rename test functions, add 404 response tests, and other tiny changes
This commit is contained in:
parent
016d886781
commit
11a0477dbb
|
@ -32,7 +32,8 @@ class AwsInstance(CloudInstance):
|
||||||
self.account_id = None
|
self.account_id = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.instance_id = requests.get(AWS_LATEST_METADATA_URI_PREFIX + 'meta-data/instance-id', timeout=2).text
|
response = requests.get(AWS_LATEST_METADATA_URI_PREFIX + 'meta-data/instance-id', timeout=2)
|
||||||
|
self.instance_id = response.text if response else None
|
||||||
self.region = self._parse_region(
|
self.region = self._parse_region(
|
||||||
requests.get(AWS_LATEST_METADATA_URI_PREFIX + 'meta-data/placement/availability-zone').text)
|
requests.get(AWS_LATEST_METADATA_URI_PREFIX + 'meta-data/placement/availability-zone').text)
|
||||||
except (requests.RequestException, IOError) as e:
|
except (requests.RequestException, IOError) as e:
|
||||||
|
|
|
@ -98,63 +98,63 @@ def test_get_account_id_good_data(good_data_mock_instance):
|
||||||
|
|
||||||
# 'region' bad data
|
# 'region' bad data
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def bad_data_mock_instance_1():
|
def bad_region_data_mock_instance():
|
||||||
return get_test_aws_instance(text={'instance_id': INSTANCE_ID_RESPONSE,
|
return get_test_aws_instance(text={'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})
|
||||||
|
|
||||||
|
|
||||||
def test_is_instance_bad_data_1(bad_data_mock_instance_1):
|
def test_is_instance_bad_region_data(bad_region_data_mock_instance):
|
||||||
assert bad_data_mock_instance_1.is_instance()
|
assert bad_region_data_mock_instance.is_instance()
|
||||||
|
|
||||||
|
|
||||||
def test_get_cloud_provider_name_bad_data_1(bad_data_mock_instance_1):
|
def test_get_cloud_provider_name_bad_region_data(bad_region_data_mock_instance):
|
||||||
assert bad_data_mock_instance_1.get_cloud_provider_name() == Environment.AWS
|
assert bad_region_data_mock_instance.get_cloud_provider_name() == Environment.AWS
|
||||||
|
|
||||||
|
|
||||||
def test_get_instance_id_bad_data_1(bad_data_mock_instance_1):
|
def test_get_instance_id_bad_region_data(bad_region_data_mock_instance):
|
||||||
assert bad_data_mock_instance_1.get_instance_id() == EXPECTED_INSTANCE_ID
|
assert bad_region_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
|
||||||
|
|
||||||
|
|
||||||
def test_get_region_bad_data_1(bad_data_mock_instance_1):
|
def test_get_region_bad_region_data(bad_region_data_mock_instance):
|
||||||
assert bad_data_mock_instance_1.get_region() is None
|
assert bad_region_data_mock_instance.get_region() is None
|
||||||
|
|
||||||
|
|
||||||
def test_get_account_id_bad_data_1(bad_data_mock_instance_1):
|
def test_get_account_id_bad_region_data(bad_region_data_mock_instance):
|
||||||
assert bad_data_mock_instance_1.get_account_id() == EXPECTED_ACCOUNT_ID
|
assert bad_region_data_mock_instance.get_account_id() == EXPECTED_ACCOUNT_ID
|
||||||
|
|
||||||
|
|
||||||
# 'account_id' bad data
|
# 'account_id' bad data
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def bad_data_mock_instance_2():
|
def bad_account_id_data_mock_instance():
|
||||||
return get_test_aws_instance(text={'instance_id': INSTANCE_ID_RESPONSE,
|
return get_test_aws_instance(text={'instance_id': INSTANCE_ID_RESPONSE,
|
||||||
'region': AVAILABILITY_ZONE_RESPONSE,
|
'region': AVAILABILITY_ZONE_RESPONSE,
|
||||||
'account_id': 'who-am-i'})
|
'account_id': 'who-am-i'})
|
||||||
|
|
||||||
|
|
||||||
def test_is_instance_bad_data_2(bad_data_mock_instance_2):
|
def test_is_instance_bad_account_id_data(bad_account_id_data_mock_instance):
|
||||||
assert bad_data_mock_instance_2.is_instance()
|
assert bad_account_id_data_mock_instance.is_instance()
|
||||||
|
|
||||||
|
|
||||||
def test_get_cloud_provider_name_bad_data_2(bad_data_mock_instance_2):
|
def test_get_cloud_provider_name_bad_account_id_data(bad_account_id_data_mock_instance):
|
||||||
assert bad_data_mock_instance_2.get_cloud_provider_name() == Environment.AWS
|
assert bad_account_id_data_mock_instance.get_cloud_provider_name() == Environment.AWS
|
||||||
|
|
||||||
|
|
||||||
def test_get_instance_id_bad_data_2(bad_data_mock_instance_2):
|
def test_get_instance_id_bad_account_id_data(bad_account_id_data_mock_instance):
|
||||||
assert bad_data_mock_instance_2.get_instance_id() == EXPECTED_INSTANCE_ID
|
assert bad_account_id_data_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
|
||||||
|
|
||||||
|
|
||||||
def test_get_region_bad_data_2(bad_data_mock_instance_2):
|
def test_get_region_bad_account_id_data(bad_account_id_data_mock_instance):
|
||||||
assert bad_data_mock_instance_2.get_region() == EXPECTED_REGION
|
assert bad_account_id_data_mock_instance.get_region() == EXPECTED_REGION
|
||||||
|
|
||||||
|
|
||||||
def test_get_account_id_bad_data_2(bad_data_mock_instance_2):
|
def test_get_account_id_data_bad_account_id_data(bad_account_id_data_mock_instance):
|
||||||
assert bad_data_mock_instance_2.get_account_id() is None
|
assert bad_account_id_data_mock_instance.get_account_id() is None
|
||||||
|
|
||||||
|
|
||||||
# 'instance_id' bad requests
|
# 'instance_id' bad requests
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def bad_request_mock_instance_1(instance_id_exception):
|
def bad_instance_id_request_mock_instance(instance_id_exception):
|
||||||
return get_test_aws_instance(text={'instance_id': None,
|
return get_test_aws_instance(text={'instance_id': None,
|
||||||
'region': AVAILABILITY_ZONE_RESPONSE,
|
'region': AVAILABILITY_ZONE_RESPONSE,
|
||||||
'account_id': INSTANCE_IDENTITY_DOCUMENT_RESPONSE},
|
'account_id': INSTANCE_IDENTITY_DOCUMENT_RESPONSE},
|
||||||
|
@ -164,33 +164,33 @@ def bad_request_mock_instance_1(instance_id_exception):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
||||||
def test_is_instance_bad_request_1(bad_request_mock_instance_1):
|
def test_is_instance_bad_instance_id_request(bad_instance_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_1.is_instance() is False
|
assert bad_instance_id_request_mock_instance.is_instance() is False
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
||||||
def test_get_cloud_provider_name_bad_request_1(bad_request_mock_instance_1):
|
def test_get_cloud_provider_name_bad_instance_id_request(bad_instance_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_1.get_cloud_provider_name() == Environment.AWS
|
assert bad_instance_id_request_mock_instance.get_cloud_provider_name() == Environment.AWS
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
||||||
def test_get_instance_id_bad_request_1(bad_request_mock_instance_1):
|
def test_get_instance_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_1.get_instance_id() is None
|
assert bad_instance_id_request_mock_instance.get_instance_id() is None
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
||||||
def test_get_region_bad_request_1(bad_request_mock_instance_1):
|
def test_get_region_bad_instance_id_request(bad_instance_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_1.get_region() is None
|
assert bad_instance_id_request_mock_instance.get_region() is None
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('instance_id_exception', [requests.RequestException, IOError])
|
||||||
def test_get_account_id_bad_request_1(bad_request_mock_instance_1):
|
def test_get_account_id_bad_instance_id_request(bad_instance_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_1.get_account_id() == EXPECTED_ACCOUNT_ID
|
assert bad_instance_id_request_mock_instance.get_account_id() == EXPECTED_ACCOUNT_ID
|
||||||
|
|
||||||
|
|
||||||
# 'region' bad requests
|
# 'region' bad requests
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def bad_request_mock_instance_2(region_exception):
|
def bad_region_request_mock_instance(region_exception):
|
||||||
return get_test_aws_instance(text={'instance_id': INSTANCE_ID_RESPONSE,
|
return get_test_aws_instance(text={'instance_id': INSTANCE_ID_RESPONSE,
|
||||||
'region': None,
|
'region': None,
|
||||||
'account_id': INSTANCE_IDENTITY_DOCUMENT_RESPONSE},
|
'account_id': INSTANCE_IDENTITY_DOCUMENT_RESPONSE},
|
||||||
|
@ -200,33 +200,33 @@ def bad_request_mock_instance_2(region_exception):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
||||||
def test_is_instance_bad_request_2(bad_request_mock_instance_2):
|
def test_is_instance_bad_region_request(bad_region_request_mock_instance):
|
||||||
assert bad_request_mock_instance_2.is_instance()
|
assert bad_region_request_mock_instance.is_instance()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
||||||
def test_get_cloud_provider_name_bad_request_2(bad_request_mock_instance_2):
|
def test_get_cloud_provider_name_bad_region_request(bad_region_request_mock_instance):
|
||||||
assert bad_request_mock_instance_2.get_cloud_provider_name() == Environment.AWS
|
assert bad_region_request_mock_instance.get_cloud_provider_name() == Environment.AWS
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
||||||
def test_get_instance_id_bad_request_2(bad_request_mock_instance_2):
|
def test_get_instance_id_bad_region_request(bad_region_request_mock_instance):
|
||||||
assert bad_request_mock_instance_2.get_instance_id() == EXPECTED_INSTANCE_ID
|
assert bad_region_request_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
||||||
def test_get_region_bad_request_2(bad_request_mock_instance_2):
|
def test_get_region_bad_region_request(bad_region_request_mock_instance):
|
||||||
assert bad_request_mock_instance_2.get_region() is None
|
assert bad_region_request_mock_instance.get_region() is None
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('region_exception', [requests.RequestException, IOError])
|
||||||
def test_get_account_id_bad_request_2(bad_request_mock_instance_2):
|
def test_get_account_id_bad_region_request(bad_region_request_mock_instance):
|
||||||
assert bad_request_mock_instance_2.get_account_id() == EXPECTED_ACCOUNT_ID
|
assert bad_region_request_mock_instance.get_account_id() == EXPECTED_ACCOUNT_ID
|
||||||
|
|
||||||
|
|
||||||
# 'account_id' bad requests
|
# 'account_id' bad requests
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def bad_request_mock_instance_3(account_id_exception):
|
def bad_account_id_request_mock_instance(account_id_exception):
|
||||||
return get_test_aws_instance(text={'instance_id': INSTANCE_ID_RESPONSE,
|
return get_test_aws_instance(text={'instance_id': INSTANCE_ID_RESPONSE,
|
||||||
'region': AVAILABILITY_ZONE_RESPONSE,
|
'region': AVAILABILITY_ZONE_RESPONSE,
|
||||||
'account_id': None},
|
'account_id': None},
|
||||||
|
@ -236,25 +236,65 @@ def bad_request_mock_instance_3(account_id_exception):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
||||||
def test_is_instance_bad_request_3(bad_request_mock_instance_3):
|
def test_is_instance_bad_account_id_request(bad_account_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_3.is_instance()
|
assert bad_account_id_request_mock_instance.is_instance()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
||||||
def test_get_cloud_provider_name_bad_request_3(bad_request_mock_instance_3):
|
def test_get_cloud_provider_name_bad_account_id_request(bad_account_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_3.get_cloud_provider_name() == Environment.AWS
|
assert bad_account_id_request_mock_instance.get_cloud_provider_name() == Environment.AWS
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
||||||
def test_get_instance_id_bad_request_3(bad_request_mock_instance_3):
|
def test_get_instance_id_bad_account_id_request(bad_account_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_3.get_instance_id() == EXPECTED_INSTANCE_ID
|
assert bad_account_id_request_mock_instance.get_instance_id() == EXPECTED_INSTANCE_ID
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
||||||
def test_get_region_bad_request_3(bad_request_mock_instance_3):
|
def test_get_region_bad_account_id_request(bad_account_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_3.get_region() == EXPECTED_REGION
|
assert bad_account_id_request_mock_instance.get_region() == EXPECTED_REGION
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
@pytest.mark.parametrize('account_id_exception', [requests.RequestException, IOError])
|
||||||
def test_get_account_id_bad_request_3(bad_request_mock_instance_3):
|
def test_get_account_id_bad_account_id_request(bad_account_id_request_mock_instance):
|
||||||
assert bad_request_mock_instance_3.get_account_id() is None
|
assert bad_account_id_request_mock_instance.get_account_id() is None
|
||||||
|
|
||||||
|
|
||||||
|
# not found request
|
||||||
|
@pytest.fixture
|
||||||
|
def not_found_request_mock_instance():
|
||||||
|
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, status_code=404)
|
||||||
|
|
||||||
|
# request made to get region
|
||||||
|
url = f'{AWS_LATEST_METADATA_URI_PREFIX}meta-data/placement/availability-zone'
|
||||||
|
m.get(url)
|
||||||
|
|
||||||
|
# request made to get account_id
|
||||||
|
url = f'{AWS_LATEST_METADATA_URI_PREFIX}dynamic/instance-identity/document'
|
||||||
|
m.get(url)
|
||||||
|
|
||||||
|
not_found_aws_instance_object = AwsInstance()
|
||||||
|
return not_found_aws_instance_object
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_instance_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.is_instance() is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_cloud_provider_name_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.get_cloud_provider_name() == Environment.AWS
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_instance_id_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.get_instance_id() is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_region_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.get_region() is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_account_id_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.get_account_id() is None
|
||||||
|
|
|
@ -44,7 +44,7 @@ class AzureInstance(CloudInstance):
|
||||||
logger.debug("Trying to parse Azure metadata.")
|
logger.debug("Trying to parse Azure metadata.")
|
||||||
self.try_parse_response(response)
|
self.try_parse_response(response)
|
||||||
else:
|
else:
|
||||||
logger.warning(f"On Azure, but metadata response not ok: {response.status_code}")
|
logger.warning(f"Metadata response not ok: {response.status_code}")
|
||||||
except requests.RequestException:
|
except requests.RequestException:
|
||||||
logger.debug("Failed to get response from Azure metadata service: This instance is not on Azure.")
|
logger.debug("Failed to get response from Azure metadata service: This instance is not on Azure.")
|
||||||
|
|
||||||
|
|
|
@ -177,3 +177,23 @@ def test_instance_attributes_bad_request(bad_request_mock_instance):
|
||||||
assert bad_request_mock_instance.instance_name is None
|
assert bad_request_mock_instance.instance_name is None
|
||||||
assert bad_request_mock_instance.instance_id is None
|
assert bad_request_mock_instance.instance_id is None
|
||||||
assert bad_request_mock_instance.location is None
|
assert bad_request_mock_instance.location is None
|
||||||
|
|
||||||
|
|
||||||
|
# not found request
|
||||||
|
@pytest.fixture
|
||||||
|
def not_found_request_mock_instance():
|
||||||
|
return get_test_azure_instance(AZURE_METADATA_SERVICE_URL, status_code=404)
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_instance_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.is_instance() is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_cloud_provider_name_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.get_cloud_provider_name() == Environment.AZURE
|
||||||
|
|
||||||
|
|
||||||
|
def test_instance_attributes_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.instance_name is None
|
||||||
|
assert not_found_request_mock_instance.instance_id is None
|
||||||
|
assert not_found_request_mock_instance.location is None
|
||||||
|
|
|
@ -39,3 +39,17 @@ def test_is_instance_bad_request(bad_request_mock_instance):
|
||||||
|
|
||||||
def test_get_cloud_provider_name_bad_request(bad_request_mock_instance):
|
def test_get_cloud_provider_name_bad_request(bad_request_mock_instance):
|
||||||
assert bad_request_mock_instance.get_cloud_provider_name() == Environment.GCP
|
assert bad_request_mock_instance.get_cloud_provider_name() == Environment.GCP
|
||||||
|
|
||||||
|
|
||||||
|
# not found request
|
||||||
|
@pytest.fixture
|
||||||
|
def not_found_request_mock_instance():
|
||||||
|
return get_test_gcp_instance(GCP_METADATA_SERVICE_URL, status_code=404)
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_instance_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.is_instance() is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_cloud_provider_name_not_found_request(not_found_request_mock_instance):
|
||||||
|
assert not_found_request_mock_instance.get_cloud_provider_name() == Environment.GCP
|
||||||
|
|
Loading…
Reference in New Issue