From 09218ef3d305b1fe1804e102e61175b6a5ca26b8 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 6 May 2022 03:57:50 -0400 Subject: [PATCH] UT: Add tests for AWSInstance --- .../common/aws/test_aws_instance.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 monkey/tests/unit_tests/common/aws/test_aws_instance.py diff --git a/monkey/tests/unit_tests/common/aws/test_aws_instance.py b/monkey/tests/unit_tests/common/aws/test_aws_instance.py new file mode 100644 index 000000000..3c1f22ab2 --- /dev/null +++ b/monkey/tests/unit_tests/common/aws/test_aws_instance.py @@ -0,0 +1,54 @@ +import pytest + +from common.aws import AWSInstance + +INSTANCE_ID = "1234" +REGION = "USA" +ACCOUNT_ID = "4321" + + +@pytest.fixture +def patch_fetch_metadata(monkeypatch): + def inner(instance_id: str, region: str, account_id: str): + return_value = (instance_id, region, account_id) + monkeypatch.setattr( + "common.aws.aws_instance.fetch_aws_instance_metadata", lambda: return_value + ) + + return inner + + +@pytest.fixture(autouse=True) +def patch_fetch_metadata_default_values(patch_fetch_metadata): + patch_fetch_metadata(INSTANCE_ID, REGION, ACCOUNT_ID) + + +def test_is_instance__true(): + aws_instance = AWSInstance() + + assert aws_instance.is_instance + + +def test_is_instance__false(patch_fetch_metadata): + patch_fetch_metadata(None, "", "") + aws_instance = AWSInstance() + + assert not aws_instance.is_instance + + +def test_instance_id(): + aws_instance = AWSInstance() + + assert aws_instance.instance_id == INSTANCE_ID + + +def test_region(): + aws_instance = AWSInstance() + + assert aws_instance.region == REGION + + +def test_account_id(): + aws_instance = AWSInstance() + + assert aws_instance.account_id == ACCOUNT_ID