Skip to content

Commit 8eb1a18

Browse files
committed
volume: Migrate 'backup set', 'backup unset' to SDK
Change-Id: Iced346df828faab1ff08a2645ff64f4cfea25cb1 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent b933330 commit 8eb1a18

File tree

4 files changed

+131
-129
lines changed

4 files changed

+131
-129
lines changed

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

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,15 @@
1313

1414
from unittest.mock import call
1515

16+
from openstack.block_storage.v2 import backup as _backup
17+
from openstack import exceptions as sdk_exceptions
18+
from openstack.test import fakes as sdk_fakes
1619
from osc_lib import exceptions
1720

1821
from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes
1922
from openstackclient.volume.v2 import volume_backup
2023

2124

22-
class TestBackupLegacy(volume_fakes.TestVolume):
23-
def setUp(self):
24-
super().setUp()
25-
26-
self.backups_mock = self.volume_client.backups
27-
self.backups_mock.reset_mock()
28-
self.volumes_mock = self.volume_client.volumes
29-
self.volumes_mock.reset_mock()
30-
self.snapshots_mock = self.volume_client.volume_snapshots
31-
self.snapshots_mock.reset_mock()
32-
self.restores_mock = self.volume_client.restores
33-
self.restores_mock.reset_mock()
34-
35-
3625
class TestBackupCreate(volume_fakes.TestVolume):
3726
volume = volume_fakes.create_one_volume()
3827
snapshot = volume_fakes.create_one_snapshot()
@@ -476,17 +465,15 @@ def test_backup_restore_with_volume_existing(self):
476465
)
477466

478467

479-
class TestBackupSet(TestBackupLegacy):
480-
backup = volume_fakes.create_one_backup(
481-
attrs={'metadata': {'wow': 'cool'}},
482-
)
483-
468+
class TestBackupSet(volume_fakes.TestVolume):
484469
def setUp(self):
485470
super().setUp()
486471

487-
self.backups_mock.get.return_value = self.backup
472+
self.backup = sdk_fakes.generate_fake_resource(
473+
_backup.Backup, metadata={'wow': 'cool'}
474+
)
475+
self.volume_sdk_client.find_backup.return_value = self.backup
488476

489-
# Get the command object to test
490477
self.cmd = volume_backup.SetVolumeBackup(self.app, None)
491478

492479
def test_backup_set_state(self):
@@ -496,26 +483,34 @@ def test_backup_set_state(self):
496483
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
497484

498485
result = self.cmd.take_action(parsed_args)
499-
self.backups_mock.reset_state.assert_called_once_with(
500-
self.backup.id, 'error'
501-
)
502486
self.assertIsNone(result)
503487

488+
self.volume_sdk_client.find_backup.assert_called_with(
489+
self.backup.id, ignore_missing=False
490+
)
491+
self.volume_sdk_client.reset_backup_status.assert_called_with(
492+
self.backup, status='error'
493+
)
494+
504495
def test_backup_set_state_failed(self):
505-
self.backups_mock.reset_state.side_effect = exceptions.CommandError()
496+
self.volume_sdk_client.reset_backup_status.side_effect = (
497+
sdk_exceptions.NotFoundException('foo')
498+
)
499+
506500
arglist = ['--state', 'error', self.backup.id]
507501
verifylist = [('state', 'error'), ('backup', self.backup.id)]
508502

509503
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
510-
try:
511-
self.cmd.take_action(parsed_args)
512-
self.fail('CommandError should be raised.')
513-
except exceptions.CommandError as e:
514-
self.assertEqual(
515-
'One or more of the set operations failed', str(e)
516-
)
517-
self.backups_mock.reset_state.assert_called_with(
518-
self.backup.id, 'error'
504+
exc = self.assertRaises(
505+
exceptions.CommandError, self.cmd.take_action, parsed_args
506+
)
507+
self.assertEqual('One or more of the set operations failed', str(exc))
508+
509+
self.volume_sdk_client.find_backup.assert_called_with(
510+
self.backup.id, ignore_missing=False
511+
)
512+
self.volume_sdk_client.reset_backup_status.assert_called_with(
513+
self.backup, status='error'
519514
)
520515

521516

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

Lines changed: 69 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,15 @@
1313

1414
from unittest.mock import call
1515

16+
from openstack.block_storage.v3 import backup as _backup
17+
from openstack import exceptions as sdk_exceptions
18+
from openstack.test import fakes as sdk_fakes
1619
from osc_lib import exceptions
1720

1821
from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes
1922
from openstackclient.volume.v3 import volume_backup
2023

2124

22-
class TestBackupLegacy(volume_fakes.TestVolume):
23-
def setUp(self):
24-
super().setUp()
25-
26-
self.backups_mock = self.volume_client.backups
27-
self.backups_mock.reset_mock()
28-
self.volumes_mock = self.volume_client.volumes
29-
self.volumes_mock.reset_mock()
30-
self.snapshots_mock = self.volume_client.volume_snapshots
31-
self.snapshots_mock.reset_mock()
32-
self.restores_mock = self.volume_client.restores
33-
self.restores_mock.reset_mock()
34-
35-
3625
class TestBackupCreate(volume_fakes.TestVolume):
3726
volume = volume_fakes.create_one_volume()
3827
snapshot = volume_fakes.create_one_snapshot()
@@ -574,17 +563,15 @@ def test_backup_restore_with_volume_existing(self):
574563
)
575564

