-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |