|
14 | 14 |
|
15 | 15 | import copy |
16 | 16 | import mock |
| 17 | +import random |
| 18 | +import uuid |
17 | 19 |
|
18 | 20 | from openstackclient.tests import fakes |
19 | 21 | from openstackclient.tests.identity.v3 import fakes as identity_fakes |
@@ -220,3 +222,85 @@ def setUp(self): |
220 | 222 | endpoint=fakes.AUTH_URL, |
221 | 223 | token=fakes.AUTH_TOKEN |
222 | 224 | ) |
| 225 | + |
| 226 | + |
| 227 | +class FakeVolume(object): |
| 228 | + """Fake one or more volumes. |
| 229 | +
|
| 230 | + TODO(xiexs): Currently, only volume API v2 is supported by this class. |
| 231 | + """ |
| 232 | + |
| 233 | + @staticmethod |
| 234 | + def create_one_volume(attrs={}): |
| 235 | + """Create a fake volume. |
| 236 | +
|
| 237 | + :param Dictionary attrs: |
| 238 | + A dictionary with all attributes of volume |
| 239 | + :retrun: |
| 240 | + A FakeResource object with id, name, status, etc. |
| 241 | + """ |
| 242 | + # Set default attribute |
| 243 | + volume_info = { |
| 244 | + 'id': 'volume-id' + uuid.uuid4().hex, |
| 245 | + 'name': 'volume-name' + uuid.uuid4().hex, |
| 246 | + 'description': 'description' + uuid.uuid4().hex, |
| 247 | + 'status': random.choice(['available', 'in_use']), |
| 248 | + 'size': random.randint(1, 20), |
| 249 | + 'volume_type': |
| 250 | + random.choice(['fake_lvmdriver-1', 'fake_lvmdriver-2']), |
| 251 | + 'metadata': { |
| 252 | + 'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex, |
| 253 | + 'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex, |
| 254 | + 'key' + uuid.uuid4().hex: 'val' + uuid.uuid4().hex}, |
| 255 | + 'snapshot_id': random.randint(1, 5), |
| 256 | + 'availability_zone': 'zone' + uuid.uuid4().hex, |
| 257 | + 'attachments': { |
| 258 | + 'device': '/dev/' + uuid.uuid4().hex, |
| 259 | + 'server_id': uuid.uuid4().hex}, |
| 260 | + } |
| 261 | + |
| 262 | + # Overwrite default attributes if there are some attributes set |
| 263 | + volume_info.update(attrs) |
| 264 | + |
| 265 | + volume = fakes.FakeResource( |
| 266 | + None, |
| 267 | + volume_info, |
| 268 | + loaded=True) |
| 269 | + return volume |
| 270 | + |
| 271 | + @staticmethod |
| 272 | + def create_volumes(attrs={}, count=2): |
| 273 | + """Create multiple fake volumes. |
| 274 | +
|
| 275 | + :param Dictionary attrs: |
| 276 | + A dictionary with all attributes of volume |
| 277 | + :param Integer count: |
| 278 | + The number of volumes to be faked |
| 279 | + :return: |
| 280 | + A list of FakeResource objects |
| 281 | + """ |
| 282 | + volumes = [] |
| 283 | + for n in range(0, count): |
| 284 | + volumes.append(FakeVolume.create_one_volume(attrs)) |
| 285 | + |
| 286 | + return volumes |
| 287 | + |
| 288 | + @staticmethod |
| 289 | + def get_volumes(volumes=None, count=2): |
| 290 | + """Get an iterable MagicMock object with a list of faked volumes. |
| 291 | +
|
| 292 | + If volumes list is provided, then initialize the Mock object with the |
| 293 | + list. Otherwise create one. |
| 294 | +
|
| 295 | + :param List volumes: |
| 296 | + A list of FakeResource objects faking volumes |
| 297 | + :param Integer count: |
| 298 | + The number of volumes to be faked |
| 299 | + :return |
| 300 | + An iterable Mock object with side_effect set to a list of faked |
| 301 | + volumes |
| 302 | + """ |
| 303 | + if volumes is None: |
| 304 | + volumes = FakeVolume.create_volumes(count) |
| 305 | + |
| 306 | + return mock.MagicMock(side_effect=volumes) |
0 commit comments