| 1 |
# Copyright 2013 - Mirantis, Inc. |
| 2 |
# Copyright 2017 - Nokia Networks. |
| 3 |
# Copyright 2017 - Red Hat, Inc. |
| 4 |
# All Rights Reserved. |
| 5 |
# |
| 6 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 |
# not use this file except in compliance with the License. You may obtain |
| 8 |
# a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, software |
| 13 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 |
# License for the specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
import copy |
| 19 |
|
| 20 |
from yaql.language import utils as yaql_utils |
| 21 |
|
| 22 |
from mistral_lib.tests import base as tests_base |
| 23 |
from mistral_lib import utils |
| 24 |
|
| 25 |
import testtools.matchers as ttm |
| 26 |
|
| 27 |
|
| 28 |
LEFT = { |
| 29 |
'key1': { |
| 30 |
'key11': "val11" |
| 31 |
}, |
| 32 |
'key2': 'val2' |
| 33 |
} |
| 34 |
|
| 35 |
RIGHT = { |
| 36 |
'key1': { |
| 37 |
'key11': "val111111", |
| 38 |
'key12': "val12", |
| 39 |
'key13': { |
| 40 |
'key131': 'val131' |
| 41 |
} |
| 42 |
}, |
| 43 |
'key2': 'val2222', |
| 44 |
'key3': 'val3' |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
class TestUtils(tests_base.TestCase): |
| 49 |
|
| 50 |
def test_merge_dicts(self): |
| 51 |
left = copy.deepcopy(LEFT) |
| 52 |
right = copy.deepcopy(RIGHT) |
| 53 |
|
| 54 |
expected = { |
| 55 |
'key1': { |
| 56 |
'key11': "val111111", |
| 57 |
'key12': "val12", |
| 58 |
'key13': { |
| 59 |
'key131': 'val131' |
| 60 |
} |
| 61 |
}, |
| 62 |
'key2': 'val2222', |
| 63 |
'key3': 'val3' |
| 64 |
} |
| 65 |
|
| 66 |
utils.merge_dicts(left, right) |
| 67 |
|
| 68 |
self.assertDictEqual(left, expected) |
| 69 |
|
| 70 |
def test_merge_dicts_overwrite_false(self): |
| 71 |
left = copy.deepcopy(LEFT) |
| 72 |
right = copy.deepcopy(RIGHT) |
| 73 |
|
| 74 |
expected = { |
| 75 |
'key1': { |
| 76 |
'key11': "val11", |
| 77 |
'key12': "val12", |
| 78 |
'key13': { |
| 79 |
'key131': 'val131' |
| 80 |
} |
| 81 |
}, |
| 82 |
'key2': 'val2', |
| 83 |
'key3': 'val3' |
| 84 |
} |
| 85 |
|
| 86 |
utils.merge_dicts(left, right, overwrite=False) |
| 87 |
|
| 88 |
self.assertDictEqual(left, expected) |
| 89 |
|
| 90 |
def test_itersubclasses(self): |
| 91 |
class A(object): |
| 92 |
pass |
| 93 |
|
| 94 |
class B(A): |
| 95 |
pass |
| 96 |
|
| 97 |
class C(A): |
| 98 |
pass |
| 99 |
|
| 100 |
class D(C): |
| 101 |
pass |
| 102 |
|
| 103 |
self.assertEqual([B, C, D], list(utils.iter_subclasses(A))) |
| 104 |
|
| 105 |
def test_get_dict_from_entries(self): |
| 106 |
input = ['param1', {'param2': 2}] |
| 107 |
input_dict = utils.get_dict_from_entries(input) |
| 108 |
|
| 109 |
self.assertIn('param1', input_dict) |
| 110 |
self.assertIn('param2', input_dict) |
| 111 |
self.assertEqual(2, input_dict.get('param2')) |
| 112 |
self.assertIs(input_dict.get('param1'), utils.NotDefined) |
| 113 |
|
| 114 |
def test_get_input_dict_from_string(self): |
| 115 |
self.assertDictEqual( |
| 116 |
{ |
| 117 |
'param1': utils.NotDefined, |
| 118 |
'param2': 2, |
| 119 |
'param3': 'var3' |
| 120 |
}, |
| 121 |
utils.get_dict_from_string('param1, param2=2, param3="var3"') |
| 122 |
) |
| 123 |
|
| 124 |
self.assertDictEqual({}, utils.get_dict_from_string('')) |
| 125 |
|
| 126 |
def test_cut_string(self): |
| 127 |
s = 'Hello, Mistral!' |
| 128 |
|
| 129 |
self.assertEqual('Hello...', utils.cut_string(s, length=5)) |
| 130 |
self.assertEqual(s, utils.cut_string(s, length=100)) |
| 131 |
self.assertEqual(s, utils.cut_string(s, length=-1)) |
| 132 |
|
| 133 |
def test_cut_list(self): |
| 134 |
list_ = ['Hello, Mistral!', 'Hello, OpenStack!'] |
| 135 |
|
| 136 |
self.assertEqual("['Hello, M...", utils.cut_list(list_, 13)) |
| 137 |
self.assertEqual("['Hello, Mistr...", utils.cut_list(list_, 17)) |
| 138 |
self.assertEqual( |
| 139 |
"['Hello, Mistral!', 'He...", |
| 140 |
utils.cut_list(list_, 26)) |
| 141 |
|
| 142 |
self.assertEqual( |
| 143 |
"['Hello, Mistral!', 'Hello, OpenStack!']", |
| 144 |
utils.cut_list(list_, 100) |
| 145 |
) |
| 146 |
|
| 147 |
self.assertEqual( |
| 148 |
"['Hello, Mistral!', 'Hello, OpenStack!']", |
| 149 |
utils.cut_list(list_, -1) |
| 150 |
) |
| 151 |
|
| 152 |
self.assertEqual("[1, 2...", utils.cut_list([1, 2, 3, 4, 5], 8)) |
| 153 |
self.assertEqual("[1, 2,...", utils.cut_list([1, 2, 3, 4, 5], 9)) |
| 154 |
self.assertEqual("[1, 2, 3...", utils.cut_list([1, 2, 3, 4, 5], 11)) |
| 155 |
|
| 156 |
self.assertRaises(ValueError, utils.cut_list, (1, 2)) |
| 157 |
|
| 158 |
def test_cut_list_with_large_dict_of_str(self): |
| 159 |
d = [str(i) for i in range(65535)] |
| 160 |
s = utils.cut(d, 65535) |
| 161 |
self.assertThat(len(s), ttm.Not(ttm.GreaterThan(65535))) |
| 162 |
|
| 163 |
def test_cut_list_with_large_dict_of_int(self): |
| 164 |
d = [i for i in range(65535)] |
| 165 |
s = utils.cut(d, 65535) |
| 166 |
self.assertThat(len(s), ttm.Not(ttm.GreaterThan(65535))) |
| 167 |
|
| 168 |
def test_cut_list_with_large_dict_of_dict(self): |
| 169 |
d = [{'value': str(i)} for i in range(65535)] |
| 170 |
s = utils.cut(d, 65535) |
| 171 |
self.assertThat(len(s), ttm.Not(ttm.GreaterThan(65535))) |
| 172 |
|
| 173 |
def test_cut_list_for_state_info(self): |
| 174 |
d = [{'value': 'This is a string that exceeds 35 characters'} |
| 175 |
for i in range(2000)] |
| 176 |
s = utils.cut(d, 65500) |
| 177 |
self.assertThat(len(s), ttm.Not(ttm.GreaterThan(65500))) |
| 178 |
|
| 179 |
def test_cut_dict_with_strings(self): |
| 180 |
d = {'key1': 'value1', 'key2': 'value2'} |
| 181 |
|
| 182 |
s = utils.cut_dict(d, 13) |
| 183 |
|
| 184 |
self.assertIn(s, ["{'key1': '...", "{'key2': '..."]) |
| 185 |
|
| 186 |
s = utils.cut_dict(d, 15) |
| 187 |
|
| 188 |
self.assertIn(s, ["{'key1': 'va...", "{'key2': 'va..."]) |
| 189 |
|
| 190 |
s = utils.cut_dict(d, 22) |
| 191 |
|
| 192 |
self.assertIn( |
| 193 |
s, |
| 194 |
["{'key1': 'value1', ...", "{'key2': 'value2', ..."] |
| 195 |
) |
| 196 |
|
| 197 |
self.assertIn( |
| 198 |
utils.cut_dict(d, 100), |
| 199 |
[ |
| 200 |
"{'key1': 'value1', 'key2': 'value2'}", |
| 201 |
"{'key2': 'value2', 'key1': 'value1'}" |
| 202 |
] |
| 203 |
) |
| 204 |
|
| 205 |
self.assertIn( |
| 206 |
utils.cut_dict(d, -1), |
| 207 |
[ |
| 208 |
"{'key1': 'value1', 'key2': 'value2'}", |
| 209 |
"{'key2': 'value2', 'key1': 'value1'}" |
| 210 |
] |
| 211 |
) |
| 212 |
|
| 213 |
self.assertRaises(ValueError, utils.cut_dict, (1, 2)) |
| 214 |
|
| 215 |
def test_cut_dict_with_digits(self): |
| 216 |
d = {1: 2, 3: 4} |
| 217 |
|
| 218 |
s = utils.cut_dict(d, 10) |
| 219 |
|
| 220 |
self.assertIn(s, ["{1: 2, ...", "{3: 4, ..."]) |
| 221 |
|
| 222 |
s = utils.cut_dict(d, 11) |
| 223 |
|
| 224 |
self.assertIn(s, ["{1: 2, 3...", "{3: 4, 1..."]) |
| 225 |
|
| 226 |
s = utils.cut_dict(d, 100) |
| 227 |
|
| 228 |
self.assertIn(s, ["{1: 2, 3: 4}", "{3: 4, 1: 2}"]) |
| 229 |
|
| 230 |
def test_cut_dict_with_large_dict_of_str(self): |
| 231 |
d = {} |
| 232 |
for i in range(65535): |
| 233 |
d[str(i)] = str(i) |
| 234 |
s = utils.cut(d, 65535) |
| 235 |
self.assertThat(len(s), ttm.Not(ttm.GreaterThan(65535))) |
| 236 |
|
| 237 |
def test_cut_dict_with_large_dict_of_int(self): |
| 238 |
d = {} |
| 239 |
for i in range(65535): |
| 240 |
d[i] = i |
| 241 |
s = utils.cut(d, 65535) |
| 242 |
self.assertThat(len(s), ttm.Not(ttm.GreaterThan(65535))) |
| 243 |
|
| 244 |
def test_cut_dict_with_large_dict_of_dict(self): |
| 245 |
d = {} |
| 246 |
for i in range(65535): |
| 247 |
d[i] = {'value': str(i)} |
| 248 |
s = utils.cut(d, 65535) |
| 249 |
self.assertThat(len(s), ttm.Not(ttm.GreaterThan(65535))) |
| 250 |
|
| 251 |
def test_cut_dict_for_state_info(self): |
| 252 |
d = {} |
| 253 |
for i in range(2000): |
| 254 |
d[i] = {'value': 'This is a string that exceeds 35 characters'} |
| 255 |
s = utils.cut(d, 65500) |
| 256 |
self.assertThat(len(s), ttm.Not(ttm.GreaterThan(65500))) |
| 257 |
|
| 258 |
def test_mask_data(self): |
| 259 |
payload = {'adminPass': 'fooBarBaz'} |
| 260 |
expected = {'adminPass': '***'} |
| 261 |
self.assertEqual(expected, utils.mask_data(payload)) |
| 262 |
|
| 263 |
payload = """adminPass='fooBarBaz'""" |
| 264 |
expected = """adminPass='***'""" |
| 265 |
self.assertEqual(expected, utils.mask_data(payload)) |
| 266 |
|
| 267 |
payload = [{'adminPass': 'fooBarBaz'}, {"new_pass": "blah"}] |
| 268 |
expected = [{'adminPass': '***'}, {"new_pass": "***"}] |
| 269 |
self.assertEqual(expected, utils.mask_data(payload)) |
| 270 |
|
| 271 |
payload = ["adminPass", 'fooBarBaz'] |
| 272 |
expected = ["adminPass", 'fooBarBaz'] |
| 273 |
self.assertEqual(expected, utils.mask_data(payload)) |
| 274 |
|
| 275 |
def test_json_serialize_frozen_dict(self): |
| 276 |
data = yaql_utils.FrozenDict(a=1, b=2, c=iter([1, 2, 3])) |
| 277 |
|
| 278 |
json_str = utils.to_json_str(data) |
| 279 |
|
| 280 |
self.assertIsNotNone(json_str) |
| 281 |
|
| 282 |
self.assertIn('"a": 1', json_str) |
| 283 |
self.assertIn('"b": 2', json_str) |
| 284 |
self.assertIn('"c": [1, 2, 3]', json_str) |
| 285 |
|
| 286 |
def test_json_serialize_generator(self): |
| 287 |
def _list_stream(_list): |
| 288 |
for i in _list: |
| 289 |
yield i |
| 290 |
|
| 291 |
gen = _list_stream( |
| 292 |
[1, yaql_utils.FrozenDict(a=1), _list_stream([12, 15])] |
| 293 |
) |
| 294 |
|
| 295 |
self.assertEqual('[1, {"a": 1}, [12, 15]]', utils.to_json_str(gen)) |
| 296 |
|
| 297 |
def test_json_serialize_dict_of_generators(self): |
| 298 |
def _f(cnt): |
| 299 |
for i in range(1, cnt + 1): |
| 300 |
yield i |
| 301 |
|
| 302 |
data = {'numbers': _f(3)} |
| 303 |
|
| 304 |
self.assertEqual('{"numbers": [1, 2, 3]}', utils.to_json_str(data)) |
| 305 |
|
| 306 |
def test_json_serialize_range(self): |
| 307 |
self.assertEqual("[1, 2, 3, 4]", utils.to_json_str(range(1, 5))) |
| 308 |
|
| 309 |
def test_json_serialize_iterator_of_frozen_dicts(self): |
| 310 |
data = iter( |
| 311 |
[ |
| 312 |
yaql_utils.FrozenDict(a=1, b=2, c=iter([1, 2, 3])), |
| 313 |
yaql_utils.FrozenDict( |
| 314 |
a=11, |
| 315 |
b=yaql_utils.FrozenDict(b='222'), |
| 316 |
c=iter( |
| 317 |
[ |
| 318 |
1, |
| 319 |
yaql_utils.FrozenDict( |
| 320 |
a=iter([4, yaql_utils.FrozenDict(a=99)]) |
| 321 |
) |
| 322 |
] |
| 323 |
) |
| 324 |
) |
| 325 |
] |
| 326 |
) |
| 327 |
|
| 328 |
json_str = utils.to_json_str(data) |
| 329 |
|
| 330 |
self.assertIsNotNone(json_str) |
| 331 |
|
| 332 |
# Checking the first item. |
| 333 |
self.assertIn('"a": 1', json_str) |
| 334 |
self.assertIn('"b": 2', json_str) |
| 335 |
self.assertIn('"c": [1, 2, 3]', json_str) |
| 336 |
|
| 337 |
# Checking the first item. |
| 338 |
self.assertIn('"a": 11', json_str) |
| 339 |
self.assertIn('"b": {"b": "222"}', json_str) |
| 340 |
self.assertIn('"c": [1, {"a": [4, {"a": 99}]}]', json_str) |