|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import random |
| 14 | +from unittest import mock |
| 15 | +import uuid |
| 16 | + |
| 17 | +from cinderclient import api_versions |
| 18 | + |
| 19 | +from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes |
| 20 | +from openstackclient.tests.unit import fakes |
| 21 | +from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes |
| 22 | +from openstackclient.tests.unit import utils |
| 23 | +from openstackclient.tests.unit.volume.v2 import fakes as volume_v2_fakes |
| 24 | + |
| 25 | + |
| 26 | +class FakeVolumeClient(object): |
| 27 | + |
| 28 | + def __init__(self, **kwargs): |
| 29 | + self.auth_token = kwargs['token'] |
| 30 | + self.management_url = kwargs['endpoint'] |
| 31 | + self.api_version = api_versions.APIVersion('3.0') |
| 32 | + |
| 33 | + self.attachments = mock.Mock() |
| 34 | + self.attachments.resource_class = fakes.FakeResource(None, {}) |
| 35 | + self.volumes = mock.Mock() |
| 36 | + self.volumes.resource_class = fakes.FakeResource(None, {}) |
| 37 | + |
| 38 | + |
| 39 | +class TestVolume(utils.TestCommand): |
| 40 | + |
| 41 | + def setUp(self): |
| 42 | + super().setUp() |
| 43 | + |
| 44 | + self.app.client_manager.volume = FakeVolumeClient( |
| 45 | + endpoint=fakes.AUTH_URL, |
| 46 | + token=fakes.AUTH_TOKEN |
| 47 | + ) |
| 48 | + self.app.client_manager.identity = identity_fakes.FakeIdentityv3Client( |
| 49 | + endpoint=fakes.AUTH_URL, |
| 50 | + token=fakes.AUTH_TOKEN |
| 51 | + ) |
| 52 | + self.app.client_manager.compute = compute_fakes.FakeComputev2Client( |
| 53 | + endpoint=fakes.AUTH_URL, |
| 54 | + token=fakes.AUTH_TOKEN, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +# TODO(stephenfin): Check if the responses are actually the same |
| 59 | +FakeVolume = volume_v2_fakes.FakeVolume |
| 60 | + |
| 61 | + |
| 62 | +class FakeVolumeAttachment: |
| 63 | + """Fake one or more volume attachments.""" |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def create_one_volume_attachment(attrs=None): |
| 67 | + """Create a fake volume attachment. |
| 68 | +
|
| 69 | + :param attrs: A dictionary with all attributes of volume attachment |
| 70 | + :return: A FakeResource object with id, status, etc. |
| 71 | + """ |
| 72 | + attrs = attrs or {} |
| 73 | + |
| 74 | + attachment_id = uuid.uuid4().hex |
| 75 | + volume_id = attrs.pop('volume_id', None) or uuid.uuid4().hex |
| 76 | + server_id = attrs.pop('instance', None) or uuid.uuid4().hex |
| 77 | + |
| 78 | + # Set default attribute |
| 79 | + attachment_info = { |
| 80 | + 'id': attachment_id, |
| 81 | + 'volume_id': volume_id, |
| 82 | + 'instance': server_id, |
| 83 | + 'status': random.choice([ |
| 84 | + 'attached', |
| 85 | + 'attaching', |
| 86 | + 'detached', |
| 87 | + 'reserved', |
| 88 | + 'error_attaching', |
| 89 | + 'error_detaching', |
| 90 | + 'deleted', |
| 91 | + ]), |
| 92 | + 'attach_mode': random.choice(['ro', 'rw']), |
| 93 | + 'attached_at': '2015-09-16T09:28:52.000000', |
| 94 | + 'detached_at': None, |
| 95 | + 'connection_info': { |
| 96 | + 'access_mode': 'rw', |
| 97 | + 'attachment_id': attachment_id, |
| 98 | + 'auth_method': 'CHAP', |
| 99 | + 'auth_password': 'AcUZ8PpxLHwzypMC', |
| 100 | + 'auth_username': '7j3EZQWT3rbE6pcSGKvK', |
| 101 | + 'cacheable': False, |
| 102 | + 'driver_volume_type': 'iscsi', |
| 103 | + 'encrypted': False, |
| 104 | + 'qos_specs': None, |
| 105 | + 'target_discovered': False, |
| 106 | + 'target_iqn': |
| 107 | + f'iqn.2010-10.org.openstack:volume-{attachment_id}', |
| 108 | + 'target_lun': '1', |
| 109 | + 'target_portal': '192.168.122.170:3260', |
| 110 | + 'volume_id': volume_id, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + # Overwrite default attributes if there are some attributes set |
| 115 | + attachment_info.update(attrs) |
| 116 | + |
| 117 | + attachment = fakes.FakeResource( |
| 118 | + None, |
| 119 | + attachment_info, |
| 120 | + loaded=True) |
| 121 | + return attachment |
| 122 | + |
| 123 | + @staticmethod |
| 124 | + def create_volume_attachments(attrs=None, count=2): |
| 125 | + """Create multiple fake volume attachments. |
| 126 | +
|
| 127 | + :param attrs: A dictionary with all attributes of volume attachment |
| 128 | + :param count: The number of volume attachments to be faked |
| 129 | + :return: A list of FakeResource objects |
| 130 | + """ |
| 131 | + attachments = [] |
| 132 | + |
| 133 | + for n in range(0, count): |
| 134 | + attachments.append( |
| 135 | + FakeVolumeAttachment.create_one_volume_attachment(attrs)) |
| 136 | + |
| 137 | + return attachments |
| 138 | + |
| 139 | + @staticmethod |
| 140 | + def get_volume_attachments(attachments=None, count=2): |
| 141 | + """Get an iterable MagicMock object with a list of faked volumes. |
| 142 | +
|
| 143 | + If attachments list is provided, then initialize the Mock object with |
| 144 | + the list. Otherwise create one. |
| 145 | +
|
| 146 | + :param attachments: A list of FakeResource objects faking volume |
| 147 | + attachments |
| 148 | + :param count: The number of volume attachments to be faked |
| 149 | + :return An iterable Mock object with side_effect set to a list of faked |
| 150 | + volume attachments |
| 151 | + """ |
| 152 | + if attachments is None: |
| 153 | + attachments = FakeVolumeAttachment.create_volume_attachments(count) |
| 154 | + |
| 155 | + return mock.Mock(side_effect=attachments) |
0 commit comments