Skip to content

Commit e128370

Browse files
committed
Fix wrong attribute name and add functional test for --snapshot
Change-Id: I91f2091ef06a55bcf5373d1beeea2dd81e9f1334 Closes-Bug: #1567895
1 parent 52a12e7 commit e128370

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

functional/tests/volume/v2/test_volume.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# under the License.
1212

1313
import os
14+
import time
1415
import uuid
1516

1617
from functional.common import test
@@ -20,6 +21,8 @@ class VolumeTests(test.TestCase):
2021
"""Functional tests for volume. """
2122

2223
NAME = uuid.uuid4().hex
24+
SNAPSHOT_NAME = uuid.uuid4().hex
25+
VOLUME_FROM_SNAPSHOT_NAME = uuid.uuid4().hex
2326
OTHER_NAME = uuid.uuid4().hex
2427
HEADERS = ['"Display Name"']
2528
FIELDS = ['name']
@@ -28,17 +31,20 @@ class VolumeTests(test.TestCase):
2831
def setUpClass(cls):
2932
os.environ['OS_VOLUME_API_VERSION'] = '2'
3033
opts = cls.get_show_opts(cls.FIELDS)
34+
35+
# Create test volume
3136
raw_output = cls.openstack('volume create --size 1 ' + cls.NAME + opts)
3237
expected = cls.NAME + '\n'
3338
cls.assertOutput(expected, raw_output)
3439

3540
@classmethod
3641
def tearDownClass(cls):
37-
# Rename test
42+
# Rename test volume
3843
raw_output = cls.openstack(
3944
'volume set --name ' + cls.OTHER_NAME + ' ' + cls.NAME)
4045
cls.assertOutput('', raw_output)
41-
# Delete test
46+
47+
# Delete test volume
4248
raw_output = cls.openstack('volume delete ' + cls.OTHER_NAME)
4349
cls.assertOutput('', raw_output)
4450

@@ -78,3 +84,47 @@ def test_volume_set_size(self):
7884
opts = self.get_show_opts(["name", "size"])
7985
raw_output = self.openstack('volume show ' + self.NAME + opts)
8086
self.assertEqual(self.NAME + "\n2\n", raw_output)
87+
88+
def test_volume_snapshot(self):
89+
opts = self.get_show_opts(self.FIELDS)
90+
91+
# Create snapshot from test volume
92+
raw_output = self.openstack('snapshot create ' + self.NAME +
93+
' --name ' + self.SNAPSHOT_NAME + opts)
94+
expected = self.SNAPSHOT_NAME + '\n'
95+
self.assertOutput(expected, raw_output)
96+
self.wait_for("snapshot", self.SNAPSHOT_NAME, "available")
97+
98+
# Create volume from snapshot
99+
raw_output = self.openstack('volume create --size 2 --snapshot ' +
100+
self.SNAPSHOT_NAME + ' ' +
101+
self.VOLUME_FROM_SNAPSHOT_NAME + opts)
102+
expected = self.VOLUME_FROM_SNAPSHOT_NAME + '\n'
103+
self.assertOutput(expected, raw_output)
104+
self.wait_for("volume", self.VOLUME_FROM_SNAPSHOT_NAME, "available")
105+
106+
# Delete volume that create from snapshot
107+
raw_output = self.openstack('volume delete ' +
108+
self.VOLUME_FROM_SNAPSHOT_NAME)
109+
self.assertOutput('', raw_output)
110+
111+
# Delete test snapshot
112+
raw_output = self.openstack('snapshot delete ' + self.SNAPSHOT_NAME)
113+
self.assertOutput('', raw_output)
114+
115+
def wait_for(self, check_type, check_name, desired_status, wait=120,
116+
interval=5, failures=['ERROR']):
117+
status = "notset"
118+
total_sleep = 0
119+
opts = self.get_show_opts(['status'])
120+
while total_sleep < wait:
121+
status = self.openstack(check_type + ' show ' + check_name + opts)
122+
status = status.rstrip()
123+
print('Checking {} {} Waiting for {} current status: {}'
124+
.format(check_type, check_name, desired_status, status))
125+
if status == desired_status:
126+
break
127+
self.assertNotIn(status, failures)
128+
time.sleep(interval)
129+
total_sleep += interval
130+
self.assertEqual(desired_status, status)

openstackclient/tests/volume/v2/test_volume.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import copy
1616

17+
import mock
1718
from mock import call
1819

1920
from openstackclient.common import utils
@@ -40,6 +41,9 @@ def setUp(self):
4041
self.images_mock = self.app.client_manager.image.images
4142
self.images_mock.reset_mock()
4243

44+
self.snapshots_mock = self.app.client_manager.volume.volume_snapshots
45+
self.snapshots_mock.reset_mock()
46+
4347
def setup_volumes_mock(self, count):
4448
volumes = volume_fakes.FakeVolume.create_volumes(count=count)
4549

@@ -376,6 +380,45 @@ def test_volume_create_image_name(self):
376380
self.assertEqual(self.columns, columns)
377381
self.assertEqual(self.datalist, data)
378382

383+
def test_volume_create_with_snapshot(self):
384+
arglist = [
385+
'--size', str(self.new_volume.size),
386+
'--snapshot', volume_fakes.snapshot_id,
387+
self.new_volume.name,
388+
]
389+
verifylist = [
390+
('size', self.new_volume.size),
391+
('snapshot', volume_fakes.snapshot_id),
392+
('name', self.new_volume.name),
393+
]
394+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
395+
396+
fake_snapshot = mock.Mock()
397+
fake_snapshot.id = volume_fakes.snapshot_id
398+
self.snapshots_mock.get.return_value = fake_snapshot
399+
400+
# In base command class ShowOne in cliff, abstract method take_action()
401+
# returns a two-part tuple with a tuple of column names and a tuple of
402+
# data to be shown.
403+
columns, data = self.cmd.take_action(parsed_args)
404+
405+
self.volumes_mock.create.assert_called_once_with(
406+
size=self.new_volume.size,
407+
snapshot_id=fake_snapshot.id,
408+
name=self.new_volume.name,
409+
description=None,
410+
volume_type=None,
411+
user_id=None,
412+
project_id=None,
413+
availability_zone=None,
414+
metadata=None,
415+
imageRef=None,
416+
source_volid=None
417+
)
418+
419+
self.assertEqual(self.columns, columns)
420+
self.assertEqual(self.datalist, data)
421+
379422

380423
class TestVolumeDelete(TestVolume):
381424

0 commit comments

Comments
 (0)