forked from p34709852/monkey
BlackBox test fixes: improved the mechanism of locating gcp keys and improved error handling if tests can't connect to gcp
This commit is contained in:
parent
aa959c3879
commit
e76d53a2a8
|
@ -72,8 +72,12 @@ LOGGER = logging.getLogger(__name__)
|
|||
@pytest.fixture(autouse=True, scope="session")
|
||||
def GCPHandler(request, no_gcp):
|
||||
if not no_gcp:
|
||||
try:
|
||||
GCPHandler = gcp_machine_handlers.GCPHandler()
|
||||
GCPHandler.start_machines(" ".join(GCP_TEST_MACHINE_LIST))
|
||||
except Exception as e:
|
||||
LOGGER.error("GCP Handler failed to initialize: %s." % e)
|
||||
pytest.exit("Encountered an error while starting GCP machines. Stopping the tests.")
|
||||
wait_machine_bootup()
|
||||
|
||||
def fin():
|
||||
|
|
|
@ -1,35 +1,47 @@
|
|||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class GCPHandler(object):
|
||||
|
||||
# gcloud commands
|
||||
AUTHENTICATION_COMMAND = "gcloud auth activate-service-account --key-file=%s"
|
||||
SET_PROPERTY_PROJECT = "gcloud config set project %s"
|
||||
MACHINE_STARTING_COMMAND = "gcloud compute instances start %s --zone=%s"
|
||||
MACHINE_STOPPING_COMMAND = "gcloud compute instances stop %s --zone=%s"
|
||||
|
||||
# Default configuration parameters
|
||||
DEFAULT_RELATIVE_KEY_PATH = "../../gcp_keys/gcp_key.json"
|
||||
DEFAULT_ZONE = "europe-west3-a"
|
||||
DEFAULT_PROJECT = "guardicore-22050661"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
key_path="../gcp_keys/gcp_key.json",
|
||||
zone="europe-west3-a",
|
||||
project_id="guardicore-22050661",
|
||||
relative_key_path=DEFAULT_RELATIVE_KEY_PATH,
|
||||
zone=DEFAULT_ZONE,
|
||||
project_id=DEFAULT_PROJECT,
|
||||
):
|
||||
self.zone = zone
|
||||
try:
|
||||
abs_key_path = GCPHandler.get_absolute_key_path(relative_key_path)
|
||||
# pass the key file to gcp
|
||||
subprocess.call(GCPHandler.get_auth_command(key_path), shell=True) # noqa: DUO116
|
||||
subprocess.call(GCPHandler.get_auth_command(abs_key_path), shell=True) # noqa: DUO116
|
||||
LOGGER.info("GCP Handler passed key")
|
||||
# set project
|
||||
subprocess.call( # noqa: DUO116
|
||||
GCPHandler.get_set_project_command(project_id), shell=True
|
||||
)
|
||||
subprocess.call(GCPHandler.get_set_project_command(project_id), shell=True) # noqa: DUO116
|
||||
LOGGER.info("GCP Handler set project")
|
||||
LOGGER.info("GCP Handler initialized successfully")
|
||||
except Exception as e:
|
||||
LOGGER.error("GCP Handler failed to initialize: %s." % e)
|
||||
|
||||
@staticmethod
|
||||
def get_absolute_key_path(relative_key_path: str) -> str:
|
||||
file_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
absolute_key_path = os.path.join(file_dir, relative_key_path)
|
||||
if not os.path.isfile(absolute_key_path):
|
||||
raise FileNotFoundError(
|
||||
"GCP key not found. " "Add a service key to envs/monkey_zoo/gcp_keys/gcp_key.json"
|
||||
)
|
||||
return os.path.join(file_dir, relative_key_path)
|
||||
|
||||
def start_machines(self, machine_list):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue