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)
        # Remove trailing _ in "unmask_"
        action = action.rstrip("_").replace("_", "-")
        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,
                            )
Beispiel #2
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)
        # Remove trailing _ in "unmask_"
        action = action.rstrip('_').replace('_', '-')
        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)