Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kthare10 committed Jan 13, 2024
1 parent 907abb5 commit dff10ec
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 31 deletions.
65 changes: 51 additions & 14 deletions fabrictestbed_extensions/fablib/fablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,14 +572,23 @@ def __init__(
def validate_config(self):
"""
Validate Fablib config - checks if all the required configuration exists for slice provisioning to work successfully
- Checks Credential Manager Host is configured properly
- Checks Orchestrator Host is configured properly
- Checks Core API Host is configured properly
- Checks Bastion Host is configured properly
- Check Sliver keys exist
- Check Bastion keys exist and are not expired
- Check Bastion Username is configured
- Check Project Id is configured
@raises Exception if the configuration is invalid
"""
Utils.is_reachable(hostname=self.get_credmgr_host(), port=443)
Expand Down Expand Up @@ -608,16 +617,18 @@ def validate_config(self):

print("Configuration is valid and please save the config!")

def get_user_info(self):
def get_user_info(self) -> dict:
"""
Get User information
:return returns a dictionary containing User's Information
:rtype: dict
"""
return self.get_slice_manager().get_user_info()

def determine_bastion_username(self):
"""
Determine Bastion Username
Determine Bastion Username.
Query User Information from Core API and updates the bastion username
"""
# Fetch User Info and Projects
Expand All @@ -632,6 +643,9 @@ def determine_bastion_username(self):
)

def create_ssh_config(self):
"""
Create SSH config file
"""
bastion_ssh_config_file = self.get_bastion_ssh_config_file()
if bastion_ssh_config_file is None or os.path.exists(bastion_ssh_config_file):
raise ConfigException("Bastion SSH Config File location not specified")
Expand Down Expand Up @@ -709,9 +723,15 @@ def create_bastion_keys(
):
"""
Create Bastion Keys
@param bastion_key_location bastion key location
@param store_pubkey flag indicating if the public key should be saved
@param overwrite overwrite the bastion key file if it exists already
:param bastion_key_location: bastion key location
:type bastion_key_location: str
:param store_pubkey: flag indicating if the public key should be saved
:type store_pubkey: bool
:param overwrite: overwrite the bastion key file if it exists already
:type overwrite: bool
"""
if bastion_key_location is None:
bastion_key_location = self.get_bastion_key_location()
Expand Down Expand Up @@ -742,9 +762,15 @@ def create_sliver_keys(
):
"""
Create Sliver Keys
@param sliver_priv_key_location bastion key location
@param store_pubkey flag indicating if the public key should be saved
@param overwrite overwrite the bastion key file if it exists already
:param sliver_priv_key_location: sliver key location
:type sliver_priv_key_location: str
:param store_pubkey: flag indicating if the public key should be saved
:type store_pubkey: bool
:param overwrite: overwrite the bastion key file if it exists already
:type overwrite: bool
"""
if sliver_priv_key_location is None:
sliver_priv_key_location = self.get_default_slice_private_key_file()
Expand Down Expand Up @@ -779,12 +805,23 @@ def __create_and_save_key(
):
"""
Create Key and save key
@param private_file_path private key location
@param description description
@param key_type key type bastion or sliver
@param public_file_path public key location
@param comment comment
@param store_pubkey flag indicating if the public key should be saved
:param private_file_path: private key location
:type private_file_path: str
:param description: description
:type description: str
:param key_type: key type bastion or sliver
:type key_type: str
:param public_file_path: public key location
:type public_file_path: str
:param comment: comment
:type comment: str
:param store_pubkey flag indicating if the public key should be saved
:type store_pubkey: bool
"""
ssh_keys = self.get_slice_manager().create_ssh_keys(
key_type=key_type,
Expand Down
54 changes: 37 additions & 17 deletions tests/unit/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from fabrictestbed.slice_manager import SliceManagerException
from fabrictestbed.util.constants import Constants

from fabrictestbed_extensions.fablib.config.config import ConfigException, Config
from fabrictestbed_extensions.fablib.config.config import Config, ConfigException
from fabrictestbed_extensions.fablib.constants import Constants as FablibConstants
from fabrictestbed_extensions.fablib.fablib import FablibManager

Expand All @@ -29,7 +29,9 @@ class FablibManagerTests(unittest.TestCase):
FablibConstants.FABRIC_BASTION_KEY_LOCATION,
]

DUMMY_TOKEN_LOCATION = str(pathlib.Path(__file__).parent / "data" / "dummy-token.json")
DUMMY_TOKEN_LOCATION = str(
pathlib.Path(__file__).parent / "data" / "dummy-token.json"
)

def setUp(self):
# Run each test with an empty environment.
Expand Down Expand Up @@ -58,7 +60,9 @@ def test_fablib_manager_test_only_orchestrator_host(self):
def test_fablib_manager_test_only_project_id(self):
os.environ[Constants.FABRIC_PROJECT_ID] = "dummy"
os.environ[Constants.FABRIC_TOKEN_LOCATION] = self.DUMMY_TOKEN_LOCATION
self.assertRaises(SliceManagerException, FablibManager, fabric_rc=self.rcfile.name)
self.assertRaises(
SliceManagerException, FablibManager, fabric_rc=self.rcfile.name
)

