|
13 | 13 | # |
14 | 14 |
|
15 | 15 | import copy |
| 16 | +from unittest import mock |
16 | 17 |
|
17 | 18 | from cinderclient import api_versions |
18 | 19 | from osc_lib.cli import format_columns |
19 | 20 | from osc_lib import exceptions |
| 21 | +from osc_lib import utils |
20 | 22 |
|
21 | 23 | from openstackclient.tests.unit.volume.v2 import fakes as volume_fakes |
22 | 24 | from openstackclient.volume.v3 import volume |
@@ -119,3 +121,59 @@ def test_volume_summary_with_metadata(self): |
119 | 121 | self.mock_vol_1.size + self.mock_vol_2.size, |
120 | 122 | format_columns.DictColumn(combine_meta)) |
121 | 123 | self.assertCountEqual(datalist, tuple(data)) |
| 124 | + |
| 125 | + |
| 126 | +class TestVolumeRevertToSnapshot(volume_fakes.TestVolume): |
| 127 | + |
| 128 | + def setUp(self): |
| 129 | + super().setUp() |
| 130 | + |
| 131 | + self.volumes_mock = self.app.client_manager.volume.volumes |
| 132 | + self.volumes_mock.reset_mock() |
| 133 | + self.snapshots_mock = self.app.client_manager.volume.volume_snapshots |
| 134 | + self.snapshots_mock.reset_mock() |
| 135 | + self.mock_volume = volume_fakes.create_one_volume() |
| 136 | + self.mock_snapshot = volume_fakes.create_one_snapshot( |
| 137 | + attrs={'volume_id': self.volumes_mock.id}) |
| 138 | + |
| 139 | + # Get the command object to test |
| 140 | + self.cmd = volume.VolumeRevertToSnapshot(self.app, None) |
| 141 | + |
| 142 | + def test_volume_revert_to_snapshot_pre_340(self): |
| 143 | + arglist = [ |
| 144 | + self.mock_snapshot.id, |
| 145 | + ] |
| 146 | + verifylist = [ |
| 147 | + ('snapshot', self.mock_snapshot.id), |
| 148 | + ] |
| 149 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 150 | + |
| 151 | + exc = self.assertRaises( |
| 152 | + exceptions.CommandError, |
| 153 | + self.cmd.take_action, |
| 154 | + parsed_args) |
| 155 | + self.assertIn( |
| 156 | + '--os-volume-api-version 3.40 or greater is required', |
| 157 | + str(exc)) |
| 158 | + |
| 159 | + def test_volume_revert_to_snapshot(self): |
| 160 | + self.app.client_manager.volume.api_version = \ |
| 161 | + api_versions.APIVersion('3.40') |
| 162 | + arglist = [ |
| 163 | + self.mock_snapshot.id, |
| 164 | + ] |
| 165 | + verifylist = [ |
| 166 | + ('snapshot', self.mock_snapshot.id), |
| 167 | + ] |
| 168 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 169 | + |
| 170 | + find_mock_result = [self.mock_snapshot, self.mock_volume] |
| 171 | + with mock.patch.object(utils, 'find_resource', |
| 172 | + side_effect=find_mock_result) as find_mock: |
| 173 | + self.cmd.take_action(parsed_args) |
| 174 | + |
| 175 | + self.volumes_mock.revert_to_snapshot.assert_called_once_with( |
| 176 | + volume=self.mock_volume, |
| 177 | + snapshot=self.mock_snapshot, |
| 178 | + ) |
| 179 | + self.assertEqual(2, find_mock.call_count) |
0 commit comments