# Copyright 2024 Cloudbase Solutions Srl
# All Rights Reserved.

import logging
from unittest import mock

import ddt

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


@ddt.ddt
class BaseAmazonLinuxOSMorphingToolsTestCase(test_base.CoriolisBaseTestCase):
    """Test suite for the BaseAmazonLinuxOSMorphingTools class."""

    def setUp(self):
        super(BaseAmazonLinuxOSMorphingToolsTestCase, self).setUp()
        self.detected_os_info = {
            'os_type': 'linux',
            'distribution_name': amazon.AMAZON_DISTRO_NAME_IDENTIFIER,
            'release_version': '2',
            'friendly_release_name': mock.sentinel.friendly_release_name,
        }
        self.enable_repos = ['repo1', 'repo2']
        self.morphing_tools = amazon.BaseAmazonLinuxOSMorphingTools(
            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)

    def test_check_os_supported(self):
        detected_os_info = {
            "distribution_name": amazon.AMAZON_DISTRO_NAME_IDENTIFIER,
            "release_version": "2"
        }

        result = amazon.BaseAmazonLinuxOSMorphingTools.check_os_supported(
            detected_os_info)

        self.assertTrue(result)

    def test_check_os_not_supported(self):
        detected_os_info = {
            "distribution_name": 'unsupported',
        }
        result = amazon.BaseAmazonLinuxOSMorphingTools.check_os_supported(
            detected_os_info)

        self.assertFalse(result)

    @ddt.data(
        # Amazon Linux 2 and earlier use yum-config-manager.
        ('2', 'yum-config-manager --enable'),
        # Amazon Linux 2023+ uses dnf config-manager.
        ('2023', 'dnf config-manager --set-enabled'),
    )
    @ddt.unpack
    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot')
    def test_enable_repos(self, version, expected_cmd, mock_exec_cmd_chroot):
        self.morphing_tools._version = version

        self.morphing_tools.enable_repos(self.enable_repos)

        mock_exec_cmd_chroot.assert_has_calls([
            mock.call("%s repo1" % expected_cmd),
            mock.call("%s repo2" % expected_cmd),
        ])

    @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):
        self.morphing_tools._version = '2'
        mock_exec_cmd_chroot.side_effect = exception.CoriolisException()

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

        mock_exec_cmd_chroot.assert_called_once_with(
            "yum-config-manager --enable repo1")
