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

Add osmorphing.osdetect* unit tests #306

Merged
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
Empty file.
Empty file.
38 changes: 38 additions & 0 deletions coriolis/tests/osmorphing/osdetect/test_amazon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 Cloudbase Solutions Srl
# All Rights Reserved.


from unittest import mock

from coriolis.osmorphing.osdetect import amazon
from coriolis.osmorphing.osdetect import base
from coriolis.tests import test_base


class AmazonLinuxOSDetectToolsTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for AmazonLinuxOSDetectTools class."""

@mock.patch.object(base.BaseLinuxOSDetectTools, '_get_os_release')
def test_detect_os(self, mock_get_os_release):
mock_get_os_release.return_value = {
"ID": "amzn",
"VERSION": mock.sentinel.version,
"NAME": "Amazon Linux"
}

expected_info = {
"os_type": amazon.constants.OS_TYPE_LINUX,
"distribution_name": amazon.AMAZON_DISTRO_NAME,
"release_version": mock.sentinel.version,
"friendly_release_name": "Amazon Linux %s" % mock.sentinel.version
}

amazon_os_detect_tools = amazon.AmazonLinuxOSDetectTools(
mock.sentinel.conn, mock.sentinel.os_root_dir,
mock.sentinel.operation_timeout)

result = amazon_os_detect_tools.detect_os()

mock_get_os_release.assert_called_once_with()

self.assertEqual(result, expected_info)
172 changes: 172 additions & 0 deletions coriolis/tests/osmorphing/osdetect/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Copyright 2024 Cloudbase Solutions Srl
# All Rights Reserved.

import os
from unittest import mock

from coriolis import exception
from coriolis.osmorphing.osdetect import base
from coriolis.tests import test_base


class BaseOSDetectToolsTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the BaseOSDetectTools class."""

@mock.patch.object(base.BaseOSDetectTools, '__abstractmethods__', set())
def setUp(self):
super(BaseOSDetectToolsTestCase, self).setUp()
self.base_os_detect_tools = base.BaseOSDetectTools(
mock.sentinel.conn, mock.sentinel.os_root_dir,
mock.sentinel.operation_timeout)

def test_returned_detected_os_info_fields(self):
self.assertRaises(
NotImplementedError,
self.base_os_detect_tools.returned_detected_os_info_fields
)

def test_detect_os(self):
self.assertRaises(
NotImplementedError,
self.base_os_detect_tools.detect_os
)

def test_set_environment(self):
self.base_os_detect_tools.set_environment(mock.sentinel.environment)

self.assertEqual(
self.base_os_detect_tools._environment, mock.sentinel.environment
)


class BaseLinuxOSDetectToolsTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the BaseLinuxOSDetectTools class."""

@mock.patch.object(
base.BaseLinuxOSDetectTools, '__abstractmethods__', set()
)
def setUp(self):
super(BaseLinuxOSDetectToolsTestCase, self).setUp()
self.chroot_path = '/mock/chroot/path'
self.os_root_dir = '/mock/os/root/dir'
self.base_os_detect = base.BaseLinuxOSDetectTools(
mock.sentinel.conn, self.os_root_dir,
mock.sentinel.operation_timeout)

def test_returned_detected_os_info_fields(self):
result = self.base_os_detect.returned_detected_os_info_fields()

self.assertEqual(
result, base.REQUIRED_DETECTED_OS_FIELDS
)

@mock.patch.object(base.utils, 'read_ssh_file')
def test__read_file(self, mock_read_ssh_file):
result = self.base_os_detect._read_file(self.chroot_path)

mocked_full_path = os.path.join(
self.base_os_detect._os_root_dir, self.chroot_path)

mock_read_ssh_file.assert_called_once_with(
self.base_os_detect._conn, mocked_full_path)

self.assertEqual(result, mock_read_ssh_file.return_value)

@mock.patch.object(base.utils, 'read_ssh_ini_config_file')
def test__read_config_file(self, mock_read_ssh_ini_config):
result = self.base_os_detect._read_config_file(self.chroot_path)

mocked_full_path = os.path.join(
self.base_os_detect._os_root_dir, self.chroot_path)

mock_read_ssh_ini_config.assert_called_once_with(
self.base_os_detect._conn, mocked_full_path, check_exists=False)

self.assertEqual(result, mock_read_ssh_ini_config.return_value)

@mock.patch.object(base.BaseLinuxOSDetectTools, '_read_config_file')
def test__get_os_release(self, mock_read_config_file):
result = self.base_os_detect._get_os_release()

mock_read_config_file.assert_called_once_with(
"etc/os-release", check_exists=True)

self.assertEqual(result, mock_read_config_file.return_value)

@mock.patch.object(base.utils, 'test_ssh_path')
def test__test_path(self, mock_test_ssh_path):
result = self.base_os_detect._test_path(self.chroot_path)

mocked_full_path = os.path.join(
self.base_os_detect._os_root_dir, self.chroot_path)
mock_test_ssh_path.assert_called_once_with(
self.base_os_detect._conn, mocked_full_path)

self.assertEqual(result, mock_test_ssh_path.return_value)

@mock.patch.object(base.utils, 'exec_ssh_cmd')
def test__exec_cmd(self, mock_exec_ssh_cmd):
result = self.base_os_detect._exec_cmd(mock.sentinel.cmd, timeout=120)

mock_exec_ssh_cmd.assert_called_once_with(
self.base_os_detect._conn, mock.sentinel.cmd,
environment=self.base_os_detect._environment, get_pty=True,
timeout=120)

self.assertEqual(result, mock_exec_ssh_cmd.return_value)

@mock.patch.object(base.utils, 'exec_ssh_cmd')
def test__exec_cmd_without_timeout(self, mock_exec_ssh_cmd):
result = self.base_os_detect._exec_cmd(mock.sentinel.cmd)

mock_exec_ssh_cmd.assert_called_once_with(
self.base_os_detect._conn, mock.sentinel.cmd,
environment=self.base_os_detect._environment, get_pty=True,
timeout=self.base_os_detect._osdetect_operation_timeout)

self.assertEqual(result, mock_exec_ssh_cmd.return_value)

@mock.patch.object(base.utils, 'exec_ssh_cmd')
def test__exec_cmd_with_exception(self, mock_exec_ssh_cmd):
mock_exec_ssh_cmd.side_effect = exception.MinionMachineCommandTimeout

self.assertRaises(
exception.OSMorphingSSHOperationTimeout,
self.base_os_detect._exec_cmd,
mock.sentinel.cmd
)

@mock.patch.object(base.utils, 'exec_ssh_cmd_chroot')
def test__exec_cmd_chroot(self, mock_exec_ssh_cmd_chroot):
result = self.base_os_detect._exec_cmd_chroot(
mock.sentinel.cmd, timeout=120)

mock_exec_ssh_cmd_chroot.assert_called_once_with(
self.base_os_detect._conn, self.base_os_detect._os_root_dir,
mock.sentinel.cmd, environment=self.base_os_detect._environment,
get_pty=True, timeout=120)

self.assertEqual(result, mock_exec_ssh_cmd_chroot.return_value)

@mock.patch.object(base.utils, 'exec_ssh_cmd_chroot')
def test__exec_cmd_chroot_without_timeout(self, mock_exec_ssh_cmd_chroot):
result = self.base_os_detect._exec_cmd_chroot(mock.sentinel.cmd)

mock_exec_ssh_cmd_chroot.assert_called_once_with(
self.base_os_detect._conn, self.base_os_detect._os_root_dir,
mock.sentinel.cmd, environment=self.base_os_detect._environment,
get_pty=True,
timeout=self.base_os_detect._osdetect_operation_timeout)

self.assertEqual(result, mock_exec_ssh_cmd_chroot.return_value)

@mock.patch.object(base.utils, 'exec_ssh_cmd_chroot')
def test__exec_cmd_chroot_with_exception(self, mock_exec_ssh_cmd_chroot):
mock_exec_ssh_cmd_chroot.side_effect = [
exception.MinionMachineCommandTimeout]

self.assertRaises(
exception.OSMorphingSSHOperationTimeout,
self.base_os_detect._exec_cmd_chroot,
mock.sentinel.cmd
)
76 changes: 76 additions & 0 deletions coriolis/tests/osmorphing/osdetect/test_centos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2024 Cloudbase Solutions Srl
# All Rights Reserved.

import logging
from unittest import mock

from coriolis.osmorphing.osdetect import base
from coriolis.osmorphing.osdetect import centos
from coriolis.tests import test_base


class CentOSOSDetectToolsTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the CentOSOSDetectTools class."""