576565

577-
class TestBackupSet(TestBackupLegacy):
578-
backup = volume_fakes.create_one_backup(
579-
attrs={'metadata': {'wow': 'cool'}},
580-
)
581-
566+
class TestBackupSet(volume_fakes.TestVolume):
582567
def setUp(self):
583568
super().setUp()
584569

585-
self.backups_mock.get.return_value = self.backup
570+
self.backup = sdk_fakes.generate_fake_resource(
571+
_backup.Backup, metadata={'wow': 'cool'}
572+
)
573+
self.volume_sdk_client.find_backup.return_value = self.backup
586574

587-
# Get the command object to test
588575
self.cmd = volume_backup.SetVolumeBackup(self.app, None)
589576

590577
def test_backup_set_name(self):
@@ -601,14 +588,16 @@ def test_backup_set_name(self):
601588
]
602589
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
603590

604-
# In base command class ShowOne in cliff, abstract method take_action()
605-
# returns nothing
606591
result = self.cmd.take_action(parsed_args)
607-
self.backups_mock.update.assert_called_once_with(
608-
self.backup.id, **{'name': 'new_name'}
609-
)
610592
self.assertIsNone(result)
611593

594+
self.volume_sdk_client.find_backup.assert_called_with(
595+
self.backup.id, ignore_missing=False
596+
)
597+
self.volume_sdk_client.update_backup.assert_called_once_with(
598+
self.backup, name='new_name'
599+
)
600+
612601
def test_backup_set_name_pre_v39(self):
613602
self.set_volume_api_version('3.8')
614603

@@ -644,13 +633,14 @@ def test_backup_set_description(self):
644633
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
645634

646635
result = self.cmd.take_action(parsed_args)
636+
self.assertIsNone(result)
647637

