Skip to content

Commit e0020ae

Browse files
committed
volume: Migrate 'snapshot create' to SDK
Change-Id: I0c2811b8518c41658803a7b2053f0f5d5114ed67 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent fc42f12 commit e0020ae

File tree

4 files changed

+221
-131
lines changed

4 files changed

+221
-131
lines changed

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

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from unittest import mock
1515

1616
from openstack.block_storage.v2 import snapshot as _snapshot
17+
from openstack.block_storage.v3 import volume as _volume
1718
from openstack import exceptions as sdk_exceptions
1819
from openstack.test import fakes as sdk_fakes
1920
from osc_lib.cli import format_columns
@@ -37,7 +38,7 @@ def setUp(self):
3738
self.project_mock.reset_mock()
3839

3940

40-
class TestVolumeSnapshotCreate(TestVolumeSnapshot):
41+
class TestVolumeSnapshotCreate(volume_fakes.TestVolume):
4142
columns = (
4243
'created_at',
4344
'description',
@@ -52,69 +53,71 @@ class TestVolumeSnapshotCreate(TestVolumeSnapshot):
5253
def setUp(self):
5354
super().setUp()
5455

55-
self.volume = volume_fakes.create_one_volume()
56-
self.new_snapshot = volume_fakes.create_one_snapshot(
57-
attrs={'volume_id': self.volume.id}
56+
self.volume = sdk_fakes.generate_fake_resource(_volume.Volume)
57+
self.volume_sdk_client.find_volume.return_value = self.volume
58+
self.snapshot = sdk_fakes.generate_fake_resource(
59+
_snapshot.Snapshot, volume_id=self.volume.id
5860
)
61+
self.volume_sdk_client.create_snapshot.return_value = self.snapshot
62+
self.volume_sdk_client.manage_snapshot.return_value = self.snapshot
5963

6064
self.data = (
61-
self.new_snapshot.created_at,
62-
self.new_snapshot.description,
63-
self.new_snapshot.id,
64-
self.new_snapshot.name,
65-
format_columns.DictColumn(self.new_snapshot.metadata),
66-
self.new_snapshot.size,
67-
self.new_snapshot.status,
68-
self.new_snapshot.volume_id,
65+
self.snapshot.created_at,
66+
self.snapshot.description,
67+
self.snapshot.id,
68+
self.snapshot.name,
69+
format_columns.DictColumn(self.snapshot.metadata),
70+
self.snapshot.size,
71+
self.snapshot.status,
72+
self.snapshot.volume_id,
6973
)
7074

71-
self.volumes_mock.get.return_value = self.volume
72-
self.snapshots_mock.create.return_value = self.new_snapshot
73-
self.snapshots_mock.manage.return_value = self.new_snapshot
74-
# Get the command object to test
7575
self.cmd = volume_snapshot.CreateVolumeSnapshot(self.app, None)
7676

7777
def test_snapshot_create(self):
7878
arglist = [
7979
"--volume",
80-
self.new_snapshot.volume_id,
80+
self.snapshot.volume_id,
8181
"--description",
82-
self.new_snapshot.description,
82+
self.snapshot.description,
8383
"--force",
8484
'--property',
8585
'Alpha=a',
8686
'--property',
8787
'Beta=b',
88-
self.new_snapshot.name,
88+
self.snapshot.name,
8989
]
9090
verifylist = [
91-
("volume", self.new_snapshot.volume_id),
92-
("description", self.new_snapshot.description),
91+
("volume", self.snapshot.volume_id),
92+
("description", self.snapshot.description),
9393
("force", True),
94-
('property', {'Alpha': 'a', 'Beta': 'b'}),
95-
("snapshot_name", self.new_snapshot.name),
94+
('properties', {'Alpha': 'a', 'Beta': 'b'}),
95+
("snapshot_name", self.snapshot.name),
9696
]
9797
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
9898

9999
columns, data = self.cmd.take_action(parsed_args)
100100

101-
self.snapshots_mock.create.assert_called_with(
102-
self.new_snapshot.volume_id,
101+
self.assertEqual(self.columns, columns)
102+
self.assertEqual(self.data, data)
103+
self.volume_sdk_client.find_volume.assert_called_once_with(
104+
self.snapshot.volume_id, ignore_missing=False
105+
)
106+
self.volume_sdk_client.create_snapshot.assert_called_with(
107+
volume_id=self.snapshot.volume_id,
103108
force=True,
104-
name=self.new_snapshot.name,
105-
description=self.new_snapshot.description,
109+
name=self.snapshot.name,
110+
description=self.snapshot.description,
106111
metadata={'Alpha': 'a', 'Beta': 'b'},
107112
)
108-
self.assertEqual(self.columns, columns)
109-
self.assertEqual(self.data, data)
110113

111114
def test_snapshot_create_without_name(self):
112115
arglist = [
113116
"--volume",
114-
self.new_snapshot.volume_id,
117+
self.snapshot.volume_id,
115118
]
116119
verifylist = [
117-
("volume", self.new_snapshot.volume_id),
120+
("volume", self.snapshot.volume_id),
118121
]
119122
self.assertRaises(
120123
test_utils.ParserException,
@@ -127,29 +130,31 @@ def test_snapshot_create_without_name(self):
127130
def test_snapshot_create_without_volume(self):
128131
arglist = [
129132
"--description",
130-
self.new_snapshot.description,
133+
self.snapshot.description,
131134
"--force",
132-
self.new_snapshot.name,
135+
self.snapshot.name,
133136
]
134137
verifylist = [
135-
("description", self.new_snapshot.description),
138+
("description", self.snapshot.description),
136139
("force", True),
137-
("snapshot_name", self.new_snapshot.name),
140+
("snapshot_name", self.snapshot.name),
138141
]
139142
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
140143

141144
columns, data = self.cmd.take_action(parsed_args)
142145

143-
self.volumes_mock.get.assert_called_once_with(self.new_snapshot.name)
144-
self.snapshots_mock.create.assert_called_once_with(
145-
self.new_snapshot.volume_id,
146+
self.assertEqual(self.columns, columns)
147+
self.assertEqual(self.data, data)
148+
self.volume_sdk_client.find_volume.assert_called_once_with(
149+
self.snapshot.name, ignore_missing=False
150+
)
151+
self.volume_sdk_client.create_snapshot.assert_called_once_with(
152+
volume_id=self.snapshot.volume_id,
146153
force=True,
147-
name=self.new_snapshot.name,
148-
description=self.new_snapshot.description,
154+
name=self.snapshot.name,
155+
description=self.snapshot.description,
149156
metadata=None,
150157
)
151-
self.assertEqual(self.columns, columns)
152-
self.assertEqual(self.data, data)
153158

154159
def test_snapshot_create_with_remote_source(self):
155160
arglist = [
@@ -158,32 +163,35 @@ def test_snapshot_create_with_remote_source(self):
158163
'--remote-source',
159164
'source-id=test_source_id',
160165
'--volume',
161-
self.new_snapshot.volume_id,
162-
self.new_snapshot.name,
166+
self.snapshot.volume_id,
167+
self.snapshot.name,
163168
]
164169
ref_dict = {
165170
'source-name': 'test_source_name',
166171
'source-id': 'test_source_id',
167172
}
168173
verifylist = [
169174
('remote_source', ref_dict),
170-
('volume', self.new_snapshot.volume_id),
171-
("snapshot_name", self.new_snapshot.name),
175+
('volume', self.snapshot.volume_id),
176+
("snapshot_name", self.snapshot.name),
172177
]
173178
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
174179

175180
columns, data = self.cmd.take_action(parsed_args)
176181

177-
self.snapshots_mock.manage.assert_called_with(
178-
volume_id=self.new_snapshot.volume_id,
182+
self.assertEqual(self.columns, columns)
183+
self.assertEqual(self.data, data)
184+
self.volume_sdk_client.find_volume.assert_called_once_with(
185+
self.snapshot.volume_id, ignore_missing=False
186+
)
187+
self.volume_sdk_client.manage_snapshot.assert_called_with(
188+
volume_id=self.snapshot.volume_id,
179189
ref=ref_dict,
180-
name=self.new_snapshot.name,
190+
name=self.snapshot.name,
181191
description=None,
182192
metadata=None,
183193
)
184-
self.snapshots_mock.create.assert_not_called()
185-
self.assertEqual(self.columns, columns)
186-
self.assertEqual(self.data, data)
194+
self.volume_sdk_client.create_snapshot.assert_not_called()
187195

188196

189197
class TestVolumeSnapshotDelete(volume_fakes.TestVolume):

0 commit comments

Comments
 (0)