2015-04-02 11:10:39 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Rahul Handay <rahulha@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-03-21 17:57:27 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2015-04-02 11:10:39 +00:00
|
|
|
mock_open,
|
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
import salt.utils.args
|
2017-07-18 16:31:01 +00:00
|
|
|
import salt.utils.files
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.puppet as puppet
|
2015-04-02 11:10:39 +00:00
|
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-03-21 17:57:27 +00:00
|
|
|
class PuppetTestCase(TestCase, LoaderModuleMockMixin):
|
2015-04-02 11:10:39 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.modules.puppet
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {puppet: {}}
|
2017-03-21 17:57:27 +00:00
|
|
|
|
2015-04-02 11:10:39 +00:00
|
|
|
def test_run(self):
|
|
|
|
'''
|
|
|
|
Test to execute a puppet run
|
|
|
|
'''
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(return_value={"A": "B"})
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
with patch.object(salt.utils.args, 'clean_kwargs', mock):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(return_value={'retcode': 0})
|
|
|
|
mock_lst = MagicMock(return_value=[])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run_all': mock,
|
|
|
|
'cmd.run': mock_lst}):
|
|
|
|
self.assertTrue(puppet.run())
|
2015-04-02 11:10:39 +00:00
|
|
|
|
|
|
|
def test_noop(self):
|
|
|
|
'''
|
|
|
|
Test to execute a puppet noop run
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value={"stderr": "A", "stdout": "B"})
|
|
|
|
with patch.object(puppet, 'run', mock):
|
|
|
|
self.assertDictEqual(puppet.noop(), {'stderr': 'A', 'stdout': 'B'})
|
|
|
|
|
|
|
|
def test_enable(self):
|
|
|
|
'''
|
|
|
|
Test to enable the puppet agent
|
|
|
|
'''
|
|
|
|
mock_lst = MagicMock(return_value=[])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(return_value=True)
|
|
|
|
with patch.object(os.path, 'isfile', mock):
|
2015-04-02 11:10:39 +00:00
|
|
|
mock = MagicMock(return_value=True)
|
2015-10-28 14:18:34 +00:00
|
|
|
with patch.object(os, 'remove', mock):
|
|
|
|
self.assertTrue(puppet.enable())
|
2015-04-02 11:10:39 +00:00
|
|
|
|
2015-10-28 14:18:34 +00:00
|
|
|
with patch.object(os, 'remove',
|
|
|
|
MagicMock(side_effect=IOError)):
|
|
|
|
self.assertRaises(CommandExecutionError, puppet.enable)
|
2015-04-02 11:10:39 +00:00
|
|
|
|
2015-10-28 14:18:34 +00:00
|
|
|
self.assertFalse(puppet.enable())
|
2015-04-02 11:10:39 +00:00
|
|
|
|
|
|
|
def test_disable(self):
|
|
|
|
'''
|
|
|
|
Test to disable the puppet agent
|
|
|
|
'''
|
|
|
|
mock_lst = MagicMock(return_value=[])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(side_effect=[True, False])
|
|
|
|
with patch.object(os.path, 'isfile', mock):
|
|
|
|
self.assertFalse(puppet.disable())
|
2015-04-02 11:10:39 +00:00
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()):
|
2015-10-28 14:18:34 +00:00
|
|
|
self.assertTrue(puppet.disable())
|
2015-04-02 11:10:39 +00:00
|
|
|
|
2016-04-12 14:41:25 +00:00
|
|
|
try:
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()) as m_open:
|
2016-04-12 14:41:25 +00:00
|
|
|
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
|
|
|
|
self.assertRaises(CommandExecutionError, puppet.disable)
|
|
|
|
except StopIteration:
|
|
|
|
pass
|
2015-04-02 11:10:39 +00:00
|
|
|
|
|
|
|
def test_status(self):
|
|
|
|
'''
|
|
|
|
Test to display puppet agent status
|
|
|
|
'''
|
|
|
|
mock_lst = MagicMock(return_value=[])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(side_effect=[True])
|
|
|
|
with patch.object(os.path, 'isfile', mock):
|
|
|
|
self.assertEqual(puppet.status(),
|
|
|
|
"Administratively disabled")
|
|
|
|
|
|
|
|
mock = MagicMock(side_effect=[False, True])
|
|
|
|
with patch.object(os.path, 'isfile', mock):
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open(read_data="1")):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(return_value=True)
|
|
|
|
with patch.object(os, 'kill', mock):
|
|
|
|
self.assertEqual(puppet.status(),
|
|
|
|
"Applying a catalog")
|
|
|
|
|
|
|
|
mock = MagicMock(side_effect=[False, True])
|
|
|
|
with patch.object(os.path, 'isfile', mock):
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(return_value=True)
|
|
|
|
with patch.object(os, 'kill', mock):
|
|
|
|
self.assertEqual(puppet.status(), "Stale lockfile")
|
|
|
|
|
|
|
|
mock = MagicMock(side_effect=[False, False, True])
|
|
|
|
with patch.object(os.path, 'isfile', mock):
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open(read_data="1")):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(return_value=True)
|
|
|
|
with patch.object(os, 'kill', mock):
|
|
|
|
self.assertEqual(puppet.status(), "Idle daemon")
|
|
|
|
|
|
|
|
mock = MagicMock(side_effect=[False, False, True])
|
|
|
|
with patch.object(os.path, 'isfile', mock):
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock = MagicMock(return_value=True)
|
|
|
|
with patch.object(os, 'kill', mock):
|
|
|
|
self.assertEqual(puppet.status(), "Stale pidfile")
|
|
|
|
|
|
|
|
mock = MagicMock(side_effect=[False, False, False])
|
|
|
|
with patch.object(os.path, 'isfile', mock):
|
|
|
|
self.assertEqual(puppet.status(), "Stopped")
|
2015-04-02 11:10:39 +00:00
|
|
|
|
|
|
|
def test_summary(self):
|
|
|
|
'''
|
|
|
|
Test to show a summary of the last puppet agent run
|
|
|
|
'''
|
|
|
|
mock_lst = MagicMock(return_value=[])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen',
|
2015-10-28 14:18:34 +00:00
|
|
|
mock_open(read_data="resources: 1")):
|
|
|
|
self.assertDictEqual(puppet.summary(), {'resources': 1})
|
2015-04-02 11:10:39 +00:00
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open()) as m_open:
|
2016-04-12 14:41:25 +00:00
|
|
|
m_open.side_effect = IOError(13, 'Permission denied:', '/file')
|
|
|
|
self.assertRaises(CommandExecutionError, puppet.summary)
|
2015-04-02 11:10:39 +00:00
|
|
|
|
|
|
|
def test_plugin_sync(self):
|
|
|
|
'''
|
|
|
|
Test to runs a plugin synch between the puppet master and agent
|
|
|
|
'''
|
|
|
|
mock_lst = MagicMock(return_value=[])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock_lst = MagicMock(side_effect=[False, True])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
|
|
|
self.assertEqual(puppet.plugin_sync(), "")
|
2015-04-02 11:10:39 +00:00
|
|
|
|
2015-10-28 14:18:34 +00:00
|
|
|
self.assertTrue(puppet.plugin_sync())
|
2015-04-02 11:10:39 +00:00
|
|
|
|
|
|
|
def test_facts(self):
|
|
|
|
'''
|
|
|
|
Test to run facter and return the results
|
|
|
|
'''
|
|
|
|
mock_lst = MagicMock(return_value=[])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock_lst = MagicMock(return_value="True")
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
|
|
|
mock = MagicMock(return_value=["a", "b"])
|
|
|
|
with patch.object(puppet, '_format_fact', mock):
|
|
|
|
self.assertDictEqual(puppet.facts(), {'a': 'b'})
|
2015-04-02 11:10:39 +00:00
|
|
|
|
|
|
|
def test_fact(self):
|
|
|
|
'''
|
|
|
|
Test to run facter for a specific fact
|
|
|
|
'''
|
|
|
|
mock_lst = MagicMock(return_value=[])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
2015-10-28 14:18:34 +00:00
|
|
|
mock_lst = MagicMock(side_effect=[False, True])
|
|
|
|
with patch.dict(puppet.__salt__, {'cmd.run': mock_lst}):
|
|
|
|
self.assertEqual(puppet.fact("salt"), "")
|
2015-04-02 11:10:39 +00:00
|
|
|
|
2015-10-28 14:18:34 +00:00
|
|
|
self.assertTrue(puppet.fact("salt"))
|