def test_persist_no_conf_success(self):
        '''
        Tests successful add of config file when previously not one
        '''
        asn_cmd = {
            'pid': 1337,
            'retcode': 0,
            'stderr': '',
            'stdout': 'net.ipv4.ip_forward = 1'
        }
        mock_asn_cmd = MagicMock(return_value=asn_cmd)

        sys_cmd = 'systemd 208\n+PAM +LIBWRAP'
        mock_sys_cmd = MagicMock(return_value=sys_cmd)

        with patch('salt.utils.fopen', mock_open()) as m_open:
            with patch.dict(linux_sysctl.__salt__, {
                    'cmd.run_stdout': mock_sys_cmd,
                    'cmd.run_all': mock_asn_cmd
            }):
                with patch.dict(systemd.__context__,
                                {'salt.utils.systemd.booted': True}):
                    linux_sysctl.persist('net.ipv4.ip_forward', 1)
                    helper_open = m_open()
                    helper_open.write.assert_called_once_with(
                        '#\n# Kernel sysctl configuration\n#\n')
def test_persist_no_conf_success():
    """
    Tests successful add of config file when previously not one
    """
    config = "/etc/sysctl.conf"
    with patch("os.path.isfile", MagicMock(return_value=False)), patch(
        "os.path.exists", MagicMock(return_value=True)
    ), patch("salt.utils.path.which", MagicMock(return_value="/bin/sysctl")):
        asn_cmd = {
            "pid": 1337,
            "retcode": 0,
            "stderr": "",
            "stdout": "net.ipv4.ip_forward = 1",
        }
        mock_asn_cmd = MagicMock(return_value=asn_cmd)

        sys_cmd = "systemd 208\n+PAM +LIBWRAP"
        mock_sys_cmd = MagicMock(return_value=sys_cmd)

        with patch("salt.utils.files.fopen", mock_open()) as m_open, patch.dict(
            linux_sysctl.__context__, {"salt.utils.systemd.version": 232}
        ), patch.dict(
            linux_sysctl.__salt__,
            {"cmd.run_stdout": mock_sys_cmd, "cmd.run_all": mock_asn_cmd},
        ), patch.dict(
            systemd.__context__,
            {"salt.utils.systemd.booted": True, "salt.utils.systemd.version": 232},
        ):
            linux_sysctl.persist("net.ipv4.ip_forward", 1, config=config)
            writes = m_open.write_calls()
            assert writes == ["#\n# Kernel sysctl configuration\n#\n"], writes
Exemple #3
0
    def test_persist_no_conf_success(self):
        '''
        Tests successful add of config file when previously not one
        '''
        config = '/etc/sysctl.conf'
        with patch('os.path.isfile', MagicMock(return_value=False)), \
                patch('os.path.exists', MagicMock(return_value=True)):
            asn_cmd = {
                'pid': 1337,
                'retcode': 0,
                'stderr': '',
                'stdout': 'net.ipv4.ip_forward = 1'
            }
            mock_asn_cmd = MagicMock(return_value=asn_cmd)

            sys_cmd = 'systemd 208\n+PAM +LIBWRAP'
            mock_sys_cmd = MagicMock(return_value=sys_cmd)

            with patch('salt.utils.files.fopen', mock_open()) as m_open, \
                    patch.dict(linux_sysctl.__context__,
                               {'salt.utils.systemd.version': 232}), \
                    patch.dict(linux_sysctl.__salt__,
                               {'cmd.run_stdout': mock_sys_cmd,
                                'cmd.run_all': mock_asn_cmd}), \
                    patch.dict(systemd.__context__,
                               {'salt.utils.systemd.booted': True,
                                'salt.utils.systemd.version': 232}):
                linux_sysctl.persist('net.ipv4.ip_forward', 1, config=config)
                writes = m_open.write_calls()
                assert writes == ['#\n# Kernel sysctl configuration\n#\n'
                                  ], writes
    def test_persist_read_conf_success(self):
        '''
        Tests sysctl.conf read success
        '''
        asn_cmd = {
            'pid': 1337,
            'retcode': 0,
            'stderr': '',
            'stdout': 'net.ipv4.ip_forward = 1'
        }
        mock_asn_cmd = MagicMock(return_value=asn_cmd)

        sys_cmd = 'systemd 208\n+PAM +LIBWRAP'
        mock_sys_cmd = MagicMock(return_value=sys_cmd)

        with patch('salt.utils.fopen', mock_open()):
            with patch.dict(linux_sysctl.__salt__, {
                    'cmd.run_stdout': mock_sys_cmd,
                    'cmd.run_all': mock_asn_cmd
            }):
                with patch.dict(systemd.__context__,
                                {'salt.utils.systemd.booted': True}):
                    self.assertEqual(
                        linux_sysctl.persist('net.ipv4.ip_forward', 1),
                        'Updated')
    def test_persist_read_conf_success(self):
        """
        Tests sysctl.conf read success
        """
        with patch("os.path.isfile", MagicMock(return_value=True)), patch(
                "os.path.exists", MagicMock(return_value=True)):
            asn_cmd = {
                "pid": 1337,
                "retcode": 0,
                "stderr": "",
                "stdout": "net.ipv4.ip_forward = 1",
            }
            mock_asn_cmd = MagicMock(return_value=asn_cmd)

            sys_cmd = "systemd 208\n+PAM +LIBWRAP"
            mock_sys_cmd = MagicMock(return_value=sys_cmd)

            with patch("salt.utils.files.fopen", mock_open()):
                with patch.dict(linux_sysctl.__context__,
                                {"salt.utils.systemd.version": 232}):
                    with patch.dict(
                            linux_sysctl.__salt__,
                        {
                            "cmd.run_stdout": mock_sys_cmd,
                            "cmd.run_all": mock_asn_cmd
                        },
                    ):
                        with patch.dict(systemd.__context__,
                                        {"salt.utils.systemd.booted": True}):
                            self.assertEqual(
                                linux_sysctl.persist("net.ipv4.ip_forward", 1),
                                "Updated",
                            )
