test_platforms.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. from __future__ import absolute_import
  2. import errno
  3. import os
  4. import sys
  5. import signal
  6. from celery import _find_option_with_arg
  7. from celery import platforms
  8. from celery.five import open_fqdn
  9. from celery.platforms import (
  10. get_fdmax,
  11. ignore_errno,
  12. set_process_title,
  13. signals,
  14. maybe_drop_privileges,
  15. setuid,
  16. setgid,
  17. initgroups,
  18. parse_uid,
  19. parse_gid,
  20. detached,
  21. DaemonContext,
  22. create_pidlock,
  23. Pidfile,
  24. LockFailed,
  25. setgroups,
  26. _setgroups_hack,
  27. close_open_fds,
  28. )
  29. try:
  30. import resource
  31. except ImportError: # pragma: no cover
  32. resource = None # noqa
  33. from celery.tests.case import (
  34. Case, WhateverIO, Mock, SkipTest,
  35. call, override_stdouts, mock_open, patch,
  36. )
  37. class test_find_option_with_arg(Case):
  38. def test_long_opt(self):
  39. self.assertEqual(
  40. _find_option_with_arg(['--foo=bar'], long_opts=['--foo']),
  41. 'bar'
  42. )
  43. def test_short_opt(self):
  44. self.assertEqual(
  45. _find_option_with_arg(['-f', 'bar'], short_opts=['-f']),
  46. 'bar'
  47. )
  48. class test_close_open_fds(Case):
  49. def test_closes(self):
  50. with patch('os.close') as _close:
  51. with patch('os.closerange', create=True) as closerange:
  52. with patch('celery.platforms.get_fdmax') as fdmax:
  53. fdmax.return_value = 3
  54. close_open_fds()
  55. if not closerange.called:
  56. _close.assert_has_calls([call(2), call(1), call(0)])
  57. _close.side_effect = OSError()
  58. _close.side_effect.errno = errno.EBADF
  59. close_open_fds()
  60. class test_ignore_errno(Case):
  61. def test_raises_EBADF(self):
  62. with ignore_errno('EBADF'):
  63. exc = OSError()
  64. exc.errno = errno.EBADF
  65. raise exc
  66. def test_otherwise(self):
  67. with self.assertRaises(OSError):
  68. with ignore_errno('EBADF'):
  69. exc = OSError()
  70. exc.errno = errno.ENOENT
  71. raise exc
  72. class test_set_process_title(Case):
  73. def when_no_setps(self):
  74. prev = platforms._setproctitle = platforms._setproctitle, None
  75. try:
  76. set_process_title('foo')
  77. finally:
  78. platforms._setproctitle = prev
  79. class test_Signals(Case):
  80. @patch('signal.getsignal')
  81. def test_getitem(self, getsignal):
  82. signals['SIGINT']
  83. getsignal.assert_called_with(signal.SIGINT)
  84. def test_supported(self):
  85. self.assertTrue(signals.supported('INT'))
  86. self.assertFalse(signals.supported('SIGIMAGINARY'))
  87. def test_reset_alarm(self):
  88. if sys.platform == 'win32':
  89. raise SkipTest('signal.alarm not available on Windows')
  90. with patch('signal.alarm') as _alarm:
  91. signals.reset_alarm()
  92. _alarm.assert_called_with(0)
  93. def test_arm_alarm(self):
  94. if hasattr(signal, 'setitimer'):
  95. with patch('signal.setitimer', create=True) as seti:
  96. signals.arm_alarm(30)
  97. self.assertTrue(seti.called)
  98. def test_signum(self):
  99. self.assertEqual(signals.signum(13), 13)
  100. self.assertEqual(signals.signum('INT'), signal.SIGINT)
  101. self.assertEqual(signals.signum('SIGINT'), signal.SIGINT)
  102. with self.assertRaises(TypeError):
  103. signals.signum('int')
  104. signals.signum(object())
  105. @patch('signal.signal')
  106. def test_ignore(self, set):
  107. signals.ignore('SIGINT')
  108. set.assert_called_with(signals.signum('INT'), signals.ignored)
  109. signals.ignore('SIGTERM')
  110. set.assert_called_with(signals.signum('TERM'), signals.ignored)
  111. @patch('signal.signal')
  112. def test_setitem(self, set):
  113. handle = lambda *a: a
  114. signals['INT'] = handle
  115. set.assert_called_with(signal.SIGINT, handle)
  116. @patch('signal.signal')
  117. def test_setitem_raises(self, set):
  118. set.side_effect = ValueError()
  119. signals['INT'] = lambda *a: a
  120. if not platforms.IS_WINDOWS:
  121. class test_get_fdmax(Case):
  122. @patch('resource.getrlimit')
  123. def test_when_infinity(self, getrlimit):
  124. with patch('os.sysconf') as sysconfig:
  125. sysconfig.side_effect = KeyError()
  126. getrlimit.return_value = [None, resource.RLIM_INFINITY]
  127. default = object()
  128. self.assertIs(get_fdmax(default), default)
  129. @patch('resource.getrlimit')
  130. def test_when_actual(self, getrlimit):
  131. with patch('os.sysconf') as sysconfig:
  132. sysconfig.side_effect = KeyError()
  133. getrlimit.return_value = [None, 13]
  134. self.assertEqual(get_fdmax(None), 13)
  135. class test_maybe_drop_privileges(Case):
  136. @patch('celery.platforms.parse_uid')
  137. @patch('pwd.getpwuid')
  138. @patch('celery.platforms.setgid')
  139. @patch('celery.platforms.setuid')
  140. @patch('celery.platforms.initgroups')
  141. def test_with_uid(self, initgroups, setuid, setgid,
  142. getpwuid, parse_uid):
  143. class pw_struct(object):
  144. pw_gid = 50001
  145. def raise_on_second_call(*args, **kwargs):
  146. setuid.side_effect = OSError()
  147. setuid.side_effect.errno = errno.EPERM
  148. setuid.side_effect = raise_on_second_call
  149. getpwuid.return_value = pw_struct()
  150. parse_uid.return_value = 5001
  151. maybe_drop_privileges(uid='user')
  152. parse_uid.assert_called_with('user')
  153. getpwuid.assert_called_with(5001)
  154. setgid.assert_called_with(50001)
  155. initgroups.assert_called_with(5001, 50001)
  156. setuid.assert_has_calls([call(5001), call(0)])
  157. @patch('celery.platforms.parse_uid')
  158. @patch('celery.platforms.parse_gid')
  159. @patch('celery.platforms.setgid')
  160. @patch('celery.platforms.setuid')
  161. @patch('celery.platforms.initgroups')
  162. def test_with_guid(self, initgroups, setuid, setgid,
  163. parse_gid, parse_uid):
  164. def raise_on_second_call(*args, **kwargs):
  165. setuid.side_effect = OSError()
  166. setuid.side_effect.errno = errno.EPERM
  167. setuid.side_effect = raise_on_second_call
  168. parse_uid.return_value = 5001
  169. parse_gid.return_value = 50001
  170. maybe_drop_privileges(uid='user', gid='group')
  171. parse_uid.assert_called_with('user')
  172. parse_gid.assert_called_with('group')
  173. setgid.assert_called_with(50001)
  174. initgroups.assert_called_with(5001, 50001)
  175. setuid.assert_has_calls([call(5001), call(0)])
  176. setuid.side_effect = None
  177. with self.assertRaises(RuntimeError):
  178. maybe_drop_privileges(uid='user', gid='group')
  179. setuid.side_effect = OSError()
  180. setuid.side_effect.errno = errno.EINVAL
  181. with self.assertRaises(OSError):
  182. maybe_drop_privileges(uid='user', gid='group')
  183. @patch('celery.platforms.setuid')
  184. @patch('celery.platforms.setgid')
  185. @patch('celery.platforms.parse_gid')
  186. def test_only_gid(self, parse_gid, setgid, setuid):
  187. parse_gid.return_value = 50001
  188. maybe_drop_privileges(gid='group')
  189. parse_gid.assert_called_with('group')
  190. setgid.assert_called_with(50001)
  191. self.assertFalse(setuid.called)
  192. class test_setget_uid_gid(Case):
  193. @patch('celery.platforms.parse_uid')
  194. @patch('os.setuid')
  195. def test_setuid(self, _setuid, parse_uid):
  196. parse_uid.return_value = 5001
  197. setuid('user')
  198. parse_uid.assert_called_with('user')
  199. _setuid.assert_called_with(5001)
  200. @patch('celery.platforms.parse_gid')
  201. @patch('os.setgid')
  202. def test_setgid(self, _setgid, parse_gid):
  203. parse_gid.return_value = 50001
  204. setgid('group')
  205. parse_gid.assert_called_with('group')
  206. _setgid.assert_called_with(50001)
  207. def test_parse_uid_when_int(self):
  208. self.assertEqual(parse_uid(5001), 5001)
  209. @patch('pwd.getpwnam')
  210. def test_parse_uid_when_existing_name(self, getpwnam):
  211. class pwent(object):
  212. pw_uid = 5001
  213. getpwnam.return_value = pwent()
  214. self.assertEqual(parse_uid('user'), 5001)
  215. @patch('pwd.getpwnam')
  216. def test_parse_uid_when_nonexisting_name(self, getpwnam):
  217. getpwnam.side_effect = KeyError('user')
  218. with self.assertRaises(KeyError):
  219. parse_uid('user')
  220. def test_parse_gid_when_int(self):
  221. self.assertEqual(parse_gid(50001), 50001)
  222. @patch('grp.getgrnam')
  223. def test_parse_gid_when_existing_name(self, getgrnam):
  224. class grent(object):
  225. gr_gid = 50001
  226. getgrnam.return_value = grent()
  227. self.assertEqual(parse_gid('group'), 50001)
  228. @patch('grp.getgrnam')
  229. def test_parse_gid_when_nonexisting_name(self, getgrnam):
  230. getgrnam.side_effect = KeyError('group')
  231. with self.assertRaises(KeyError):
  232. parse_gid('group')
  233. class test_initgroups(Case):
  234. @patch('pwd.getpwuid')
  235. @patch('os.initgroups', create=True)
  236. def test_with_initgroups(self, initgroups_, getpwuid):
  237. getpwuid.return_value = ['user']
  238. initgroups(5001, 50001)
  239. initgroups_.assert_called_with('user', 50001)
  240. @patch('celery.platforms.setgroups')
  241. @patch('grp.getgrall')
  242. @patch('pwd.getpwuid')
  243. def test_without_initgroups(self, getpwuid, getgrall, setgroups):
  244. prev = getattr(os, 'initgroups', None)
  245. try:
  246. delattr(os, 'initgroups')
  247. except AttributeError:
  248. pass
  249. try:
  250. getpwuid.return_value = ['user']
  251. class grent(object):
  252. gr_mem = ['user']
  253. def __init__(self, gid):
  254. self.gr_gid = gid
  255. getgrall.return_value = [grent(1), grent(2), grent(3)]
  256. initgroups(5001, 50001)
  257. setgroups.assert_called_with([1, 2, 3])
  258. finally:
  259. if prev:
  260. os.initgroups = prev
  261. class test_detached(Case):
  262. def test_without_resource(self):
  263. prev, platforms.resource = platforms.resource, None
  264. try:
  265. with self.assertRaises(RuntimeError):
  266. detached()
  267. finally:
  268. platforms.resource = prev
  269. @patch('celery.platforms._create_pidlock')
  270. @patch('celery.platforms.signals')
  271. @patch('celery.platforms.maybe_drop_privileges')
  272. @patch('os.geteuid')
  273. @patch(open_fqdn)
  274. def test_default(self, open, geteuid, maybe_drop,
  275. signals, pidlock):
  276. geteuid.return_value = 0
  277. context = detached(uid='user', gid='group')
  278. self.assertIsInstance(context, DaemonContext)
  279. signals.reset.assert_called_with('SIGCLD')
  280. maybe_drop.assert_called_with(uid='user', gid='group')
  281. open.return_value = Mock()
  282. geteuid.return_value = 5001
  283. context = detached(uid='user', gid='group', logfile='/foo/bar')
  284. self.assertIsInstance(context, DaemonContext)
  285. self.assertTrue(context.after_chdir)
  286. context.after_chdir()
  287. open.assert_called_with('/foo/bar', 'a')
  288. open.return_value.close.assert_called_with()
  289. context = detached(pidfile='/foo/bar/pid')
  290. self.assertIsInstance(context, DaemonContext)
  291. self.assertTrue(context.after_chdir)
  292. context.after_chdir()
  293. pidlock.assert_called_with('/foo/bar/pid')
  294. class test_DaemonContext(Case):
  295. @patch('os.fork')
  296. @patch('os.setsid')
  297. @patch('os._exit')
  298. @patch('os.chdir')
  299. @patch('os.umask')
  300. @patch('os.close')
  301. @patch('os.closerange')
  302. @patch('os.open')
  303. @patch('os.dup2')
  304. def test_open(self, dup2, open, close, closer, umask, chdir,
  305. _exit, setsid, fork):
  306. x = DaemonContext(workdir='/opt/workdir')
  307. x.stdfds = [0, 1, 2]
  308. fork.return_value = 0
  309. with x:
  310. self.assertTrue(x._is_open)
  311. with x:
  312. pass
  313. self.assertEqual(fork.call_count, 2)
  314. setsid.assert_called_with()
  315. self.assertFalse(_exit.called)
  316. chdir.assert_called_with(x.workdir)
  317. umask.assert_called_with(x.umask)
  318. self.assertTrue(dup2.called)
  319. fork.reset_mock()
  320. fork.return_value = 1
  321. x = DaemonContext(workdir='/opt/workdir')
  322. x.stdfds = [0, 1, 2]
  323. with x:
  324. pass
  325. self.assertEqual(fork.call_count, 1)
  326. _exit.assert_called_with(0)
  327. x = DaemonContext(workdir='/opt/workdir', fake=True)
  328. x.stdfds = [0, 1, 2]
  329. x._detach = Mock()
  330. with x:
  331. pass
  332. self.assertFalse(x._detach.called)
  333. x.after_chdir = Mock()
  334. with x:
  335. pass
  336. x.after_chdir.assert_called_with()
  337. class test_Pidfile(Case):
  338. @patch('celery.platforms.Pidfile')
  339. def test_create_pidlock(self, Pidfile):
  340. p = Pidfile.return_value = Mock()
  341. p.is_locked.return_value = True
  342. p.remove_if_stale.return_value = False
  343. with override_stdouts() as (_, err):
  344. with self.assertRaises(SystemExit):
  345. create_pidlock('/var/pid')
  346. self.assertIn('already exists', err.getvalue())
  347. p.remove_if_stale.return_value = True
  348. ret = create_pidlock('/var/pid')
  349. self.assertIs(ret, p)
  350. def test_context(self):
  351. p = Pidfile('/var/pid')
  352. p.write_pid = Mock()
  353. p.remove = Mock()
  354. with p as _p:
  355. self.assertIs(_p, p)
  356. p.write_pid.assert_called_with()
  357. p.remove.assert_called_with()
  358. def test_acquire_raises_LockFailed(self):
  359. p = Pidfile('/var/pid')
  360. p.write_pid = Mock()
  361. p.write_pid.side_effect = OSError()
  362. with self.assertRaises(LockFailed):
  363. with p:
  364. pass
  365. @patch('os.path.exists')
  366. def test_is_locked(self, exists):
  367. p = Pidfile('/var/pid')
  368. exists.return_value = True
  369. self.assertTrue(p.is_locked())
  370. exists.return_value = False
  371. self.assertFalse(p.is_locked())
  372. def test_read_pid(self):
  373. with mock_open() as s:
  374. s.write('1816\n')
  375. s.seek(0)
  376. p = Pidfile('/var/pid')
  377. self.assertEqual(p.read_pid(), 1816)
  378. def test_read_pid_partially_written(self):
  379. with mock_open() as s:
  380. s.write('1816')
  381. s.seek(0)
  382. p = Pidfile('/var/pid')
  383. with self.assertRaises(ValueError):
  384. p.read_pid()
  385. def test_read_pid_raises_ENOENT(self):
  386. exc = IOError()
  387. exc.errno = errno.ENOENT
  388. with mock_open(side_effect=exc):
  389. p = Pidfile('/var/pid')
  390. self.assertIsNone(p.read_pid())
  391. def test_read_pid_raises_IOError(self):
  392. exc = IOError()
  393. exc.errno = errno.EAGAIN
  394. with mock_open(side_effect=exc):
  395. p = Pidfile('/var/pid')
  396. with self.assertRaises(IOError):
  397. p.read_pid()
  398. def test_read_pid_bogus_pidfile(self):
  399. with mock_open() as s:
  400. s.write('eighteensixteen\n')
  401. s.seek(0)
  402. p = Pidfile('/var/pid')
  403. with self.assertRaises(ValueError):
  404. p.read_pid()
  405. @patch('os.unlink')
  406. def test_remove(self, unlink):
  407. unlink.return_value = True
  408. p = Pidfile('/var/pid')
  409. p.remove()
  410. unlink.assert_called_with(p.path)
  411. @patch('os.unlink')
  412. def test_remove_ENOENT(self, unlink):
  413. exc = OSError()
  414. exc.errno = errno.ENOENT
  415. unlink.side_effect = exc
  416. p = Pidfile('/var/pid')
  417. p.remove()
  418. unlink.assert_called_with(p.path)
  419. @patch('os.unlink')
  420. def test_remove_EACCES(self, unlink):
  421. exc = OSError()
  422. exc.errno = errno.EACCES
  423. unlink.side_effect = exc
  424. p = Pidfile('/var/pid')
  425. p.remove()
  426. unlink.assert_called_with(p.path)
  427. @patch('os.unlink')
  428. def test_remove_OSError(self, unlink):
  429. exc = OSError()
  430. exc.errno = errno.EAGAIN
  431. unlink.side_effect = exc
  432. p = Pidfile('/var/pid')
  433. with self.assertRaises(OSError):
  434. p.remove()
  435. unlink.assert_called_with(p.path)
  436. @patch('os.kill')
  437. def test_remove_if_stale_process_alive(self, kill):
  438. p = Pidfile('/var/pid')
  439. p.read_pid = Mock()
  440. p.read_pid.return_value = 1816
  441. kill.return_value = 0
  442. self.assertFalse(p.remove_if_stale())
  443. kill.assert_called_with(1816, 0)
  444. p.read_pid.assert_called_with()
  445. kill.side_effect = OSError()
  446. kill.side_effect.errno = errno.ENOENT
  447. self.assertFalse(p.remove_if_stale())
  448. @patch('os.kill')
  449. def test_remove_if_stale_process_dead(self, kill):
  450. with override_stdouts():
  451. p = Pidfile('/var/pid')
  452. p.read_pid = Mock()
  453. p.read_pid.return_value = 1816
  454. p.remove = Mock()
  455. exc = OSError()
  456. exc.errno = errno.ESRCH
  457. kill.side_effect = exc
  458. self.assertTrue(p.remove_if_stale())
  459. kill.assert_called_with(1816, 0)
  460. p.remove.assert_called_with()
  461. def test_remove_if_stale_broken_pid(self):
  462. with override_stdouts():
  463. p = Pidfile('/var/pid')
  464. p.read_pid = Mock()
  465. p.read_pid.side_effect = ValueError()
  466. p.remove = Mock()
  467. self.assertTrue(p.remove_if_stale())
  468. p.remove.assert_called_with()
  469. def test_remove_if_stale_no_pidfile(self):
  470. p = Pidfile('/var/pid')
  471. p.read_pid = Mock()
  472. p.read_pid.return_value = None
  473. p.remove = Mock()
  474. self.assertTrue(p.remove_if_stale())
  475. p.remove.assert_called_with()
  476. @patch('os.fsync')
  477. @patch('os.getpid')
  478. @patch('os.open')
  479. @patch('os.fdopen')
  480. @patch(open_fqdn)
  481. def test_write_pid(self, open_, fdopen, osopen, getpid, fsync):
  482. getpid.return_value = 1816
  483. osopen.return_value = 13
  484. w = fdopen.return_value = WhateverIO()
  485. w.close = Mock()
  486. r = open_.return_value = WhateverIO()
  487. r.write('1816\n')
  488. r.seek(0)
  489. p = Pidfile('/var/pid')
  490. p.write_pid()
  491. w.seek(0)
  492. self.assertEqual(w.readline(), '1816\n')
  493. self.assertTrue(w.close.called)
  494. getpid.assert_called_with()
  495. osopen.assert_called_with(p.path, platforms.PIDFILE_FLAGS,
  496. platforms.PIDFILE_MODE)
  497. fdopen.assert_called_with(13, 'w')
  498. fsync.assert_called_with(13)
  499. open_.assert_called_with(p.path)
  500. @patch('os.fsync')
  501. @patch('os.getpid')
  502. @patch('os.open')
  503. @patch('os.fdopen')
  504. @patch(open_fqdn)
  505. def test_write_reread_fails(self, open_, fdopen,
  506. osopen, getpid, fsync):
  507. getpid.return_value = 1816
  508. osopen.return_value = 13
  509. w = fdopen.return_value = WhateverIO()
  510. w.close = Mock()
  511. r = open_.return_value = WhateverIO()
  512. r.write('11816\n')
  513. r.seek(0)
  514. p = Pidfile('/var/pid')
  515. with self.assertRaises(LockFailed):
  516. p.write_pid()
  517. class test_setgroups(Case):
  518. @patch('os.setgroups', create=True)
  519. def test_setgroups_hack_ValueError(self, setgroups):
  520. def on_setgroups(groups):
  521. if len(groups) <= 200:
  522. setgroups.return_value = True
  523. return
  524. raise ValueError()
  525. setgroups.side_effect = on_setgroups
  526. _setgroups_hack(list(range(400)))
  527. setgroups.side_effect = ValueError()
  528. with self.assertRaises(ValueError):
  529. _setgroups_hack(list(range(400)))
  530. @patch('os.setgroups', create=True)
  531. def test_setgroups_hack_OSError(self, setgroups):
  532. exc = OSError()
  533. exc.errno = errno.EINVAL
  534. def on_setgroups(groups):
  535. if len(groups) <= 200:
  536. setgroups.return_value = True
  537. return
  538. raise exc
  539. setgroups.side_effect = on_setgroups
  540. _setgroups_hack(list(range(400)))
  541. setgroups.side_effect = exc
  542. with self.assertRaises(OSError):
  543. _setgroups_hack(list(range(400)))
  544. exc2 = OSError()
  545. exc.errno = errno.ESRCH
  546. setgroups.side_effect = exc2
  547. with self.assertRaises(OSError):
  548. _setgroups_hack(list(range(400)))
  549. @patch('os.sysconf')
  550. @patch('celery.platforms._setgroups_hack')
  551. def test_setgroups(self, hack, sysconf):
  552. sysconf.return_value = 100
  553. setgroups(list(range(400)))
  554. hack.assert_called_with(list(range(100)))
  555. @patch('os.sysconf')
  556. @patch('celery.platforms._setgroups_hack')
  557. def test_setgroups_sysconf_raises(self, hack, sysconf):
  558. sysconf.side_effect = ValueError()
  559. setgroups(list(range(400)))
  560. hack.assert_called_with(list(range(400)))
  561. @patch('os.getgroups')
  562. @patch('os.sysconf')
  563. @patch('celery.platforms._setgroups_hack')
  564. def test_setgroups_raises_ESRCH(self, hack, sysconf, getgroups):
  565. sysconf.side_effect = ValueError()
  566. esrch = OSError()
  567. esrch.errno = errno.ESRCH
  568. hack.side_effect = esrch
  569. with self.assertRaises(OSError):
  570. setgroups(list(range(400)))
  571. @patch('os.getgroups')
  572. @patch('os.sysconf')
  573. @patch('celery.platforms._setgroups_hack')
  574. def test_setgroups_raises_EPERM(self, hack, sysconf, getgroups):
  575. sysconf.side_effect = ValueError()
  576. eperm = OSError()
  577. eperm.errno = errno.EPERM
  578. hack.side_effect = eperm
  579. getgroups.return_value = list(range(400))
  580. setgroups(list(range(400)))
  581. getgroups.assert_called_with()
  582. getgroups.return_value = [1000]
  583. with self.assertRaises(OSError):
  584. setgroups(list(range(400)))
  585. getgroups.assert_called_with()