|
| 1 | +import argparse |
1 | 2 | import unittest |
2 | | -import unittest.mock |
| 3 | +from unittest.mock import patch, MagicMock, create_autospec |
3 | 4 |
|
4 | | -from aws_gate.cli import main |
| 5 | +from marshmallow import ValidationError |
| 6 | + |
| 7 | +from aws_gate.cli import main, _get_profile, _get_region |
5 | 8 |
|
6 | 9 |
|
7 | 10 | class TestCli(unittest.TestCase): |
| 11 | + def setUp(self): |
| 12 | + self._args = MagicMock(profile='args_profile', region='args_region') |
| 13 | + self._config = MagicMock(default_profile='config_profile', default_region='config_region') |
| 14 | + self._default_region = 'default_region' |
| 15 | + self._default_profile = 'default_profile' |
8 | 16 |
|
9 | 17 | def test_cli_param_error(self): |
10 | 18 | with self.assertRaises(SystemExit): |
11 | 19 | main() |
| 20 | + |
| 21 | + def test_get_profile_from_args(self): |
| 22 | + self.assertEqual(_get_profile(args=self._args, config=self._config, |
| 23 | + default=self._default_profile), 'args_profile') |
| 24 | + |
| 25 | + def test_get_profile_from_config(self): |
| 26 | + self.assertEqual(_get_profile(args=create_autospec(argparse.Namespace), config=self._config, |
| 27 | + default=self._default_profile), 'config_profile') |
| 28 | + |
| 29 | + def test_get_profile_from_default(self): |
| 30 | + self.assertEqual(_get_profile(args=create_autospec(argparse.Namespace), config=MagicMock(default_profile=None), |
| 31 | + default=self._default_profile), 'default_profile') |
| 32 | + |
| 33 | + def test_get_region_from_args(self): |
| 34 | + self.assertEqual(_get_region(args=self._args, config=self._config, |
| 35 | + default=self._default_region), 'args_region') |
| 36 | + |
| 37 | + def test_get_region_from_config(self): |
| 38 | + self.assertEqual(_get_region(args=create_autospec(argparse.Namespace), config=self._config, |
| 39 | + default=self._default_region), 'config_region') |
| 40 | + |
| 41 | + def test_get_region_from_default(self): |
| 42 | + self.assertEqual(_get_region(args=create_autospec(argparse.Namespace), config=MagicMock(default_region=None), |
| 43 | + default=self._default_region), 'default_region') |
| 44 | + |
| 45 | + def test_cli_invalid_config(self): |
| 46 | + with patch('aws_gate.cli.parse_arguments', return_value=MagicMock(subcommand='bootstrap')), \ |
| 47 | + patch('aws_gate.cli.load_config_from_files', side_effect=ValidationError(message='error')): |
| 48 | + with self.assertRaises(ValueError): |
| 49 | + main() |
| 50 | + |
| 51 | + def test_cli_bootstrap(self): |
| 52 | + with patch('aws_gate.cli.parse_arguments', return_value=MagicMock(subcommand='bootstrap')), \ |
| 53 | + patch('aws_gate.cli.bootstrap') as m: |
| 54 | + main() |
| 55 | + |
| 56 | + self.assertTrue(m.called) |
| 57 | + |
| 58 | + def test_cli_session(self): |
| 59 | + with patch('aws_gate.cli.parse_arguments', return_value=MagicMock(subcommand='session')), \ |
| 60 | + patch('aws_gate.cli.session') as m: |
| 61 | + main() |
| 62 | + |
| 63 | + self.assertTrue(m.called) |
| 64 | + |
| 65 | + def test_cli_list(self): |
| 66 | + with patch('aws_gate.cli.parse_arguments', return_value=MagicMock(subcommand='list')), \ |
| 67 | + patch('aws_gate.cli.list_instances') as m: |
| 68 | + main() |
| 69 | + |
| 70 | + self.assertTrue(m.called) |
0 commit comments