Skip to content

Commit 14a4f89

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "tests: Add volume v1, v2, v3 FakeClientMixin"
2 parents dd5a21f + 98fb167 commit 14a4f89

9 files changed

Lines changed: 115 additions & 44 deletions

File tree

openstackclient/tests/unit/common/test_availability_zone.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ def _build_network_az_datalist(network_az, long_datalist=False):
7878
return (datalist,)
7979

8080

81-
class TestAvailabilityZone(network_fakes.FakeClientMixin, utils.TestCommand):
81+
class TestAvailabilityZone(
82+
network_fakes.FakeClientMixin,
83+
volume_fakes.FakeClientMixin,
84+
utils.TestCommand,
85+
):
8286
def setUp(self):
8387
super().setUp()
8488

8589
self.app.client_manager.sdk_connection.compute = mock.Mock()
8690
self.compute_client = self.app.client_manager.sdk_connection.compute
8791
self.compute_client.availability_zones = mock.Mock()
8892

89-
self.app.client_manager.sdk_connection.volume = mock.Mock()
90-
self.volume_sdk_client = self.app.client_manager.sdk_connection.volume
91-
self.volume_sdk_client.availability_zones = mock.Mock()
92-
9393

9494
class TestAvailabilityZoneList(TestAvailabilityZone):
9595
compute_azs = compute_fakes.create_availability_zones()

openstackclient/tests/unit/compute/v2/fakes.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from openstackclient.tests.unit.image.v2 import fakes as image_fakes
4242
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
4343
from openstackclient.tests.unit import utils
44-
from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes
44+
from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes
4545

4646
floating_ip_num = 100
4747
fix_ip_num = 100
@@ -151,6 +151,7 @@ def __init__(self, **kwargs):
151151
class TestComputev2(
152152
network_fakes.FakeClientMixin,
153153
image_fakes.FakeClientMixin,
154+
volume_fakes.FakeClientMixin,
154155
utils.TestCommand,
155156
):
156157
def setUp(self):
@@ -171,12 +172,6 @@ def setUp(self):
171172
token=fakes.AUTH_TOKEN,
172173
)
173174

174-
self.app.client_manager.volume = volume_fakes.FakeVolumeClient(
175-
endpoint=fakes.AUTH_URL,
176-
token=fakes.AUTH_TOKEN,
177-
)
178-
self.volume_client = self.app.client_manager.volume
179-
180175

