Skip to content

Commit 1ca4cc1

Browse files
authored
Improve test coverage (#43)
1 parent 56f3db6 commit 1ca4cc1

File tree

7 files changed

+121
-24
lines changed

7 files changed

+121
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
* Use default region and profile in list command
55
* Always use own version of session-manager-plugin if available
6+
* Improve tests coverage
67

78
0.4.1 (2019-08-12)
89
------------------

aws_gate/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
__version__ = '0.4.1'
22
__description__ = 'aws-gate - AWS SSM Session Manager client CLI'
3+
__author__ = 'Adam Stevko'
4+
__author_email__ = '[email protected]'
5+
__url__ = 'https://github.com/xen0l/aws-gate'

aws_gate/cli.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
def _get_profile(args, config, default):
2424
profile = None
25-
if 'profile' in args:
25+
if hasattr(args, 'profile'):
2626
profile = args.profile
2727
return profile or config.default_profile or default
2828

2929

3030
def _get_region(args, config, default):
3131
region = None
32-
if 'region' in args:
32+
if hasattr(args, 'region'):
3333
region = args.region
3434
return region or config.default_region or default
3535

@@ -103,11 +103,13 @@ def main():
103103
try:
104104
config = load_config_from_files()
105105
except (ValidationError, ScannerError) as e:
106-
raise ValueError('Invalid configuration provided: {}'.format(e.message))
106+
raise ValueError('Invalid configuration provided: {}'.format(e))
107107

108108
profile = _get_profile(args=args, config=config, default=default_profile)
109109
region = _get_region(args=args, config=config, default=default_region)
110110

111+
logger.debug('Using AWS profile "%s" in region "%s"', profile, region)
112+
111113
if args.subcommand == 'bootstrap':
112114
bootstrap(force=args.force)
113115
if args.subcommand == 'session':

setup.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from setuptools import setup, find_packages
44

5-
from aws_gate import __version__, __description__
5+
from aws_gate import __version__, __description__, __author__, __author_email__, __url__
66

77
__location__ = os.path.join(
88
os.getcwd(), os.path.dirname(inspect.getfile(inspect.currentframe()))
@@ -17,25 +17,39 @@ def get_install_requirements(path):
1717
return requires
1818

1919

20+
NAME = 'aws-gate'
21+
PROJECT_URLS = {
22+
"Bug Tracker": "https://github.com/xen0l/aws-gate/issues",
23+
"Source Code": "https://github.com/xen0l/aws-gate",
24+
}
25+
CLASSIFIERS = [
26+
"Programming Language :: Python :: 3",
27+
"License :: OSI Approved :: BSD License",
28+
"Operating System :: OS Independent",
29+
"Topic :: System :: Systems Administration",
30+
]
31+
INSTALL_REQUIRES = get_install_requirements('requirements/requirements.txt')
32+
EXTRA_REQUIRES = {
33+
'tests': get_install_requirements('requirements/requirements_dev.txt')
34+
}
35+
SCRIPTS = [
36+
'bin/aws-gate'
37+
]
38+
2039
setup(
21-
name='aws-gate',
40+
name=NAME,
2241
version=__version__,
2342
description=__description__,
2443
long_description=open('README.md').read(),
2544
long_description_content_type="text/markdown",
26-
include_package_data=True,
27-
author='Adam Stevko',
28-
author_email='[email protected]',
45+
url=__url__,
46+
project_urls=PROJECT_URLS,
47+
author=__author__,
48+
author_email=__author_email__,
2949
packages=find_packages(),
30-
url='https://github.com/xen0l/aws-gate',
31-
install_requires=get_install_requirements('requirements/requirements.txt'),
32-
classifiers=[
33-
"Programming Language :: Python :: 3",
34-
"License :: OSI Approved :: BSD License",
35-
"Operating System :: OS Independent",
36-
"Topic :: System :: Systems Administration",
37-
],
38-
scripts=[
39-
'bin/aws-gate',
40-
],
50+
classifiers=CLASSIFIERS,
51+
install_requires=INSTALL_REQUIRES,
52+
extra_requires=EXTRA_REQUIRES,
53+
include_package_data=True,
54+
scripts=SCRIPTS
4155
)

test/unit/test_cli.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,70 @@
1+
import argparse
12
import unittest
2-
import unittest.mock
3+
from unittest.mock import patch, MagicMock, create_autospec
34

4-
from aws_gate.cli import main
5+
from marshmallow import ValidationError
6+
7+
from aws_gate.cli import main, _get_profile, _get_region
58

69

710
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'
816

917
def test_cli_param_error(self):
1018
with self.assertRaises(SystemExit):
1119
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)

test/unit/test_config.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from marshmallow import ValidationError
66

77
from aws_gate.constants import DEFAULT_GATE_CONFIG_PATH, DEFAULT_GATE_CONFIGD_PATH
8-
from aws_gate.config import GateConfig, EmptyConfigurationError, load_config_from_files, _locate_config_files
8+
from aws_gate.config import GateConfig, EmptyConfigurationError, load_config_from_files, _locate_config_files, \
9+
validate_profile, validate_region
910

1011

1112
class TestConfig(unittest.TestCase):
@@ -129,3 +130,13 @@ def test_config_get_host(self):
129130

130131
self.assertEqual(config.get_host('foobar'), expected_host)
131132
self.assertEqual(config.get_host('non-existent'), {})
133+
134+
def test_validate_profile(self):
135+
with patch('aws_gate.config.is_existing_profile', return_value=False):
136+
with self.assertRaises(ValidationError):
137+
validate_profile('test-profile')
138+
139+
def test_validate_region(self):
140+
with patch('aws_gate.config.is_existing_region', return_value=False):
141+
with self.assertRaises(ValidationError):
142+
validate_region('test-region')

test/unit/test_decorators.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11

22
import unittest
3-
from unittest.mock import patch
3+
from unittest.mock import patch, call
44

5-
from aws_gate.decorators import plugin_required, plugin_version
5+
from aws_gate.decorators import plugin_required, plugin_version, _plugin_exists
66

77

88
class TestDecorators(unittest.TestCase):
9+
def test_plugin_exists(self):
10+
with patch('aws_gate.decorators.os.path.exists') as m:
11+
_plugin_exists('foo')
12+
13+
self.assertTrue(m.called)
14+
self.assertEqual(m.call_args, call('foo'))
15+
916
def test_plugin_required(self):
1017
with patch('aws_gate.decorators._plugin_exists', return_value=True):
1118
@plugin_required

0 commit comments

Comments
 (0)