Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Systemd cleanup in distro #153

Merged
merged 2 commits into from
Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions ipa/ipa_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ def __init__(self):

def _set_init_system(self, client):
"""Determine the init system of distribution."""
raise NotImplementedError(NOT_IMPLEMENTED)
if not self.init_system:
try:
out = ipa_utils.execute_ssh_command(
client,
'ps -p 1 -o comm='
)
except Exception as e:
raise IpaDistroException(
'An error occurred while retrieving'
' the distro init system: %s' % e
)
if out:
self.init_system = out.strip()

def get_install_cmd(self):
"""Return install package command for distribution."""
Expand All @@ -61,6 +73,35 @@ def get_update_cmd(self):
"""Return command to update instance."""
raise NotImplementedError(NOT_IMPLEMENTED)

def get_vm_info(self, client):
"""Return vm info."""
out = ''
self._set_init_system(client)

if self.init_system == 'systemd':
try:
out += 'systemd-analyze:\n\n'
out += ipa_utils.execute_ssh_command(
client,
'systemd-analyze'
)

out += 'systemd-analyze blame:\n\n'
out += ipa_utils.execute_ssh_command(
client,
'systemd-analyze blame'
)

out += 'journalctl -b:\n\n'
out += ipa_utils.execute_ssh_command(
client,
'sudo journalctl -b'
)
except Exception as error:
out = 'Failed to collect VM info: {0}.'.format(error)

return out