181176
def create_one_aggregate(attrs=None):
182177
"""Create a fake aggregate.

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from openstackclient.tests.unit.image.v2 import fakes as image_fakes
3535
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
3636
from openstackclient.tests.unit import utils
37-
from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes
37+
from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes
3838

3939

4040
class TestPowerStateColumn(utils.TestCase):
@@ -95,9 +95,6 @@ def setUp(self):
9595
self.volumes_mock = self.volume_client.volumes
9696
self.volumes_mock.reset_mock()
9797

98-
self.app.client_manager.sdk_connection.volume = mock.Mock()
99-
self.volume_sdk_client = self.app.client_manager.sdk_connection.volume
100-
10198
# Get a shortcut to the volume client VolumeManager Mock
10299
self.snapshots_mock = self.volume_client.volume_snapshots
103100
self.snapshots_mock.reset_mock()

openstackclient/tests/unit/compute/v2/test_server_volume.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ def setUp(self):
2727
super().setUp()
2828

2929
self.app.client_manager.sdk_connection.compute = mock.Mock()
30-
self.app.client_manager.sdk_connection.volume = mock.Mock()
3130
self.compute_client = self.app.client_manager.sdk_connection.compute
32-
self.volume_sdk_client = self.app.client_manager.sdk_connection.volume
3331

3432

3533
class TestServerVolumeList(TestServerVolume):

openstackclient/tests/unit/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
15-
#
1615

17-
from io import StringIO
16+
import io
1817
import os
1918

2019
import fixtures
@@ -75,7 +74,7 @@ def setUp(self):
7574

7675
def check_parser(self, cmd, args, verify_args):
7776
cmd_parser = cmd.get_parser('check_parser')
78-
stderr = StringIO()
77+
stderr = io.StringIO()
7978
with fixtures.MonkeyPatch('sys.stderr', stderr):
8079
try:
8180
parsed_args = cmd_parser.parse_args(args)

openstackclient/tests/unit/volume/v1/fakes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, **kwargs):
5353
self.management_url = kwargs['endpoint']
5454

5555

56-
class TestVolumev1(utils.TestCommand):
56+
class FakeClientMixin:
5757
def setUp(self):
5858
super().setUp()
5959

@@ -63,6 +63,11 @@ def setUp(self):
6363
)
6464
self.volume_client = self.app.client_manager.volume
6565

66+
67+
class TestVolumev1(FakeClientMixin, utils.TestCommand):
68+
def setUp(self):
69+
super().setUp()
70+
6671
self.app.client_manager.identity = identity_fakes.FakeIdentityv2Client(
6772
endpoint=fakes.AUTH_URL,
6873
token=fakes.AUTH_TOKEN,

openstackclient/tests/unit/volume/v2/fakes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __init__(self, **kwargs):
8989
self.volumes.resource_class = fakes.FakeResource(None, {})
9090

9191

92-
class TestVolume(utils.TestCommand):
92+
class FakeClientMixin:
9393
def setUp(self):
9494
super().setUp()
9595

@@ -105,6 +105,11 @@ def setUp(self):
105105
)
106106
self.volume_sdk_client = self.app.client_manager.sdk_connection.volume
107107

108+
109+
class TestVolume(FakeClientMixin, utils.TestCommand):
110+
def setUp(self):
111+
super().setUp()
112+
108113
self.app.client_manager.identity = identity_fakes.FakeIdentityv3Client(
109114
endpoint=fakes.AUTH_URL, token=fakes.AUTH_TOKEN
110115
)

openstackclient/tests/unit/volume/v2/test_volume_backup.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ class TestBackup(volume_fakes.TestVolume):
4141
def setUp(self):
4242
super().setUp()
4343

44-
self.app.client_manager.sdk_connection.volume = mock.Mock()
45-
self.volume_sdk_client = self.app.client_manager.sdk_connection.volume
4644
patcher = mock.patch.object(
4745
sdk_utils, 'supports_microversion', return_value=True
4846
)
@@ -325,24 +323,27 @@ def test_delete_multiple_backups_with_exception(self):
325323
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
326324

327325
find_mock_result = [self.backups[0], exceptions.CommandError]
328-
with mock.patch.object(
329-
self.volume_sdk_client, 'find_backup', side_effect=find_mock_result
330-
) as find_mock:
331-
try:
332-
self.cmd.take_action(parsed_args)
333-
self.fail('CommandError should be raised.')
334-
except exceptions.CommandError as e:
335-
self.assertEqual('1 of 2 backups failed to delete.', str(e))
336-
337-
find_mock.assert_any_call(self.backups[0].id, ignore_missing=False)
338-
find_mock.assert_any_call('unexist_backup', ignore_missing=False)
339-
340-
self.assertEqual(2, find_mock.call_count)
341-
self.volume_sdk_client.delete_backup.assert_called_once_with(
342-
self.backups[0].id,
343-
ignore_missing=False,
344-
force=False,
345-
)
326+
self.volume_sdk_client.find_backup.side_effect = find_mock_result
327+
328+
try:
329+
self.cmd.take_action(parsed_args)
330+
self.fail('CommandError should be raised.')
331+
except exceptions.CommandError as e:
332+
self.assertEqual('1 of 2 backups failed to delete.', str(e))
333+
334+
self.volume_sdk_client.find_backup.assert_any_call(
335+
self.backups[0].id, ignore_missing=False
336+
)
337+
self.volume_sdk_client.find_backup.assert_any_call(
338+
'unexist_backup', ignore_missing=False
339+
)
340+
341+
self.assertEqual(2, self.volume_sdk_client.find_backup.call_count)
342+
self.volume_sdk_client.delete_backup.assert_called_once_with(
343+
self.backups[0].id,
344+
ignore_missing=False,
345+
force=False,
346+
)
346347

347348

348349
class TestBackupList(TestBackup):

openstackclient/tests/unit/volume/v3/fakes.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from openstack.block_storage.v3 import _proxy
1919
from openstack.block_storage.v3 import availability_zone as _availability_zone
2020
from openstack.block_storage.v3 import extension as _extension
21+
from openstack.block_storage.v3 import volume as _volume
2122

2223
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
2324
from openstackclient.tests.unit import fakes
@@ -32,6 +33,8 @@ def __init__(self, **kwargs):
3233
self.management_url = kwargs['endpoint']
3334
self.api_version = api_versions.APIVersion('3.0')
3435

36+
self.availability_zones = mock.Mock()
37+
self.availability_zones.resource_class = fakes.FakeResource(None, {})
3538
self.attachments = mock.Mock()
3639
self.attachments.resource_class = fakes.FakeResource(None, {})
3740
self.clusters = mock.Mock()
@@ -44,10 +47,16 @@ def __init__(self, **kwargs):
4447
self.group_types.resource_class = fakes.FakeResource(None, {})
4548
self.messages = mock.Mock()
4649
self.messages.resource_class = fakes.FakeResource(None, {})
50+
self.quota_classes = mock.Mock()
51+
self.quota_classes.resource_class = fakes.FakeResource(None, {})
52+
self.quotas = mock.Mock()
53+
self.quotas.resource_class = fakes.FakeResource(None, {})
4754
self.resource_filters = mock.Mock()
4855
self.resource_filters.resource_class = fakes.FakeResource(None, {})
4956
self.volumes = mock.Mock()
5057
self.volumes.resource_class = fakes.FakeResource(None, {})
58+
self.volume_snapshots = mock.Mock()
59+
self.volume_snapshots.resource_class = fakes.FakeResource(None, {})
5160
self.volume_types = mock.Mock()
5261
self.volume_types.resource_class = fakes.FakeResource(None, {})
5362
self.services = mock.Mock()
@@ -56,7 +65,7 @@ def __init__(self, **kwargs):
5665
self.workers.resource_class = fakes.FakeResource(None, {})
5766

5867

59-
class TestVolume(utils.TestCommand):
68+
class FakeClientMixin:
6069
def setUp(self):
6170
super().setUp()
6271

@@ -72,6 +81,11 @@ def setUp(self):
7281
)
7382
self.volume_sdk_client = self.app.client_manager.sdk_connection.volume
7483

84+
85+
class TestVolume(FakeClientMixin, utils.TestCommand):
86+
def setUp(self):
87+
super().setUp()
88+
7589
self.app.client_manager.identity = identity_fakes.FakeIdentityv3Client(
7690
endpoint=fakes.AUTH_URL, token=fakes.AUTH_TOKEN
7791
)
@@ -82,6 +96,7 @@ def setUp(self):
8296

8397

8498
# TODO(stephenfin): Check if the responses are actually the same
99+
create_one_snapshot = volume_v2_fakes.create_one_snapshot
85100
create_one_volume = volume_v2_fakes.create_one_volume
86101
create_one_volume_type = volume_v2_fakes.create_one_volume_type
87102

@@ -243,6 +258,62 @@ def create_resource_filters(attrs=None, count=2):
243258
return resource_filters
244259

245260

261+
def create_one_sdk_volume(attrs=None):
262+
"""Create a fake volume.
263+
264+
:param dict attrs:
265+
A dictionary with all attributes of volume
266+
:return:
267+
A FakeResource object with id, name, status, etc.
268+
"""
269+
attrs = attrs or {}
270+
271+
# Set default attribute
272+
volume_info = {
273+
'id': 'volume-id' + uuid.uuid4().hex,
274+
'name': 'volume-name' + uuid.uuid4().hex,
275+
'description': 'description' + uuid.uuid4().hex,
276+
'status': random.choice(['available', 'in_use']),
277+
'size': random.randint(1, 20),
278+
'volume_type': random.choice(['fake_lvmdriver-1', 'fake_lvmdriver-2']),
279+
'bootable': random.choice(['true', 'false']),
280+
'metadata': {
281+
'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex,
282+
'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex,
283+
'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex,
284+
},
285+
'snapshot_id': random.randint(1, 5),
286+
'availability_zone': 'zone' + uuid.uuid4().hex,
287+
'attachments': [
288+
{
289+
'device': '/dev/' + uuid.uuid4().hex,
290+
'server_id': uuid.uuid4().hex,
291+
},
292+
],
293+
}
294+
295+
# Overwrite default attributes if there are some attributes set
296+
volume_info.update(attrs)
297+
return _volume.Volume(**volume_info)
298+
299+
300+
def create_sdk_volumes(attrs=None, count=2):
301+
"""Create multiple fake volumes.
302+
303+
:param dict attrs:
304+
A dictionary with all attributes of volume
305+
:param Integer count:
306+
The number of volumes to be faked
307+
:return:
308+
A list of FakeResource objects
309+
"""
310+
volumes = []
311+
for n in range(0, count):
312+
volumes.append(create_one_sdk_volume(attrs))
313+
314+
return volumes
315+
316+
246317
def create_one_volume_group(attrs=None):
247318
"""Create a fake group.
248319

0 commit comments

Comments
 (0)