1
0

test_stringutils.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import re
  4. import sys
  5. import textwrap
  6. # Import Salt libs
  7. from tests.support.mock import patch
  8. from tests.support.unit import TestCase, LOREM_IPSUM
  9. import salt.utils.stringutils
  10. # Import 3rd-party libs
  11. from salt.ext import six
  12. from salt.ext.six.moves import builtins, range # pylint: disable=redefined-builtin
  13. UNICODE = '中国語 (繁体)'
  14. STR = BYTES = UNICODE.encode('utf-8')
  15. # This is an example of a unicode string with й constructed using two separate
  16. # code points. Do not modify it.
  17. EGGS = '\u044f\u0438\u0306\u0446\u0430'
  18. LATIN1_UNICODE = 'räksmörgås'
  19. LATIN1_BYTES = LATIN1_UNICODE.encode('latin-1')
  20. DOUBLE_TXT = '''\
  21. # set variable identifying the chroot you work in (used in the prompt below)
  22. if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
  23. debian_chroot=$(cat /etc/debian_chroot)
  24. fi
  25. '''
  26. SINGLE_TXT = '''\
  27. # set variable identifying the chroot you work in (used in the prompt below)
  28. if [ -z '$debian_chroot' ] && [ -r /etc/debian_chroot ]; then
  29. debian_chroot=$(cat /etc/debian_chroot)
  30. fi
  31. '''
  32. SINGLE_DOUBLE_TXT = '''\
  33. # set variable identifying the chroot you work in (used in the prompt below)
  34. if [ -z '$debian_chroot' ] && [ -r /etc/debian_chroot ]; then
  35. debian_chroot=$(cat /etc/debian_chroot)
  36. fi
  37. # set variable identifying the chroot you work in (used in the prompt below)
  38. if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
  39. debian_chroot=$(cat /etc/debian_chroot)
  40. fi
  41. '''
  42. SINGLE_DOUBLE_SAME_LINE_TXT = '''\
  43. # set variable identifying the chroot you work in (used in the prompt below)
  44. if [ -z '$debian_chroot' ] && [ -r "/etc/debian_chroot" ]; then
  45. debian_chroot=$(cat /etc/debian_chroot)
  46. fi
  47. '''
  48. MATCH = '''\
  49. # set variable identifying the chroot you work in (used in the prompt below)
  50. if [ -z '$debian_chroot' ] && [ -r /etc/debian_chroot ]; then
  51. debian_chroot=$(cat /etc/debian_chroot)
  52. fi
  53. # set variable identifying the chroot you work in (used in the prompt below)
  54. if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
  55. debian_chroot=$(cat /etc/debian_chroot)
  56. fi
  57. # set variable identifying the chroot you work in (used in the prompt below)
  58. if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
  59. debian_chroot=$(cat /etc/debian_chroot)
  60. fi
  61. # set variable identifying the chroot you work in (used in the prompt below)
  62. if [ -z '$debian_chroot' ] && [ -r /etc/debian_chroot ]; then
  63. debian_chroot=$(cat /etc/debian_chroot)
  64. fi
  65. # set variable identifying the chroot you work in (used in the prompt below)
  66. if [ -z '$debian_chroot' ] && [ -r "/etc/debian_chroot" ]; then
  67. debian_chroot=$(cat /etc/debian_chroot)
  68. fi
  69. '''
  70. class TestBuildWhitespaceRegex(TestCase):
  71. def test_single_quotes(self):
  72. regex = salt.utils.stringutils.build_whitespace_split_regex(SINGLE_TXT)
  73. self.assertTrue(re.search(regex, MATCH))
  74. def test_double_quotes(self):
  75. regex = salt.utils.stringutils.build_whitespace_split_regex(DOUBLE_TXT)
  76. self.assertTrue(re.search(regex, MATCH))
  77. def test_single_and_double_quotes(self):
  78. regex = salt.utils.stringutils.build_whitespace_split_regex(SINGLE_DOUBLE_TXT)
  79. self.assertTrue(re.search(regex, MATCH))
  80. def test_issue_2227(self):
  81. regex = salt.utils.stringutils.build_whitespace_split_regex(SINGLE_DOUBLE_SAME_LINE_TXT)
  82. self.assertTrue(re.search(regex, MATCH))
  83. class StringutilsTestCase(TestCase):
  84. def test_contains_whitespace(self):
  85. does_contain_whitespace = 'A brown fox jumped over the red hen.'
  86. does_not_contain_whitespace = 'Abrownfoxjumpedovertheredhen.'
  87. self.assertTrue(salt.utils.stringutils.contains_whitespace(does_contain_whitespace))
  88. self.assertFalse(salt.utils.stringutils.contains_whitespace(does_not_contain_whitespace))
  89. def test_to_num(self):
  90. self.assertEqual(7, salt.utils.stringutils.to_num('7'))
  91. self.assertIsInstance(salt.utils.stringutils.to_num('7'), int)
  92. self.assertEqual(7, salt.utils.stringutils.to_num('7.0'))
  93. self.assertIsInstance(salt.utils.stringutils.to_num('7.0'), float)
  94. self.assertEqual(salt.utils.stringutils.to_num('Seven'), 'Seven')
  95. self.assertIsInstance(salt.utils.stringutils.to_num('Seven'), six.text_type)
  96. def test_to_none(self):
  97. self.assertIsNone(salt.utils.stringutils.to_none(''))
  98. self.assertIsNone(salt.utils.stringutils.to_none(' '))
  99. # Ensure that we do not inadvertently convert certain strings or 0 to None
  100. self.assertIsNotNone(salt.utils.stringutils.to_none('None'))
  101. self.assertIsNotNone(salt.utils.stringutils.to_none(0))
  102. def test_is_binary(self):
  103. self.assertFalse(salt.utils.stringutils.is_binary(LOREM_IPSUM))
  104. # Also test bytestring
  105. self.assertFalse(
  106. salt.utils.stringutils.is_binary(
  107. salt.utils.stringutils.is_binary(LOREM_IPSUM)
  108. )
  109. )
  110. zero_str = '{0}{1}'.format(LOREM_IPSUM, '\0')
  111. self.assertTrue(salt.utils.stringutils.is_binary(zero_str))
  112. # Also test bytestring
  113. self.assertTrue(
  114. salt.utils.stringutils.is_binary(
  115. salt.utils.stringutils.to_bytes(zero_str)
  116. )
  117. )
  118. # To to ensure safe exit if str passed doesn't evaluate to True
  119. self.assertFalse(salt.utils.stringutils.is_binary(''))
  120. self.assertFalse(salt.utils.stringutils.is_binary(b''))
  121. nontext = 3 * (''.join([chr(x) for x in range(1, 32) if x not in (8, 9, 10, 12, 13)]))
  122. almost_bin_str = '{0}{1}'.format(LOREM_IPSUM[:100], nontext[:42])
  123. self.assertFalse(salt.utils.stringutils.is_binary(almost_bin_str))
  124. # Also test bytestring
  125. self.assertFalse(
  126. salt.utils.stringutils.is_binary(
  127. salt.utils.stringutils.to_bytes(almost_bin_str)
  128. )
  129. )
  130. bin_str = almost_bin_str + '\x01'
  131. self.assertTrue(salt.utils.stringutils.is_binary(bin_str))
  132. # Also test bytestring
  133. self.assertTrue(
  134. salt.utils.stringutils.is_binary(
  135. salt.utils.stringutils.to_bytes(bin_str)
  136. )
  137. )
  138. def test_to_str(self):
  139. for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None):
  140. self.assertRaises(TypeError, salt.utils.stringutils.to_str, x)
  141. if six.PY3:
  142. self.assertEqual(salt.utils.stringutils.to_str('plugh'), 'plugh')
  143. self.assertEqual(salt.utils.stringutils.to_str('áéíóúý', 'utf-8'), 'áéíóúý')
  144. self.assertEqual(salt.utils.stringutils.to_str(BYTES, 'utf-8'), UNICODE)
  145. self.assertEqual(salt.utils.stringutils.to_str(bytearray(BYTES), 'utf-8'), UNICODE)
  146. # Test situation when a minion returns incorrect utf-8 string because of... million reasons
  147. ut2 = b'\x9c'
  148. self.assertRaises(UnicodeDecodeError, salt.utils.stringutils.to_str, ut2, 'utf-8')
  149. self.assertEqual(salt.utils.stringutils.to_str(ut2, 'utf-8', 'replace'), u'\ufffd')
  150. self.assertRaises(UnicodeDecodeError, salt.utils.stringutils.to_str, bytearray(ut2), 'utf-8')
  151. self.assertEqual(salt.utils.stringutils.to_str(bytearray(ut2), 'utf-8', 'replace'), u'\ufffd')
  152. else:
  153. self.assertEqual(salt.utils.stringutils.to_str('plugh'), str('plugh')) # future lint: disable=blacklisted-function
  154. self.assertEqual(salt.utils.stringutils.to_str('áéíóúý', 'utf-8'), 'áéíóúý'.encode('utf-8'))
  155. self.assertEqual(salt.utils.stringutils.to_str(UNICODE, 'utf-8'), STR)
  156. self.assertEqual(salt.utils.stringutils.to_str(bytearray(STR), 'utf-8'), STR)
  157. # Test utf-8 fallback with Windows default codepage
  158. with patch.object(builtins, '__salt_system_encoding__', 'CP1252'):
  159. self.assertEqual(salt.utils.stringutils.to_str('Ψ'), 'Ψ'.encode('utf-8'))
  160. def test_to_bytes(self):
  161. for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None):
  162. self.assertRaises(TypeError, salt.utils.stringutils.to_bytes, x)
  163. if six.PY3:
  164. self.assertEqual(salt.utils.stringutils.to_bytes('xyzzy'), b'xyzzy')
  165. self.assertEqual(salt.utils.stringutils.to_bytes(BYTES), BYTES)
  166. self.assertEqual(salt.utils.stringutils.to_bytes(bytearray(BYTES)), BYTES)
  167. self.assertEqual(salt.utils.stringutils.to_bytes(UNICODE, 'utf-8'), BYTES)
  168. # Test utf-8 fallback with ascii default encoding
  169. with patch.object(builtins, '__salt_system_encoding__', 'ascii'):
  170. self.assertEqual(salt.utils.stringutils.to_bytes('Ψ'), b'\xce\xa8')
  171. else:
  172. self.assertEqual(salt.utils.stringutils.to_bytes('xyzzy'), 'xyzzy')
  173. self.assertEqual(salt.utils.stringutils.to_bytes(BYTES), BYTES)
  174. self.assertEqual(salt.utils.stringutils.to_bytes(bytearray(BYTES)), BYTES)
  175. self.assertEqual(salt.utils.stringutils.to_bytes(UNICODE, 'utf-8'), BYTES)
  176. def test_to_unicode(self):
  177. self.assertEqual(
  178. salt.utils.stringutils.to_unicode(
  179. EGGS,
  180. normalize=True
  181. ),
  182. 'яйца'
  183. )
  184. self.assertNotEqual(
  185. salt.utils.stringutils.to_unicode(
  186. EGGS,
  187. normalize=False
  188. ),
  189. 'яйца'
  190. )
  191. self.assertEqual(
  192. salt.utils.stringutils.to_unicode(
  193. LATIN1_BYTES, encoding='latin-1'
  194. ),
  195. LATIN1_UNICODE
  196. )
  197. if six.PY3:
  198. self.assertEqual(salt.utils.stringutils.to_unicode('plugh'), 'plugh')
  199. self.assertEqual(salt.utils.stringutils.to_unicode('áéíóúý'), 'áéíóúý')
  200. self.assertEqual(salt.utils.stringutils.to_unicode(BYTES, 'utf-8'), UNICODE)
  201. self.assertEqual(salt.utils.stringutils.to_unicode(bytearray(BYTES), 'utf-8'), UNICODE)
  202. else:
  203. self.assertEqual(salt.utils.stringutils.to_unicode(str('xyzzy'), 'utf-8'), 'xyzzy') # future lint: disable=blacklisted-function
  204. self.assertEqual(salt.utils.stringutils.to_unicode(BYTES, 'utf-8'), UNICODE)
  205. # Test that unicode chars are decoded properly even when using
  206. # locales which are not UTF-8 compatible
  207. with patch.object(builtins, '__salt_system_encoding__', 'ascii'):
  208. self.assertEqual(salt.utils.stringutils.to_unicode('Ψ'.encode('utf-8')), 'Ψ')
  209. with patch.object(builtins, '__salt_system_encoding__', 'CP1252'):
  210. self.assertEqual(salt.utils.stringutils.to_unicode('Ψ'.encode('utf-8')), 'Ψ')
  211. def test_to_unicode_multi_encoding(self):
  212. result = salt.utils.stringutils.to_unicode(LATIN1_BYTES, encoding=('utf-8', 'latin1'))
  213. assert result == LATIN1_UNICODE
  214. def test_build_whitespace_split_regex(self):
  215. # With 3.7+, re.escape only escapes special characters, no longer
  216. # escaping all characters other than ASCII letters, numbers and
  217. # underscores. This includes commas.
  218. if sys.version_info >= (3, 7):
  219. expected_regex = '(?m)^(?:[\\s]+)?Lorem(?:[\\s]+)?ipsum(?:[\\s]+)?dolor(?:[\\s]+)?sit(?:[\\s]+)?amet,' \
  220. '(?:[\\s]+)?$'
  221. else:
  222. expected_regex = '(?m)^(?:[\\s]+)?Lorem(?:[\\s]+)?ipsum(?:[\\s]+)?dolor(?:[\\s]+)?sit(?:[\\s]+)?amet\\,' \
  223. '(?:[\\s]+)?$'
  224. ret = salt.utils.stringutils.build_whitespace_split_regex(' '.join(LOREM_IPSUM.split()[:5]))
  225. self.assertEqual(ret, expected_regex)
  226. def test_get_context(self):
  227. expected_context = textwrap.dedent('''\
  228. ---
  229. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque eget urna a arcu lacinia sagittis.
  230. Sed scelerisque, lacus eget malesuada vestibulum, justo diam facilisis tortor, in sodales dolor
  231. [...]
  232. ---''')
  233. ret = salt.utils.stringutils.get_context(LOREM_IPSUM, 1, num_lines=1)
  234. self.assertEqual(ret, expected_context)
  235. def test_get_context_has_enough_context(self):
  236. template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
  237. context = salt.utils.stringutils.get_context(template, 8)
  238. expected = '---\n[...]\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\n[...]\n---'
  239. self.assertEqual(expected, context)
  240. def test_get_context_at_top_of_file(self):
  241. template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
  242. context = salt.utils.stringutils.get_context(template, 1)
  243. expected = '---\n1\n2\n3\n4\n5\n6\n[...]\n---'
  244. self.assertEqual(expected, context)
  245. def test_get_context_at_bottom_of_file(self):
  246. template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
  247. context = salt.utils.stringutils.get_context(template, 15)
  248. expected = '---\n[...]\na\nb\nc\nd\ne\nf\n---'
  249. self.assertEqual(expected, context)
  250. def test_get_context_2_context_lines(self):
  251. template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
  252. context = salt.utils.stringutils.get_context(template, 8, num_lines=2)
  253. expected = '---\n[...]\n6\n7\n8\n9\na\n[...]\n---'
  254. self.assertEqual(expected, context)
  255. def test_get_context_with_marker(self):
  256. template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
  257. context = salt.utils.stringutils.get_context(template, 8, num_lines=2, marker=' <---')
  258. expected = '---\n[...]\n6\n7\n8 <---\n9\na\n[...]\n---'
  259. self.assertEqual(expected, context)
  260. def test_expr_match(self):
  261. val = 'foo/bar/baz'
  262. # Exact match
  263. self.assertTrue(salt.utils.stringutils.expr_match(val, val))
  264. # Glob match
  265. self.assertTrue(salt.utils.stringutils.expr_match(val, 'foo/*/baz'))
  266. # Glob non-match
  267. self.assertFalse(salt.utils.stringutils.expr_match(val, 'foo/*/bar'))
  268. # Regex match
  269. self.assertTrue(salt.utils.stringutils.expr_match(val, r'foo/\w+/baz'))
  270. # Regex non-match
  271. self.assertFalse(salt.utils.stringutils.expr_match(val, r'foo/\w/baz'))
  272. def test_check_whitelist_blacklist(self):
  273. '''
  274. Ensure that whitelist matching works on both PY2 and PY3
  275. '''
  276. whitelist = ['one/two/three', r'web[0-9]']
  277. blacklist = ['four/five/six', r'web[5-9]']
  278. # Tests with string whitelist/blacklist
  279. self.assertFalse(
  280. salt.utils.stringutils.check_whitelist_blacklist(
  281. 'web_one',
  282. whitelist=whitelist[1],
  283. blacklist=None,
  284. )
  285. )
  286. self.assertFalse(
  287. salt.utils.stringutils.check_whitelist_blacklist(
  288. 'web_one',
  289. whitelist=whitelist[1],
  290. blacklist=[],
  291. )
  292. )
  293. self.assertTrue(
  294. salt.utils.stringutils.check_whitelist_blacklist(
  295. 'web1',
  296. whitelist=whitelist[1],
  297. blacklist=None,
  298. )
  299. )
  300. self.assertTrue(
  301. salt.utils.stringutils.check_whitelist_blacklist(
  302. 'web1',
  303. whitelist=whitelist[1],
  304. blacklist=[],
  305. )
  306. )
  307. self.assertFalse(
  308. salt.utils.stringutils.check_whitelist_blacklist(
  309. 'web5',
  310. whitelist=None,
  311. blacklist=blacklist[1],
  312. )
  313. )
  314. self.assertFalse(
  315. salt.utils.stringutils.check_whitelist_blacklist(
  316. 'web5',
  317. whitelist=[],
  318. blacklist=blacklist[1],
  319. )
  320. )
  321. self.assertTrue(
  322. salt.utils.stringutils.check_whitelist_blacklist(
  323. 'web_five',
  324. whitelist=None,
  325. blacklist=blacklist[1],
  326. )
  327. )
  328. self.assertTrue(
  329. salt.utils.stringutils.check_whitelist_blacklist(
  330. 'web_five',
  331. whitelist=[],
  332. blacklist=blacklist[1],
  333. )
  334. )
  335. self.assertFalse(
  336. salt.utils.stringutils.check_whitelist_blacklist(
  337. 'web5',
  338. whitelist=whitelist[1],
  339. blacklist=blacklist[1],
  340. )
  341. )
  342. self.assertTrue(
  343. salt.utils.stringutils.check_whitelist_blacklist(
  344. 'web4',
  345. whitelist=whitelist[1],
  346. blacklist=blacklist[1],
  347. )
  348. )
  349. # Tests with list whitelist/blacklist
  350. self.assertFalse(
  351. salt.utils.stringutils.check_whitelist_blacklist(
  352. 'web_one',
  353. whitelist=whitelist,
  354. blacklist=None,
  355. )
  356. )
  357. self.assertFalse(
  358. salt.utils.stringutils.check_whitelist_blacklist(
  359. 'web_one',
  360. whitelist=whitelist,
  361. blacklist=[],
  362. )
  363. )
  364. self.assertTrue(
  365. salt.utils.stringutils.check_whitelist_blacklist(
  366. 'web1',
  367. whitelist=whitelist,
  368. blacklist=None,
  369. )
  370. )
  371. self.assertTrue(
  372. salt.utils.stringutils.check_whitelist_blacklist(
  373. 'web1',
  374. whitelist=whitelist,
  375. blacklist=[],
  376. )
  377. )
  378. self.assertFalse(
  379. salt.utils.stringutils.check_whitelist_blacklist(
  380. 'web5',
  381. whitelist=None,
  382. blacklist=blacklist,
  383. )
  384. )
  385. self.assertFalse(
  386. salt.utils.stringutils.check_whitelist_blacklist(
  387. 'web5',
  388. whitelist=[],
  389. blacklist=blacklist,
  390. )
  391. )
  392. self.assertTrue(
  393. salt.utils.stringutils.check_whitelist_blacklist(
  394. 'web_five',
  395. whitelist=None,
  396. blacklist=blacklist,
  397. )
  398. )
  399. self.assertTrue(
  400. salt.utils.stringutils.check_whitelist_blacklist(
  401. 'web_five',
  402. whitelist=[],
  403. blacklist=blacklist,
  404. )
  405. )
  406. self.assertFalse(
  407. salt.utils.stringutils.check_whitelist_blacklist(
  408. 'web5',
  409. whitelist=whitelist,
  410. blacklist=blacklist,
  411. )
  412. )
  413. self.assertTrue(
  414. salt.utils.stringutils.check_whitelist_blacklist(
  415. 'web4',
  416. whitelist=whitelist,
  417. blacklist=blacklist,
  418. )
  419. )
  420. # Tests with set whitelist/blacklist
  421. self.assertFalse(
  422. salt.utils.stringutils.check_whitelist_blacklist(
  423. 'web_one',
  424. whitelist=set(whitelist),
  425. blacklist=None,
  426. )
  427. )
  428. self.assertFalse(
  429. salt.utils.stringutils.check_whitelist_blacklist(
  430. 'web_one',
  431. whitelist=set(whitelist),
  432. blacklist=set(),
  433. )
  434. )
  435. self.assertTrue(
  436. salt.utils.stringutils.check_whitelist_blacklist(
  437. 'web1',
  438. whitelist=set(whitelist),
  439. blacklist=None,
  440. )
  441. )
  442. self.assertTrue(
  443. salt.utils.stringutils.check_whitelist_blacklist(
  444. 'web1',
  445. whitelist=set(whitelist),
  446. blacklist=set(),
  447. )
  448. )
  449. self.assertFalse(
  450. salt.utils.stringutils.check_whitelist_blacklist(
  451. 'web5',
  452. whitelist=None,
  453. blacklist=set(blacklist),
  454. )
  455. )
  456. self.assertFalse(
  457. salt.utils.stringutils.check_whitelist_blacklist(
  458. 'web5',
  459. whitelist=set(),
  460. blacklist=set(blacklist),
  461. )
  462. )
  463. self.assertTrue(
  464. salt.utils.stringutils.check_whitelist_blacklist(
  465. 'web_five',
  466. whitelist=None,
  467. blacklist=set(blacklist),
  468. )
  469. )
  470. self.assertTrue(
  471. salt.utils.stringutils.check_whitelist_blacklist(
  472. 'web_five',
  473. whitelist=set(),
  474. blacklist=set(blacklist),
  475. )
  476. )
  477. self.assertFalse(
  478. salt.utils.stringutils.check_whitelist_blacklist(
  479. 'web5',
  480. whitelist=set(whitelist),
  481. blacklist=set(blacklist),
  482. )
  483. )
  484. self.assertTrue(
  485. salt.utils.stringutils.check_whitelist_blacklist(
  486. 'web4',
  487. whitelist=set(whitelist),
  488. blacklist=set(blacklist),
  489. )
  490. )
  491. # Test with invalid type for whitelist/blacklist
  492. self.assertRaises(
  493. TypeError,
  494. salt.utils.stringutils.check_whitelist_blacklist,
  495. 'foo', whitelist=123
  496. )
  497. self.assertRaises(
  498. TypeError,
  499. salt.utils.stringutils.check_whitelist_blacklist,
  500. 'foo', blacklist=123
  501. )