Skip to content

Commit

Permalink
Add unit tests for osmorphing.osdetect.windows.py
Browse files Browse the repository at this point in the history
module
Signed-off-by: Mihaela Balutoiu <mbalutoiu@cloudbasesolutions.com>
  • Loading branch information
mihaelabalutoiu committed Apr 1, 2024
1 parent 5e52491 commit c5c3214
Showing 1 changed file with 192 additions and 0 deletions.
192 changes: 192 additions & 0 deletions coriolis/tests/osmorphing/osdetect/test_windows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Copyright 2024 Cloudbase Solutions Srl
# All Rights Reserved.

import logging
from unittest import mock

import ddt

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


@ddt.ddt
class WindowsOSDetectToolsTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the WindowsOSDetectTools class."""

def setUp(self):
super(WindowsOSDetectToolsTestCase, self).setUp()
self.conn = mock.MagicMock()
self.os_root_dir = 'C:\\'

self.windows_os_detect_tools = windows.WindowsOSDetectTools(
self.conn, self.os_root_dir, mock.sentinel.operation_timeout)

def test_returned_detected_os_info_fields(self):
expected_base_fields = [
"os_type",
"distribution_name",
"release_version",
"friendly_release_name",
"version_number",
"edition_id",
"installation_type",
"product_name"
]

result = (
windows.WindowsOSDetectTools.returned_detected_os_info_fields()
)

self.assertEqual(result, expected_base_fields)

def test__load_registry_hive(self):
self.windows_os_detect_tools._load_registry_hive(
mock.sentinel.subkey, mock.sentinel.path)

self.conn.exec_command.assert_called_once_with(
"reg.exe", ["load", mock.sentinel.subkey, mock.sentinel.path])

def test__unload_registry_hive(self):
self.windows_os_detect_tools._unload_registry_hive(
mock.sentinel.subkey)

self.conn.exec_command.assert_called_once_with(
"reg.exe", ["unload", mock.sentinel.subkey])

@mock.patch.object(windows.re, 'search')
def test__get_ps_fl_value(self, mock_search):
result = self.windows_os_detect_tools._get_ps_fl_value(
mock.sentinel.data, mock.sentinel.name)

mock_search.assert_called_once_with(
r'^%s\s*: (.*)$' % mock.sentinel.name,
mock.sentinel.data, windows.re.MULTILINE)

self.assertEqual(result, mock_search.return_value.groups()[0])

@ddt.data(
{
'CurrentVersion': mock.sentinel.CurrentVersion,
'CurrentMajorVersionNumber': (
mock.sentinel.CurrentMajorVersionNumber),
'should_raise': False
},
{
'CurrentVersion': None,
'CurrentMajorVersionNumber': None,
'should_raise': True
},
{
'CurrentVersion': mock.sentinel.CurrentVersion,
'CurrentMajorVersionNumber': None,
'should_raise': False
}
)
@mock.patch.object(windows.WindowsOSDetectTools, '_load_registry_hive')
@mock.patch.object(windows.WindowsOSDetectTools, '_unload_registry_hive')
@mock.patch.object(windows.WindowsOSDetectTools, '_get_ps_fl_value')
@mock.patch.object(windows.version, 'LooseVersion')
@mock.patch.object(windows.uuid, 'uuid4')
def test__get_image_version_info(
self, data, mock_uuid4, mock_LooseVersion, mock_get_ps_fl_value,
mock_unload_registry_hive, mock_load_registry_hive):
mock_get_ps_fl_value.side_effect = [
data['CurrentVersion'],
data['CurrentMajorVersionNumber'],
mock.sentinel.CurrentMinorVersionNumber,
mock.sentinel.CurrentBuildNumber,
mock.sentinel.InstallationType,
mock.sentinel.ProductName,
mock.sentinel.EditionID
]

if data['should_raise']:
self.assertRaises(
exception.CoriolisException,
self.windows_os_detect_tools._get_image_version_info)
else:
result = self.windows_os_detect_tools._get_image_version_info()

mock_load_registry_hive.assert_called_once_with(
"HKLM\\%s" % mock_uuid4.return_value,
"%sWindows\\System32\\config\\SOFTWARE" % self.os_root_dir)

self.conn.exec_ps_command.assert_called_once_with(
"Get-ItemProperty "
"'HKLM:\\%s\\Microsoft\\Windows NT\\CurrentVersion' "
"| select CurrentVersion, CurrentMajorVersionNumber, "
"CurrentMinorVersionNumber, CurrentBuildNumber, "
"InstallationType, ProductName, EditionID | FL" %
mock_uuid4.return_value)

mock_unload_registry_hive.assert_called_once_with(
"HKLM\\%s" % mock_uuid4.return_value)

expected_result = (
mock_LooseVersion.return_value,
mock.sentinel.EditionID,
mock.sentinel.InstallationType,
mock.sentinel.ProductName
)

self.assertEqual(result, expected_result)

@ddt.data(
{
'version_number': mock.sentinel.version_number,
'edition_id': 'server',
'installation_type': mock.sentinel.installation_type,
'product_name': mock.sentinel.product_name,
'distribution_name': windows.WINDOWS_SERVER_IDENTIFIER,
'should_raise': False
},
{
'version_number': mock.sentinel.version_number,
'edition_id': 'client',
'installation_type': mock.sentinel.installation_type,
'product_name': mock.sentinel.product_name,
'distribution_name': windows.WINDOWS_CLIENT_IDENTIFIER,
'should_raise': False
},
{
'should_raise': True
}
)
@mock.patch.object(windows.WindowsOSDetectTools, '_get_image_version_info')
def test_detect_os(self, data, mock_get_image_version_info):
if data['should_raise']:
mock_get_image_version_info.side_effect = (
exception.CoriolisException)
with self.assertLogs('coriolis.osmorphing.osdetect.windows',
level=logging.DEBUG):
self.assertRaises(
exception.CoriolisException,
self.windows_os_detect_tools.detect_os)
else:
mock_get_image_version_info.return_value = (
data['version_number'],
data['edition_id'],
data['installation_type'],
data['product_name']
)

expected_result = {
"version_number": data['version_number'],
"edition_id": data['edition_id'],
"installation_type": data['installation_type'],
"product_name": data['product_name'],
"os_type": windows.constants.OS_TYPE_WINDOWS,
"distribution_name": data['distribution_name'],
"release_version": data['product_name'],
"friendly_release_name": "Windows %s" % data['product_name']
}

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

self.assertEqual(result, expected_result)

mock_get_image_version_info.assert_called_once_with()

0 comments on commit c5c3214

Please sign in to comment.