def install_package(self, client, package):
"""Install package on instance."""
install_cmd = "{sudo} '{install} {package}'".format(
Expand All @@ -87,8 +128,7 @@ def install_package(self, client, package):

def reboot(self, client):
"""Execute reboot command on instance."""
if not self.init_system:
self._set_init_system(client)
self._set_init_system(client)

reboot_cmd = "{sudo} '{stop_ssh};{reboot}'".format(
sudo=self.get_sudo_exec_wrapper(),
Expand Down
21 changes: 10 additions & 11 deletions ipa/ipa_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,14 @@ def _collect_vm_info(self):
"""
Gather basic info about VM
"""
try:
self.logger.info('Collecting basic info about VM')
client = self._get_ssh_client()
self.logger.info('systemd-analyze :')
self.execute_ssh_command(client, 'systemd-analyze')
self.logger.info('systemd-analyze blame')
self.execute_ssh_command(client, 'systemd-analyze blame')
self.logger.info('journalctl -b : ')
self.execute_ssh_command(client, 'sudo journalctl -b')
except Exception as error:
self.logger.info('Fail to collect VM info : {0}.'.format(error))
self.logger.info('Collecting basic info about VM')
client = self._get_ssh_client()

out = self.distro.get_vm_info(client)

with open(self.log_file, 'a') as log_file:
log_file.write('\n')
log_file.write(out)

def _update_history(self):
"""Save the current test information to history json."""
Expand Down Expand Up @@ -766,9 +763,11 @@ def test_image(self):

if status and self.early_exit:
break

# flag set to collect VM info
if self.collect_vm_info:
self._collect_vm_info()

# If tests pass and cleanup flag is none, or
# cleanup flag is true, terminate instance.
if status == 0 and self.cleanup is None or self.cleanup:
Expand Down
16 changes: 0 additions & 16 deletions ipa/ipa_sles.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from ipa import ipa_utils
from ipa.ipa_distro import Distro
from ipa.ipa_exceptions import IpaSLESException


class SLES(Distro):
"""SLES distro class."""

def _set_init_system(self, client):
"""Determine the init system of distribution."""
try:
out = ipa_utils.execute_ssh_command(
client,
'ps -p 1 -o comm='
)
except Exception as e:
raise IpaSLESException(
'An error occurred while retrieving'
' the distro init system: %s' % e
)
if out:
self.init_system = out.strip()

def get_install_cmd(self):
"""Return install package command for SLES."""
return 'zypper -n --no-gpg-checks in -y'
Expand Down
40 changes: 31 additions & 9 deletions tests/test_ipa_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from ipa.ipa_distro import Distro
from ipa.ipa_exceptions import IpaDistroException

from unittest.mock import MagicMock
from unittest.mock import call, MagicMock, patch

import pytest

Expand All @@ -49,20 +50,41 @@ def test_distro_not_implemented_methods(method):
)


def test_distro_set_init_system():
"""Test set init system raises not implemented exception."""
distro = Distro()
def test_distro_set_init_system_exception():
"""Test distro set init system method exception."""
client = MagicMock()
distro = Distro()

pytest.raises(
NotImplementedError,
getattr(distro, '_set_init_system'),
client
)
with patch('ipa.ipa_utils.execute_ssh_command', MagicMock(
side_effect=Exception('ERROR!'))) as mocked:
pytest.raises(
IpaDistroException,
distro._set_init_system,
client
)

mocked.assert_called_once_with(client, 'ps -p 1 -o comm=')


def test_distro_get_commands():
"""Test distro reboot and sudo command return values."""
distro = Distro()
assert distro.get_reboot_cmd() == 'shutdown -r now'
assert distro.get_sudo_exec_wrapper() == 'sudo sh -c'


def test_distro_get_vm_info():
"""Test distro get vm info method."""
client = MagicMock()
distro = Distro()
distro.init_system = 'systemd'

with patch('ipa.ipa_utils.execute_ssh_command',
MagicMock(return_value='')) as mocked:
distro.get_vm_info(client)

mocked.assert_has_calls([
call(client, 'systemd-analyze'),
call(client, 'systemd-analyze blame'),
call(client, 'sudo journalctl -b')
])
34 changes: 25 additions & 9 deletions tests/test_ipa_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,16 +590,32 @@ def test_azure_wait_on_instance(self,
provider._wait_on_instance('Stopped')
assert mock_get_instance_state.call_count == 1

@patch.object(IpaProvider, 'execute_ssh_command')
@patch.object(IpaProvider, '_get_ssh_client')
def test_collect_vm_info(self,
mock_get_ssh_client,
mock_execute_ssh_command):
def test_collect_vm_info(self, mock_get_ssh_client):
"""Test collect_vm_info method. """
mock_get_ssh_client.return_value = MagicMock()
mock_execute_ssh_command.return_value = None
distro = MagicMock()
client = MagicMock()
distro.get_vm_info.return_value = \
'Failed to collect VM info: Does not exist.'
mock_get_ssh_client.return_value = client

provider = IpaProvider(*args, **self.kwargs)
provider.logger.info = MagicMock()
provider._collect_vm_info()
provider.distro = distro
provider.log_file = 'fake_file.name'
provider.logger = MagicMock()

with patch('builtins.open', create=True) as mock_open:
mock_open.return_value = MagicMock(spec=io.IOBase)
file_handle = mock_open.return_value.__enter__.return_value

provider._collect_vm_info()

file_handle.write.assert_has_calls([
call('\n'),
call('Failed to collect VM info: Does not exist.')
])

provider.logger.info.assert_called_once_with(
'Collecting basic info about VM'
)
assert mock_get_ssh_client.call_count == 1
assert mock_execute_ssh_command.call_count == 3
16 changes: 0 additions & 16 deletions tests/test_ipa_sles_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,6 @@
from unittest.mock import MagicMock, patch


def test_sles_set_init_system_exception():
"""Test SLES set init system method exception."""
client = MagicMock()
sles = SLES()

with patch('ipa.ipa_utils.execute_ssh_command', MagicMock(
side_effect=Exception('ERROR!'))) as mocked:
pytest.raises(
IpaSLESException,
sles._set_init_system,
client
)

mocked.assert_called_once_with(client, 'ps -p 1 -o comm=')


def test_sles_get_stop_ssh_cmd():
"""Test SLES get stop ssh cmd method."""
sles = SLES()
Expand Down