From f3c2a2d01254927a05774c0670c968bd166bcef5 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 16 Nov 2021 11:44:31 +0200 Subject: [PATCH] Common, UT: remove unused GCP cloud instance infrastructure. This infrastructure/classes store information about cloud instances monkeys are ran upon, but this data isn't used anywhere --- monkey/common/cloud/azure/__init__.py | 0 monkey/common/cloud/gcp/__init__.py | 0 monkey/common/cloud/gcp/gcp_instance.py | 54 ------------------ .../common/cloud/gcp/test_gcp_instance.py | 55 ------------------- 4 files changed, 109 deletions(-) delete mode 100644 monkey/common/cloud/azure/__init__.py delete mode 100644 monkey/common/cloud/gcp/__init__.py delete mode 100644 monkey/common/cloud/gcp/gcp_instance.py delete mode 100644 monkey/tests/unit_tests/common/cloud/gcp/test_gcp_instance.py diff --git a/monkey/common/cloud/azure/__init__.py b/monkey/common/cloud/azure/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/monkey/common/cloud/gcp/__init__.py b/monkey/common/cloud/gcp/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/monkey/common/cloud/gcp/gcp_instance.py b/monkey/common/cloud/gcp/gcp_instance.py deleted file mode 100644 index 1fc208165..000000000 --- a/monkey/common/cloud/gcp/gcp_instance.py +++ /dev/null @@ -1,54 +0,0 @@ -import logging - -import requests - -from common.cloud.environment_names import Environment -from common.cloud.instance import CloudInstance -from common.common_consts.timeouts import SHORT_REQUEST_TIMEOUT - -logger = logging.getLogger(__name__) - -GCP_METADATA_SERVICE_URL = "http://metadata.google.internal/" - - -class GcpInstance(CloudInstance): - """ - Used to determine if on GCP. See https://cloud.google.com/compute/docs/storing-retrieving - -metadata#runninggce - """ - - def is_instance(self): - return self._on_gcp - - def get_cloud_provider_name(self) -> Environment: - return Environment.GCP - - def __init__(self): - self._on_gcp = False - - try: - # If not on GCP, this domain shouldn't resolve. - response = requests.get(GCP_METADATA_SERVICE_URL, timeout=SHORT_REQUEST_TIMEOUT) - - if response: - logger.debug("Got ok metadata response: on GCP") - self._on_gcp = True - - if "Metadata-Flavor" not in response.headers: - logger.warning("Got unexpected GCP Metadata format") - else: - if not response.headers["Metadata-Flavor"] == "Google": - logger.warning( - "Got unexpected Metadata flavor: {}".format( - response.headers["Metadata-Flavor"] - ) - ) - else: - logger.warning( - "On GCP, but metadata response not ok: {}".format(response.status_code) - ) - except requests.RequestException: - logger.debug( - "Failed to get response from GCP metadata service: This instance is not on GCP" - ) - self._on_gcp = False diff --git a/monkey/tests/unit_tests/common/cloud/gcp/test_gcp_instance.py b/monkey/tests/unit_tests/common/cloud/gcp/test_gcp_instance.py deleted file mode 100644 index 9170b81c5..000000000 --- a/monkey/tests/unit_tests/common/cloud/gcp/test_gcp_instance.py +++ /dev/null @@ -1,55 +0,0 @@ -import pytest -import requests -import requests_mock - -from common.cloud.environment_names import Environment -from common.cloud.gcp.gcp_instance import GCP_METADATA_SERVICE_URL, GcpInstance - - -def get_test_gcp_instance(url, **kwargs): - with requests_mock.Mocker() as m: - m.get(url, **kwargs) - test_gcp_instance_object = GcpInstance() - return test_gcp_instance_object - - -# good request -@pytest.fixture -def good_request_mock_instance(): - return get_test_gcp_instance(GCP_METADATA_SERVICE_URL) - - -def test_is_instance_good_request(good_request_mock_instance): - assert good_request_mock_instance.is_instance() - - -def test_get_cloud_provider_name_good_request(good_request_mock_instance): - assert good_request_mock_instance.get_cloud_provider_name() == Environment.GCP - - -# bad request -@pytest.fixture -def bad_request_mock_instance(): - return get_test_gcp_instance(GCP_METADATA_SERVICE_URL, exc=requests.RequestException) - - -def test_is_instance_bad_request(bad_request_mock_instance): - assert bad_request_mock_instance.is_instance() is False - - -def test_get_cloud_provider_name_bad_request(bad_request_mock_instance): - 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