Skip to content

Commit

Permalink
SystemInfo: New module
Browse files Browse the repository at this point in the history
  • Loading branch information
philpep committed Mar 22, 2015
1 parent 504d21f commit 6e7c9dc
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 81 deletions.
9 changes: 9 additions & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ Service
.. attribute:: is_enabled


SystemInfo
~~~~~~~~~~


.. autoclass:: testinfra.modules.SystemInfo
:members:



.. toctree::
:maxdepth: 2

Expand Down
9 changes: 0 additions & 9 deletions testinfra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

g = threading.local()
g.backend = None
g.sysinfo = None


def run(command, *args, **kwargs):
Expand All @@ -32,13 +31,5 @@ def run(command, *args, **kwargs):
return g.backend.run(command, *args, **kwargs)


def get_system_info():
if g.sysinfo is None:
import testinfra.sysinfo
g.sysinfo = testinfra.sysinfo.get_system_info()
return g.sysinfo


def set_backend(backend_type, *args, **kwargs):
g.backend = backend.get_backend(backend_type, *args, **kwargs)
g.sysinfo = None
12 changes: 2 additions & 10 deletions testinfra/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@
from testinfra.modules.interface import Interface
from testinfra.modules.package import Package
from testinfra.modules.service import Service


class _SystemInfo(object):

def __getattr__(self, attr):
from testinfra import get_system_info
return getattr(get_system_info(), attr)

system = _SystemInfo()
from testinfra.modules.systeminfo import SystemInfo


__all__ = [
"Command", "File", "Package", "Group", "Interface",
"Service", "system",
"Service", "SystemInfo",
]
5 changes: 2 additions & 3 deletions testinfra/modules/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import pytest

from testinfra import get_system_info
from testinfra.modules.base import Module


Expand All @@ -38,8 +37,8 @@ def __repr__(self):
@classmethod
def as_fixture(cls):
@pytest.fixture(scope="session")
def f():
if get_system_info().type == "linux":
def f(SystemInfo):
if SystemInfo.type == "linux":
return LinuxInterface
else:
raise NotImplementedError
Expand Down
100 changes: 100 additions & 0 deletions testinfra/modules/systeminfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# -*- coding: utf8 -*-
# Copyright © 2015 Philippe Pepiot
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import unicode_literals

import pytest

from testinfra.modules.base import Module


class SystemInfo(Module):
"""Return system informations"""

def __init__(self):
self._sysinfo = None
super(SystemInfo, self).__init__()

@property
def sysinfo(self):
if self._sysinfo is None:
self._sysinfo = self.get_system_info()
return self._sysinfo

def get_system_info(self):
sysinfo = {}
sysinfo["type"] = self.check_output("uname -s").lower()
lsb = self.run("lsb_release -a")
if lsb.rc == 0:
for line in lsb.stdout.splitlines():
key, value = line.split(":", 1)
key = key.strip().lower()
value = value.strip().lower()
if key == "distributor id":
sysinfo["distribution"] = value
elif key == "release":
sysinfo["release"] = value
elif key == "codename":
sysinfo["codename"] = value
else:
version = self.run("cat /etc/debian_version")
if version.rc == 0:
sysinfo["distribution"] = "debian"
sysinfo["release"] = version.stdout.splitlines()[0]
return sysinfo

@property
def type(self):
"""OS type
>>> Sysinfo.type
'linux'
"""
return self.sysinfo["type"]

@property
def distribution(self):
"""Distribution name
>>> Sysinfo.distribution
'debian'
"""
return self.sysinfo["distribution"]

@property
def release(self):
"""Distribution release number
>>> Sysinfo.release
'7.8'
"""
return self.sysinfo["release"]

@property
def codename(self):
"""Relase code name
>>> Sysinfo.codename
'wheezy'
"""
return self.sysinfo["codename"]

@classmethod
def as_fixture(cls):
@pytest.fixture(scope="session")
def f():
return SystemInfo()
f.__doc__ = cls.__doc__
return f
14 changes: 5 additions & 9 deletions testinfra/modules/test/test_sysinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
from __future__ import unicode_literals


from testinfra import get_system_info


def test_osinfo(mock_subprocess):
def test_osinfo(mock_subprocess, SystemInfo):
values = [
# uname -s
(b"Linux\n", b""),
Expand All @@ -35,8 +32,7 @@ def test_osinfo(mock_subprocess):
"communicate.side_effect": values,
"returncode": 0,
})
info = get_system_info()
assert info.type == "linux"
assert info.distribution == "debian"
assert info.release == "7.8"
assert info.codename == "wheezy"
assert SystemInfo.type == "linux"
assert SystemInfo.distribution == "debian"
assert SystemInfo.release == "7.8"
assert SystemInfo.codename == "wheezy"
1 change: 1 addition & 0 deletions testinfra/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
Interface = modules.Interface.as_fixture()
Command = modules.Command.as_fixture()
Service = modules.Service.as_fixture()
SystemInfo = modules.SystemInfo.as_fixture()


@pytest.fixture(autouse=True, scope="session")
Expand Down
50 changes: 0 additions & 50 deletions testinfra/sysinfo.py

This file was deleted.

0 comments on commit 6e7c9dc

Please sign in to comment.