| 1 |
# (c) 2016, Adrian Likins <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import unittest |
| 21 |
|
| 22 |
from ansible.errors import AnsibleParserError, AnsibleFieldAttributeError |
| 23 |
from ansible._internal._datatag._tags import TrustedAsTemplate |
| 24 |
from ansible.playbook.attribute import FieldAttribute, NonInheritableFieldAttribute |
| 25 |
from ansible._internal._templating._engine import TemplateEngine |
| 26 |
from ansible.playbook import base |
| 27 |
|
| 28 |
from units.mock.loader import DictDataLoader |
| 29 |
|
| 30 |
|
| 31 |
class TestBase(unittest.TestCase): |
| 32 |
ClassUnderTest = base.Base |
| 33 |
|
| 34 |
def setUp(self): |
| 35 |
self.assorted_vars = {'var_2_key': 'var_2_value', |
| 36 |
'var_1_key': 'var_1_value', |
| 37 |
'a_list': ['a_list_1', 'a_list_2'], |
| 38 |
'a_dict': {'a_dict_key': 'a_dict_value'}, |
| 39 |
'a_int': 42, |
| 40 |
'a_float': 37.371, |
| 41 |
'a_bool': True, |
| 42 |
'a_none': None, |
| 43 |
} |
| 44 |
self.b = self.ClassUnderTest() |
| 45 |
|
| 46 |
def _base_validate(self, ds): |
| 47 |
bsc = self.ClassUnderTest() |
| 48 |
parent = ExampleParentBaseSubClass() |
| 49 |
bsc._parent = parent |
| 50 |
bsc._dep_chain = [parent] |
| 51 |
parent._dep_chain = None |
| 52 |
bsc.load_data(ds) |
| 53 |
fake_loader = DictDataLoader({}) |
| 54 |
templar = TemplateEngine(loader=fake_loader) |
| 55 |
bsc.post_validate(templar) |
| 56 |
return bsc |
| 57 |
|
| 58 |
def test(self): |
| 59 |
self.assertIsInstance(self.b, base.Base) |
| 60 |
self.assertIsInstance(self.b, self.ClassUnderTest) |
| 61 |
|
| 62 |
# dump me doesn't return anything or change anything so not much to assert |
| 63 |
def test_dump_me_empty(self): |
| 64 |
self.b.dump_me() |
| 65 |
|
| 66 |
def test_dump_me(self): |
| 67 |
ds = {'environment': [], |
| 68 |
'vars': {'var_2_key': 'var_2_value', |
| 69 |
'var_1_key': 'var_1_value'}} |
| 70 |
b = self._base_validate(ds) |
| 71 |
b.dump_me() |
| 72 |
|
| 73 |
def _assert_copy(self, orig, copy): |
| 74 |
self.assertIsInstance(copy, self.ClassUnderTest) |
| 75 |
self.assertIsInstance(copy, base.Base) |
| 76 |
self.assertEqual(len(orig.fattributes), len(copy.fattributes)) |
| 77 |
|
| 78 |
sentinel = 'Empty DS' |
| 79 |
self.assertEqual(getattr(orig, '_ds', sentinel), getattr(copy, '_ds', sentinel)) |
| 80 |
|
| 81 |
def test_copy_empty(self): |
| 82 |
copy = self.b.copy() |
| 83 |
self._assert_copy(self.b, copy) |
| 84 |
|
| 85 |
def test_copy_with_vars(self): |
| 86 |
ds = {'vars': self.assorted_vars} |
| 87 |
b = self._base_validate(ds) |
| 88 |
|
| 89 |
copy = b.copy() |
| 90 |
self._assert_copy(b, copy) |
| 91 |
|
| 92 |
def test_post_validate_empty(self): |
| 93 |
fake_loader = DictDataLoader({}) |
| 94 |
templar = TemplateEngine(loader=fake_loader) |
| 95 |
ret = self.b.post_validate(templar) |
| 96 |
self.assertIsNone(ret) |
| 97 |
|
| 98 |
def test_get_ds_none(self): |
| 99 |
ds = self.b.get_ds() |
| 100 |
self.assertIsNone(ds) |
| 101 |
|
| 102 |
def test_load_data_ds_is_none(self): |
| 103 |
self.assertRaises(AssertionError, self.b.load_data, None) |
| 104 |
|
| 105 |
def test_load_data_invalid_attr(self): |
| 106 |
ds = {'not_a_valid_attr': [], |
| 107 |
'other': None} |
| 108 |
|
| 109 |
self.assertRaises(AnsibleParserError, self.b.load_data, ds) |
| 110 |
|
| 111 |
def test_load_data_invalid_attr_type(self): |
| 112 |
ds = {'environment': True} |
| 113 |
|
| 114 |
# environment is supposed to be a list. This |
| 115 |
# seems like it shouldn't work? |
| 116 |
ret = self.b.load_data(ds) |
| 117 |
self.assertEqual(True, ret._environment) |
| 118 |
|
| 119 |
def test_post_validate(self): |
| 120 |
ds = {'environment': [], |
| 121 |
'port': 443} |
| 122 |
b = self._base_validate(ds) |
| 123 |
self.assertEqual(b.port, 443) |
| 124 |
self.assertEqual(b.environment, []) |
| 125 |
|
| 126 |
def test_post_validate_invalid_attr_types(self): |
| 127 |
ds = {'environment': [], |
| 128 |
'port': 'some_port'} |
| 129 |
b = self._base_validate(ds) |
| 130 |
self.assertEqual(b.port, 'some_port') |
| 131 |
|
| 132 |
def test_vars(self): |
| 133 |
# vars as a dict. |
| 134 |
ds = {'environment': [], |
| 135 |
'vars': {'var_2_key': 'var_2_value', |
| 136 |
'var_1_key': 'var_1_value'}} |
| 137 |
b = self._base_validate(ds) |
| 138 |
self.assertEqual(b.vars['var_1_key'], 'var_1_value') |
| 139 |
|
| 140 |
def test_vars_list_of_dicts(self): |
| 141 |
ds = {'environment': [], |
| 142 |
'vars': [{'var_2_key': 'var_2_value'}, |
| 143 |
{'var_1_key': 'var_1_value'}] |
| 144 |
} |
| 145 |
self.assertRaises(AnsibleParserError, self.b.load_data, ds) |
| 146 |
|
| 147 |
def test_vars_not_dict_or_list(self): |
| 148 |
ds = {'environment': [], |
| 149 |
'vars': 'I am a string, not a dict or a list of dicts'} |
| 150 |
self.assertRaises(AnsibleParserError, self.b.load_data, ds) |
| 151 |
|
| 152 |
def test_vars_not_valid_identifier(self): |
| 153 |
ds = {'environment': [], |
| 154 |
'vars': [{'var_2_key': 'var_2_value'}, |
| 155 |
{'1an-invalid identifier': 'var_1_value'}] |
| 156 |
} |
| 157 |
self.assertRaises(AnsibleParserError, self.b.load_data, ds) |
| 158 |
|
| 159 |
def test_vars_is_list_but_not_of_dicts(self): |
| 160 |
ds = {'environment': [], |
| 161 |
'vars': ['foo', 'bar', 'this is a string not a dict'] |
| 162 |
} |
| 163 |
self.assertRaises(AnsibleParserError, self.b.load_data, ds) |
| 164 |
|
| 165 |
def test_vars_is_none(self): |
| 166 |
# If vars is None, we should get a empty dict back |
| 167 |
ds = {'environment': [], |
| 168 |
'vars': None |
| 169 |
} |
| 170 |
b = self._base_validate(ds) |
| 171 |
self.assertEqual(b.vars, {}) |
| 172 |
|
| 173 |
def test_validate_empty(self): |
| 174 |
self.b.validate() |
| 175 |
self.assertTrue(self.b._validated) |
| 176 |
|
| 177 |
def test_getters(self): |
| 178 |
# not sure why these exist, but here are tests anyway |
| 179 |
loader = self.b.get_loader() |
| 180 |
variable_manager = self.b.get_variable_manager() |
| 181 |
self.assertEqual(loader, self.b._loader) |
| 182 |
self.assertEqual(variable_manager, self.b._variable_manager) |
| 183 |
|
| 184 |
|
| 185 |
class TestExtendValue(unittest.TestCase): |
| 186 |
# _extend_value could be a module or staticmethod but since its |
| 187 |
# not, the test is here. |
| 188 |
def test_extend_value_list_newlist(self): |
| 189 |
b = base.Base() |
| 190 |
value_list = ['first', 'second'] |
| 191 |
new_value_list = ['new_first', 'new_second'] |
| 192 |
ret = b._extend_value(value_list, new_value_list) |
| 193 |
self.assertEqual(value_list + new_value_list, ret) |
| 194 |
|
| 195 |
def test_extend_value_list_newlist_prepend(self): |
| 196 |
b = base.Base() |
| 197 |
value_list = ['first', 'second'] |
| 198 |
new_value_list = ['new_first', 'new_second'] |
| 199 |
ret_prepend = b._extend_value(value_list, new_value_list, prepend=True) |
| 200 |
self.assertEqual(new_value_list + value_list, ret_prepend) |
| 201 |
|
| 202 |
def test_extend_value_newlist_list(self): |
| 203 |
b = base.Base() |
| 204 |
value_list = ['first', 'second'] |
| 205 |
new_value_list = ['new_first', 'new_second'] |
| 206 |
ret = b._extend_value(new_value_list, value_list) |
| 207 |
self.assertEqual(new_value_list + value_list, ret) |
| 208 |
|
| 209 |
def test_extend_value_newlist_list_prepend(self): |
| 210 |
b = base.Base() |
| 211 |
value_list = ['first', 'second'] |
| 212 |
new_value_list = ['new_first', 'new_second'] |
| 213 |
ret = b._extend_value(new_value_list, value_list, prepend=True) |
| 214 |
self.assertEqual(value_list + new_value_list, ret) |
| 215 |
|
| 216 |
def test_extend_value_string_newlist(self): |
| 217 |
b = base.Base() |
| 218 |
some_string = 'some string' |
| 219 |
new_value_list = ['new_first', 'new_second'] |
| 220 |
ret = b._extend_value(some_string, new_value_list) |
| 221 |
self.assertEqual([some_string] + new_value_list, ret) |
| 222 |
|
| 223 |
def test_extend_value_string_newstring(self): |
| 224 |
b = base.Base() |
| 225 |
some_string = 'some string' |
| 226 |
new_value_string = 'this is the new values' |
| 227 |
ret = b._extend_value(some_string, new_value_string) |
| 228 |
self.assertEqual([some_string, new_value_string], ret) |
| 229 |
|
| 230 |
def test_extend_value_list_newstring(self): |
| 231 |
b = base.Base() |
| 232 |
value_list = ['first', 'second'] |
| 233 |
new_value_string = 'this is the new values' |
| 234 |
ret = b._extend_value(value_list, new_value_string) |
| 235 |
self.assertEqual(value_list + [new_value_string], ret) |
| 236 |
|
| 237 |
def test_extend_value_none_none(self): |
| 238 |
b = base.Base() |
| 239 |
ret = b._extend_value(None, None) |
| 240 |
self.assertEqual(len(ret), 0) |
| 241 |
self.assertFalse(ret) |
| 242 |
|
| 243 |
def test_extend_value_none_list(self): |
| 244 |
b = base.Base() |
| 245 |
ret = b._extend_value(None, ['foo']) |
| 246 |
self.assertEqual(ret, ['foo']) |
| 247 |
|
| 248 |
|
| 249 |
class ExampleException(Exception): |
| 250 |
pass |
| 251 |
|
| 252 |
|
| 253 |
# naming fails me... |
| 254 |
class ExampleParentBaseSubClass(base.Base): |
| 255 |
test_attr_parent_string = FieldAttribute(isa='string', default='A string attr for a class that may be a parent for testing') |
| 256 |
|
| 257 |
def __init__(self): |
| 258 |
|
| 259 |
super(ExampleParentBaseSubClass, self).__init__() |
| 260 |
self._dep_chain = None |
| 261 |
|
| 262 |
def get_dep_chain(self): |
| 263 |
return self._dep_chain |
| 264 |
|
| 265 |
|
| 266 |
class ExampleSubClass(base.Base): |
| 267 |
test_attr_blip = NonInheritableFieldAttribute(isa='string', default='example sub class test_attr_blip', |
| 268 |
always_post_validate=True) |
| 269 |
|
| 270 |
def __init__(self): |
| 271 |
super(ExampleSubClass, self).__init__() |
| 272 |
|
| 273 |
|
| 274 |
class BaseSubClass(base.Base): |
| 275 |
name = FieldAttribute(isa='string', default='', always_post_validate=True) |
| 276 |
test_attr_bool = FieldAttribute(isa='bool', always_post_validate=True) |
| 277 |
test_attr_int = FieldAttribute(isa='int', always_post_validate=True) |
| 278 |
test_attr_float = FieldAttribute(isa='float', default=3.14159, always_post_validate=True) |
| 279 |
test_attr_list = FieldAttribute(isa='list', listof=(str,), always_post_validate=True) |
| 280 |
test_attr_mixed_list = FieldAttribute(isa='list', listof=(str, int), always_post_validate=True) |
| 281 |
test_attr_list_no_listof = FieldAttribute(isa='list', always_post_validate=True) |
| 282 |
test_attr_list_required = FieldAttribute(isa='list', listof=(str,), required=True, |
| 283 |
default=list, always_post_validate=True) |
| 284 |
test_attr_string = FieldAttribute(isa='string', default='the_test_attr_string_default_value') |
| 285 |
test_attr_string_required = FieldAttribute(isa='string', required=True, |
| 286 |
default='the_test_attr_string_default_value') |
| 287 |
test_attr_percent = FieldAttribute(isa='percent', always_post_validate=True) |
| 288 |
test_attr_dict = FieldAttribute(isa='dict', default=lambda: {'a_key': 'a_value'}, always_post_validate=True) |
| 289 |
test_attr_class = FieldAttribute(isa='class', class_type=ExampleSubClass) |
| 290 |
test_attr_class_post_validate = FieldAttribute(isa='class', class_type=ExampleSubClass, |
| 291 |
always_post_validate=True) |
| 292 |
test_attr_unknown_isa = FieldAttribute(isa='not_a_real_isa', always_post_validate=True) |
| 293 |
test_attr_example = FieldAttribute(isa='string', default='the_default', |
| 294 |
always_post_validate=True) |
| 295 |
test_attr_none = FieldAttribute(isa='string', always_post_validate=True) |
| 296 |
test_attr_preprocess = FieldAttribute(isa='string', default='the default for preprocess') |
| 297 |
test_attr_method = FieldAttribute(isa='string', default='some attr with a getter', |
| 298 |
always_post_validate=True) |
| 299 |
test_attr_method_missing = FieldAttribute(isa='string', default='some attr with a missing getter', |
| 300 |
always_post_validate=True) |
| 301 |
|
| 302 |
def _get_attr_test_attr_method(self): |
| 303 |
return 'foo bar' |
| 304 |
|
| 305 |
def _validate_test_attr_example(self, attr, name, value): |
| 306 |
if not isinstance(value, str): |
| 307 |
raise ExampleException('test_attr_example is not a string: %s type=%s' % (value, type(value))) |
| 308 |
|
| 309 |
def _post_validate_test_attr_example(self, attr, value, templar): |
| 310 |
after_template_value = templar.template(value) |
| 311 |
return after_template_value |
| 312 |
|
| 313 |
def _post_validate_test_attr_none(self, attr, value, templar): |
| 314 |
return None |
| 315 |
|
| 316 |
|
| 317 |
# terrible name, but it is a TestBase subclass for testing subclasses of Base |
| 318 |
class TestBaseSubClass(TestBase): |
| 319 |
ClassUnderTest = BaseSubClass |
| 320 |
|
| 321 |
def _base_validate(self, ds): |
| 322 |
ds['test_attr_list_required'] = [] |
| 323 |
return super(TestBaseSubClass, self)._base_validate(ds) |
| 324 |
|
| 325 |
def test_attr_bool(self): |
| 326 |
ds = {'test_attr_bool': True} |
| 327 |
bsc = self._base_validate(ds) |
| 328 |
self.assertEqual(bsc.test_attr_bool, True) |
| 329 |
|
| 330 |
def test_attr_int(self): |
| 331 |
MOST_RANDOM_NUMBER = 37 |
| 332 |
ds = {'test_attr_int': MOST_RANDOM_NUMBER} |
| 333 |
bsc = self._base_validate(ds) |
| 334 |
self.assertEqual(bsc.test_attr_int, MOST_RANDOM_NUMBER) |
| 335 |
|
| 336 |
def test_attr_int_del(self): |
| 337 |
MOST_RANDOM_NUMBER = 37 |
| 338 |
ds = {'test_attr_int': MOST_RANDOM_NUMBER} |
| 339 |
bsc = self._base_validate(ds) |
| 340 |
del bsc.test_attr_int |
| 341 |
self.assertNotIn('_test_attr_int', bsc.__dict__) |
| 342 |
|
| 343 |
def test_attr_float(self): |
| 344 |
roughly_pi = 4.0 |
| 345 |
ds = {'test_attr_float': roughly_pi} |
| 346 |
bsc = self._base_validate(ds) |
| 347 |
self.assertEqual(bsc.test_attr_float, roughly_pi) |
| 348 |
|
| 349 |
def test_attr_percent(self): |
| 350 |
percentage = '90%' |
| 351 |
percentage_float = 90.0 |
| 352 |
ds = {'test_attr_percent': percentage} |
| 353 |
bsc = self._base_validate(ds) |
| 354 |
self.assertEqual(bsc.test_attr_percent, percentage_float) |
| 355 |
|
| 356 |
# This method works hard and gives it its all and everything it's got. It doesn't |
| 357 |
# leave anything on the field. It deserves to pass. It has earned it. |
| 358 |
def test_attr_percent_110_percent(self): |
| 359 |
percentage = '110.11%' |
| 360 |
percentage_float = 110.11 |
| 361 |
ds = {'test_attr_percent': percentage} |
| 362 |
bsc = self._base_validate(ds) |
| 363 |
self.assertEqual(bsc.test_attr_percent, percentage_float) |
| 364 |
|
| 365 |
# This method is just here for the paycheck. |
| 366 |
def test_attr_percent_60_no_percent_sign(self): |
| 367 |
percentage = '60' |
| 368 |
percentage_float = 60.0 |
| 369 |
ds = {'test_attr_percent': percentage} |
| 370 |
bsc = self._base_validate(ds) |
| 371 |
self.assertEqual(bsc.test_attr_percent, percentage_float) |
| 372 |
|
| 373 |
def test_attr_dict(self): |
| 374 |
test_dict = {'a_different_key': 'a_different_value'} |
| 375 |
ds = {'test_attr_dict': test_dict} |
| 376 |
bsc = self._base_validate(ds) |
| 377 |
self.assertEqual(bsc.test_attr_dict, test_dict) |
| 378 |
|
| 379 |
def test_attr_dict_string(self): |
| 380 |
test_value = 'just_some_random_string' |
| 381 |
ds = {'test_attr_dict': test_value} |
| 382 |
self.assertRaisesRegex(AnsibleParserError, 'is not a dictionary', self._base_validate, ds) |
| 383 |
|
| 384 |
def test_attr_class(self): |
| 385 |
esc = ExampleSubClass() |
| 386 |
ds = {'test_attr_class': esc} |
| 387 |
bsc = self._base_validate(ds) |
| 388 |
self.assertIs(bsc.test_attr_class, esc) |
| 389 |
|
| 390 |
def test_attr_class_wrong_type(self): |
| 391 |
not_a_esc = ExampleSubClass |
| 392 |
ds = {'test_attr_class': not_a_esc} |
| 393 |
bsc = self._base_validate(ds) |
| 394 |
self.assertIs(bsc.test_attr_class, not_a_esc) |
| 395 |
|
| 396 |
def test_attr_class_post_validate(self): |
| 397 |
esc = ExampleSubClass() |
| 398 |
ds = {'test_attr_class_post_validate': esc} |
| 399 |
bsc = self._base_validate(ds) |
| 400 |
self.assertIs(bsc.test_attr_class_post_validate, esc) |
| 401 |
|
| 402 |
def test_attr_class_post_validate_class_not_instance(self): |
| 403 |
not_a_esc = ExampleSubClass |
| 404 |
ds = {'test_attr_class_post_validate': not_a_esc} |
| 405 |
self.assertRaisesRegex(AnsibleParserError, "Error processing keyword 'test_attr_class_post_validate': The value .* could not be converted to 'class'.", |
| 406 |
self._base_validate, ds) |
| 407 |
|
| 408 |
def test_attr_class_post_validate_wrong_class(self): |
| 409 |
not_a_esc = 37 |
| 410 |
ds = {'test_attr_class_post_validate': not_a_esc} |
| 411 |
self.assertRaisesRegex(AnsibleParserError, "Error processing keyword 'test_attr_class_post_validate': The value 37 could not be converted to 'class'", |
| 412 |
self._base_validate, ds) |
| 413 |
|
| 414 |
def test_attr_remote_user(self): |
| 415 |
ds = {'remote_user': 'testuser'} |
| 416 |
bsc = self._base_validate(ds) |
| 417 |
# TODO: attempt to verify we called parent getters etc |
| 418 |
self.assertEqual(bsc.remote_user, 'testuser') |
| 419 |
|
| 420 |
def test_attr_example_undefined(self): |
| 421 |
ds = {'test_attr_example': '{{ some_var_that_shouldnt_exist_to_test_omit }}'} |
| 422 |
exc_regex_str = 'test_attr_example.*has an invalid value, which includes an undefined variable.*some_var_that_shouldnt*' |
| 423 |
self.assertRaises(AnsibleParserError) |
| 424 |
|
| 425 |
def test_subclass_validate_method(self): |
| 426 |
ds = {'test_attr_list': ['string_list_item_1', 'string_list_item_2'], |
| 427 |
'test_attr_example': 'the_test_attr_example_value_string'} |
| 428 |
# Not throwing an exception here is the test |
| 429 |
bsc = self._base_validate(ds) |
| 430 |
self.assertEqual(bsc.test_attr_example, 'the_test_attr_example_value_string') |
| 431 |
|
| 432 |
def test_subclass_validate_method_invalid(self): |
| 433 |
ds = {'test_attr_example': [None]} |
| 434 |
self.assertRaises(ExampleException, self._base_validate, ds) |
| 435 |
|
| 436 |
def test_attr_none(self): |
| 437 |
ds = {'test_attr_none': 'foo'} |
| 438 |
bsc = self._base_validate(ds) |
| 439 |
self.assertEqual(bsc.test_attr_none, None) |
| 440 |
|
| 441 |
def test_attr_string(self): |
| 442 |
the_string_value = "the new test_attr_string_value" |
| 443 |
ds = {'test_attr_string': the_string_value} |
| 444 |
bsc = self._base_validate(ds) |
| 445 |
self.assertEqual(bsc.test_attr_string, the_string_value) |
| 446 |
|
| 447 |
def test_attr_string_invalid_list(self): |
| 448 |
ds = {'test_attr_string': ['The new test_attr_string', 'value, however in a list']} |
| 449 |
self.assertRaises(AnsibleParserError, self._base_validate, ds) |
| 450 |
|
| 451 |
def test_attr_string_required(self): |
| 452 |
the_string_value = "the new test_attr_string_required_value" |
| 453 |
ds = {'test_attr_string_required': the_string_value} |
| 454 |
bsc = self._base_validate(ds) |
| 455 |
self.assertEqual(bsc.test_attr_string_required, the_string_value) |
| 456 |
|
| 457 |
def test_attr_list_invalid(self): |
| 458 |
ds = {'test_attr_list': {}} |
| 459 |
self.assertRaises(AnsibleParserError, self._base_validate, ds) |
| 460 |
|
| 461 |
def test_attr_list(self): |
| 462 |
string_list = ['foo', 'bar'] |
| 463 |
ds = {'test_attr_list': string_list} |
| 464 |
bsc = self._base_validate(ds) |
| 465 |
self.assertEqual(string_list, bsc._test_attr_list) |
| 466 |
|
| 467 |
def test_attr_mixed_list(self): |
| 468 |
mixed_list = ['foo', 1] |
| 469 |
ds = {'test_attr_mixed_list': mixed_list} |
| 470 |
bsc = self._base_validate(ds) |
| 471 |
self.assertEqual(mixed_list, bsc._test_attr_mixed_list) |
| 472 |
|
| 473 |
def test_attr_mixed_list_invalid(self): |
| 474 |
ds = {'test_attr_mixed_list': [['foo'], 1]} |
| 475 |
self.assertRaises(AnsibleParserError, self._base_validate, ds) |
| 476 |
|
| 477 |
def test_attr_list_none(self): |
| 478 |
ds = {'test_attr_list': None} |
| 479 |
bsc = self._base_validate(ds) |
| 480 |
self.assertEqual(None, bsc._test_attr_list) |
| 481 |
|
| 482 |
def test_attr_list_no_listof(self): |
| 483 |
test_list = ['foo', 'bar', 123] |
| 484 |
ds = {'test_attr_list_no_listof': test_list} |
| 485 |
bsc = self._base_validate(ds) |
| 486 |
self.assertEqual(test_list, bsc._test_attr_list_no_listof) |
| 487 |
|
| 488 |
def test_attr_list_required(self): |
| 489 |
string_list = ['foo', 'bar'] |
| 490 |
ds = {'test_attr_list_required': string_list} |
| 491 |
bsc = self.ClassUnderTest() |
| 492 |
bsc.load_data(ds) |
| 493 |
fake_loader = DictDataLoader({}) |
| 494 |
templar = TemplateEngine(loader=fake_loader) |
| 495 |
bsc.post_validate(templar) |
| 496 |
self.assertEqual(string_list, bsc._test_attr_list_required) |
| 497 |
|
| 498 |
def test_attr_list_required_empty_string(self): |
| 499 |
string_list = [""] |
| 500 |
ds = {'test_attr_list_required': string_list} |
| 501 |
bsc = self.ClassUnderTest() |
| 502 |
bsc.load_data(ds) |
| 503 |
fake_loader = DictDataLoader({}) |
| 504 |
templar = TemplateEngine(loader=fake_loader) |
| 505 |
self.assertRaisesRegex(AnsibleParserError, 'cannot have empty values', |
| 506 |
bsc.post_validate, templar) |
| 507 |
|
| 508 |
def test_attr_unknown(self): |
| 509 |
self.assertRaises( |
| 510 |
AnsibleFieldAttributeError, |
| 511 |
self._base_validate, |
| 512 |
{'test_attr_unknown_isa': True} |
| 513 |
) |
| 514 |
|
| 515 |
def test_attr_method(self): |
| 516 |
ds = {'test_attr_method': 'value from the ds'} |
| 517 |
bsc = self._base_validate(ds) |
| 518 |
# The value returned by the subclasses _get_attr_test_attr_method |
| 519 |
self.assertEqual(bsc.test_attr_method, 'foo bar') |
| 520 |
|
| 521 |
def test_attr_method_missing(self): |
| 522 |
a_string = 'The value set from the ds' |
| 523 |
ds = {'test_attr_method_missing': a_string} |
| 524 |
bsc = self._base_validate(ds) |
| 525 |
self.assertEqual(bsc.test_attr_method_missing, a_string) |
| 526 |
|
| 527 |
def test_get_validated_value_string_preserve_tags(self): |
| 528 |
attribute = FieldAttribute(isa='string') |
| 529 |
value = TrustedAsTemplate().tag('bar') |
| 530 |
templar = TemplateEngine(None) |
| 531 |
bsc = self.ClassUnderTest() |
| 532 |
result = bsc.get_validated_value('foo', attribute, value, templar) |
| 533 |
assert TrustedAsTemplate.is_tagged_on(result) |
| 534 |
assert result == 'bar' |