1# coding: utf-8
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6#     http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14from __future__ import unicode_literals
15
16from testinfra.modules.base import InstanceModule
17from testinfra.utils import cached_property
18
19
20class Sysctl(InstanceModule):
21    """Test kernel parameters
22
23    >>> host.sysctl("kernel.osrelease")
24    "3.16.0-4-amd64"
25    >>> host.sysctl("vm.dirty_ratio")
26    20
27    """
28    @cached_property
29    def _sysctl_command(self):
30        return self.find_command('sysctl')
31
32    def __call__(self, name):
33        value = self.check_output("%s -n %s", self._sysctl_command, name)
34        try:
35            return int(value)
36        except ValueError:
37            return value
38
39    def __repr__(self):
40        return "<sysctl>"
41