|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | +"""Volume v2 API Library Tests""" |
| 15 | + |
| 16 | +import http |
| 17 | +from unittest import mock |
| 18 | +import uuid |
| 19 | + |
| 20 | +from openstack.block_storage.v2 import _proxy |
| 21 | +from osc_lib import exceptions as osc_lib_exceptions |
| 22 | + |
| 23 | +from openstackclient.api import volume_v2 as volume |
| 24 | +from openstackclient.tests.unit import fakes |
| 25 | +from openstackclient.tests.unit import utils |
| 26 | + |
| 27 | + |
| 28 | +class TestConsistencyGroup(utils.TestCase): |
| 29 | + def setUp(self): |
| 30 | + super().setUp() |
| 31 | + |
| 32 | + self.volume_sdk_client = mock.Mock(_proxy.Proxy) |
| 33 | + |
| 34 | + def test_find_consistency_group_by_id(self): |
| 35 | + cg_id = uuid.uuid4().hex |
| 36 | + cg_name = 'name-' + uuid.uuid4().hex |
| 37 | + data = { |
| 38 | + 'consistencygroup': { |
| 39 | + 'id': cg_id, |
| 40 | + 'name': cg_name, |
| 41 | + 'status': 'available', |
| 42 | + 'availability_zone': 'az1', |
| 43 | + 'created_at': '2015-09-16T09:28:52.000000', |
| 44 | + 'description': 'description-' + uuid.uuid4().hex, |
| 45 | + 'volume_types': ['123456'], |
| 46 | + } |
| 47 | + } |
| 48 | + self.volume_sdk_client.get.side_effect = [ |
| 49 | + fakes.FakeResponse(data=data), |
| 50 | + ] |
| 51 | + |
| 52 | + result = volume.find_consistency_group(self.volume_sdk_client, cg_id) |
| 53 | + |
| 54 | + self.volume_sdk_client.get.assert_has_calls( |
| 55 | + [ |
| 56 | + mock.call(f'/consistencygroups/{cg_id}'), |
| 57 | + ] |
| 58 | + ) |
| 59 | + self.assertEqual(data['consistencygroup'], result) |
| 60 | + |
| 61 | + def test_find_consistency_group_by_name(self): |
| 62 | + cg_id = uuid.uuid4().hex |
| 63 | + cg_name = 'name-' + uuid.uuid4().hex |
| 64 | + data = { |
| 65 | + 'consistencygroups': [ |
| 66 | + { |
| 67 | + 'id': cg_id, |
| 68 | + 'name': cg_name, |
| 69 | + } |
| 70 | + ], |
| 71 | + } |
| 72 | + self.volume_sdk_client.get.side_effect = [ |
| 73 | + fakes.FakeResponse(status_code=http.HTTPStatus.NOT_FOUND), |
| 74 | + fakes.FakeResponse(data=data), |
| 75 | + ] |
| 76 | + |
| 77 | + result = volume.find_consistency_group(self.volume_sdk_client, cg_name) |
| 78 | + |
| 79 | + self.volume_sdk_client.get.assert_has_calls( |
| 80 | + [ |
| 81 | + mock.call(f'/consistencygroups/{cg_name}'), |
| 82 | + mock.call('/consistencygroups'), |
| 83 | + ] |
| 84 | + ) |
| 85 | + self.assertEqual(data['consistencygroups'][0], result) |
| 86 | + |
| 87 | + def test_find_consistency_group_not_found(self): |
| 88 | + data = {'consistencygroups': []} |
| 89 | + self.volume_sdk_client.get.side_effect = [ |
| 90 | + fakes.FakeResponse(status_code=http.HTTPStatus.NOT_FOUND), |
| 91 | + fakes.FakeResponse(data=data), |
| 92 | + ] |
| 93 | + self.assertRaises( |
| 94 | + osc_lib_exceptions.NotFound, |
| 95 | + volume.find_consistency_group, |
| 96 | + self.volume_sdk_client, |
| 97 | + 'invalid-cg', |
| 98 | + ) |
| 99 | + |
| 100 | + def test_find_consistency_group_by_name_duplicate(self): |
| 101 | + cg_name = 'name-' + uuid.uuid4().hex |
| 102 | + data = { |
| 103 | + 'consistencygroups': [ |
| 104 | + { |
| 105 | + 'id': uuid.uuid4().hex, |
| 106 | + 'name': cg_name, |
| 107 | + }, |
| 108 | + { |
| 109 | + 'id': uuid.uuid4().hex, |
| 110 | + 'name': cg_name, |
| 111 | + }, |
| 112 | + ], |
| 113 | + } |
| 114 | + self.volume_sdk_client.get.side_effect = [ |
| 115 | + fakes.FakeResponse(status_code=http.HTTPStatus.NOT_FOUND), |
| 116 | + fakes.FakeResponse(data=data), |
| 117 | + ] |
| 118 | + |
| 119 | + self.assertRaises( |
| 120 | + osc_lib_exceptions.NotFound, |
| 121 | + volume.find_consistency_group, |
| 122 | + self.volume_sdk_client, |
| 123 | + cg_name, |
| 124 | + ) |
0 commit comments