def test_fablib_manager_test_only_token_location(self):
os.environ[Constants.FABRIC_TOKEN_LOCATION] = "dummy"
Expand All @@ -75,9 +79,7 @@ def test_fablib_manager_test_with_no_token_file(self):

# FablibManager() without a valid token or token location
# should raise a "SliceManagerException: Unable to refresh tokens: no refresh token found!
self.assertRaises(
ConfigException, FablibManager, fabric_rc=self.rcfile.name
)
self.assertRaises(ConfigException, FablibManager, fabric_rc=self.rcfile.name)

def test_fablib_manager_test_with_dummy_token(self):
# TODO: That FablibManager() calls build_slice_manager()
Expand Down Expand Up @@ -128,13 +130,17 @@ def test_fablib_manager_with_some_config(self):
# Check that the error message is what we expected: the only
# error should be about missing token.
self.assertTrue(
"Token file does not exist, please provide the token at location" in str(ctx.exception)
"Token file does not exist, please provide the token at location"
in str(ctx.exception)
)

def test_FablibManager_no_config_no_env_var(self):
# Instantiate Fablib manager without any config or environment variables
# Results in using the defaults and initialization fails when token file is not found!
self.assertRaises(ConfigException, FablibManager, )
self.assertRaises(
ConfigException,
FablibManager,
)

def test_FablibManager_no_config_no_env_var_token_location(self):
# Instantiate Fablib manager without any config or environment variables
Expand All @@ -149,7 +155,7 @@ def test_FablibManager_no_config_no_env_var_token_location(self):
# error should be about missing token.
self.assertEqual(
str(ctx.exception),
"Unable to validate provided token: ValidateCode.UNPARSABLE_TOKEN/Not enough segments"
"Unable to validate provided token: ValidateCode.UNPARSABLE_TOKEN/Not enough segments",
)

def test_FablibManager_no_config_no_env_var_token_location_offline(self):
Expand All @@ -161,25 +167,36 @@ def test_FablibManager_no_config_no_env_var_token_location_offline(self):
# Check that the error is what we expected.
self.assertIsInstance(ctx.exception, AttributeError)

def test_FablibManager_no_config_no_env_var_token_location_offline_project_id_bastion_user_name(self):
def test_FablibManager_no_config_no_env_var_token_location_offline_project_id_bastion_user_name(
self,
):
# Instantiate Fablib manager without any config or environment variables
project_id = "DUMMY_PROJECT_ID"
bastion_username = "DUMMY_BASTION_USERNAME"

fablib = FablibManager(token_location=self.DUMMY_TOKEN_LOCATION, offline=True,
project_id=project_id, bastion_username=bastion_username)
fablib = FablibManager(
token_location=self.DUMMY_TOKEN_LOCATION,
offline=True,
project_id=project_id,
bastion_username=bastion_username,
)
self.assertEqual(project_id, fablib.get_project_id())
self.assertEqual(bastion_username, fablib.get_bastion_username())
self.assertEqual(self.DUMMY_TOKEN_LOCATION, fablib.get_token_location())

for attrs, attr_props in Config.REQUIRED_ATTRS.items():
if attrs not in [FablibConstants.PROJECT_ID, FablibConstants.BASTION_USERNAME,
FablibConstants.TOKEN_LOCATION]:
if attrs not in [
FablibConstants.PROJECT_ID,
FablibConstants.BASTION_USERNAME,
FablibConstants.TOKEN_LOCATION,
]:
default_value = attr_props.get(FablibConstants.DEFAULT)
if default_value:
self.assertEqual(default_value, fablib.runtime_config.get(attrs))

def test_FablibManager_no_config_no_env_var_token_location_offline_project_id_bastion_user_name_env(self):
def test_FablibManager_no_config_no_env_var_token_location_offline_project_id_bastion_user_name_env(
self,
):
# Instantiate Fablib manager without any config or environment variables
project_id = "DUMMY_PROJECT_ID"
bastion_username = "DUMMY_BASTION_USERNAME"
Expand All @@ -193,8 +210,11 @@ def test_FablibManager_no_config_no_env_var_token_location_offline_project_id_ba
self.assertEqual(self.DUMMY_TOKEN_LOCATION, fablib.get_token_location())

for attrs, attr_props in Config.REQUIRED_ATTRS.items():
if attrs not in [FablibConstants.PROJECT_ID, FablibConstants.BASTION_USERNAME,
FablibConstants.TOKEN_LOCATION]:
if attrs not in [
FablibConstants.PROJECT_ID,
FablibConstants.BASTION_USERNAME,
FablibConstants.TOKEN_LOCATION,
]:
default_value = attr_props.get(FablibConstants.DEFAULT)
if default_value:
self.assertEqual(default_value, fablib.runtime_config.get(attrs))

0 comments on commit dff10ec

Please sign in to comment.