forked from p15670423/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")
|
@pytest.fixture(autouse=True, scope="session")
|
||||||
def GCPHandler(request, no_gcp):
|
def GCPHandler(request, no_gcp):
|
||||||
if not no_gcp:
|
if not no_gcp:
|
||||||
GCPHandler = gcp_machine_handlers.GCPHandler()
|
try:
|
||||||
GCPHandler.start_machines(" ".join(GCP_TEST_MACHINE_LIST))
|
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()
|
wait_machine_bootup()
|
||||||
|
|
||||||
def fin():
|
def fin():
|
||||||
|
|
|
@ -1,35 +1,47 @@
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class GCPHandler(object):
|
class GCPHandler(object):
|
||||||
|
# gcloud commands
|
||||||
AUTHENTICATION_COMMAND = "gcloud auth activate-service-account --key-file=%s"
|
AUTHENTICATION_COMMAND = "gcloud auth activate-service-account --key-file=%s"
|
||||||
SET_PROPERTY_PROJECT = "gcloud config set project %s"
|
SET_PROPERTY_PROJECT = "gcloud config set project %s"
|
||||||
MACHINE_STARTING_COMMAND = "gcloud compute instances start %s --zone=%s"
|
MACHINE_STARTING_COMMAND = "gcloud compute instances start %s --zone=%s"
|
||||||
MACHINE_STOPPING_COMMAND = "gcloud compute instances stop %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__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
key_path="../gcp_keys/gcp_key.json",
|
relative_key_path=DEFAULT_RELATIVE_KEY_PATH,
|
||||||
zone="europe-west3-a",
|
zone=DEFAULT_ZONE,
|
||||||
project_id="guardicore-22050661",
|
project_id=DEFAULT_PROJECT,
|
||||||
):
|
):
|
||||||
self.zone = zone
|
self.zone = zone
|
||||||
try:
|
abs_key_path = GCPHandler.get_absolute_key_path(relative_key_path)
|
||||||
# pass the key file to gcp
|
# 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")
|
LOGGER.info("GCP Handler passed key")
|
||||||
# set project
|
# set project
|
||||||
subprocess.call( # noqa: DUO116
|
subprocess.call(GCPHandler.get_set_project_command(project_id), shell=True) # noqa: DUO116
|
||||||
GCPHandler.get_set_project_command(project_id), shell=True
|
LOGGER.info("GCP Handler set project")
|
||||||
|
LOGGER.info("GCP Handler initialized successfully")
|
||||||
|
|
||||||
|
@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"
|
||||||
)
|
)
|
||||||
LOGGER.info("GCP Handler set project")
|
return os.path.join(file_dir, relative_key_path)
|
||||||
LOGGER.info("GCP Handler initialized successfully")
|
|
||||||
except Exception as e:
|
|
||||||
LOGGER.error("GCP Handler failed to initialize: %s." % e)
|
|
||||||
|
|
||||||
def start_machines(self, machine_list):
|
def start_machines(self, machine_list):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue