Skip to content

Commit

Permalink
Merge pull request #60 from HPCNow/main
Browse files Browse the repository at this point in the history
froster v0.12.17 - test_config.py finished
  • Loading branch information
victormachadoperez authored Jun 7, 2024
2 parents f54f39f + b112952 commit de3468b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "froster"
version = "0.12.15"
version = "0.12.17"
description = "Froster is a tool for easy data transfer between local file systems and AWS S3 storage."
authors = ["Victor Machado <[email protected]>"]
readme = "README.md"
Expand Down
49 changes: 32 additions & 17 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile
from unittest.mock import patch
import unittest

import warnings
warnings.filterwarnings("always", category=ResourceWarning)
warnings.filterwarnings("ignore", category=ResourceWarning)
Expand Down Expand Up @@ -54,6 +55,8 @@


def init_froster(self):
'''Initialize the froster objects.'''

self.cmd = Commands()
self.parser = self.cmd.parse_arguments()
self.args = self.parser.parse_args()
Expand Down Expand Up @@ -83,6 +86,7 @@ def init_froster(self):


def deinit_froster(self):
'''Deinitialize the froster objects.'''

if hasattr(self, 'cfg'):
if hasattr(self.cfg, 'aws_dir') and os.path.exists(self.cfg.aws_dir):
Expand Down Expand Up @@ -114,6 +118,7 @@ def deinit_froster(self):


def delete_buckets(self):
'''Delete created S3 buckets if they exists.'''

if self.aws.check_credentials():
# Get the buckets list
Expand All @@ -127,6 +132,8 @@ def delete_buckets(self):


def check_ini_file(self, ini_file, section, key, value):
'''Check the value of a key in a section of an ini file.'''

config = configparser.ConfigParser()
config.read(ini_file)
self.assertIn(section, config.sections())
Expand All @@ -135,16 +142,20 @@ def check_ini_file(self, ini_file, section, key, value):

@patch('builtins.print')
class TestConfig(unittest.TestCase):
'''Test the subcmd_confing method.'''

# Method executed only once before all tests
@classmethod
def setUpClass(cls):

# Check if the AWS credentials are set
if AWS_ACCESS_KEY_ID is None or AWS_SECRET is None:
raise ValueError("AWS credentials are not set")

# Method executed before every test
def setUp(self):

# Initialize the froster objects
init_froster(self)

# Delete any existing buckets
Expand All @@ -156,12 +167,13 @@ def tearDown(self):
# Delete any existing buckets
delete_buckets(self)

# Deinitialize the froster objects
deinit_froster(self)

# HELPER RUNS

def helper_set_default_cli_arguments(self):
'''- Set default arguments.'''
'''- Set default cli arguments.'''

