1
0

test_stringutils.py 21 KB

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