1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from unittest import mock
from uuid import uuid4
import testtools
from openstack.cloud import _utils
from openstack import exceptions
from openstack.tests.unit import base
RANGE_DATA = [
dict(id=1, key1=1, key2=5),
dict(id=2, key1=1, key2=20),
dict(id=3, key1=2, key2=10),
dict(id=4, key1=2, key2=30),
dict(id=5, key1=3, key2=40),
dict(id=6, key1=3, key2=40),
]
class TestUtils(base.TestCase):
def test__filter_list_name_or_id(self):
el1 = dict(id=100, name='donald')
el2 = dict(id=200, name='pluto')
data = [el1, el2]
ret = _utils._filter_list(data, 'donald', None)
self.assertEqual([el1], ret)
def test__filter_list_name_or_id_special(self):
el1 = dict(id=100, name='donald')
el2 = dict(id=200, name='pluto[2017-01-10]')
data = [el1, el2]
ret = _utils._filter_list(data, 'pluto[2017-01-10]', None)
self.assertEqual([el2], ret)
def test__filter_list_name_or_id_partial_bad(self):
el1 = dict(id=100, name='donald')
el2 = dict(id=200, name='pluto[2017-01-10]')
data = [el1, el2]
ret = _utils._filter_list(data, 'pluto[2017-01]', None)
self.assertEqual([], ret)
def test__filter_list_name_or_id_partial_glob(self):
el1 = dict(id=100, name='donald')
el2 = dict(id=200, name='pluto[2017-01-10]')
data = [el1, el2]
ret = _utils._filter_list(data, 'pluto*', None)
self.assertEqual([el2], ret)
def test__filter_list_name_or_id_non_glob_glob(self):
el1 = dict(id=100, name='donald')
el2 = dict(id=200, name='pluto[2017-01-10]')
data = [el1, el2]
ret = _utils._filter_list(data, 'pluto', None)
self.assertEqual([], ret)
def test__filter_list_name_or_id_glob(self):
el1 = dict(id=100, name='donald')
el2 = dict(id=200, name='pluto')
el3 = dict(id=200, name='pluto-2')
data = [el1, el2, el3]
ret = _utils._filter_list(data, 'pluto*', None)
self.assertEqual([el2, el3], ret)
def test__filter_list_name_or_id_glob_not_found(self):
el1 = dict(id=100, name='donald')
el2 = dict(id=200, name='pluto')
el3 = dict(id=200, name='pluto-2')
data = [el1, el2, el3]
ret = _utils._filter_list(data, 'q*', None)
self.assertEqual([], ret)
def test__filter_list_unicode(self):
el1 = dict(
id=100,
name='中文',
last='duck',
other=dict(category='duck', financial=dict(status='poor')),
)
el2 = dict(
id=200,
name='中文',
last='trump',
other=dict(category='human', financial=dict(status='rich')),
)
el3 = dict(
id=300,
name='donald',
last='ronald mac',
other=dict(category='clown', financial=dict(status='rich')),
)
data = [el1, el2, el3]
ret = _utils._filter_list(
data, '中文', {'other': {'financial': {'status': 'rich'}}}
)
self.assertEqual([el2], ret)
def test__filter_list_filter(self):
el1 = dict(id=100, name='donald', other='duck')
el2 = dict(id=200, name='donald', other='trump')
data = [el1, el2]
ret = _utils._filter_list(data, 'donald', {'other': 'duck'})
self.assertEqual([el1], ret)
def test__filter_list_filter_jmespath(self):
el1 = dict(id=100, name='donald', other='duck')
el2 = dict(id=200, name='donald', other='trump')
data = [el1, el2]
ret = _utils._filter_list(data, 'donald', "[?other == `duck`]")
self.assertEqual([el1], ret)
def test__filter_list_dict1(self):
el1 = dict(
id=100, name='donald', last='duck', other=dict(category='duck')
)
el2 = dict(
id=200, name='donald', last='trump', other=dict(category='human')
)
el3 = dict(
id=300,
name='donald',
last='ronald mac',
other=dict(category='clown'),
)
data = [el1, el2, el3]
ret = _utils._filter_list(
data, 'donald', {'other': {'category': 'clown'}}
)
self.assertEqual([el3], ret)
def test__filter_list_dict2(self):
el1 = dict(
id=100,
name='donald',
last='duck',
other=dict(category='duck', financial=dict(status='poor')),
)
el2 = dict(
id=200,
name='donald',
last='trump',
other=dict(category='human', financial=dict(status='rich')),
)
el3 = dict(
id=300,
name='donald',
last='ronald mac',
other=dict(category='clown', financial=dict(status='rich')),
)
data = [el1, el2, el3]
ret = _utils._filter_list(
data, 'donald', {'other': {'financial': {'status': 'rich'}}}
)
self.assertEqual([el2, el3], ret)
def test_safe_dict_min_ints(self):
"""Test integer comparison"""
data = [{'f1': 3}, {'f1': 2}, {'f1': 1}]
retval = _utils.safe_dict_min('f1', data)
self.assertEqual(1, retval)
def test_safe_dict_min_strs(self):
"""Test integer as strings comparison"""
data = [{'f1': '3'}, {'f1': '2'}, {'f1': '1'}]
retval = _utils.safe_dict_min('f1', data)
self.assertEqual(1, retval)
def test_safe_dict_min_None(self):
"""Test None values"""
data = [{'f1': 3}, {'f1': None}, {'f1': 1}]
retval = _utils.safe_dict_min('f1', data)
self.assertEqual(1, retval)
def test_safe_dict_min_key_missing(self):
"""Test missing key for an entry still works"""
data = [{'f1': 3}, {'x': 2}, {'f1': 1}]
retval = _utils.safe_dict_min('f1', data)
self.assertEqual(1, retval)
def test_safe_dict_min_key_not_found(self):
"""Test key not found in any elements returns None"""
data = [{'f1': 3}, {'f1': 2}, {'f1': 1}]
retval = _utils.safe_dict_min('doesnotexist', data)
self.assertIsNone(retval)
def test_safe_dict_min_not_int(self):
"""Test non-integer key value raises OSCE"""
data = [{'f1': 3}, {'f1': "aaa"}, {'f1': 1}]
with testtools.ExpectedException(
exceptions.SDKException,
"Search for minimum value failed. "
"Value for f1 is not an integer: aaa",
):
_utils.safe_dict_min('f1', data)
def test_safe_dict_max_ints(self):
"""Test integer comparison"""
data = [{'f1': 3}, {'f1': 2}, {'f1': 1}]
retval = _utils.safe_dict_max('f1', data)
self.assertEqual(3, retval)
def test_safe_dict_max_strs(self):
"""Test integer as strings comparison"""
data = [{'f1': '3'}, {'f1': '2'}, {'f1': '1'}]
retval = _utils.safe_dict_max('f1', data)
self.assertEqual(3, retval)
def test_safe_dict_max_None(self):
"""Test None values"""
data = [{'f1': 3}, {'f1': None}, {'f1': 1}]
retval = _utils.safe_dict_max('f1', data)
self.assertEqual(3, retval)
def test_safe_dict_max_key_missing(self):
"""Test missing key for an entry still works"""
data = [{'f1': 3}, {'x': 2}, {'f1': 1}]
retval = _utils.safe_dict_max('f1', data)
self.assertEqual(3, retval)
def test_safe_dict_max_key_not_found(self):
"""Test key not found in any elements returns None"""
data = [{'f1': 3}, {'f1': 2}, {'f1': 1}]
retval = _utils.safe_dict_max('doesnotexist', data)
self.assertIsNone(retval)
def test_safe_dict_max_not_int(self):
"""Test non-integer key value raises OSCE"""
data = [{'f1': 3}, {'f1': "aaa"}, {'f1': 1}]
with testtools.ExpectedException(
exceptions.SDKException,
"Search for maximum value failed. "
"Value for f1 is not an integer: aaa",
):
_utils.safe_dict_max('f1', data)
def test_parse_range_None(self):
self.assertIsNone(_utils.parse_range(None))
def test_parse_range_invalid(self):
self.assertIsNone(_utils.parse_range("<invalid"))
def test_parse_range_int_only(self):
retval = _utils.parse_range("1024")
self.assertIsInstance(retval, tuple)
self.assertIsNone(retval[0])
self.assertEqual(1024, retval[1])
def test_parse_range_lt(self):
retval = _utils.parse_range("<1024")
self.assertIsInstance(retval, tuple)
self.assertEqual("<", retval[0])
self.assertEqual(1024, retval[1])
def test_parse_range_gt(self):
retval = _utils.parse_range(">1024")
self.assertIsInstance(retval, tuple)
self.assertEqual(">", retval[0])
self.assertEqual(1024, retval[1])
def test_parse_range_le(self):
retval = _utils.parse_range("<=1024")
self.assertIsInstance(retval, tuple)
self.assertEqual("<=", retval[0])
self.assertEqual(1024, retval[1])
def test_parse_range_ge(self):
retval = _utils.parse_range(">=1024")
self.assertIsInstance(retval, tuple)
self.assertEqual(">=", retval[0])
self.assertEqual(1024, retval[1])
def test_range_filter_min(self):
retval = _utils.range_filter(RANGE_DATA, "key1", "min")
self.assertIsInstance(retval, list)
self.assertEqual(2, len(retval))
self.assertEqual(RANGE_DATA[:2], retval)
def test_range_filter_max(self):
retval = _utils.range_filter(RANGE_DATA, "key1", "max")
self.assertIsInstance(retval, list)
self.assertEqual(2, len(retval))
self.assertEqual(RANGE_DATA[-2:], retval)
def test_range_filter_range(self):
retval = _utils.range_filter(RANGE_DATA, "key1", "<3")
self.assertIsInstance(retval, list)
self.assertEqual(4, len(retval))
self.assertEqual(RANGE_DATA[:4], retval)
def test_range_filter_exact(self):
retval = _utils.range_filter(RANGE_DATA, "key1", "2")
self.assertIsInstance(retval, list)
self.assertEqual(2, len(retval))
self.assertEqual(RANGE_DATA[2:4], retval)
def test_range_filter_invalid_int(self):
with testtools.ExpectedException(
exceptions.SDKException, "Invalid range value: <1A0"
):
_utils.range_filter(RANGE_DATA, "key1", "<1A0")
def test_range_filter_invalid_op(self):
with testtools.ExpectedException(
exceptions.SDKException, "Invalid range value: <>100"
):
_utils.range_filter(RANGE_DATA, "key1", "<>100")
def test_get_entity_pass_object(self):
obj = mock.Mock(id=uuid4().hex)
self.cloud.use_direct_get = True
self.assertEqual(obj, _utils._get_entity(self.cloud, '', obj, {}))
def test_get_entity_pass_dict(self):
d = dict(id=uuid4().hex)
self.cloud.use_direct_get = True
self.assertEqual(d, _utils._get_entity(self.cloud, '', d, {}))
def test_get_entity_no_use_direct_get(self):
# test we are defaulting to the search_<resource> methods
# if the use_direct_get flag is set to False(default).
uuid = uuid4().hex
resource = 'network'
func = f'search_{resource}s'
filters = {}
with mock.patch.object(self.cloud, func) as search:
_utils._get_entity(self.cloud, resource, uuid, filters)
search.assert_called_once_with(uuid, filters)
def test_get_entity_no_uuid_like(self):
# test we are defaulting to the search_<resource> methods
# if the name_or_id param is a name(string) but not a uuid.
self.cloud.use_direct_get = True
name = 'name_no_uuid'
resource = 'network'
func = f'search_{resource}s'
filters = {}
with mock.patch.object(self.cloud, func) as search:
_utils._get_entity(self.cloud, resource, name, filters)
search.assert_called_once_with(name, filters)
def test_get_entity_pass_uuid(self):
uuid = uuid4().hex
self.cloud.use_direct_get = True
resources = [
'flavor',
'image',
'volume',
'network',
'subnet',
'port',
'floating_ip',
'security_group',
]
for r in resources:
f = f'get_{r}_by_id'
with mock.patch.object(self.cloud, f) as get:
_utils._get_entity(self.cloud, r, uuid, {})
get.assert_called_once_with(uuid)
def test_get_entity_pass_search_methods(self):
self.cloud.use_direct_get = True
resources = [
'flavor',
'image',
'volume',
'network',
'subnet',
'port',
'floating_ip',
'security_group',
]
filters = {}
name = 'name_no_uuid'
for r in resources:
f = f'search_{r}s'
with mock.patch.object(self.cloud, f) as search:
_utils._get_entity(self.cloud, r, name, {})
search.assert_called_once_with(name, filters)
def test_get_entity_get_and_search(self):
resources = [
'flavor',
'image',
'volume',
'network',
'subnet',
'port',
'floating_ip',
'security_group',
]
for r in resources:
self.assertTrue(hasattr(self.cloud, f'get_{r}_by_id'))
self.assertTrue(hasattr(self.cloud, f'search_{r}s'))
|