Improved test coverage for library/advanced_format.py
Increases test coverage to almost 100% of statements. The exception being the initial call of main. Closes-Bug: #1922726 Signed-off-by: Jiri Podivin <jpodivin@redhat.com> Change-Id: Id6f0ab2dec9b52a6f765626c7c85831f85771dd0
This commit is contained in:
parent
8b8401bb67
commit
b50b40c4ab
|
@ -21,6 +21,71 @@ class TestAdvancedFormat(base.TestCase):
|
|||
def setUp(self):
|
||||
super(TestAdvancedFormat, self).setUp()
|
||||
self.read_int = advanced_format.read_int
|
||||
self.advanced_format = advanced_format
|
||||
self.module = mock.MagicMock()
|
||||
self.module.params = {'msg': 'foo'}
|
||||
|
||||
@mock.patch(
|
||||
'validations_common.library.advanced_format.read_int',
|
||||
return_value=0)
|
||||
@mock.patch(
|
||||
'validations_common.library.advanced_format.AnsibleModule')
|
||||
@mock.patch(
|
||||
'validations_common.library.advanced_format.yaml_safe_load',
|
||||
return_value={'options': mock.MagicMock()})
|
||||
def test_advanced_format_run_same_sizes(self, mock_safe_load,
|
||||
mock_ansible_module,
|
||||
mock_read_int):
|
||||
"""Verify that advanced_format correctly works with provided YAML.
|
||||
"""
|
||||
|
||||
mock_module = mock.MagicMock()
|
||||
mock_module.params = {'drive': 'foo'}
|
||||
mock_ansible_module.return_value = mock_module
|
||||
|
||||
self.advanced_format.main()
|
||||
mock_safe_load.assert_called_once_with(self.advanced_format.DOCUMENTATION)
|
||||
|
||||
mock_module.exit_json.assert_called_once_with(
|
||||
changed=False,
|
||||
msg="The disk foo probably doesn't use Advance Format.")
|
||||
|
||||
@mock.patch(
|
||||
'validations_common.library.advanced_format.read_int',
|
||||
side_effect=[100, 1000])
|
||||
@mock.patch(
|
||||
'validations_common.library.advanced_format.AnsibleModule')
|
||||
@mock.patch(
|
||||
'validations_common.library.advanced_format.yaml_safe_load',
|
||||
return_value={'options': mock.MagicMock()})
|
||||
def test_advanced_format_run_different_sizes(self, mock_safe_load,
|
||||
mock_ansible_module,
|
||||
mock_read_int):
|
||||
"""Verify that advanced_format correctly works with provided YAML.
|
||||
"""
|
||||
|
||||
mock_module = mock.MagicMock()
|
||||
mock_module.params = {'drive': 'foo'}
|
||||
mock_ansible_module.return_value = mock_module
|
||||
|
||||
self.advanced_format.main()
|
||||
mock_safe_load.assert_called_once_with(self.advanced_format.DOCUMENTATION)
|
||||
|
||||
mock_module.exit_json.assert_called_once_with(
|
||||
changed=True,
|
||||
warnings=mock.ANY)
|
||||
|
||||
def test_advanced_format_attributes(self):
|
||||
"""Verify that module contains required attributes.
|
||||
"""
|
||||
required_names = set(
|
||||
[
|
||||
'DOCUMENTATION',
|
||||
'EXAMPLES',
|
||||
'AnsibleModule'
|
||||
])
|
||||
advanced_format_names = set(dir(self.advanced_format))
|
||||
self.assertTrue(advanced_format_names.issuperset(required_names))
|
||||
|
||||
@mock.patch('ansible.module_utils.basic.AnsibleModule', autospec=True)
|
||||
@mock.patch('six.moves.builtins.open', autospec=True)
|
||||
|
@ -33,3 +98,48 @@ class TestAdvancedFormat(base.TestCase):
|
|||
self.read_int(**args)
|
||||
|
||||
mock_open.assert_called_once_with(args['file_path'])
|
||||
|
||||
@mock.patch('ansible.module_utils.basic.AnsibleModule', autospec=True)
|
||||
@mock.patch(
|
||||
'six.moves.builtins.open',
|
||||
autospec=True,
|
||||
side_effect=IOError())
|
||||
def test_read_int_ioerror(self, mock_open, mock_module):
|
||||
"""Verify that IOError causes fail_json call.
|
||||
|
||||
As the msg argument is ultimately a string and subject
|
||||
to potential changes without effect on its function, we only verify
|
||||
the presence of call.
|
||||
"""
|
||||
args = {
|
||||
'module': mock_module,
|
||||
'file_path': './foo/bar'
|
||||
}
|
||||
|
||||
self.read_int(**args)
|
||||
|
||||
mock_open.assert_called_once_with(args['file_path'])
|
||||
mock_module.fail_json.assert_called_once()
|
||||
|
||||
@mock.patch(
|
||||
'validations_common.library.advanced_format.int',
|
||||
side_effect=ValueError())
|
||||
@mock.patch('ansible.module_utils.basic.AnsibleModule', autospec=True)
|
||||
@mock.patch('six.moves.builtins.open', autospec=True)
|
||||
def test_read_int_valueerror(self, mock_open, mock_module, mock_adv_format_int):
|
||||
"""Verify that ValueError raised by int conversion
|
||||
causes fail_json call.
|
||||
|
||||
As the msg argument is ultimately a string and subject
|
||||
to potential changes without effect on its function, we only verify
|
||||
the presence of call.
|
||||
"""
|
||||
args = {
|
||||
'module': mock_module,
|
||||
'file_path': './foo/bar'
|
||||
}
|
||||
|
||||
self.read_int(**args)
|
||||
|
||||
mock_open.assert_called_once_with(args['file_path'])
|
||||
mock_module.fail_json.assert_called_once()
|
||||
|
|
Loading…
Reference in New Issue