sysctl.persist: fix crash when unable to apply a value

while here, adjust the sysctl state documentation as it can
also be used for configuring other kernels
This commit is contained in:
Jasper Lievisse Adriaanse 2018-01-01 18:36:14 +01:00
parent f6f7966656
commit e57f170001
No known key found for this signature in database
GPG Key ID: 3C7E42C828FDBDB5
3 changed files with 82 additions and 3 deletions

View File

@ -4,6 +4,7 @@ Module for viewing and modifying OpenBSD sysctl parameters
'''
from __future__ import absolute_import
import os
import re
# Import salt libs
import salt.utils.files
@ -73,7 +74,11 @@ def assign(name, value):
cmd = 'sysctl {0}="{1}"'.format(name, value)
data = __salt__['cmd.run_all'](cmd)
if data['retcode'] != 0:
# Certain values cannot be set from this console, at the current
# securelevel or there are other restrictions that prevent us
# from applying the setting rightaway.
if re.match(r'^sysctl:.*: Operation not permitted$', data['stderr']) or \
data['retcode'] != 0:
raise CommandExecutionError('sysctl failed: {0}'.format(
data['stderr']))
new_name, new_value = data['stdout'].split(':', 1)

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
'''
Configuration of the Linux kernel using sysctl
==============================================
Configuration of the kernel using sysctl
========================================
Control the kernel sysctl system.

View File

@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jasper Lievisse Adriaanse <j@jasper.la>`
'''
# Import Python libs
from __future__ import absolute_import
# Import Salt Libs
import salt.modules.openbsd_sysctl as openbsd_sysctl
from salt.exceptions import CommandExecutionError
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
from tests.support.mock import (
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
@skipIf(NO_MOCK, NO_MOCK_REASON)
class OpenBSDSysctlTestCase(TestCase, LoaderModuleMockMixin):
'''
TestCase for salt.modules.openbsd_sysctl module
'''
def setup_loader_modules(self):
return {openbsd_sysctl: {}}
def test_get(self):
'''
Tests the return of get function
'''
mock_cmd = MagicMock(return_value='OpenBSD')
with patch.dict(openbsd_sysctl.__salt__, {'cmd.run': mock_cmd}):
self.assertEqual(openbsd_sysctl.get('kern.ostype'), 'OpenBSD')
def test_assign_cmd_failed(self):
'''
Tests if the assignment was successful or not
'''
cmd = {'pid': 1234, 'retcode': 1, 'stderr': '',
'stdout': 'kern.securelevel: 1 -> 9000'}
mock_cmd = MagicMock(return_value=cmd)
with patch.dict(openbsd_sysctl.__salt__, {'cmd.run_all': mock_cmd}):
self.assertRaises(CommandExecutionError,
openbsd_sysctl.assign,
'kern.securelevel', 9000)
def test_assign_cmd_eperm(self):
'''
Tests if the assignment was not permitted.
'''
cmd = {'pid': 1234, 'retcode': 0, 'stdout': '',
'stderr': 'sysctl: ddb.console: Operation not permitted'}
mock_cmd = MagicMock(return_value=cmd)
with patch.dict(openbsd_sysctl.__salt__, {'cmd.run_all': mock_cmd}):
self.assertRaises(CommandExecutionError,
openbsd_sysctl.assign,
'ddb.console', 1)
def test_assign(self):
'''
Tests the return of successful assign function
'''
cmd = {'pid': 1234, 'retcode': 0, 'stderr': '',
'stdout': 'net.inet.ip.forwarding: 0 -> 1'}
ret = {'net.inet.ip.forwarding': '1'}
mock_cmd = MagicMock(return_value=cmd)
with patch.dict(openbsd_sysctl.__salt__, {'cmd.run_all': mock_cmd}):
self.assertEqual(openbsd_sysctl.assign(
'net.inet.ip.forwarding', 1), ret)