<commit_before># Copyright 2015 Datera
# 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 mock

from cinder import context
from cinder import exception
from cinder import test
from cinder.volume import configuration as conf
from cinder.volume.drivers import datera
from cinder.volume import volume_types


datera.DEFAULT_SI_SLEEP = 0.01
URL_TEMPLATES = datera.URL_TEMPLATES
OS_PREFIX = datera.OS_PREFIX
UNMANAGE_PREFIX = datera.UNMANAGE_PREFIX


class DateraVolumeTestCase(test.TestCase):

    def setUp(self):
        super(DateraVolumeTestCase, self).setUp()

        self.cfg = mock.Mock(spec=conf.Configuration)
        self.cfg.san_ip = '127.0.0.1'
        self.cfg.san_is_local = True
        self.cfg.datera_api_token = '<PASSWORD>'
        self.cfg.datera_api_port = '7717'
        self.cfg.datera_api_version = '1'
        self.cfg.datera_num_replicas = '2'
        self.cfg.datera_503_timeout = 0.01
        self.cfg.datera_503_interval = 0.001
        self.cfg.datera_acl_allow_all = False
        self.cfg.datera_debug = False
        self.cfg.san_login = 'user'
        self.cfg.san_password = '<PASSWORD>'

        mock_exec = mock.Mock()
        mock_exec.return_value = ('', '')

        self.driver = datera.DateraDriver(execute=mock_exec,
                                          configuration=self.cfg)
        self.driver.set_initialized()
        self.driver.configuration.get = _config_getter
        self.volume = _stub_volume()
        self.api_patcher = mock.patch('cinder.volume.drivers.datera.'
                                      'DateraDriver._issue_api_request')
        self.mock_api = self.api_patcher.start()

        self.addCleanup(self.api_patcher.stop)

    def test_volume_create_success(self):
        self.mock_api.return_value = stub_single_ai
        self.assertIsNone(self.driver.create_volume(self.volume))

    def test_volume_create_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        self.assertRaises(exception.DateraAPIException,
                          self.driver.create_volume, self.volume)

    def test_volume_create_delay(self):
        """Verify after 1st retry volume becoming available is a success."""

        def _progress_api_return(mock_api):
            if mock_api.retry_count == 1:
                _bad_vol_ai = stub_single_ai.copy()
                _bad_vol_ai['storage_instances']['storage-1'][
                    'volumes']['volume-1']['op_status'] = 'unavailable'
                return _bad_vol_ai
            else:
                self.mock_api.retry_count += 1
                return stub_single_ai
        self.mock_api.retry_count = 0
        self.mock_api.return_value = _progress_api_return(self.mock_api)
        self.assertEqual(1, self.mock_api.retry_count)
        self.assertIsNone(self.driver.create_volume(self.volume))

    @mock.patch.object(volume_types, 'get_volume_type')
    def test_create_volume_with_extra_specs(self, mock_get_type):
        self.mock_api.return_value = stub_single_ai
        mock_get_type.return_value = {
            'name': u'<NAME>',
            'qos_specs_id': None,
            'deleted': False,
            'created_at': '2015-08-14 04:18:11',
            'updated_at': None,
            'extra_specs': {
                u'volume_backend_name': u'datera',
                u'qos:max_iops_read': u'2000',
                u'qos:max_iops_write': u'4000',
                u'qos:max_iops_total': u'4000'
            },
            'is_public': True,
            'deleted_at': None,
            'id': u'dffb4a83-b8fb-4c19-9f8c-713bb75db3b1',
            'description': None
        }

        mock_volume = _stub_volume(
            volume_type_id='dffb4a83-b8fb-4c19-9f8c-713bb75db3b1'
        )

        self.assertIsNone(self.driver.create_volume(mock_volume))
        self.assertTrue(mock_get_type.called)

    def test_create_cloned_volume_success(self):
        source_volume = _stub_volume(
            id='7f91abfa-7964-41ed-88fc-207c3a290b4f',
            display_name='foo'
        )
        self.assertIsNone(self.driver.create_cloned_volume(self.volume,
                                                           source_volume))

    @mock.patch.object(datera.DateraDriver, 'extend_volume')
    def test_create_cloned_volume_success_larger(self, mock_extend):
        cloned_volume = _stub_volume(
            id='7f91abfa-7964-41ed-88fc-207c3a290b4f',
            display_name='foo',
            size=2
        )
        self.driver.create_cloned_volume(cloned_volume, self.volume)
        mock_extend.assert_called_once_with(cloned_volume,
                                            cloned_volume['size'])

    def test_create_cloned_volume_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        source_volume = _stub_volume(
            id='7f91abfa-7964-41ed-88fc-207c3a290b4f',
            display_name='foo'
        )
        self.assertRaises(exception.DateraAPIException,
                          self.driver.create_cloned_volume, self.volume,
                          source_volume)

    def test_delete_volume_success(self):
        self.assertIsNone(self.driver.delete_volume(self.volume))

    def test_delete_volume_not_found(self):
        self.mock_api.side_effect = exception.NotFound
        self.assertIsNone(self.driver.delete_volume(self.volume))

    def test_delete_volume_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        self.assertRaises(exception.DateraAPIException,
                          self.driver.delete_volume, self.volume)

    def test_ensure_export_success(self):
        with mock.patch('time.sleep'):
            self.mock_api.side_effect = self._generate_fake_api_request()
            ctxt = context.get_admin_context()
            self.assertIsNone(self.driver.ensure_export(ctxt,
                                                        self.volume,
                                                        None))

    def test_ensure_export_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        ctxt = context.get_admin_context()
        self.assertRaises(exception.DateraAPIException,
                          self.driver.ensure_export, ctxt, self.volume, None)

    def test_create_export_target_does_not_exist_success(self):
        with mock.patch('time.sleep'):
            self.mock_api.side_effect = self._generate_fake_api_request(
                targets_exist=False)
            ctxt = context.get_admin_context()
            self.assertIsNone(self.driver.create_export(ctxt,
                                                        self.volume,
                                                        None))

    def test_create_export_fails(self):
        with mock.patch('time.sleep'):
            self.mock_api.side_effect = exception.DateraAPIException
            ctxt = context.get_admin_context()
            self.assertRaises(exception.DateraAPIException,
                              self.driver.create_export,
                              ctxt,
                              self.volume,
                              None)

    def test_initialize_connection_success(self):
        self.mock_api.side_effect = self._generate_fake_api_request()
        connector = {}

        expected = {
            'driver_volume_type': 'iscsi',
            'data': {
                'target_discovered': False,
                'volume_id': self.volume['id'],
                'target_iqn': ('iqn.2013-05.com.daterainc:c20aba21-6ef6-'
                               '446b-b374-45733b4883ba--ST--storage-1:01:'
                               'sn:34e5b20fbadd3abb'),
                'target_portal': '172.28.94.11:3260',
                'target_lun': 0,
                'discard': False}}
        self.assertEqual(expected,
                         self.driver.initialize_connection(self.volume,
                                                           connector))

    def test_initialize_connection_fails(self):<commit_msg>Set sleep time to 0 in Datera driver<commit_after># Copyright 2015 Datera
