test_find.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. import os
  5. import sys
  6. import shutil
  7. import tempfile
  8. import stat
  9. # Import Salt Testing libs
  10. from tests.support.unit import skipIf, TestCase
  11. from tests.support.paths import TMP
  12. # Import salt libs
  13. import salt.utils.files
  14. import salt.utils.find
  15. # Import 3rd-party libs
  16. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  17. from salt.ext import six
  18. class TestFind(TestCase):
  19. def test_parse_interval(self):
  20. self.assertRaises(ValueError, salt.utils.find._parse_interval, 'w')
  21. self.assertRaises(ValueError, salt.utils.find._parse_interval, '1')
  22. self.assertRaises(ValueError, salt.utils.find._parse_interval, '1s1w')
  23. self.assertRaises(ValueError, salt.utils.find._parse_interval, '1s1s')
  24. result, resolution, modifier = salt.utils.find._parse_interval('')
  25. self.assertEqual(result, 0)
  26. self.assertIs(resolution, None)
  27. self.assertEqual(modifier, '')
  28. result, resolution, modifier = salt.utils.find._parse_interval('1s')
  29. self.assertEqual(result, 1.0)
  30. self.assertEqual(resolution, 1)
  31. self.assertEqual(modifier, '')
  32. result, resolution, modifier = salt.utils.find._parse_interval('1m')
  33. self.assertEqual(result, 60.0)
  34. self.assertEqual(resolution, 60)
  35. self.assertEqual(modifier, '')
  36. result, resolution, modifier = salt.utils.find._parse_interval('1h')
  37. self.assertEqual(result, 3600.0)
  38. self.assertEqual(resolution, 3600)
  39. self.assertEqual(modifier, '')
  40. result, resolution, modifier = salt.utils.find._parse_interval('1d')
  41. self.assertEqual(result, 86400.0)
  42. self.assertEqual(resolution, 86400)
  43. self.assertEqual(modifier, '')
  44. result, resolution, modifier = salt.utils.find._parse_interval('1w')
  45. self.assertEqual(result, 604800.0)
  46. self.assertEqual(resolution, 604800)
  47. self.assertEqual(modifier, '')
  48. result, resolution, modifier = salt.utils.find._parse_interval('1w3d6h')
  49. self.assertEqual(result, 885600.0)
  50. self.assertEqual(resolution, 3600)
  51. self.assertEqual(modifier, '')
  52. result, resolution, modifier = salt.utils.find._parse_interval('1m1s')
  53. self.assertEqual(result, 61.0)
  54. self.assertEqual(resolution, 1)
  55. self.assertEqual(modifier, '')
  56. result, resolution, modifier = salt.utils.find._parse_interval('1m2s')
  57. self.assertEqual(result, 62.0)
  58. self.assertEqual(resolution, 1)
  59. self.assertEqual(modifier, '')
  60. result, resolution, modifier = salt.utils.find._parse_interval('+1d')
  61. self.assertEqual(result, 86400.0)
  62. self.assertEqual(resolution, 86400)
  63. self.assertEqual(modifier, '+')
  64. result, resolution, modifier = salt.utils.find._parse_interval('-1d')
  65. self.assertEqual(result, 86400.0)
  66. self.assertEqual(resolution, 86400)
  67. self.assertEqual(modifier, '-')
  68. def test_parse_size(self):
  69. self.assertRaises(ValueError, salt.utils.find._parse_size, '')
  70. self.assertRaises(ValueError, salt.utils.find._parse_size, '1s1s')
  71. min_size, max_size = salt.utils.find._parse_size('1')
  72. self.assertEqual(min_size, 1)
  73. self.assertEqual(max_size, 1)
  74. min_size, max_size = salt.utils.find._parse_size('1b')
  75. self.assertEqual(min_size, 1)
  76. self.assertEqual(max_size, 1)
  77. min_size, max_size = salt.utils.find._parse_size('1k')
  78. self.assertEqual(min_size, 1024)
  79. self.assertEqual(max_size, 2047)
  80. min_size, max_size = salt.utils.find._parse_size('1m')
  81. self.assertEqual(min_size, 1048576)
  82. self.assertEqual(max_size, 2097151)
  83. min_size, max_size = salt.utils.find._parse_size('1g')
  84. self.assertEqual(min_size, 1073741824)
  85. self.assertEqual(max_size, 2147483647)
  86. min_size, max_size = salt.utils.find._parse_size('1t')
  87. self.assertEqual(min_size, 1099511627776)
  88. self.assertEqual(max_size, 2199023255551)
  89. min_size, max_size = salt.utils.find._parse_size('0m')
  90. self.assertEqual(min_size, 0)
  91. self.assertEqual(max_size, 1048575)
  92. min_size, max_size = salt.utils.find._parse_size('-1m')
  93. self.assertEqual(min_size, 0)
  94. self.assertEqual(max_size, 1048576)
  95. min_size, max_size = salt.utils.find._parse_size('+1m')
  96. self.assertEqual(min_size, 1048576)
  97. self.assertEqual(max_size, sys.maxsize)
  98. min_size, max_size = salt.utils.find._parse_size('+1M')
  99. self.assertEqual(min_size, 1048576)
  100. self.assertEqual(max_size, sys.maxsize)
  101. def test_option_requires(self):
  102. option = salt.utils.find.Option()
  103. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_PATH)
  104. def test_name_option_match(self):
  105. option = salt.utils.find.NameOption('name', '*.txt')
  106. self.assertIs(option.match('', '', ''), None)
  107. self.assertIs(option.match('', 'hello.txt', '').group(), 'hello.txt')
  108. self.assertIs(option.match('', 'HELLO.TXT', ''), None)
  109. def test_iname_option_match(self):
  110. option = salt.utils.find.InameOption('name', '*.txt')
  111. self.assertIs(option.match('', '', ''), None)
  112. self.assertIs(option.match('', 'hello.txt', '').group(), 'hello.txt')
  113. self.assertIs(option.match('', 'HELLO.TXT', '').group(), 'HELLO.TXT')
  114. def test_regex_option_match(self):
  115. self.assertRaises(
  116. ValueError, salt.utils.find.RegexOption, 'name', '(.*}'
  117. )
  118. option = salt.utils.find.RegexOption('name', r'.*\.txt')
  119. self.assertIs(option.match('', '', ''), None)
  120. self.assertIs(option.match('', 'hello.txt', '').group(), 'hello.txt')
  121. self.assertIs(option.match('', 'HELLO.TXT', ''), None)
  122. def test_iregex_option_match(self):
  123. self.assertRaises(
  124. ValueError, salt.utils.find.IregexOption, 'name', '(.*}'
  125. )
  126. option = salt.utils.find.IregexOption('name', r'.*\.txt')
  127. self.assertIs(option.match('', '', ''), None)
  128. self.assertIs(option.match('', 'hello.txt', '').group(), 'hello.txt')
  129. self.assertIs(option.match('', 'HELLO.TXT', '').group(), 'HELLO.TXT')
  130. def test_type_option_requires(self):
  131. self.assertRaises(ValueError, salt.utils.find.TypeOption, 'type', 'w')
  132. option = salt.utils.find.TypeOption('type', 'd')
  133. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_STAT)
  134. def test_type_option_match(self):
  135. option = salt.utils.find.TypeOption('type', 'b')
  136. self.assertEqual(option.match('', '', [stat.S_IFREG]), False)
  137. option = salt.utils.find.TypeOption('type', 'c')
  138. self.assertEqual(option.match('', '', [stat.S_IFREG]), False)
  139. option = salt.utils.find.TypeOption('type', 'd')
  140. self.assertEqual(option.match('', '', [stat.S_IFREG]), False)
  141. option = salt.utils.find.TypeOption('type', 'f')
  142. self.assertEqual(option.match('', '', [stat.S_IFREG]), True)
  143. option = salt.utils.find.TypeOption('type', 'l')
  144. self.assertEqual(option.match('', '', [stat.S_IFREG]), False)
  145. option = salt.utils.find.TypeOption('type', 'p')
  146. self.assertEqual(option.match('', '', [stat.S_IFREG]), False)
  147. option = salt.utils.find.TypeOption('type', 's')
  148. self.assertEqual(option.match('', '', [stat.S_IFREG]), False)
  149. option = salt.utils.find.TypeOption('type', 'b')
  150. self.assertEqual(option.match('', '', [stat.S_IFBLK]), True)
  151. option = salt.utils.find.TypeOption('type', 'c')
  152. self.assertEqual(option.match('', '', [stat.S_IFCHR]), True)
  153. option = salt.utils.find.TypeOption('type', 'd')
  154. self.assertEqual(option.match('', '', [stat.S_IFDIR]), True)
  155. option = salt.utils.find.TypeOption('type', 'l')
  156. self.assertEqual(option.match('', '', [stat.S_IFLNK]), True)
  157. option = salt.utils.find.TypeOption('type', 'p')
  158. self.assertEqual(option.match('', '', [stat.S_IFIFO]), True)
  159. option = salt.utils.find.TypeOption('type', 's')
  160. self.assertEqual(option.match('', '', [stat.S_IFSOCK]), True)
  161. @skipIf(sys.platform.startswith('win'), 'pwd not available on Windows')
  162. def test_owner_option_requires(self):
  163. self.assertRaises(
  164. ValueError, salt.utils.find.OwnerOption, 'owner', 'notexist'
  165. )
  166. option = salt.utils.find.OwnerOption('owner', 'root')
  167. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_STAT)
  168. @skipIf(sys.platform.startswith('win'), 'pwd not available on Windows')
  169. def test_owner_option_match(self):
  170. option = salt.utils.find.OwnerOption('owner', 'root')
  171. self.assertEqual(option.match('', '', [0] * 5), True)
  172. option = salt.utils.find.OwnerOption('owner', '500')
  173. self.assertEqual(option.match('', '', [500] * 5), True)
  174. @skipIf(sys.platform.startswith('win'), 'grp not available on Windows')
  175. def test_group_option_requires(self):
  176. self.assertRaises(
  177. ValueError, salt.utils.find.GroupOption, 'group', 'notexist'
  178. )
  179. if sys.platform.startswith(('darwin', 'freebsd', 'openbsd')):
  180. group_name = 'wheel'
  181. else:
  182. group_name = 'root'
  183. option = salt.utils.find.GroupOption('group', group_name)
  184. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_STAT)
  185. @skipIf(sys.platform.startswith('win'), 'grp not available on Windows')
  186. def test_group_option_match(self):
  187. if sys.platform.startswith(('darwin', 'freebsd', 'openbsd')):
  188. group_name = 'wheel'
  189. else:
  190. group_name = 'root'
  191. option = salt.utils.find.GroupOption('group', group_name)
  192. self.assertEqual(option.match('', '', [0] * 6), True)
  193. option = salt.utils.find.GroupOption('group', '500')
  194. self.assertEqual(option.match('', '', [500] * 6), True)
  195. def test_size_option_requires(self):
  196. self.assertRaises(
  197. ValueError, salt.utils.find.SizeOption, 'size', '1s1s'
  198. )
  199. option = salt.utils.find.SizeOption('size', '+1G')
  200. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_STAT)
  201. def test_size_option_match(self):
  202. option = salt.utils.find.SizeOption('size', '+1k')
  203. self.assertEqual(option.match('', '', [10000] * 7), True)
  204. option = salt.utils.find.SizeOption('size', '+1G')
  205. self.assertEqual(option.match('', '', [10000] * 7), False)
  206. def test_mtime_option_requires(self):
  207. self.assertRaises(
  208. ValueError, salt.utils.find.MtimeOption, 'mtime', '4g'
  209. )
  210. option = salt.utils.find.MtimeOption('mtime', '1d')
  211. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_STAT)
  212. def test_mtime_option_match(self):
  213. option = salt.utils.find.MtimeOption('mtime', '-1w')
  214. self.assertEqual(option.match('', '', [1] * 9), False)
  215. option = salt.utils.find.MtimeOption('mtime', '-1s')
  216. self.assertEqual(option.match('', '', [10 ** 10] * 9), True)
  217. class TestGrepOption(TestCase):
  218. def setUp(self):
  219. super(TestGrepOption, self).setUp()
  220. self.tmpdir = tempfile.mkdtemp(dir=TMP)
  221. def tearDown(self):
  222. shutil.rmtree(self.tmpdir)
  223. super(TestGrepOption, self).tearDown()
  224. def test_grep_option_requires(self):
  225. self.assertRaises(
  226. ValueError, salt.utils.find.GrepOption, 'grep', '(foo)|(bar}'
  227. )
  228. option = salt.utils.find.GrepOption('grep', '(foo)|(bar)')
  229. find = salt.utils.find
  230. self.assertEqual(
  231. option.requires(), (find._REQUIRES_CONTENTS | find._REQUIRES_STAT)
  232. )
  233. def test_grep_option_match_regular_file(self):
  234. hello_file = os.path.join(self.tmpdir, 'hello.txt')
  235. with salt.utils.files.fopen(hello_file, 'w') as fp_:
  236. fp_.write(salt.utils.stringutils.to_str('foo'))
  237. option = salt.utils.find.GrepOption('grep', 'foo')
  238. self.assertEqual(
  239. option.match(self.tmpdir, 'hello.txt', os.stat(hello_file)),
  240. hello_file
  241. )
  242. option = salt.utils.find.GrepOption('grep', 'bar')
  243. self.assertEqual(
  244. option.match(self.tmpdir, 'hello.txt', os.stat(hello_file)),
  245. None
  246. )
  247. @skipIf(sys.platform.startswith('win'), 'No /dev/null on Windows')
  248. def test_grep_option_match_dev_null(self):
  249. option = salt.utils.find.GrepOption('grep', 'foo')
  250. self.assertEqual(
  251. option.match('dev', 'null', os.stat('/dev/null')), None
  252. )
  253. class TestPrintOption(TestCase):
  254. def setUp(self):
  255. super(TestPrintOption, self).setUp()
  256. self.tmpdir = tempfile.mkdtemp(dir=TMP)
  257. def tearDown(self):
  258. shutil.rmtree(self.tmpdir)
  259. super(TestPrintOption, self).tearDown()
  260. def test_print_option_defaults(self):
  261. option = salt.utils.find.PrintOption('print', '')
  262. self.assertEqual(option.need_stat, False)
  263. self.assertEqual(option.print_title, False)
  264. self.assertEqual(option.fmt, ['path'])
  265. def test_print_option_requires(self):
  266. option = salt.utils.find.PrintOption('print', '')
  267. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_PATH)
  268. option = salt.utils.find.PrintOption('print', 'name')
  269. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_PATH)
  270. option = salt.utils.find.PrintOption('print', 'path')
  271. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_PATH)
  272. option = salt.utils.find.PrintOption('print', 'name,path')
  273. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_PATH)
  274. option = salt.utils.find.PrintOption('print', 'user')
  275. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_STAT)
  276. option = salt.utils.find.PrintOption('print', 'path user')
  277. self.assertEqual(option.requires(), salt.utils.find._REQUIRES_STAT)
  278. def test_print_option_execute(self):
  279. hello_file = os.path.join(self.tmpdir, 'hello.txt')
  280. with salt.utils.files.fopen(hello_file, 'w') as fp_:
  281. fp_.write(salt.utils.stringutils.to_str('foo'))
  282. option = salt.utils.find.PrintOption('print', '')
  283. self.assertEqual(option.execute('', [0] * 9), '')
  284. option = salt.utils.find.PrintOption('print', 'path')
  285. self.assertEqual(option.execute('test_name', [0] * 9), 'test_name')
  286. option = salt.utils.find.PrintOption('print', 'name')
  287. self.assertEqual(option.execute('test_name', [0] * 9), 'test_name')
  288. option = salt.utils.find.PrintOption('print', 'size')
  289. self.assertEqual(option.execute(hello_file, os.stat(hello_file)), 3)
  290. option = salt.utils.find.PrintOption('print', 'type')
  291. self.assertEqual(option.execute(hello_file, os.stat(hello_file)), 'f')
  292. option = salt.utils.find.PrintOption('print', 'mode')
  293. self.assertEqual(option.execute(hello_file, range(10)), 0)
  294. option = salt.utils.find.PrintOption('print', 'mtime')
  295. self.assertEqual(option.execute(hello_file, range(10)), 8)
  296. option = salt.utils.find.PrintOption('print', 'md5')
  297. self.assertEqual(
  298. option.execute(hello_file, os.stat(hello_file)),
  299. 'acbd18db4cc2f85cedef654fccc4a4d8'
  300. )
  301. option = salt.utils.find.PrintOption('print', 'path name')
  302. self.assertEqual(
  303. option.execute('test_name', [0] * 9), ['test_name', 'test_name']
  304. )
  305. option = salt.utils.find.PrintOption('print', 'size name')
  306. self.assertEqual(
  307. option.execute('test_name', [0] * 9), [0, 'test_name']
  308. )
  309. @skipIf(sys.platform.startswith('win'), "pwd not available on Windows")
  310. def test_print_user(self):
  311. option = salt.utils.find.PrintOption('print', 'user')
  312. self.assertEqual(option.execute('', [0] * 10), 'root')
  313. option = salt.utils.find.PrintOption('print', 'user')
  314. self.assertEqual(option.execute('', [2 ** 31] * 10), 2 ** 31)
  315. @skipIf(sys.platform.startswith('win'), "grp not available on Windows")
  316. def test_print_group(self):
  317. option = salt.utils.find.PrintOption('print', 'group')
  318. if sys.platform.startswith(('darwin', 'freebsd', 'openbsd')):
  319. group_name = 'wheel'
  320. else:
  321. group_name = 'root'
  322. self.assertEqual(option.execute('', [0] * 10), group_name)
  323. # This seems to be not working in Ubuntu 12.04 32 bit
  324. #option = salt.utils.find.PrintOption('print', 'group')
  325. #self.assertEqual(option.execute('', [2 ** 31] * 10), 2 ** 31)
  326. @skipIf(sys.platform.startswith('win'), "no /dev/null on windows")
  327. def test_print_md5(self):
  328. option = salt.utils.find.PrintOption('print', 'md5')
  329. self.assertEqual(option.execute('/dev/null', os.stat('/dev/null')), '')
  330. class TestFinder(TestCase):
  331. def setUp(self):
  332. super(TestFinder, self).setUp()
  333. self.tmpdir = tempfile.mkdtemp(dir=TMP)
  334. def tearDown(self):
  335. shutil.rmtree(self.tmpdir)
  336. super(TestFinder, self).tearDown()
  337. @skipIf(sys.platform.startswith('win'), 'No /dev/null on Windows')
  338. def test_init(self):
  339. finder = salt.utils.find.Finder({})
  340. self.assertEqual(
  341. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  342. )
  343. self.assertEqual(finder.criteria, [])
  344. finder = salt.utils.find.Finder({'_': None})
  345. self.assertEqual(
  346. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  347. )
  348. self.assertEqual(finder.criteria, [])
  349. self.assertRaises(ValueError, salt.utils.find.Finder, {'': None})
  350. self.assertRaises(ValueError, salt.utils.find.Finder, {'name': None})
  351. self.assertRaises(
  352. ValueError, salt.utils.find.Finder, {'nonexist': 'somevalue'}
  353. )
  354. finder = salt.utils.find.Finder({'name': 'test_name'})
  355. self.assertEqual(
  356. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  357. )
  358. self.assertEqual(
  359. six.text_type(finder.criteria[0].__class__)[-12:-2], 'NameOption'
  360. )
  361. finder = salt.utils.find.Finder({'iname': 'test_name'})
  362. self.assertEqual(
  363. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  364. )
  365. self.assertEqual(
  366. six.text_type(finder.criteria[0].__class__)[-13:-2], 'InameOption'
  367. )
  368. finder = salt.utils.find.Finder({'regex': r'.*\.txt'})
  369. self.assertEqual(
  370. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  371. )
  372. self.assertEqual(
  373. six.text_type(finder.criteria[0].__class__)[-13:-2], 'RegexOption'
  374. )
  375. finder = salt.utils.find.Finder({'iregex': r'.*\.txt'})
  376. self.assertEqual(
  377. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  378. )
  379. self.assertEqual(
  380. six.text_type(finder.criteria[0].__class__)[-14:-2], 'IregexOption'
  381. )
  382. finder = salt.utils.find.Finder({'type': 'd'})
  383. self.assertEqual(
  384. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  385. )
  386. self.assertEqual(
  387. six.text_type(finder.criteria[0].__class__)[-12:-2], 'TypeOption'
  388. )
  389. finder = salt.utils.find.Finder({'owner': 'root'})
  390. self.assertEqual(
  391. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  392. )
  393. self.assertEqual(
  394. six.text_type(finder.criteria[0].__class__)[-13:-2], 'OwnerOption'
  395. )
  396. if sys.platform.startswith(('darwin', 'freebsd', 'openbsd')):
  397. group_name = 'wheel'
  398. else:
  399. group_name = 'root'
  400. finder = salt.utils.find.Finder({'group': group_name})
  401. self.assertEqual(
  402. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  403. )
  404. self.assertEqual(
  405. six.text_type(finder.criteria[0].__class__)[-13:-2], 'GroupOption'
  406. )
  407. finder = salt.utils.find.Finder({'size': '+1G'})
  408. self.assertEqual(
  409. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  410. )
  411. self.assertEqual(
  412. six.text_type(finder.criteria[0].__class__)[-12:-2], 'SizeOption'
  413. )
  414. finder = salt.utils.find.Finder({'mtime': '1d'})
  415. self.assertEqual(
  416. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  417. )
  418. self.assertEqual(
  419. six.text_type(finder.criteria[0].__class__)[-13:-2], 'MtimeOption'
  420. )
  421. finder = salt.utils.find.Finder({'grep': 'foo'})
  422. self.assertEqual(
  423. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  424. )
  425. self.assertEqual(
  426. six.text_type(finder.criteria[0].__class__)[-12:-2], 'GrepOption'
  427. )
  428. finder = salt.utils.find.Finder({'print': 'name'})
  429. self.assertEqual(
  430. six.text_type(finder.actions[0].__class__)[-13:-2], 'PrintOption'
  431. )
  432. self.assertEqual(finder.criteria, [])
  433. def test_find(self):
  434. hello_file = os.path.join(self.tmpdir, 'hello.txt')
  435. with salt.utils.files.fopen(hello_file, 'w') as fp_:
  436. fp_.write(salt.utils.stringutils.to_str('foo'))
  437. finder = salt.utils.find.Finder({})
  438. self.assertEqual(list(finder.find(self.tmpdir)), [self.tmpdir, hello_file])
  439. finder = salt.utils.find.Finder({'mindepth': 1})
  440. self.assertEqual(list(finder.find(self.tmpdir)), [hello_file])
  441. finder = salt.utils.find.Finder({'maxdepth': 0})
  442. self.assertEqual(list(finder.find(self.tmpdir)), [self.tmpdir])
  443. finder = salt.utils.find.Finder({'name': 'hello.txt'})
  444. self.assertEqual(list(finder.find(self.tmpdir)), [hello_file])
  445. finder = salt.utils.find.Finder({'type': 'f', 'print': 'path'})
  446. self.assertEqual(list(finder.find(self.tmpdir)), [hello_file])
  447. finder = salt.utils.find.Finder({'size': '+1G', 'print': 'path'})
  448. self.assertEqual(list(finder.find(self.tmpdir)), [])
  449. finder = salt.utils.find.Finder(
  450. {'name': 'hello.txt', 'print': 'path name'}
  451. )
  452. self.assertEqual(
  453. list(finder.find(self.tmpdir)), [[hello_file, 'hello.txt']]
  454. )
  455. finder = salt.utils.find.Finder({'name': 'test_name'})
  456. self.assertEqual(list(finder.find('')), [])