def setUp(self):
super(CentOSOSDetectToolsTestCase, self).setUp()
self.centos_os_detect_tools = centos.CentOSOSDetectTools(
mock.sentinel.conn, mock.sentinel.os_root_dir,
mock.sentinel.operation_timeout)

@mock.patch.object(base.BaseLinuxOSDetectTools, '_test_path')
@mock.patch.object(base.BaseLinuxOSDetectTools, '_read_file')
def test_detect_os(self, mock_read_file, mock_test_path):
mock_test_path.return_value = True
mock_read_file.return_value = b"CentOS Linux release 7.9 (Core)"

expected_info = {
"os_type": centos.constants.OS_TYPE_LINUX,
"distribution_name": centos.CENTOS_DISTRO_IDENTIFIER,
"release_version": '7.9',
"friendly_release_name": "%s Version %s" % (
centos.CENTOS_DISTRO_IDENTIFIER, '7.9')
}

result = self.centos_os_detect_tools.detect_os()

mock_test_path.assert_called_once_with("etc/redhat-release")
mock_read_file.assert_called_once_with("etc/redhat-release")

self.assertEqual(result, expected_info)

@mock.patch.object(base.BaseLinuxOSDetectTools, '_test_path')
@mock.patch.object(base.BaseLinuxOSDetectTools, '_read_file')
def test_detect_os_centos_stream(self, mock_read_file, mock_test_path):
mock_test_path.return_value = True
mock_read_file.return_value = b"CentOS Stream release 8.3"

expected_info = {
"os_type": centos.constants.OS_TYPE_LINUX,
"distribution_name": centos.CENTOS_STREAM_DISTRO_IDENTIFIER,
"release_version": '8.3',
"friendly_release_name": "%s Version %s" % (
centos.CENTOS_STREAM_DISTRO_IDENTIFIER, '8.3')
}

result = self.centos_os_detect_tools.detect_os()

mock_test_path.assert_called_once_with("etc/redhat-release")
mock_read_file.assert_called_once_with("etc/redhat-release")

self.assertEqual(result, expected_info)

@mock.patch.object(base.BaseLinuxOSDetectTools, '_test_path')
@mock.patch.object(base.BaseLinuxOSDetectTools, '_read_file')
def test_detect_os_not_centos(self, mock_read_file, mock_test_path):
mock_test_path.return_value = True
mock_read_file.return_value = b"dummy release 8.3"

with self.assertLogs('coriolis.osmorphing.osdetect.centos',
level=logging.DEBUG):
result = self.centos_os_detect_tools.detect_os()

self.assertEqual(result, {})

mock_test_path.assert_called_once_with("etc/redhat-release")
mock_read_file.assert_called_once_with("etc/redhat-release")
36 changes: 36 additions & 0 deletions coriolis/tests/osmorphing/osdetect/test_coreos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2024 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from coriolis.osmorphing.osdetect import base
from coriolis.osmorphing.osdetect import coreos
from coriolis.tests import test_base


class CoreOSOSDetectToolsTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the CoreOSOSDetectTools class."""

@mock.patch.object(base.BaseLinuxOSDetectTools, '_get_os_release')
def test_detect_os(self, mock_get_os_release):
mock_get_os_release.return_value = {
"ID": "coreos",
"VERSION_ID": mock.sentinel.version
}

expected_info = {
"os_type": coreos.constants.OS_TYPE_LINUX,
"distribution_name": coreos.COREOS_DISTRO_IDENTIFIER,
"release_version": mock.sentinel.version,
"friendly_release_name": "CoreOS Linux %s" % mock.sentinel.version
}

coreos_os_detect_tools = coreos.CoreOSOSDetectTools(
mock.sentinel.conn, mock.sentinel.os_root_dir,
mock.sentinel.operation_timeout)

result = coreos_os_detect_tools.detect_os()

mock_get_os_release.assert_called_once_with()

self.assertEqual(result, expected_info)
Loading
Loading