#
#   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.

from unittest import mock

from keystoneauth1 import discover
from openstack.block_storage import v2 as block_storage_v2
from openstack.image import v2 as image_v2

from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes
from openstackclient.tests.unit import utils


class FakeClientMixin:
    def setUp(self):
        super().setUp()

        self.app.client_manager.volume = mock.Mock(spec=block_storage_v2.Proxy)
        self.app.client_manager.volume.api_version = '2'
        self.volume_client = self.app.client_manager.volume
        self.set_volume_api_version()  # default to the lowest

    def set_volume_api_version(self, version: str | None = None):
        """Set a fake block storage API version.

        :param version: The fake microversion to "support". This must be None
            since cinder v2 didn't support microversions.
        :returns: None
        """
        assert version is None

        self.volume_client.default_microversion = None
        self.volume_client.get_endpoint_data.return_value = (
            discover.EndpointData(
                min_microversion=None,
                max_microversion=None,
            )
        )


class TestVolume(
    identity_fakes.FakeClientMixin,
    FakeClientMixin,
    utils.TestCommand,
):
    def setUp(self):
        super().setUp()

        # avoid circular imports by defining this manually rather than using
        # openstackclient.tests.unit.image.v2.fakes.FakeClientMixin
        # TODO(stephenfin): Switch to spec_set once keystoneauth exposes
        # instance attributes as class attributes
        # https://review.opendev.org/c/openstack/keystoneauth/+/994090
        self.app.client_manager.image = mock.Mock(spec=image_v2.Proxy)
        self.image_client = self.app.client_manager.image
