import abc

import os

from unittest.mock import patch, MagicMock

from library_test.base_test import BaseLibraryTest





class TestProcessTest(BaseLibraryTest, metaclass=abc.ABCMeta):

    TESTCASE_DIR = os.path.join(os.path.dirname(__file__), "testcase")



    @classmethod

    def get_module_path(cls):

        return "ascend_deployer.library.process_test"



    @classmethod

    def setUpClass(cls) -> None:

        super().setUpClass()





class AnsibleModuleMocker:

    def __init__(self):

        self.params = {}

        self.exit_json = MagicMock()

        self.fail_json = MagicMock()

        self.get_bin_path = MagicMock()

        self.run_command = MagicMock()





class TestProcessTestGeneral(TestProcessTest):



    @classmethod

    def get_testcase_path(cls):

        return os.path.join(cls.TESTCASE_DIR, "test_process_test.yml")



    def test_mcu_test(self):

        # The test cannot obtain the npu-smi executable installation method.

        from ascend_deployer.library.process_test import test_mcu

        module = AnsibleModuleMocker()

        module.get_bin_path.return_value = False

        self.assertEqual({}, test_mcu(module))



        # Test npu-smi info -l command execution failed

        module.get_bin_path.return_value = True

        module.run_command.return_value = (-1, "err out", "")

        self.assertEqual({}, test_mcu(module))



        # Test query mcu version successful

        module.get_bin_path.return_value = True

        npu_info = """            Total Count                    : 1



            NPU ID                         : 8

            Product Name                   : IT21PDDA01

            Serial Number                  : 033VNY10MB000071

            Chip Count                     : 1"""

        module.run_command.return_value = (0, npu_info, "")

        multi_run_command_return = {8: {'success': True,

                                        'output': 'Version                        : 24.2.1',

                                        'error': '',

                                        'rc': 0,

                                        'upgrade_rc': 0,

                                        'activate_rc': 0}}

        with patch('ascend_deployer.module_utils.common_utils.McuMultiProcess.multi_run_command',

                   return_value=multi_run_command_return):

            self.assertEqual({'npu_id_8': '24.2.1'}, test_mcu(module))



        # Test version query command execution failed

        module.get_bin_path.return_value = True

        npu_info = """            Total Count                    : 1



            NPU ID                         : 8

            Product Name                   : IT21PDDA01

            Serial Number                  : 033VNY10MB000071

            Chip Count                     : 1"""

        module.run_command.return_value = (0, npu_info, "")

        multi_run_command_return = {8: {'success': False,

                                        'output': 'command execute err',

                                        'error': '',

                                        'rc': 0,

                                        'upgrade_rc': 0,

                                        'activate_rc': 0}}

        with patch('ascend_deployer.module_utils.common_utils.McuMultiProcess.multi_run_command',

                   return_value=multi_run_command_return):

            self.assertEqual({'npu_id_8': 'ERROR'}, test_mcu(module))