648-
# Set expected values
649-
kwargs = {'description': 'new_description'}
650-
self.backups_mock.update.assert_called_once_with(
651-
self.backup.id, **kwargs
638+
self.volume_sdk_client.find_backup.assert_called_with(
639+
self.backup.id, ignore_missing=False
640+
)
641+
self.volume_sdk_client.update_backup.assert_called_once_with(
642+
self.backup, description='new_description'
652643
)
653-
self.assertIsNone(result)
654644

655645
def test_backup_set_description_pre_v39(self):
656646
self.set_volume_api_version('3.8')
@@ -679,26 +669,34 @@ def test_backup_set_state(self):
679669
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
680670

681671
result = self.cmd.take_action(parsed_args)
682-
self.backups_mock.reset_state.assert_called_once_with(
683-
self.backup.id, 'error'
684-
)
685672
self.assertIsNone(result)
686673

674+
self.volume_sdk_client.find_backup.assert_called_with(
675+
self.backup.id, ignore_missing=False
676+
)
677+
self.volume_sdk_client.reset_backup_status.assert_called_with(
678+
self.backup, status='error'
679+
)
680+
687681
def test_backup_set_state_failed(self):
688-
self.backups_mock.reset_state.side_effect = exceptions.CommandError()
682+
self.volume_sdk_client.reset_backup_status.side_effect = (
683+
sdk_exceptions.NotFoundException('foo')
684+
)
685+
689686
arglist = ['--state', 'error', self.backup.id]
690687
verifylist = [('state', 'error'), ('backup', self.backup.id)]
691688

692689
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
693-
try:
694-
self.cmd.take_action(parsed_args)
695-
self.fail('CommandError should be raised.')
696-
except exceptions.CommandError as e:
697-
self.assertEqual(
698-
'One or more of the set operations failed', str(e)
699-
)
700-
self.backups_mock.reset_state.assert_called_with(
701-
self.backup.id, 'error'
690+
exc = self.assertRaises(
691+
exceptions.CommandError, self.cmd.take_action, parsed_args
692+
)
693+
self.assertEqual('One or more of the set operations failed', str(exc))
694+
695+
self.volume_sdk_client.find_backup.assert_called_with(
696+
self.backup.id, ignore_missing=False
697+
)
698+
self.volume_sdk_client.reset_backup_status.assert_called_with(
699+
self.backup, status='error'
702700
)
703701

704702
def test_backup_set_no_property(self):
@@ -715,15 +713,14 @@ def test_backup_set_no_property(self):
715713
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
716714

717715
result = self.cmd.take_action(parsed_args)
716+
self.assertIsNone(result)
718717

719-
# Set expected values
720-
kwargs = {
721-
'metadata': {},
722-
}
723-
self.backups_mock.update.assert_called_once_with(
724-
self.backup.id, **kwargs
718+
self.volume_sdk_client.find_backup.assert_called_with(
719+
self.backup.id, ignore_missing=False
720+
)
721+
self.volume_sdk_client.update_backup.assert_called_once_with(
722+
self.backup, metadata={}
725723
)
726-
self.assertIsNone(result)
727724

728725
def test_backup_set_no_property_pre_v343(self):
729726
self.set_volume_api_version('3.42')
@@ -758,15 +755,14 @@ def test_backup_set_property(self):
758755
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
759756

760757
result = self.cmd.take_action(parsed_args)
758+
self.assertIsNone(result)
761759

762-
# Set expected values
763-
kwargs = {
764-
'metadata': {'wow': 'cool', 'foo': 'bar'},
765-
}
766-
self.backups_mock.update.assert_called_once_with(
767-
self.backup.id, **kwargs
760+
self.volume_sdk_client.find_backup.assert_called_with(
761+
self.backup.id, ignore_missing=False
762+
)
763+
self.volume_sdk_client.update_backup.assert_called_once_with(
764+
self.backup, metadata={'wow': 'cool', 'foo': 'bar'}
768765
)
769-
self.assertIsNone(result)
770766

771767
def test_backup_set_property_pre_v343(self):
772768
self.set_volume_api_version('3.42')
@@ -788,17 +784,16 @@ def test_backup_set_property_pre_v343(self):
788784
self.assertIn("--os-volume-api-version 3.43 or greater", str(exc))
789785

790786

791-
class TestBackupUnset(TestBackupLegacy):
792-
backup = volume_fakes.create_one_backup(
793-
attrs={'metadata': {'foo': 'bar'}},
794-
)
795-
787+
class TestBackupUnset(volume_fakes.TestVolume):
796788
def setUp(self):
797789
super().setUp()
798790

799-
self.backups_mock.get.return_value = self.backup
791+
self.backup = sdk_fakes.generate_fake_resource(
792+
_backup.Backup, metadata={'foo': 'bar', 'wow': 'cool'}
793+
)
794+
self.volume_sdk_client.find_backup.return_value = self.backup
795+
self.volume_sdk_client.delete_backup_metadata.return_value = None
800796

801-
# Get the command object to test
802797
self.cmd = volume_backup.UnsetVolumeBackup(self.app, None)
803798

804799
def test_backup_unset_property(self):
@@ -816,15 +811,14 @@ def test_backup_unset_property(self):
816811
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
817812

818813
result = self.cmd.take_action(parsed_args)
814+
self.assertIsNone(result)
819815

820-
# Set expected values
821-
kwargs = {
822-
'metadata': {},
823-
}
824-
self.backups_mock.update.assert_called_once_with(
825-
self.backup.id, **kwargs
816+
self.volume_sdk_client.find_backup.assert_called_with(
817+
self.backup.id, ignore_missing=False
818+
)
819+
self.volume_sdk_client.delete_backup_metadata.assert_called_once_with(
820+
self.backup, keys=['wow']
826821
)
827-
self.assertIsNone(result)
828822

829823
def test_backup_unset_property_pre_v343(self):
830824
self.set_volume_api_version('3.42')
@@ -907,7 +901,9 @@ def test_backup_show(self):
907901
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
908902

909903
columns, data = self.cmd.take_action(parsed_args)
910-
self.volume_sdk_client.find_backup.assert_called_with(self.backup.id)
904+
self.volume_sdk_client.find_backup.assert_called_with(
905+
self.backup.id, ignore_missing=False
906+
)
911907

912908
self.assertEqual(self.columns, columns)
913909
self.assertEqual(self.data, data)

openstackclient/volume/v2/volume_backup.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,19 @@ def get_parser(self, prog_name):
417417
return parser
418418

419419
def take_action(self, parsed_args):
420-
volume_client = self.app.client_manager.volume
421-
backup = utils.find_resource(volume_client.backups, parsed_args.backup)
420+
volume_client = self.app.client_manager.sdk_connection.volume
421+
422+
backup = volume_client.find_backup(
423+
parsed_args.backup,
424+
ignore_missing=False,
425+
)
422426

423427
result = 0
424428
if parsed_args.state:
425429
try:
426-
volume_client.backups.reset_state(backup.id, parsed_args.state)
430+
volume_client.reset_backup_status(
431+
backup, status=parsed_args.state
432+
)
427433
except Exception as e:
428434
LOG.error(_("Failed to set backup state: %s"), e)
429435
result += 1

0 commit comments

Comments
 (0)