def test_list_pkgs_as_list(self): ''' 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() with patch.dict( pacman.__salt__, { '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), { 'A': ['1.0'], 'B': ['2.0'] }) sortmock.assert_called_once() stringifymock.assert_not_called()
def test_verify_log(self): ''' Test that verify_log works as expected ''' message = 'Insecure logging configuration detected! Sensitive data may be logged.' mock_cheese = MagicMock() with patch.object(log, 'warning', mock_cheese): verify_log({'log_level': 'cheeseshop'}) mock_cheese.assert_called_once_with(message) mock_trace = MagicMock() with patch.object(log, 'warning', mock_trace): verify_log({'log_level': 'trace'}) mock_trace.assert_called_once_with(message) mock_none = MagicMock() with patch.object(log, 'warning', mock_none): verify_log({}) mock_none.assert_called_once_with(message) mock_info = MagicMock() with patch.object(log, 'warning', mock_info): verify_log({'log_level': 'info'}) mock_info.assert_not_called()
def test_verify_log(self): """ Test that verify_log works as expected """ message = "Insecure logging configuration detected! Sensitive data may be logged." mock_cheese = MagicMock() with patch.object(log, "warning", mock_cheese): verify_log({"log_level": "cheeseshop"}) mock_cheese.assert_called_once_with(message) mock_trace = MagicMock() with patch.object(log, "warning", mock_trace): verify_log({"log_level": "trace"}) mock_trace.assert_called_once_with(message) mock_none = MagicMock() with patch.object(log, "warning", mock_none): verify_log({}) mock_none.assert_called_once_with(message) mock_info = MagicMock() with patch.object(log, "warning", mock_info): verify_log({"log_level": "info"}) mock_info.assert_not_called()
def test_pvcreate_existing_pvs(self): ''' Test a scenario when all the submitted devices are already LVM PVs. ''' pvdisplay = MagicMock(return_value=True) with patch('salt.modules.linux_lvm.pvdisplay', pvdisplay): with patch.object(os.path, 'exists', return_value=True): ret = {'stdout': 'saltines', 'stderr': 'cheese', 'retcode': 0, 'pid': '1337'} cmd_mock = MagicMock(return_value=ret) with patch.dict(linux_lvm.__salt__, {'cmd.run_all': cmd_mock}): self.assertEqual(linux_lvm.pvcreate('A', metadatasize=1000), True) cmd_mock.assert_not_called()
def _runner(self, expected_ret, test=False, check_set=False, new_set=None, new_set_assertion=True): mock_check_set = MagicMock(return_value=check_set) mock_new_set = MagicMock() if new_set is None else MagicMock(return_value=new_set) with patch.dict(ipset.__salt__, {'ipset.check_set': mock_check_set, 'ipset.new_set': mock_new_set}): with patch.dict(ipset.__opts__, {'test': test}): actual_ret = ipset.set_present(self.fake_name, self.fake_set_type) mock_check_set.assert_called_once_with(self.fake_name) if new_set_assertion: mock_new_set.assert_called_once_with(self.fake_name, self.fake_set_type, 'ipv4') else: mock_new_set.assert_not_called() self.assertDictEqual(actual_ret, expected_ret)
def _runner(self, expected_ret, test=False, check_set=True, flush=True, flush_assertion=True): mock_check_set = MagicMock(return_value=check_set) mock_flush = MagicMock(return_value=flush) with patch.dict(ipset.__opts__, {'test': test}): with patch.dict(ipset.__salt__, {'ipset.check_set': mock_check_set, 'ipset.flush': mock_flush}): actual_ret = ipset.flush(self.fake_name) mock_check_set.assert_called_once_with(self.fake_name) if flush_assertion: mock_flush.assert_called_once_with(self.fake_name, 'ipv4') else: mock_flush.assert_not_called() self.assertDictEqual(actual_ret, expected_ret)
def _runner(self, expected_ret, test=False, check=False, delete=False, delete_assertion=False): mock_check = MagicMock(return_value=check) mock_delete = MagicMock(return_value=delete) with patch.dict(ipset.__opts__, {'test': test}): with patch.dict(ipset.__salt__, {'ipset.check': mock_check, 'ipset.delete': mock_delete}): actual_ret = ipset.absent(self.fake_name, self.fake_entries, set_name=self.fake_name) mock_check.assert_has_calls([call(self.fake_name, e, 'ipv4') for e in self.fake_entries], any_order=True) if delete_assertion: expected_calls = [call(self.fake_name, e, 'ipv4', set_name=self.fake_name) for e in self.fake_entries] if delete is not True: expected_calls = expected_calls[:1] mock_delete.assert_has_calls(expected_calls, any_order=True) else: mock_delete.assert_not_called() self.assertDictEqual(actual_ret, expected_ret)
def test_list_pkgs_as_list(self): ''' 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']} with patch.dict(pacman.__salt__, { '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) sortmock.assert_called_with(mock_ret) stringifymock.assert_not_called()
def _runner(self, expected_ret, test=False, check=False, add=False, add_assertion=False): mock_check = MagicMock(return_value=check) mock_add = MagicMock(return_value=add) with patch.dict(ipset.__opts__, {'test': test}): with patch.dict(ipset.__salt__, {'ipset.check': mock_check, 'ipset.add': mock_add}): actual_ret = ipset.present(self.fake_name, self.fake_entries, set_name=self.fake_name) mock_check.assert_has_calls([call(self.fake_name, e, 'ipv4') for e in self.fake_entries], any_order=True) if add_assertion: expected_calls = [call(self.fake_name, e, 'ipv4', set_name=self.fake_name) for e in self.fake_entries] if add is not True: # if the add fails, then it will only get called once. expected_calls = expected_calls[:1] mock_add.assert_has_calls(expected_calls, any_order=True) else: mock_add.assert_not_called() self.assertDictEqual(actual_ret, expected_ret)
def _runner(self, expected_ret, test=False, check_set=True, delete_set='', flush_assertion=False, delete_set_assertion=False): mock_check_set = MagicMock(return_value=check_set) mock_flush = MagicMock() mock_delete_set = MagicMock() if delete_set is None else MagicMock(return_value=delete_set) with patch.dict(ipset.__opts__, {'test': test}): with patch.dict(ipset.__salt__, {'ipset.check_set': mock_check_set, 'ipset.flush': mock_flush, 'ipset.delete_set': mock_delete_set}): actual_ret = ipset.set_absent(self.fake_name) mock_check_set.assert_called_once_with(self.fake_name, 'ipv4') if flush_assertion: mock_flush.assert_called_once_with(self.fake_name, 'ipv4') else: mock_flush.assert_not_called() if delete_set_assertion: mock_delete_set.assert_called_once_with(self.fake_name, 'ipv4') else: mock_delete_set.assert_not_called() self.assertDictEqual(actual_ret, expected_ret)
def _mask_unmask(self, action, runtime): ''' Common code for mask/unmask tests ''' # We want the traceback if the function name can't be found in the # systemd execution module, so don't provide a fallback value for the # call to getattr() here. func = getattr(systemd, action) systemctl_command = ['systemctl', action] if runtime: systemctl_command.append('--runtime') systemctl_command.append(self.unit_name + '.service') args = [self.unit_name, runtime] masked_mock = self.mock_true if action == 'unmask' else self.mock_false with patch.object(systemd, '_check_for_unit_changes', self.mock_none): if action == 'unmask': mock_not_run = MagicMock(return_value={'retcode': 0, 'stdout': '', 'stderr': '', 'pid': 12345}) with patch.dict(systemd.__salt__, {'cmd.run_all': mock_not_run}): with patch.object(systemd, 'masked', self.mock_false): # Test not masked (should take no action and return True) self.assertTrue(systemd.unmask(self.unit_name)) # Also should not have called cmd.run_all mock_not_run.assert_not_called() with patch.object(systemd, 'masked', masked_mock): # Has scopes available with patch.object(salt.utils.systemd, 'has_scope', self.mock_true): # Scope enabled, successful with patch.dict( systemd.__salt__, {'config.get': self.mock_true, 'cmd.run_all': self.mock_run_all_success}): ret = func(*args) self.assertTrue(ret) self.mock_run_all_success.assert_called_with( ['systemd-run', '--scope'] + systemctl_command, python_shell=False, redirect_stderr=True) # Scope enabled, failed with patch.dict( systemd.__salt__, {'config.get': self.mock_true, 'cmd.run_all': self.mock_run_all_failure}): self.assertRaises( CommandExecutionError, func, *args) self.mock_run_all_failure.assert_called_with( ['systemd-run', '--scope'] + systemctl_command, python_shell=False, redirect_stderr=True) # Scope disabled, successful with patch.dict( systemd.__salt__, {'config.get': self.mock_false, 'cmd.run_all': self.mock_run_all_success}): ret = func(*args) self.assertTrue(ret) self.mock_run_all_success.assert_called_with( systemctl_command, python_shell=False, redirect_stderr=True) # Scope disabled, failed with patch.dict( systemd.__salt__, {'config.get': self.mock_false, 'cmd.run_all': self.mock_run_all_failure}): self.assertRaises( CommandExecutionError, func, *args) self.mock_run_all_failure.assert_called_with( systemctl_command, python_shell=False, redirect_stderr=True) # Does not have scopes available with patch.object(salt.utils.systemd, 'has_scope', self.mock_false): # The results should be the same irrespective of # whether or not scope is enabled, since scope is not # available, so we repeat the below tests with it both # enabled and disabled. for scope_mock in (self.mock_true, self.mock_false): # Successful with patch.dict( systemd.__salt__, {'config.get': scope_mock, 'cmd.run_all': self.mock_run_all_success}): ret = func(*args) self.assertTrue(ret) self.mock_run_all_success.assert_called_with( systemctl_command, python_shell=False, redirect_stderr=True) # Failed with patch.dict( systemd.__salt__, {'config.get': scope_mock, 'cmd.run_all': self.mock_run_all_failure}): self.assertRaises( CommandExecutionError, func, *args) self.mock_run_all_failure.assert_called_with( systemctl_command, python_shell=False, redirect_stderr=True)
def test_disable(self): """ Test for Disable the named service to start at boot """ rc_update_mock = MagicMock(return_value=0) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.disable('name')) rc_update_mock.assert_called_once_with('rc-update delete name', python_shell=False) rc_update_mock.reset_mock() # disable service service_name = 'name' runlevels = ['l1'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.disable('name', runlevels='l1')) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # same as above with list runlevels = ['l1'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.disable('name', runlevels=['l1'])) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # remove from 'l1', and leave at 'l2' runlevels = ['l1', 'l2'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.disable('name', runlevels=['l1'])) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # remove from non-enabled level runlevels = ['l2'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.disable('name', runlevels=['l1'])) rc_update_mock.assert_not_called() rc_update_mock.reset_mock() # remove from 'l1' and 'l3', leave at 'l2' runlevels = ['l1', 'l2', 'l3'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.disable('name', runlevels=['l1', 'l3'])) rc_update_mock.assert_called_once_with('rc-update delete name l1 l3', python_shell=False) rc_update_mock.reset_mock() # rc-update failed rc_update_mock = MagicMock(return_value=1) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.disable('name')) rc_update_mock.assert_called_once_with('rc-update delete name', python_shell=False) rc_update_mock.reset_mock() # move service delete failed runlevels = ['l1'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.disable('name', runlevels='l1')) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # move service delete succeeds. add fails runlevels = ['l1', 'l2', 'l3'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.disable('name', runlevels=['l1', 'l3'])) rc_update_mock.assert_called_once_with('rc-update delete name l1 l3', python_shell=False) rc_update_mock.reset_mock()
def test_enable(self): """ Test for Enable the named service to start at boot """ rc_update_mock = MagicMock(return_value=0) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name')) rc_update_mock.assert_called_once_with('rc-update add name', python_shell=False) rc_update_mock.reset_mock() # move service from 'l1' to 'l2' runlevel service_name = 'name' runlevels = ['l1'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_has_calls([call('rc-update delete name l1', python_shell=False), call('rc-update add name l2', python_shell=False)]) rc_update_mock.reset_mock() # requested levels are the same as the current ones with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels='l1')) rc_update_mock.assert_not_called() rc_update_mock.reset_mock() # same as above with the list instead of the string with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l1'])) rc_update_mock.assert_not_called() rc_update_mock.reset_mock() # add service to 'l2' runlevel with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l2', 'l1'])) rc_update_mock.assert_called_once_with('rc-update add name l2', python_shell=False) rc_update_mock.reset_mock() # remove service from 'l1' runlevel runlevels = ['l1', 'l2'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l2'])) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # move service from 'l2' add to 'l3', leaving at l1 with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l1', 'l3'])) rc_update_mock.assert_has_calls([call('rc-update delete name l2', python_shell=False), call('rc-update add name l3', python_shell=False)]) rc_update_mock.reset_mock() # remove from l1, l3, and add to l2, l4, and leave at l5 runlevels = ['l1', 'l3', 'l5'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l2', 'l4', 'l5'])) rc_update_mock.assert_has_calls([call('rc-update delete name l1 l3', python_shell=False), call('rc-update add name l2 l4', python_shell=False)]) rc_update_mock.reset_mock() # rc-update failed rc_update_mock = MagicMock(return_value=1) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name')) rc_update_mock.assert_called_once_with('rc-update add name', python_shell=False) rc_update_mock.reset_mock() # move service delete failed runlevels = ['l1'] level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # move service delete succeeds. add fails rc_update_mock = MagicMock() rc_update_mock.side_effect = [0, 1] with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_has_calls([call('rc-update delete name l1', python_shell=False), call('rc-update add name l2', python_shell=False)]) rc_update_mock.reset_mock()
def _mask_unmask(self, action, runtime): ''' Common code for mask/unmask tests ''' # We want the traceback if the function name can't be found in the # systemd execution module, so don't provide a fallback value for the # call to getattr() here. func = getattr(systemd, action) systemctl_command = ['systemctl', action] if runtime: systemctl_command.append('--runtime') systemctl_command.append(self.unit_name + '.service') args = [self.unit_name, runtime] masked_mock = self.mock_true if action == 'unmask' else self.mock_false with patch.object(systemd, '_check_for_unit_changes', self.mock_none): if action == 'unmask': mock_not_run = MagicMock(return_value={ 'retcode': 0, 'stdout': '', 'stderr': '', 'pid': 12345 }) with patch.dict(systemd.__salt__, {'cmd.run_all': mock_not_run}): with patch.object(systemd, 'masked', self.mock_false): # Test not masked (should take no action and return True) self.assertTrue(systemd.unmask(self.unit_name)) # Also should not have called cmd.run_all mock_not_run.assert_not_called() with patch.object(systemd, 'masked', masked_mock): # Has scopes available with patch.object(salt.utils.systemd, 'has_scope', self.mock_true): # Scope enabled, successful with patch.dict( systemd.__salt__, { 'config.get': self.mock_true, 'cmd.run_all': self.mock_run_all_success }): ret = func(*args) self.assertTrue(ret) self.mock_run_all_success.assert_called_with( ['systemd-run', '--scope'] + systemctl_command, python_shell=False, redirect_stderr=True) # Scope enabled, failed with patch.dict( systemd.__salt__, { 'config.get': self.mock_true, 'cmd.run_all': self.mock_run_all_failure }): self.assertRaises(CommandExecutionError, func, *args) self.mock_run_all_failure.assert_called_with( ['systemd-run', '--scope'] + systemctl_command, python_shell=False, redirect_stderr=True) # Scope disabled, successful with patch.dict( systemd.__salt__, { 'config.get': self.mock_false, 'cmd.run_all': self.mock_run_all_success }): ret = func(*args) self.assertTrue(ret) self.mock_run_all_success.assert_called_with( systemctl_command, python_shell=False, redirect_stderr=True) # Scope disabled, failed with patch.dict( systemd.__salt__, { 'config.get': self.mock_false, 'cmd.run_all': self.mock_run_all_failure }): self.assertRaises(CommandExecutionError, func, *args) self.mock_run_all_failure.assert_called_with( systemctl_command, python_shell=False, redirect_stderr=True) # Does not have scopes available with patch.object(salt.utils.systemd, 'has_scope', self.mock_false): # The results should be the same irrespective of # whether or not scope is enabled, since scope is not # available, so we repeat the below tests with it both # enabled and disabled. for scope_mock in (self.mock_true, self.mock_false): # Successful with patch.dict( systemd.__salt__, { 'config.get': scope_mock, 'cmd.run_all': self.mock_run_all_success }): ret = func(*args) self.assertTrue(ret) self.mock_run_all_success.assert_called_with( systemctl_command, python_shell=False, redirect_stderr=True) # Failed with patch.dict( systemd.__salt__, { 'config.get': scope_mock, 'cmd.run_all': self.mock_run_all_failure }): self.assertRaises(CommandExecutionError, func, *args) self.mock_run_all_failure.assert_called_with( systemctl_command, python_shell=False, redirect_stderr=True)
def test_disable(self): """ Test for Disable the named service to start at boot """ rc_update_mock = MagicMock(return_value=0) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.disable('name')) rc_update_mock.assert_called_once_with('rc-update delete name', python_shell=False) rc_update_mock.reset_mock() # disable service service_name = 'name' runlevels = ['l1'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.disable('name', runlevels='l1')) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # same as above with list runlevels = ['l1'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue( gentoo_service.disable('name', runlevels=['l1'])) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # remove from 'l1', and leave at 'l2' runlevels = ['l1', 'l2'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue( gentoo_service.disable('name', runlevels=['l1'])) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # remove from non-enabled level runlevels = ['l2'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue( gentoo_service.disable('name', runlevels=['l1'])) rc_update_mock.assert_not_called() rc_update_mock.reset_mock() # remove from 'l1' and 'l3', leave at 'l2' runlevels = ['l1', 'l2', 'l3'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue( gentoo_service.disable('name', runlevels=['l1', 'l3'])) rc_update_mock.assert_called_once_with('rc-update delete name l1 l3', python_shell=False) rc_update_mock.reset_mock() # rc-update failed rc_update_mock = MagicMock(return_value=1) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.disable('name')) rc_update_mock.assert_called_once_with('rc-update delete name', python_shell=False) rc_update_mock.reset_mock() # move service delete failed runlevels = ['l1'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.disable('name', runlevels='l1')) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # move service delete succeeds. add fails runlevels = ['l1', 'l2', 'l3'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse( gentoo_service.disable('name', runlevels=['l1', 'l3'])) rc_update_mock.assert_called_once_with('rc-update delete name l1 l3', python_shell=False) rc_update_mock.reset_mock()
def test_enable(self): """ Test for Enable the named service to start at boot """ rc_update_mock = MagicMock(return_value=0) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name')) rc_update_mock.assert_called_once_with('rc-update add name', python_shell=False) rc_update_mock.reset_mock() # move service from 'l1' to 'l2' runlevel service_name = 'name' runlevels = ['l1'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_has_calls([ call('rc-update delete name l1', python_shell=False), call('rc-update add name l2', python_shell=False) ]) rc_update_mock.reset_mock() # requested levels are the same as the current ones with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels='l1')) rc_update_mock.assert_not_called() rc_update_mock.reset_mock() # same as above with the list instead of the string with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l1'])) rc_update_mock.assert_not_called() rc_update_mock.reset_mock() # add service to 'l2' runlevel with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue( gentoo_service.enable('name', runlevels=['l2', 'l1'])) rc_update_mock.assert_called_once_with('rc-update add name l2', python_shell=False) rc_update_mock.reset_mock() # remove service from 'l1' runlevel runlevels = ['l1', 'l2'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue(gentoo_service.enable('name', runlevels=['l2'])) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # move service from 'l2' add to 'l3', leaving at l1 with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue( gentoo_service.enable('name', runlevels=['l1', 'l3'])) rc_update_mock.assert_has_calls([ call('rc-update delete name l2', python_shell=False), call('rc-update add name l3', python_shell=False) ]) rc_update_mock.reset_mock() # remove from l1, l3, and add to l2, l4, and leave at l5 runlevels = ['l1', 'l3', 'l5'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertTrue( gentoo_service.enable('name', runlevels=['l2', 'l4', 'l5'])) rc_update_mock.assert_has_calls([ call('rc-update delete name l1 l3', python_shell=False), call('rc-update add name l2 l4', python_shell=False) ]) rc_update_mock.reset_mock() # rc-update failed rc_update_mock = MagicMock(return_value=1) with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name')) rc_update_mock.assert_called_once_with('rc-update add name', python_shell=False) rc_update_mock.reset_mock() # move service delete failed runlevels = ['l1'] level_list_mock = MagicMock( return_value=self.__services({service_name: runlevels})) with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False) rc_update_mock.reset_mock() # move service delete succeeds. add fails rc_update_mock = MagicMock() rc_update_mock.side_effect = [0, 1] with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}): with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}): self.assertFalse(gentoo_service.enable('name', runlevels='l2')) rc_update_mock.assert_has_calls([ call('rc-update delete name l1', python_shell=False), call('rc-update add name l2', python_shell=False) ]) rc_update_mock.reset_mock()