Skip to content

Commit

Permalink
Merge 748ad03 into add2ac1
Browse files Browse the repository at this point in the history
  • Loading branch information
nnDarshan committed Jan 13, 2017
2 parents add2ac1 + 748ad03 commit 8518d5b
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
100 changes: 100 additions & 0 deletions tendrl/commons/tests/test_service_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from tendrl.commons.utils.command \
import Command
from tendrl.commons.utils.service_status \
import ServiceStatus


class TestServiceStatus(object):
def test_service_status_constructor(self, monkeypatch):
service = ServiceStatus("tendrl-node-agent")
assert service.name == "tendrl-node-agent"

def test_service_exists_true(self, monkeypatch):
def mock_command_constructor(obj, command):
assert command == "systemctl show -p LoadState" + \
" tendrl-node-agent.service"
return

monkeypatch.setattr(Command, '__init__',
mock_command_constructor)

def mock_command_run(obj):
stdout = "LoadState=loaded"
stderr = ""
rc = 0
return stdout, stderr, rc

monkeypatch.setattr(Command, 'run', mock_command_run)

service = ServiceStatus("tendrl-node-agent")
exists = service.exists()
assert exists

def test_service_exists_false(self, monkeypatch):
def mock_command_constructor(obj, command):
assert command == "systemctl show -p LoadState " + \
"tendrl-node-agent.service"
return

monkeypatch.setattr(Command, '__init__',
mock_command_constructor)

def mock_command_run(obj):
stdout = ""
stderr = ""
rc = 1
return stdout, stderr, rc

monkeypatch.setattr(Command, 'run', mock_command_run)

service = ServiceStatus("tendrl-node-agent")
exists = service.exists()
assert not exists

def test_service_statuss_true(self, monkeypatch):
def mock_command_constructor(obj, command):
assert command == "systemctl status " + \
"tendrl-node-agent.service"
return

monkeypatch.setattr(Command, '__init__',
mock_command_constructor)

def mock_command_run(obj):
stdout = "tendrl-node-agent.service - A python agent local to" + \
" every managed storage node in the sds cluster " + \
" Loaded: loaded (/usr/lib/systemd/system/tendrl" + \
"-node-agent.service;disabled;vendor preset:disabled)" + \
" Active:active(running)since Thu 2017-01-12 19:14:32" + \
" IST; 15h ago Main PID: 5216 (tendrl-node-age) " + \
" CGroup: /system.slice/tendrl-node-agent.service"
stderr = ""
rc = 0
return stdout, stderr, rc

monkeypatch.setattr(Command, 'run', mock_command_run)

service = ServiceStatus("tendrl-node-agent")
status = service.status()
assert status

def test_service_status_false(self, monkeypatch):
def mock_command_constructor(obj, command):
assert command == "systemctl status " + \
"tendrl-node-agent.service"
return

monkeypatch.setattr(Command, '__init__',
mock_command_constructor)

def mock_command_run(obj):
stdout = ""
stderr = ""
rc = 1
return stdout, stderr, rc

monkeypatch.setattr(Command, 'run', mock_command_run)

service = ServiceStatus("tendrl-node-agent")
status = service.status()
assert not status
2 changes: 1 addition & 1 deletion tendrl/commons/utils/ansible_module_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from tendrl.commons.config import TendrlConfig

config = TendrlConfig()
config = TendrlConfig("commons", "/etc/tendrl/tendrl.conf")

LOG = logging.getLogger(__name__)
MODULE_EXECUTION_PATH = os.path.expandvars(
Expand Down
5 changes: 3 additions & 2 deletions tendrl/commons/utils/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"lscpu",
"getenforce",
"gluster",
"ceph"
"ceph",
"systemctl"
]


Expand Down Expand Up @@ -40,7 +41,7 @@ def run(self):
LOG.error("could not run the command %s. Error: %s" % (
self.attributes["_raw_params"], str(e)))
return "", str(e.message), -1
stdout = result.get("stdout", "").encode("ascii")
stdout = result.get("stdout", "")
stderr = result.get("stderr", "").encode("ascii")
rc = result.get("rc", -1)
return stdout, stderr, rc
36 changes: 36 additions & 0 deletions tendrl/commons/utils/service_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from tendrl.commons.utils import command as cmd_util


class ServiceStatus(object):
"""systemd services provider."""

def __init__(self, name):
self.name = name

def _execute_service_command(self, argument):
service_cmd = "systemctl "
service_cmd += " ".join(argument) if isinstance(
argument, tuple) else argument
service_cmd += " %s.service" % self.name
command = cmd_util.Command(service_cmd)
return command.run()

def exists(self):
stdout, stderr, rc = self._execute_service_command(
(
'show',
'-p',
'LoadState',
)
)
if rc == 0 and stdout.find('LoadState=loaded') >= 0:
return True
else:
return False

def status(self):
stdout, stderr, rc = self._execute_service_command('status')
if stdout and rc == 0:
return True
else:
return False

0 comments on commit 8518d5b

Please sign in to comment.