IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

96 lines
2.7KB

  1. from focker.compose import exec_hook, \
  2. exec_prebuild, \
  3. exec_postbuild
  4. from tempfile import TemporaryDirectory
  5. import os
  6. import pytest
  7. import fcntl
  8. from focker.misc import focker_lock, \
  9. focker_unlock
  10. import inspect
  11. import ast
  12. def test_exec_hook_01():
  13. spec = [
  14. 'touch test-exec-hook-01',
  15. 'touch test-exec-hook-02'
  16. ]
  17. with TemporaryDirectory() as d:
  18. exec_hook(spec, d, 'test-exec-hook')
  19. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  20. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  21. assert not os.path.exists(d)
  22. def test_exec_hook_02():
  23. spec = 'touch test-exec-hook-01 && touch test-exec-hook-02'
  24. with TemporaryDirectory() as d:
  25. exec_hook(spec, d, 'test-exec-hook')
  26. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  27. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  28. assert not os.path.exists(d)
  29. @pytest.mark.xfail(raises=ValueError, strict=True)
  30. def test_exec_hook_03a():
  31. spec = 1
  32. with TemporaryDirectory() as d:
  33. exec_hook(spec, d, 'test-exec-hook')
  34. @pytest.mark.xfail(raises=TypeError, strict=True)
  35. def test_exec_hook_03b():
  36. spec = [1]
  37. with TemporaryDirectory() as d:
  38. exec_hook(spec, d, 'test-exec-hook')
  39. @pytest.mark.xfail(raises=FileNotFoundError, strict=True)
  40. def test_exec_hook_04():
  41. spec = 'ls'
  42. exec_hook(spec, '/non-existent-directory/wcj20fy103', 'test-exec-hook')
  43. def test_exec_hook_05():
  44. spec = 'ls'
  45. oldwd = os.getcwd()
  46. with TemporaryDirectory() as d:
  47. exec_hook(spec, d, 'test-exec-hook')
  48. assert os.getcwd() == oldwd
  49. @pytest.mark.xfail(raises=RuntimeError, strict=True)
  50. def test_exec_hook_06():
  51. spec = '/non-existent-command/hf249h'
  52. with TemporaryDirectory() as d:
  53. exec_hook(spec, d, 'test-exec-hook')
  54. def test_exec_hook_07():
  55. os.chdir('/')
  56. spec = 'flock --nonblock /var/lock/focker.lock -c ls'
  57. focker_lock()
  58. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  59. with TemporaryDirectory() as d:
  60. exec_hook(spec, d, 'test-exec-hook')
  61. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  62. focker_unlock()
  63. def _test_simple_forward(fun, fwd_fun_name='exec_hook'):
  64. src = inspect.getsource(fun)
  65. mod = ast.parse(src)
  66. assert isinstance(mod.body[0], ast.FunctionDef)
  67. assert isinstance(mod.body[0].body[0], ast.Return)
  68. assert isinstance(mod.body[0].body[0].value, ast.Call)
  69. assert mod.body[0].body[0].value.func.id == fwd_fun_name
  70. def test_exec_prebuild():
  71. _test_simple_forward(exec_prebuild)
  72. def test_exec_postbuild():
  73. _test_simple_forward(exec_postbuild)