def test_persist_no_conf_failure():
    """
    Tests adding of config file failure
    """
    asn_cmd = {
        "pid": 1337,
        "retcode": 0,
        "stderr": "sysctl: permission denied",
        "stdout": "",
    }
    mock_asn_cmd = MagicMock(return_value=asn_cmd)
    cmd = "sysctl -w net.ipv4.ip_forward=1"
    mock_cmd = MagicMock(return_value=cmd)
    with patch.dict(
        linux_sysctl.__salt__,
        {"cmd.run_stdout": mock_cmd, "cmd.run_all": mock_asn_cmd},
    ):
        with patch("salt.utils.files.fopen", mock_open()) as m_open:
            with pytest.raises(CommandExecutionError):
                linux_sysctl.persist("net.ipv4.ip_forward", 1, config=None)
Exemple #7
0
    def test_persist_no_conf_success(self):
        '''
        Tests successful add of config file when previously not one
        '''
        asn_cmd = {'pid': 1337, 'retcode': 0, 'stderr': '',
               'stdout': 'net.ipv4.ip_forward = 1'}
        mock_asn_cmd = MagicMock(return_value=asn_cmd)

        sys_cmd = 'systemd 208\n+PAM +LIBWRAP'
        mock_sys_cmd = MagicMock(return_value=sys_cmd)

        with patch('salt.utils.fopen', mock_open()) as m_open:
            with patch.dict(linux_sysctl.__salt__,
                            {'cmd.run_stdout': mock_sys_cmd,
                             'cmd.run_all': mock_asn_cmd}):
                with patch.dict(systemd.__context__,
                                {'salt.utils.systemd.booted': True}):
                    linux_sysctl.persist('net.ipv4.ip_forward', 1)
                    helper_open = m_open()
                    helper_open.write.assert_called_once_with(
                        '#\n# Kernel sysctl configuration\n#\n')
Exemple #8
0
    def test_persist_read_conf_success(self):
        '''
        Tests sysctl.conf read success
        '''
        asn_cmd = {'pid': 1337, 'retcode': 0, 'stderr': '',
               'stdout': 'net.ipv4.ip_forward = 1'}
        mock_asn_cmd = MagicMock(return_value=asn_cmd)

        sys_cmd = 'systemd 208\n+PAM +LIBWRAP'
        mock_sys_cmd = MagicMock(return_value=sys_cmd)

        with patch('salt.utils.fopen', mock_open()):
            with patch.dict(linux_sysctl.__salt__,
                            {'cmd.run_stdout': mock_sys_cmd,
                             'cmd.run_all': mock_asn_cmd}):
                with patch.dict(systemd.__context__,
                                {'salt.utils.systemd.booted': True}):
                    self.assertEqual(linux_sysctl.persist(
                                     'net.ipv4.ip_forward', 1), 'Updated')