# Copyright 2024 Cloudbase Solutions Srl
# All Rights Reserved.

import logging
from unittest import mock

import ddt

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


@ddt.ddt
class BaseRockyLinuxMorphingToolsTestCase(test_base.CoriolisBaseTestCase):
    """Test suite for the BaseRockyLinuxMorphingTools class."""

    def setUp(self):
        super(BaseRockyLinuxMorphingToolsTestCase, self).setUp()
        self.detected_os_info = {
            'os_type': 'linux',
            'distribution_name': rocky.ROCKY_LINUX_DISTRO_IDENTIFIER,
            'release_version': '8',
            'friendly_release_name': mock.sentinel.friendly_release_name,
        }
        self.enable_repos = ['repo1', 'repo2']
        self.morphing_tools = rocky.BaseRockyLinuxMorphingTools(
            mock.sentinel.conn, mock.sentinel.os_root_dir,
            mock.sentinel.os_root_dir, mock.sentinel.hypervisor,
            mock.sentinel.event_manager, self.detected_os_info,
            mock.sentinel.osmorphing_parameters)

    @ddt.data(
        ('8', True),
        ('8.4', True),
        ('9', True),
        ('9.4', True),
        ('10', True),
        ('10.0', True),
        ('11', True),
        ('7', False),
        ('abc', False),
    )
    @ddt.unpack
    def test_check_os_supported(self, release_version, expected):
        detected_os_info = {
            "distribution_name": rocky.ROCKY_LINUX_DISTRO_IDENTIFIER,
            "release_version": release_version
        }
        result = rocky.BaseRockyLinuxMorphingTools.check_os_supported(
            detected_os_info
        )

        self.assertEqual(expected, result)

    def test_check_os_not_supported(self):
        detected_os_info = {
            "distribution_name": "unsupported",
        }
        result = rocky.BaseRockyLinuxMorphingTools.check_os_supported(
            detected_os_info
        )

        self.assertFalse(result)

    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot')
    def test_enable_repos(self, mock_exec_cmd_chroot):
        self.morphing_tools.enable_repos(self.enable_repos)

        mock_exec_cmd_chroot.assert_has_calls([
            mock.call("dnf config-manager --set-enabled repo1"),
            mock.call("dnf config-manager --set-enabled repo2"),
        ])

    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot')
    def test_enable_repos_empty(self, mock_exec_cmd_chroot):
        self.morphing_tools.enable_repos([])

        mock_exec_cmd_chroot.assert_not_called()

    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot')
    def test_enable_repos_with_exception(self, mock_exec_cmd_chroot):
        mock_exec_cmd_chroot.side_effect = exception.CoriolisException()

        with self.assertLogs(
                'coriolis.osmorphing.rocky', level=logging.WARN):
            self.morphing_tools.enable_repos(['repo1'])

        mock_exec_cmd_chroot.assert_called_once_with(
            "dnf config-manager --set-enabled repo1")
