2014-02-28 00:03:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2014-07-07 22:12:11 +00:00
|
|
|
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
|
2014-02-28 00:03:08 +00:00
|
|
|
'''
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-01-23 00:36:48 +00:00
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.mac_brew as mac_brew
|
2016-05-26 04:01:49 +00:00
|
|
|
from salt.exceptions import CommandExecutionError
|
2017-05-05 16:52:32 +00:00
|
|
|
import salt.utils.pkg
|
2016-01-23 00:36:48 +00:00
|
|
|
|
2014-02-28 00:03:08 +00:00
|
|
|
# Import Salt Testing Libs
|
2017-03-22 16:42:17 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
2017-05-15 22:41:59 +00:00
|
|
|
from tests.support.mock import MagicMock, Mock, patch, NO_MOCK, NO_MOCK_REASON
|
2014-02-28 00:03:08 +00:00
|
|
|
|
2014-02-28 07:05:03 +00:00
|
|
|
TAPS_STRING = 'homebrew/dupes\nhomebrew/science\nhomebrew/x11'
|
|
|
|
TAPS_LIST = ['homebrew/dupes', 'homebrew/science', 'homebrew/x11']
|
2014-03-03 20:51:42 +00:00
|
|
|
HOMEBREW_BIN = '/usr/local/bin/brew'
|
2014-02-28 00:03:08 +00:00
|
|
|
|
|
|
|
|
2014-08-07 23:02:31 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-03-22 16:42:17 +00:00
|
|
|
class BrewTestCase(TestCase, LoaderModuleMockMixin):
|
2014-02-28 00:03:08 +00:00
|
|
|
'''
|
2016-01-23 02:15:29 +00:00
|
|
|
TestCase for salt.modules.mac_brew module
|
2014-02-28 00:03:08 +00:00
|
|
|
'''
|
2017-03-22 16:42:17 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {mac_brew: {'__opts__': {'user': MagicMock(return_value='bar')}}}
|
2014-02-28 00:03:08 +00:00
|
|
|
|
2014-03-05 20:08:57 +00:00
|
|
|
# '_list_taps' function tests: 1
|
|
|
|
|
2014-02-28 00:03:08 +00:00
|
|
|
def test_list_taps(self):
|
|
|
|
'''
|
|
|
|
Tests the return of the list of taps
|
|
|
|
'''
|
2016-05-26 04:01:49 +00:00
|
|
|
mock_taps = MagicMock(return_value={'stdout': TAPS_STRING,
|
|
|
|
'retcode': 0})
|
2014-09-02 18:12:01 +00:00
|
|
|
mock_user = MagicMock(return_value='foo')
|
2015-04-29 12:40:31 +00:00
|
|
|
mock_cmd = MagicMock(return_value='')
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__salt__, {'file.get_user': mock_user,
|
2017-04-10 13:00:57 +00:00
|
|
|
'cmd.run_all': mock_taps,
|
|
|
|
'cmd.run': mock_cmd}):
|
2016-01-23 02:15:29 +00:00
|
|
|
self.assertEqual(mac_brew._list_taps(), TAPS_LIST)
|
2014-02-28 00:03:08 +00:00
|
|
|
|
2014-03-05 20:08:57 +00:00
|
|
|
# '_tap' function tests: 3
|
|
|
|
|
2014-02-28 00:03:08 +00:00
|
|
|
def test_tap_installed(self):
|
|
|
|
'''
|
|
|
|
Tests if tap argument is already installed or not
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.mac_brew._list_taps', MagicMock(return_value=TAPS_LIST)):
|
|
|
|
self.assertTrue(mac_brew._tap('homebrew/science'))
|
2014-02-28 00:03:08 +00:00
|
|
|
|
|
|
|
def test_tap_failure(self):
|
|
|
|
'''
|
|
|
|
Tests if the tap installation failed
|
|
|
|
'''
|
2016-05-26 04:01:49 +00:00
|
|
|
mock_failure = MagicMock(return_value={'stdout': '',
|
|
|
|
'stderr': '',
|
|
|
|
'retcode': 1})
|
2014-09-02 18:12:01 +00:00
|
|
|
mock_user = MagicMock(return_value='foo')
|
|
|
|
mock_cmd = MagicMock(return_value='')
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__salt__, {'cmd.run_all': mock_failure,
|
2014-09-02 18:12:01 +00:00
|
|
|
'file.get_user': mock_user,
|
2017-04-10 13:00:57 +00:00
|
|
|
'cmd.run': mock_cmd}), \
|
|
|
|
patch('salt.modules.mac_brew._list_taps', MagicMock(return_value={})):
|
2016-01-23 02:15:29 +00:00
|
|
|
self.assertFalse(mac_brew._tap('homebrew/test'))
|
2014-02-28 00:03:08 +00:00
|
|
|
|
|
|
|
def test_tap(self):
|
|
|
|
'''
|
2015-02-15 14:03:58 +00:00
|
|
|
Tests adding unofficial GitHub repos to the list of brew taps
|
2014-02-28 00:03:08 +00:00
|
|
|
'''
|
2015-04-29 12:37:59 +00:00
|
|
|
mock_failure = MagicMock(return_value={'retcode': 0})
|
|
|
|
mock_user = MagicMock(return_value='foo')
|
|
|
|
mock_cmd = MagicMock(return_value='')
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__salt__, {'cmd.run_all': mock_failure,
|
2017-04-10 13:00:57 +00:00
|
|
|
'file.get_user': mock_user,
|
|
|
|
'cmd.run': mock_cmd}), \
|
|
|
|
patch('salt.modules.mac_brew._list_taps', MagicMock(return_value=TAPS_LIST)):
|
2016-01-23 02:15:29 +00:00
|
|
|
self.assertTrue(mac_brew._tap('homebrew/test'))
|
2014-02-28 00:03:08 +00:00
|
|
|
|
2014-03-05 20:08:57 +00:00
|
|
|
# '_homebrew_bin' function tests: 1
|
|
|
|
|
2014-02-28 00:03:08 +00:00
|
|
|
def test_homebrew_bin(self):
|
|
|
|
'''
|
|
|
|
Tests the path to the homebrew binary
|
|
|
|
'''
|
|
|
|
mock_path = MagicMock(return_value='/usr/local')
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__salt__, {'cmd.run': mock_path}):
|
|
|
|
self.assertEqual(mac_brew._homebrew_bin(), '/usr/local/bin/brew')
|
2014-02-28 00:03:08 +00:00
|
|
|
|
2014-03-05 20:08:57 +00:00
|
|
|
# 'list_pkgs' function tests: 2
|
|
|
|
# Only tested a few basics
|
|
|
|
# Full functionality should be tested in integration phase
|
|
|
|
|
|
|
|
def test_list_pkgs_removed(self):
|
|
|
|
'''
|
|
|
|
Tests removed implementation
|
|
|
|
'''
|
2016-01-23 02:15:29 +00:00
|
|
|
self.assertEqual(mac_brew.list_pkgs(removed=True), {})
|
2014-03-05 20:08:57 +00:00
|
|
|
|
2014-03-05 20:56:30 +00:00
|
|
|
def test_list_pkgs_versions_true(self):
|
2014-03-05 20:08:57 +00:00
|
|
|
'''
|
|
|
|
Tests if pkg.list_pkgs is already in context and is a list
|
|
|
|
'''
|
|
|
|
mock_context = {'foo': ['bar']}
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__context__, {'pkg.list_pkgs': mock_context}):
|
|
|
|
self.assertEqual(mac_brew.list_pkgs(versions_as_list=True),
|
2014-03-05 20:56:30 +00:00
|
|
|
mock_context)
|
2014-03-05 20:08:57 +00:00
|
|
|
|
|
|
|
# 'version' function tests: 1
|
|
|
|
|
|
|
|
def test_version(self):
|
|
|
|
'''
|
|
|
|
Tests version name returned
|
|
|
|
'''
|
|
|
|
mock_version = MagicMock(return_value='0.1.5')
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__salt__, {'pkg_resource.version': mock_version}):
|
|
|
|
self.assertEqual(mac_brew.version('foo'), '0.1.5')
|
2014-03-05 20:08:57 +00:00
|
|
|
|
|
|
|
# 'latest_version' function tests: 0
|
|
|
|
# It has not been fully implemented
|
|
|
|
|
2014-03-06 20:01:02 +00:00
|
|
|
# 'remove' function tests: 1
|
|
|
|
# Only tested a few basics
|
|
|
|
# Full functionality should be tested in integration phase
|
|
|
|
|
|
|
|
def test_remove(self):
|
|
|
|
'''
|
|
|
|
Tests if package to be removed exists
|
|
|
|
'''
|
|
|
|
mock_params = MagicMock(return_value=({'foo': None}, 'repository'))
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.modules.mac_brew.list_pkgs', return_value={'test': '0.1.5'}), \
|
|
|
|
patch.dict(mac_brew.__salt__, {'pkg_resource.parse_targets': mock_params}):
|
2016-01-23 02:15:29 +00:00
|
|
|
self.assertEqual(mac_brew.remove('foo'), {})
|
2014-03-06 20:01:02 +00:00
|
|
|
|
2014-03-05 20:08:57 +00:00
|
|
|
# 'refresh_db' function tests: 2
|
|
|
|
|
2014-03-03 20:51:42 +00:00
|
|
|
def test_refresh_db_failure(self):
|
|
|
|
'''
|
|
|
|
Tests an update of homebrew package repository failure
|
|
|
|
'''
|
|
|
|
mock_user = MagicMock(return_value='foo')
|
2016-05-26 04:01:49 +00:00
|
|
|
mock_failure = MagicMock(return_value={'stdout': '',
|
|
|
|
'stderr': '',
|
|
|
|
'retcode': 1})
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__salt__, {'file.get_user': mock_user,
|
2017-04-10 13:00:57 +00:00
|
|
|
'cmd.run_all': mock_failure}), \
|
|
|
|
patch('salt.modules.mac_brew._homebrew_bin',
|
|
|
|
MagicMock(return_value=HOMEBREW_BIN)):
|
2017-05-05 16:52:32 +00:00
|
|
|
with patch.object(salt.utils.pkg, 'clear_rtag', Mock()):
|
|
|
|
self.assertRaises(CommandExecutionError, mac_brew.refresh_db)
|
2014-03-03 20:51:42 +00:00
|
|
|
|
|
|
|
def test_refresh_db(self):
|
|
|
|
'''
|
|
|
|
Tests a successful update of homebrew package repository
|
|
|
|
'''
|
|
|
|
mock_user = MagicMock(return_value='foo')
|
2014-09-02 18:12:01 +00:00
|
|
|
mock_success = MagicMock(return_value={'retcode': 0})
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__salt__, {'file.get_user': mock_user,
|
2017-04-10 13:00:57 +00:00
|
|
|
'cmd.run_all': mock_success}), \
|
|
|
|
patch('salt.modules.mac_brew._homebrew_bin',
|
|
|
|
MagicMock(return_value=HOMEBREW_BIN)):
|
2017-05-05 16:52:32 +00:00
|
|
|
with patch.object(salt.utils.pkg, 'clear_rtag', Mock()):
|
|
|
|
self.assertTrue(mac_brew.refresh_db())
|
2014-03-03 20:51:42 +00:00
|
|
|
|
2014-03-06 20:01:02 +00:00
|
|
|
# 'install' function tests: 1
|
|
|
|
# Only tested a few basics
|
|
|
|
# Full functionality should be tested in integration phase
|
|
|
|
|
|
|
|
def test_install(self):
|
|
|
|
'''
|
|
|
|
Tests if package to be installed exists
|
|
|
|
'''
|
|
|
|
mock_params = MagicMock(return_value=[None, None])
|
2016-01-23 02:15:29 +00:00
|
|
|
with patch.dict(mac_brew.__salt__,
|
2014-03-06 20:01:02 +00:00
|
|
|
{'pkg_resource.parse_targets': mock_params}):
|
2016-01-23 02:15:29 +00:00
|
|
|
self.assertEqual(mac_brew.install('name=foo'), {})
|