blob: 7d7bf434df13746650091f802d848122158626ea [file] [log] [blame]
ghanshyamdae4b6d2018-01-12 07:49:15 +00001# Copyright 2017 NEC Corporation.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16from tempest.api.volume import api_microversion_fixture
17from tempest.common import compute
18from tempest.common import waiters
19from tempest import config
20from tempest.lib.common import api_version_utils
21from tempest.lib.common.utils import data_utils
22from tempest.lib.common.utils import test_utils
23from tempest.lib import exceptions
24from tempest import test
25
26CONF = config.CONF
27
28
29class BaseVolumeTest(api_version_utils.BaseMicroversionTest,
30 test.BaseTestCase):
31 """Base test case class for all Cinder API tests."""
32
33 _api_version = 2
34 credentials = ['primary']
35
36 @classmethod
37 def skip_checks(cls):
38 super(BaseVolumeTest, cls).skip_checks()
39
40 if not CONF.service_available.cinder:
41 skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
42 raise cls.skipException(skip_msg)
43 if cls._api_version == 2:
44 if not CONF.volume_feature_enabled.api_v2:
45 msg = "Volume API v2 is disabled"
46 raise cls.skipException(msg)
47 elif cls._api_version == 3:
48 if not CONF.volume_feature_enabled.api_v3:
49 msg = "Volume API v3 is disabled"
50 raise cls.skipException(msg)
51 else:
52 msg = ("Invalid Cinder API version (%s)" % cls._api_version)
53 raise exceptions.InvalidConfiguration(msg)
54
55 api_version_utils.check_skip_with_microversion(
56 cls.min_microversion, cls.max_microversion,
57 CONF.volume.min_microversion, CONF.volume.max_microversion)
58
59 @classmethod
60 def setup_clients(cls):
61 super(BaseVolumeTest, cls).setup_clients()
62 if cls._api_version == 3:
63 cls.backups_client = cls.os_primary.backups_v3_client
64 cls.volumes_client = cls.os_primary.volumes_v3_client
65 else:
66 cls.backups_client = cls.os_primary.backups_v2_client
67 cls.volumes_client = cls.os_primary.volumes_v2_client
68
69 cls.snapshots_client = cls.os_primary.snapshots_v2_client
70
71 @classmethod
72 def setup_credentials(cls):
73 cls.set_network_resources()
74 super(BaseVolumeTest, cls).setup_credentials()
75
76 def setUp(self):
77 super(BaseVolumeTest, self).setUp()
78 self.useFixture(api_microversion_fixture.APIMicroversionFixture(
79 self.request_microversion))
80
81 @classmethod
82 def resource_setup(cls):
83 super(BaseVolumeTest, cls).resource_setup()
84 cls.request_microversion = (
85 api_version_utils.select_request_microversion(
86 cls.min_microversion,
87 CONF.volume.min_microversion))
88
89 @classmethod
90 def create_volume(cls, wait_until='available', **kwargs):
91 """Wrapper utility that returns a test volume.
92
93 :param wait_until: wait till volume status.
94 """
95 if 'size' not in kwargs:
96 kwargs['size'] = CONF.volume.volume_size
97
98 if 'imageRef' in kwargs:
99 image = cls.os_primary.image_client_v2.show_image(
100 kwargs['imageRef'])
101 min_disk = image['min_disk']
102 kwargs['size'] = max(kwargs['size'], min_disk)
103
104 if 'name' not in kwargs:
105 name = data_utils.rand_name(cls.__name__ + '-Volume')
106 kwargs['name'] = name
107
108 volume = cls.volumes_client.create_volume(**kwargs)['volume']
109 cls.addClassResourceCleanup(
110 cls.volumes_client.wait_for_resource_deletion, volume['id'])
111 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
112 cls.volumes_client.delete_volume,
113 volume['id'])
114 waiters.wait_for_volume_resource_status(cls.volumes_client,
115 volume['id'], wait_until)
116 return volume
117
118 @classmethod
119 def create_snapshot(cls, volume_id=1, **kwargs):
120 """Wrapper utility that returns a test snapshot."""
121 if 'name' not in kwargs:
122 name = data_utils.rand_name(cls.__name__ + '-Snapshot')
123 kwargs['name'] = name
124
125 snapshot = cls.snapshots_client.create_snapshot(
126 volume_id=volume_id, **kwargs)['snapshot']
127 cls.addClassResourceCleanup(
128 cls.snapshots_client.wait_for_resource_deletion, snapshot['id'])
129 cls.addClassResourceCleanup(test_utils.call_and_ignore_notfound_exc,
130 cls.snapshots_client.delete_snapshot,
131 snapshot['id'])
132 waiters.wait_for_volume_resource_status(cls.snapshots_client,
133 snapshot['id'], 'available')
134 return snapshot
135
136 def create_backup(self, volume_id, backup_client=None, **kwargs):
137 """Wrapper utility that returns a test backup."""
138 if backup_client is None:
139 backup_client = self.backups_client
140 if 'name' not in kwargs:
141 name = data_utils.rand_name(self.__class__.__name__ + '-Backup')
142 kwargs['name'] = name
143
144 backup = backup_client.create_backup(
145 volume_id=volume_id, **kwargs)['backup']
melanie wittc96757b2018-05-31 00:05:00 +0000146 self.addCleanup(backup_client.wait_for_resource_deletion, backup['id'])
147 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
148 backup_client.delete_backup, backup['id'])
ghanshyamdae4b6d2018-01-12 07:49:15 +0000149 waiters.wait_for_volume_resource_status(backup_client, backup['id'],
150 'available')
151 return backup
152
153 def create_server(self, wait_until='ACTIVE', **kwargs):
154 name = kwargs.pop(
155 'name',
156 data_utils.rand_name(self.__class__.__name__ + '-instance'))
157
158 tenant_network = self.get_tenant_network()
159 body, _ = compute.create_test_server(
160 self.os_primary,
161 tenant_network=tenant_network,
162 name=name,
163 wait_until=wait_until,
164 **kwargs)
165
166 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
167 waiters.wait_for_server_termination,
168 self.os_primary.servers_client, body['id'])
169 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
170 self.os_primary.servers_client.delete_server,
171 body['id'])
172 return body
Miriam Yumifa8791e2018-09-04 14:42:14 -0300173
174
175class BaseVolumeAdminTest(BaseVolumeTest):
176 """Base test case class for all Volume Admin API tests."""
177
178 credentials = ['primary', 'admin']
179
180 @classmethod
181 def setup_clients(cls):
182 super(BaseVolumeAdminTest, cls).setup_clients()
183
184 cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest