|
1 | 1 | from froster import *
|
2 |
| -from unittest.mock import patch |
3 |
| -import unittest |
| 2 | +from argparse import Namespace |
4 | 3 | import configparser
|
5 | 4 | import os
|
6 | 5 | import shutil
|
7 | 6 | import tempfile
|
| 7 | +from unittest.mock import patch |
| 8 | +import unittest |
8 | 9 | import warnings
|
9 | 10 | warnings.filterwarnings("always", category=ResourceWarning)
|
10 | 11 | warnings.filterwarnings("ignore", category=ResourceWarning)
|
@@ -131,24 +132,110 @@ def check_ini_file(self, ini_file, section, key, value):
|
131 | 132 | self.assertEqual(config.get(section, key), value)
|
132 | 133 |
|
133 | 134 |
|
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) |
136 | 159 |
|
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.''' |
140 | 162 |
|
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) |
144 | 165 |
|
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) |
150 | 178 |
|
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) |
152 | 239 |
|
153 | 240 |
|
154 | 241 | @patch('builtins.print')
|
@@ -640,6 +727,9 @@ def tearDown(self):
|
640 | 727 | def test_set_s3(self, mock_print, mock_list, mock_text):
|
641 | 728 | '''- Set a new S3 bucket.'''
|
642 | 729 |
|
| 730 | + # Assert S3 is not set |
| 731 | + self.assertFalse(self.cfg.s3_init) |
| 732 | + |
643 | 733 | # Call set_s3 method
|
644 | 734 | self.assertTrue(self.cfg.set_s3(self.aws))
|
645 | 735 |
|
@@ -769,12 +859,12 @@ def test_set_s3_select_bucket(self, mock_print, mock_list, mock_text):
|
769 | 859 |
|
770 | 860 | if __name__ == '__main__':
|
771 | 861 |
|
772 |
| - if True: |
| 862 | + if False: |
773 | 863 | unittest.main(verbosity=2)
|
774 | 864 | else:
|
775 | 865 | suite = unittest.TestSuite()
|
776 | 866 | # FULL CONFIGURATION
|
777 |
| - # suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfig)) |
| 867 | + suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfig)) |
778 | 868 |
|
779 | 869 | # PARTIAL CONFIGURATION
|
780 | 870 | # suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestConfigUser))
|
|
0 commit comments