salt/tests/unit/modules/test_pacman.py

143 lines
4.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Eric Vz <eric@base10.org>`
'''
# Import Python Libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.mock import (
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import Salt Libs
from salt.modules import pacman
2016-05-31 05:07:31 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
class PacmanTestCase(TestCase):
'''
Test cases for salt.modules.pacman
'''
def setUp(self):
pacman.__salt__ = {}
pacman.__context__ = {}
def test_list_pkgs(self):
'''
Test if it list the packages currently installed in a dict
'''
cmdmock = MagicMock(return_value='A 1.0\nB 2.0')
sortmock = MagicMock()
stringifymock = MagicMock()
mock_ret = {'A': ['1.0'], 'B': ['2.0']}
2016-05-30 19:13:56 +00:00
with patch.dict(pacman.__salt__, {
2016-05-31 05:34:13 +00:00
'cmd.run': cmdmock,
'pkg_resource.add_pkg': lambda pkgs, name, version: pkgs.setdefault(name, []).append(version),
'pkg_resource.sort_pkglist': sortmock,
'pkg_resource.stringify': stringifymock
}):
self.assertDictEqual(pacman.list_pkgs(), mock_ret)
2016-05-30 19:13:56 +00:00
sortmock.assert_called_with(mock_ret)
stringifymock.assert_called_with(mock_ret)
def test_list_pkgs_as_list(self):
'''
2016-05-31 05:07:31 +00:00
Test if it lists the packages currently installed in a dict
'''
cmdmock = MagicMock(return_value='A 1.0\nB 2.0')
sortmock = MagicMock()
stringifymock = MagicMock()
mock_ret = {'A': ['1.0'], 'B': ['2.0']}
2016-05-30 19:13:56 +00:00
with patch.dict(pacman.__salt__, {
2016-05-31 05:34:13 +00:00
'cmd.run': cmdmock,
'pkg_resource.add_pkg': lambda pkgs, name, version: pkgs.setdefault(name, []).append(version),
'pkg_resource.sort_pkglist': sortmock,
'pkg_resource.stringify': stringifymock
}):
self.assertDictEqual(pacman.list_pkgs(True), mock_ret)
2016-05-30 19:13:36 +00:00
sortmock.assert_called_with(mock_ret)
self.assertTrue(stringifymock.call_count == 0)
2016-05-30 20:33:59 +00:00
def test_group_list(self):
2016-05-31 05:07:31 +00:00
'''
Test if it lists the available groups
'''
2016-05-30 20:33:59 +00:00
2016-05-31 13:17:33 +00:00
def cmdlist(cmd, **kwargs):
2016-05-31 05:34:13 +00:00
'''
Handle several different commands being run
'''
2016-05-31 05:07:31 +00:00
if cmd == ['pacman', '-Sgg']:
2016-05-30 20:33:59 +00:00
return 'group-a pkg1\ngroup-a pkg2\ngroup-f pkg9\ngroup-c pkg3\ngroup-b pkg4'
2016-05-31 05:07:31 +00:00
elif cmd == ['pacman', '-Qg']:
2016-05-30 20:33:59 +00:00
return 'group-a pkg1\ngroup-b pkg4'
else:
2016-05-31 13:17:33 +00:00
return 'Untested command ({0}, {1})!'.format(cmd, kwargs)
2016-05-30 20:33:59 +00:00
2016-05-31 05:07:31 +00:00
cmdmock = MagicMock(side_effect=cmdlist)
2016-05-30 20:33:59 +00:00
sortmock = MagicMock()
with patch.dict(pacman.__salt__, {
2016-05-31 05:07:31 +00:00
'cmd.run': cmdmock,
2016-05-30 20:33:59 +00:00
'pkg_resource.sort_pkglist': sortmock
}):
self.assertDictEqual(pacman.group_list(), {'available': ['group-c', 'group-f'], 'installed': ['group-b'], 'partially_installed': ['group-a']})
2016-05-30 21:39:16 +00:00
def test_group_info(self):
2016-05-31 05:07:31 +00:00
'''
Test if it shows the packages in a group
'''
2016-05-30 21:39:16 +00:00
2016-05-31 13:17:33 +00:00
def cmdlist(cmd, **kwargs):
2016-05-31 05:34:13 +00:00
'''
Handle several different commands being run
'''
2016-05-31 05:07:31 +00:00
if cmd == ['pacman', '-Sgg', 'testgroup']:
2016-05-30 21:39:16 +00:00
return 'testgroup pkg1\ntestgroup pkg2'
else:
2016-05-31 13:17:33 +00:00
return 'Untested command ({0}, {1})!'.format(cmd, kwargs)
2016-05-30 21:39:16 +00:00
2016-05-31 05:07:31 +00:00
cmdmock = MagicMock(side_effect=cmdlist)
2016-05-30 21:39:16 +00:00
sortmock = MagicMock()
with patch.dict(pacman.__salt__, {
2016-05-31 05:07:31 +00:00
'cmd.run': cmdmock,
2016-05-30 21:39:16 +00:00
'pkg_resource.sort_pkglist': sortmock
}):
2016-05-31 05:07:31 +00:00
self.assertEqual(pacman.group_info('testgroup')['default'], ['pkg1', 'pkg2'])
2016-05-30 20:33:59 +00:00
2016-05-30 21:56:53 +00:00
def test_group_diff(self):
2016-05-31 05:07:31 +00:00
'''
Test if it shows the difference between installed and target group contents
'''
2016-05-30 21:56:53 +00:00
listmock = MagicMock(return_value={'A': ['1.0'], 'B': ['2.0']})
2016-05-31 05:34:13 +00:00
groupmock = MagicMock(return_value={
'mandatory': [],
'optional': [],
2016-05-31 06:03:15 +00:00
'default': ['A', 'C'],
2016-05-31 05:34:13 +00:00
'conditional': []})
2016-05-30 21:56:53 +00:00
with patch.dict(pacman.__salt__, {
2016-05-31 05:07:31 +00:00
'pkg.list_pkgs': listmock,
2016-05-30 21:56:53 +00:00
'pkg.group_info': groupmock
}):
results = pacman.group_diff('testgroup')
self.assertEqual(results['default'], {'installed': ['A'], 'not installed': ['C']})
if __name__ == '__main__':
from integration import run_tests
run_tests(PacmanTestCase, needs_daemon=False)