forked from p15670423/monkey
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
This commit is contained in:
parent
0175199540
commit
f3c2a2d012
|
@ -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
|
|
|
@ -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
|
|
Loading…
Reference in New Issue