Skip to content

Commit 6409261

Browse files
author
Victor Machado
committed
"froster config" test working
1 parent 2413743 commit 6409261

File tree

1 file changed

+108
-18
lines changed

1 file changed

+108
-18
lines changed

tests/test_config.py

+108-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from froster import *
2-
from unittest.mock import patch
3-
import unittest
2+
from argparse import Namespace
43
import configparser
54
import os
65
import shutil
76
import tempfile
7+
from unittest.mock import patch
8+
import unittest
89
import warnings
910
warnings.filterwarnings("always", category=ResourceWarning)
1011
warnings.filterwarnings("ignore", category=ResourceWarning)
@@ -131,24 +132,110 @@ def check_ini_file(self, ini_file, section, key, value):
131132
self.assertEqual(config.get(section, key), value)
132133

133134

134-
# @patch('builtins.print')
135-
# class TestConfig(unittest.TestCase):
135+
@patch('builtins.print')
136+
class TestConfig(unittest.TestCase):
137+
138+
# Method executed only once before all tests
139+
@classmethod
140+
def setUpClass(cls):
141+
if AWS_ACCESS_KEY_ID is None or AWS_SECRET is None:
142+
raise ValueError("AWS credentials are not set")
143+
144+
# Method executed before every test
145+
def setUp(self):
146+
147+
init_froster(self)
148+
149+
# Delete any existing buckets
150+
delete_buckets(self)
151+
152+
# Method executed after every test
153+
def tearDown(self):
154+
155+
# Delete any existing buckets
156+
delete_buckets(self)
157+
158+
deinit_froster(self)
136159

137-
# Method executed before every test
138-
# def setUp(self):
139-
# init_froster(self)
160+
def helper_set_default_cli_arguments(self):
161+
'''- Set default arguments.'''
140162

141-
# Method executed after every test
142-
# def tearDown(self):
143-
# deinit_froster(self)
163+
self.cmd.args = Namespace(cores=4, debug=False, info=False, memory=64, noslurm=False, aws_profile='', version=False,
164+
subcmd='config', aws=False, monitor=False, nih=False, print=False, s3=False, shared=False, slurm=False, user=False)
144165

145-
# @patch('inquirer.text', side_effect=[NAME, EMAIL, S3_BUCKET_NAME, S3_ARCHIVE_DIR])
146-
# @patch('inquirer.list_input', side_effect=['+ Create new profile', AWS_REGION, AWS_PROFILE, AWS_ACCESS_KEY_ID, AWS_SECRET, '+ Create new bucket', S3_STORAGE_CLASS])
147-
# @patch('inquirer.confirm', side_effect=[False, True])
148-
# def test_subcmd_config(self, mock_text, mock_list, mock_confirm):
149-
# '''- Set full configuration'''
166+
@patch('inquirer.text', side_effect=[NAME, EMAIL, AWS_PROFILE, AWS_ACCESS_KEY_ID, AWS_SECRET, S3_BUCKET_NAME, S3_ARCHIVE_DIR])
167+
@patch('inquirer.prompt', side_effect=[{'aws_dir': AWS_DEFAULT_PATH}, {'shared_dir': SHARED_DIR}])
168+
@patch('inquirer.list_input', side_effect=['+ Create new profile', AWS_REGION, '+ Create new bucket', S3_STORAGE_CLASS])
169+
@patch('inquirer.confirm', side_effect=[False, False])
170+
def test_subcmd_config(self, mock_print, mock_text, mock_prompt, mock_list, mock_confirm):
171+
'''- Set full configuration'''
172+
173+
# Check that nothing is set
174+
self.assertFalse(self.cfg.user_init)
175+
self.assertFalse(self.cfg.aws_init)
176+
self.assertFalse(self.cfg.nih_init)
177+
self.assertFalse(self.cfg.s3_init)
150178

151-
# self.assertTrue(self.cmd.subcmd_config(self.args, self.cfg, self.aws))
179+
# Mock the CLI default arguments
180+
self.helper_set_default_cli_arguments()
181+
182+
# Mock the "froster config" command
183+
self.assertTrue(self.cmd.subcmd_config(cfg=self.cfg, aws=self.aws))
184+
185+
# Check that everything is set
186+
self.assertTrue(self.cfg.user_init)
187+
self.assertTrue(self.cfg.aws_init)
188+
self.assertTrue(self.cfg.nih_init)
189+
self.assertTrue(self.cfg.s3_init)
190+
191+
# USER config checks
192+
check_ini_file(self, self.cfg.config_file,
193+
USER_SECTION, 'name', NAME)
194+
check_ini_file(self, self.cfg.config_file,
195+
USER_SECTION, 'email', EMAIL)
196+
197+
# AWS config checks
198+
check_ini_file(self, self.cfg.config_file,
199+
AWS_SECTION, 'aws_profile', AWS_PROFILE)
200+
201+
check_ini_file(self, self.cfg.config_file,
202+
AWS_SECTION, 'aws_region', AWS_REGION)
203+
204+
check_ini_file(self, self.cfg.aws_credentials_file, AWS_PROFILE,
205+
'aws_access_key_id', AWS_ACCESS_KEY_ID)
206+
207+
check_ini_file(self, self.cfg.aws_credentials_file,
208+
AWS_PROFILE, 'aws_secret_access_key', AWS_SECRET)
209+
210+
check_ini_file(self, self.cfg.aws_config_file,
211+
AWS_PROFILE, 'region', AWS_REGION)
212+
213+
check_ini_file(self, self.cfg.aws_config_file,
214+
AWS_PROFILE, 'output', 'json')
215+
216+
self.assertTrue(self.aws.check_credentials())
217+
218+
# SHARED config checks
219+
check_ini_file(self, self.cfg.config_file,
220+
SHARED_SECTION, 'is_shared', 'False')
221+
222+
# NIH config checks
223+
check_ini_file(self, self.cfg.config_file,
224+
NIH_SECTION, 'is_nih', 'False')
225+
226+
# S3 config checks
227+
check_ini_file(self, self.cfg.config_file,
228+
S3_SECTION, 'bucket_name', S3_BUCKET_NAME)
229+
230+
check_ini_file(self, self.cfg.config_file,
231+
S3_SECTION, 'archive_dir', S3_ARCHIVE_DIR)
232+
233+
check_ini_file(self, self.cfg.config_file,
234+
S3_SECTION, 'storage_class', S3_STORAGE_CLASS)
235+
236+
# Check the bucket was created
237+
s3_buckets = self.aws.get_buckets()
238+
self.assertIn(S3_BUCKET_NAME, s3_buckets)
152239

153240

154241
@patch('builtins.print')
@@ -640,6 +727,9 @@ def tearDown(self):
640727
def test_set_s3(self, mock_print, mock_list, mock_text):
641728
'''- Set a new S3 bucket.'''
642729

730+
# Assert S3 is not set
731+
self.assertFalse(self.cfg.s3_init)
732+
643733
# Call set_s3 method
644734
self.assertTrue(self.cfg.set_s3(self.aws))
645735

@@ -769,12 +859,12 @@ def test_set_s3_select_bucket(self, mock_print, mock_list, mock_text):
769859

770860
if __name__ == '__main__':
771861

772-
if True:
862+
if False:
773863
unittest.main(verbosity=2)
774864
else:
775865
suite = unittest.TestSuite()
776866
# FULL CONFIGURATION
777-
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfig))
867+
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfig))
778868

779869
# PARTIAL CONFIGURATION
780870
# suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfigUser))

0 commit comments

Comments
 (0)