Ejemplo n.º 1
0
 def test_unmask(self):
     # Test already masked
     self._mask_unmask('unmask', False)
     # Test not masked (should take no action and return True). We don't
     # need to repeat this in test_unmask_runtime.
     with patch.object(systemd, '_check_for_unit_changes', self.mock_none):
         with patch.object(systemd, 'masked', self.mock_false):
             self.assertTrue(systemd.unmask(self.unit_name))
Ejemplo n.º 2
0
 def test_unmask(self):
     '''
         Test to unmask the specified service with systemd
     '''
     mock = MagicMock(return_value=False)
     with patch.object(systemd, '_untracked_custom_unit_found', mock):
         with patch.object(systemd, '_unit_file_changed', mock):
             with patch.dict(systemd.__salt__, {'cmd.retcode': mock}):
                 self.assertTrue(systemd.unmask("sshd"))
Ejemplo n.º 3
0
    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')
        scope_prefix = ['systemd-run', '--scope']

        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
                        self.assertTrue(mock_not_run.call_count == 0)

            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(
                            scope_prefix + 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(
                            scope_prefix + 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)
Ejemplo n.º 4
0
    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)