# 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 mock

from cinder import context
from cinder import exception
from cinder import test
from cinder.volume import configuration as conf
from cinder.volume.drivers import datera
from cinder.volume import volume_types


datera.DEFAULT_SI_SLEEP = 0
URL_TEMPLATES = datera.URL_TEMPLATES
OS_PREFIX = datera.OS_PREFIX
UNMANAGE_PREFIX = datera.UNMANAGE_PREFIX


class DateraVolumeTestCase(test.TestCase):

    def setUp(self):
        super(DateraVolumeTestCase, self).setUp()

        self.cfg = mock.Mock(spec=conf.Configuration)
        self.cfg.san_ip = '127.0.0.1'
        self.cfg.san_is_local = True
        self.cfg.datera_api_token = '<PASSWORD>'
        self.cfg.datera_api_port = '7717'
        self.cfg.datera_api_version = '1'
        self.cfg.datera_num_replicas = '2'
        self.cfg.datera_503_timeout = 0.01
        self.cfg.datera_503_interval = 0.001
        self.cfg.datera_acl_allow_all = False
        self.cfg.datera_debug = False
        self.cfg.san_login = 'user'
        self.cfg.san_password = '<PASSWORD>'

        mock_exec = mock.Mock()
        mock_exec.return_value = ('', '')

        self.driver = datera.DateraDriver(execute=mock_exec,
                                          configuration=self.cfg)
        self.driver.set_initialized()
        self.driver.configuration.get = _config_getter
        self.volume = _stub_volume()
        self.api_patcher = mock.patch('cinder.volume.drivers.datera.'
                                      'DateraDriver._issue_api_request')
        self.mock_api = self.api_patcher.start()

        self.addCleanup(self.api_patcher.stop)

    def test_volume_create_success(self):
        self.mock_api.return_value = stub_single_ai
        self.assertIsNone(self.driver.create_volume(self.volume))

    def test_volume_create_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        self.assertRaises(exception.DateraAPIException,
                          self.driver.create_volume, self.volume)

    def test_volume_create_delay(self):
        """Verify after 1st retry volume becoming available is a success."""

        def _progress_api_return(mock_api):
            if mock_api.retry_count == 1:
                _bad_vol_ai = stub_single_ai.copy()
                _bad_vol_ai['storage_instances']['storage-1'][
                    'volumes']['volume-1']['op_status'] = 'unavailable'
                return _bad_vol_ai
            else:
                self.mock_api.retry_count += 1
                return stub_single_ai
        self.mock_api.retry_count = 0
        self.mock_api.return_value = _progress_api_return(self.mock_api)
        self.assertEqual(1, self.mock_api.retry_count)
        self.assertIsNone(self.driver.create_volume(self.volume))

    @mock.patch.object(volume_types, 'get_volume_type')
    def test_create_volume_with_extra_specs(self, mock_get_type):
        self.mock_api.return_value = stub_single_ai
        mock_get_type.return_value = {
            'name': u'<NAME>',
            'qos_specs_id': None,
            'deleted': False,
            'created_at': '2015-08-14 04:18:11',
            'updated_at': None,
            'extra_specs': {
                u'volume_backend_name': u'datera',
                u'qos:max_iops_read': u'2000',
                u'qos:max_iops_write': u'4000',
                u'qos:max_iops_total': u'4000'
            },
            'is_public': True,
            'deleted_at': None,
            'id': u'dffb4a83-b8fb-4c19-9f8c-713bb75db3b1',
            'description': None
        }

        mock_volume = _stub_volume(
            volume_type_id='dffb4a83-b8fb-4c19-9f8c-713bb75db3b1'
        )

        self.assertIsNone(self.driver.create_volume(mock_volume))
        self.assertTrue(mock_get_type.called)

    def test_create_cloned_volume_success(self):
        source_volume = _stub_volume(
            id='7f91abfa-7964-41ed-88fc-207c3a290b4f',
            display_name='foo'
        )
        self.assertIsNone(self.driver.create_cloned_volume(self.volume,
                                                           source_volume))

    @mock.patch.object(datera.DateraDriver, 'extend_volume')
    def test_create_cloned_volume_success_larger(self, mock_extend):
        cloned_volume = _stub_volume(
            id='7f91abfa-7964-41ed-88fc-207c3a290b4f',
            display_name='foo',
            size=2
        )
        self.driver.create_cloned_volume(cloned_volume, self.volume)
        mock_extend.assert_called_once_with(cloned_volume,
                                            cloned_volume['size'])

    def test_create_cloned_volume_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        source_volume = _stub_volume(
            id='7f91abfa-7964-41ed-88fc-207c3a290b4f',
            display_name='foo'
        )
        self.assertRaises(exception.DateraAPIException,
                          self.driver.create_cloned_volume, self.volume,
                          source_volume)

    def test_delete_volume_success(self):
        self.assertIsNone(self.driver.delete_volume(self.volume))

    def test_delete_volume_not_found(self):
        self.mock_api.side_effect = exception.NotFound
        self.assertIsNone(self.driver.delete_volume(self.volume))

    def test_delete_volume_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        self.assertRaises(exception.DateraAPIException,
                          self.driver.delete_volume, self.volume)

    def test_ensure_export_success(self):
        self.mock_api.side_effect = self._generate_fake_api_request()
        ctxt = context.get_admin_context()
        self.assertIsNone(self.driver.ensure_export(ctxt,
                                                    self.volume,
                                                    None))

    def test_ensure_export_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        ctxt = context.get_admin_context()
        self.assertRaises(exception.DateraAPIException,
                          self.driver.ensure_export, ctxt, self.volume, None)

    def test_create_export_target_does_not_exist_success(self):
        self.mock_api.side_effect = self._generate_fake_api_request(
            targets_exist=False)
        ctxt = context.get_admin_context()
        self.assertIsNone(self.driver.create_export(ctxt,
                                                    self.volume,
                                                    None))

    def test_create_export_fails(self):
        self.mock_api.side_effect = exception.DateraAPIException
        ctxt = context.get_admin_context()
        self.assertRaises(exception.DateraAPIException,
                          self.driver.create_export,
                          ctxt,
                          self.volume,
                          None)

    def test_initialize_connection_success(self):
        self.mock_api.side_effect = self._generate_fake_api_request()
        connector = {}

        expected = {
            'driver_volume_type': 'iscsi',
            'data': {
                'target_discovered': False,
                'volume_id': self.volume['id'],
                'target_iqn': ('iqn.2013-05.com.daterainc:c20aba21-6ef6-'
                               '446b-b374-45733b4883ba--ST--storage-1:01:'
                               'sn:34e5b20fbadd3abb'),
                'target_portal': '172.28.94.11:3260',
                'target_lun': 0,
                'discard': False}}
        self.assertEqual(expected,
                         self.driver.initialize_connection(self.volume,
                                                           connector))

    def test_initialize_connection_fails(self):
{'fraction_non_alphanumeric': 0.08940844739470129, 'fraction_numerical': 0.024496269752687542, 'mean_word_length': 2.0547281535977033, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5892531', 'n_tokens_mistral': 5270, 'n_tokens_neox': 5016, 'n_words': 766}