self.cmd.args = Namespace(cores=4, debug=False, info=False, memory=64, noslurm=False, aws_profile='', version=False,
subcmd='config', aws=False, monitor=False, nih=False, print=False, s3=False, shared=False, slurm=False, user=False)
Expand Down Expand Up @@ -190,7 +202,7 @@ def helper_run_subcmd_config(self, mock_print, mock_text, mock_prompt, mock_list
@patch('inquirer.list_input', side_effect=['+ Create new profile', AWS_REGION, '+ Create new bucket', S3_STORAGE_CLASS])
@patch('inquirer.confirm', side_effect=[True, False])
def helper_run_subcmd_config_shared(self, mock_print, mock_text, mock_prompt, mock_list, mock_confirm):
'''- Helper that sets full configuration'''
'''- Helper that sets full configuration with shared directory'''

# Check that nothing is set yet
self.assertFalse(self.cfg.user_init)
Expand All @@ -209,7 +221,7 @@ def helper_run_subcmd_config_shared(self, mock_print, mock_text, mock_prompt, mo
@patch('inquirer.list_input', side_effect=['+ Create new profile', AWS_REGION, '+ Create new bucket', S3_STORAGE_CLASS, AWS_PROFILE, AWS_REGION])
@patch('inquirer.confirm', side_effect=[True, False, True, True])
def helper_run_subcmd_config_shared_existing_config(self, mock_print, mock_text, mock_prompt, mock_list, mock_confirm):
'''- Helper that sets full configuration'''
'''- Helper that sets full configuration with a shared directory where there is already a shared configuration file'''

# Check that nothing is set yet
self.assertFalse(self.cfg.user_init)
Expand All @@ -231,7 +243,7 @@ def helper_run_subcmd_config_shared_existing_config(self, mock_print, mock_text,
@patch('inquirer.list_input', side_effect=['+ Create new profile', AWS_REGION_2, '+ Create new bucket', S3_STORAGE_CLASS_2])
@patch('inquirer.confirm', side_effect=[True, False, False])
def helper_run_subcmd_config_overwrite(self, mock_print, mock_text, mock_prompt, mock_list, mock_confirm):
'''- Helper that sets full configuration'''
'''- Helper that sets full configuration and overwrites the current configuration'''

# Mock the CLI default arguments
self.helper_set_default_cli_arguments()
Expand All @@ -244,7 +256,7 @@ def helper_run_subcmd_config_overwrite(self, mock_print, mock_text, mock_prompt,
@patch('inquirer.list_input', side_effect=['+ Create new profile', AWS_REGION, '+ Create new bucket', S3_STORAGE_CLASS])
@patch('inquirer.confirm', side_effect=[True, True, False])
def helper_run_subcmd_config_shared_move_froster_archives_json(self, mock_print, mock_text, mock_prompt, mock_list, mock_confirm):
'''- Helper that sets full configuration'''
'''- Helper that sets full configuration with a shared directory and moves the froster-archives.json file'''

# Check that nothing is set yet
self.assertFalse(self.cfg.user_init)
Expand Down Expand Up @@ -294,7 +306,6 @@ def helper_run_subcmd_config_shared_move_config_sections(self, mock_print, mock_
# HELPER CHECKS

def helper_check_subcmd_config(self):
'''- Check the configuration files after setting full configuration'''

# Check that everything is set
self.assertTrue(self.cfg.user_init)
Expand Down Expand Up @@ -352,7 +363,7 @@ def helper_check_subcmd_config(self):
self.assertIn(S3_BUCKET_NAME, s3_buckets)

def helper_check_subcmd_config_shared(self):
'''- Check the configuration files after setting full configuration with shared directory'''

# Check that everything is set
self.assertTrue(self.cfg.user_init)
self.assertTrue(self.cfg.aws_init)
Expand Down Expand Up @@ -413,7 +424,7 @@ def helper_check_subcmd_config_shared(self):
self.assertIn(S3_BUCKET_NAME, s3_buckets)

def helper_check_subcmd_config_shared_existing_config(self):
'''- Check the configuration files after setting full configuration with shared directory'''

# Check that everything is set
self.assertTrue(self.cfg.user_init)
self.assertTrue(self.cfg.aws_init)
Expand Down Expand Up @@ -531,7 +542,6 @@ def helper_check_subcmd_config_overwrite(self):
self.assertIn(S3_BUCKET_NAME_2, s3_buckets)

def helper_check_subcmd_config_shared_move_froster_archives_json(self):
'''- Check the configuration files after setting full configuration with shared directory'''

self.helper_check_subcmd_config_shared()

Expand All @@ -550,14 +560,14 @@ def test_subcmd_config(self, mock_print):

self.helper_run_subcmd_config(None)

self.helper_check_subcmd_config_not_shared()
self.helper_check_subcmd_config()

def test_subcmd_config_overwrite(self, mock_print,):
'''- Overwirte current configuration'''
'''- Set full configuration overwritting current configuration'''

self.helper_run_subcmd_config(None)

self.helper_check_subcmd_config_not_shared()
self.helper_check_subcmd_config()

self.helper_run_subcmd_config_overwrite(None)

Expand All @@ -568,10 +578,10 @@ def test_subcmd_config_shared(self, mock_print):

self.helper_run_subcmd_config(None)

self.helper_check_subcmd_config_not_shared()
self.helper_check_subcmd_config()

def test_subcmd_config_shared_existing_config(self, mock_print):
'''- Set full configuration with shared directory with existing'''
'''- Set full configuration with shared directory where there is already a shared configuration file'''

self.helper_run_subcmd_config_shared_existing_config(None)

Expand All @@ -585,7 +595,7 @@ def test_subcmd_config_shared_move_froster_archives_json(self, mock_print):
self.helper_check_subcmd_config_shared_move_froster_archives_json()

def test_subcmd_config_shared_move_config(self, mock_print):
'''- Set full configuration then set shared and move the config sections to the shared directory config file'''
'''- Set full configuration and move config to the shared directory config file'''

# Not shared full configuration
self.helper_run_subcmd_config(None)
Expand All @@ -599,6 +609,7 @@ def test_subcmd_config_shared_move_config(self, mock_print):

@patch('builtins.print')
class TestConfigUser(unittest.TestCase):
'''Test the set_user method.'''

# Method executed before every test
def setUp(self):
Expand Down Expand Up @@ -630,6 +641,7 @@ def test_set_user(self, mock_print, mock_text):

@patch('builtins.print')
class TestConfigAWS(unittest.TestCase):
'''Test the set_aws method.'''

# Method executed only once before all tests
@classmethod
Expand Down Expand Up @@ -832,6 +844,7 @@ def test_set_aws_do_not_overwrite_profile(self, mock_print, mock_prompt, mock_li

@patch('builtins.print')
class TestConfigShared(unittest.TestCase):
'''Test the set_shared method.'''

# Method executed before every test
def setUp(self):
Expand Down Expand Up @@ -996,6 +1009,7 @@ def test_set_shared_froster_shared_config_exist(self, mock_print, mock_confirm,

@patch('builtins.print')
class TestConfigNIH(unittest.TestCase):
'''Test the set_nih method.'''

# Method executed before every test
def setUp(self):
Expand Down Expand Up @@ -1035,7 +1049,7 @@ def test_set_nih(self, mock_print, mock_confirm):
@patch('inquirer.confirm', side_effect=[True, True, False])
@patch('inquirer.prompt', return_value={'shared_dir': SHARED_DIR})
def test_set_nih_when_shared_config(self, mock_print, mock_confirm, mock_promp):
'''- Set the NIH flag to True and then to False.'''
'''- Set the NIH flag to True and then to False when shared configuration.'''

# Call set_shared method
self.assertTrue(self.cfg.set_shared())
Expand All @@ -1057,6 +1071,7 @@ def test_set_nih_when_shared_config(self, mock_print, mock_confirm, mock_promp):

@patch('builtins.print')
class TestConfigS3(unittest.TestCase):
'''Test the set_s3 method.'''

# Method executed before every test
@patch('inquirer.prompt', return_value={'aws_dir': AWS_DEFAULT_PATH})
Expand Down Expand Up @@ -1112,7 +1127,7 @@ def test_set_s3(self, mock_print, mock_list, mock_text):
self.assertIn(S3_BUCKET_NAME, s3_buckets)

def test_set_s3_aws_not_init(self, mock_print):
'''- Set_s3 method returns False if AWS credentials are not set.'''
'''- set_s3 method returns False if AWS credentials are not set.'''

# Mock the aws_init variable
self.cfg.aws_init = False
Expand Down

0 comments on commit de3468b

Please sign in to comment.