# Copyright (c) 2015 Red Hat, Inc.
# All Rights Reserved.
#
#    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.

import errno
import os

import ddt
import mock
from oslo_config import cfg
from oslo_utils import importutils

from manila import exception
from manila.share import configuration as config
from manila.share import driver
from manila.share.drivers.glusterfs import layout
from manila import test
from manila.tests import fake_utils


CONF = cfg.CONF


fake_local_share_path = '/mnt/nfs/testvol/fakename'

fake_path_to_private_key = '/fakepath/to/privatekey'
fake_remote_server_password = 'fakepassword'


class GlusterfsFakeShareDriver(layout.GlusterfsShareDriverBase):

    supported_layouts = ('layout_fake.FakeLayout',
                         'layout_something.SomeLayout')
    supported_protocols = ('NFS,')


@ddt.ddt
class GlusterfsShareDriverBaseTestCase(test.TestCase):
    """Tests GlusterfsShareDriverBase."""

    def setUp(self):
        super(GlusterfsShareDriverBaseTestCase, self).setUp()
        CONF.set_default('driver_handles_share_servers', False)
        fake_conf, __ = self._setup()
        self._driver = GlusterfsFakeShareDriver(False, configuration=fake_conf)
        self.fake_share = mock.Mock()
        self.fake_context = mock.Mock()
        self.fake_access = mock.Mock()

    def _setup(self):
        fake_conf = config.Configuration(None)
        fake_layout = mock.Mock()
        self.mock_object(importutils, "import_object",
                         mock.Mock(return_value=fake_layout))
        return fake_conf, fake_layout

    def test_init(self):
        self.assertRaises(IndexError, layout.GlusterfsShareDriverBase, False,
                          configuration=config.Configuration(None))

    @ddt.data({'has_snap': None, 'layout_name': None},
              {'has_snap': False, 'layout_name': 'layout_fake.FakeLayout'},
              {'has_snap': True, 'layout_name': 'layout_something.SomeLayout'})
    @ddt.unpack
    def test_init_subclass(self, has_snap, layout_name):
        conf, _layout = self._setup()
        if layout_name is not None:
            conf.glusterfs_share_layout = layout_name
        if has_snap is None:
            del(_layout._snapshots_are_supported)
        else:
            _layout._snapshots_are_supported = has_snap

        _driver = GlusterfsFakeShareDriver(False, configuration=conf)

        snap_result = {None: False}.get(has_snap, has_snap)
        layout_result = {None: 'layout_fake.FakeLayout'}.get(layout_name,