diff --git a/monkey/tests/unit_tests/common/cloud/aws/test_aws_instance.py b/monkey/tests/unit_tests/common/cloud/aws/test_aws_instance.py index 54d8e942a..30cc0ce08 100644 --- a/monkey/tests/unit_tests/common/cloud/aws/test_aws_instance.py +++ b/monkey/tests/unit_tests/common/cloud/aws/test_aws_instance.py @@ -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):