forked from p34709852/monkey
Merge pull request #1155 from guardicore/bb_test_gcp_authentication_fix
BlackBox test GCP handling fixes
This commit is contained in:
commit
4acf5a0e4b
|
@ -72,8 +72,12 @@ LOGGER = logging.getLogger(__name__)
|
|||
@pytest.fixture(autouse=True, scope="session")
|
||||
def GCPHandler(request, no_gcp):
|
||||
if not no_gcp:
|
||||
GCPHandler = gcp_machine_handlers.GCPHandler()
|
||||
GCPHandler.start_machines(" ".join(GCP_TEST_MACHINE_LIST))
|
||||
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):
|
||||
|
||||
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"
|
||||
|
||||
# Key path location relative to this file's directory
|
||||
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",
|
||||
zone=DEFAULT_ZONE,
|
||||
project_id=DEFAULT_PROJECT,
|
||||
):
|
||||
self.zone = zone
|
||||
try:
|
||||
# pass the key file to gcp
|
||||
subprocess.call(GCPHandler.get_auth_command(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
|
||||
abs_key_path = GCPHandler.get_absolute_key_path()
|
||||
|
||||
subprocess.call(GCPHandler.get_auth_command(abs_key_path), shell=True) # noqa: DUO116
|
||||
LOGGER.info("GCP Handler passed key")
|
||||
|
||||
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")
|
||||
|
||||
@staticmethod
|
||||
def get_absolute_key_path() -> str:
|
||||
file_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
absolute_key_path = os.path.join(file_dir, GCPHandler.RELATIVE_KEY_PATH)
|
||||
absolute_key_path = os.path.realpath(absolute_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")
|
||||
LOGGER.info("GCP Handler initialized successfully")
|
||||
except Exception as e:
|
||||
LOGGER.error("GCP Handler failed to initialize: %s." % e)
|
||||
return absolute_key_path
|
||||
|
||||
def start_machines(self, machine_list):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue