# Copyright 2014 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
# Copyright (c) 2021 Dell Inc. or its subsidiaries.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""This module detects whether third-party libraries, utilized by third-party
drivers, are present on the system. If they are not, it mocks them and tinkers
with sys.modules so that the drivers can be loaded by unit tests, and the unit
tests can continue to test the functionality of those drivers without the
respective external libraries' actually being present.

Any external library required by a third-party driver should be mocked here.
Current list of mocked libraries:

"""

import importlib
import sys
from unittest import mock

from oslo_utils import importutils

from ironic.drivers.modules import ipmitool
from ironic.tests.unit.drivers import third_party_driver_mock_specs \
    as mock_specs


# IPMITool driver checks the system for presence of 'ipmitool' binary during
# __init__. We bypass that check in order to run the unit tests, which do not
# depend on 'ipmitool' being on the system.
ipmitool.TIMING_SUPPORT = False
ipmitool.DUAL_BRIDGE_SUPPORT = False
ipmitool.SINGLE_BRIDGE_SUPPORT = False

redfish = importutils.try_import('redfish')
if not redfish:
    redfish = mock.MagicMock(spec_set=mock_specs.REDFISH_SPEC)
    sys.modules['redfish'] = redfish

if 'ironic.drivers.redfish' in sys.modules:
    importlib.reload(sys.modules['ironic.drivers.modules.redfish'])


# attempt to load the external 'networking_generic_switch' library, which is
# required by the optional drivers.modules.switch.generic_switch module
networking_generic_switch = importutils.try_import('networking_generic_switch')
if not networking_generic_switch:
    ngs = mock.MagicMock(spec_set=mock_specs.NETWORKING_GENERIC_SWITCH_SPEC)
    ngs_devices = mock.MagicMock(
        spec_set=mock_specs.NETWORKING_GENERIC_SWITCH_DEVICES_SPEC)
    ngs_devices_utils = mock.MagicMock(
        spec_set=mock_specs.NETWORKING_GENERIC_SWITCH_DEVICES_UTILS_SPEC)
    ngs_config = mock.MagicMock(
        spec_set=mock_specs.NETWORKING_GENERIC_SWITCH_CONFIG_SPEC)

    # Set up the module structure
    ngs.devices = ngs_devices
    ngs.devices.utils = ngs_devices_utils
    ngs.config = ngs_config

    ngs.devices.DEVICES = {}

    # Install in sys.modules
    sys.modules['networking_generic_switch'] = ngs
    sys.modules['networking_generic_switch.devices'] = ngs_devices
    sys.modules['networking_generic_switch.devices.utils'] = ngs_devices_utils
    sys.modules['networking_generic_switch.config'] = ngs_config


# if anything has loaded the generic_switch driver yet, reload it now that the
# external library has been mocked
if 'ironic.drivers.modules.switch.generic_switch' in sys.modules:
    importlib.reload(
        sys.modules['ironic.drivers.modules.switch.generic_switch'])


class MockKwargsException(Exception):
    def __init__(self, *args, **kwargs):
        super(MockKwargsException, self).__init__(*args)
        self.kwargs = kwargs
