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.

132 lines
3.8KB

  1. from focker.compose import exec_hook, \
  2. exec_prebuild, \
  3. exec_postbuild, \
  4. build_volumes
  5. from tempfile import TemporaryDirectory
  6. import os
  7. import pytest
  8. import fcntl
  9. from focker.misc import focker_lock, \
  10. focker_unlock
  11. import inspect
  12. import ast
  13. import stat
  14. from focker.zfs import zfs_find, \
  15. zfs_mountpoint, \
  16. zfs_parse_output
  17. import subprocess
  18. def test_exec_hook_01():
  19. spec = [
  20. 'touch test-exec-hook-01',
  21. 'touch test-exec-hook-02'
  22. ]
  23. with TemporaryDirectory() as d:
  24. exec_hook(spec, d, 'test-exec-hook')
  25. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  26. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  27. assert not os.path.exists(d)
  28. def test_exec_hook_02():
  29. spec = 'touch test-exec-hook-01 && touch test-exec-hook-02'
  30. with TemporaryDirectory() as d:
  31. exec_hook(spec, d, 'test-exec-hook')
  32. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  33. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  34. assert not os.path.exists(d)
  35. @pytest.mark.xfail(raises=ValueError, strict=True)
  36. def test_exec_hook_03a():
  37. spec = 1
  38. with TemporaryDirectory() as d:
  39. exec_hook(spec, d, 'test-exec-hook')
  40. @pytest.mark.xfail(raises=TypeError, strict=True)
  41. def test_exec_hook_03b():
  42. spec = [1]
  43. with TemporaryDirectory() as d:
  44. exec_hook(spec, d, 'test-exec-hook')
  45. @pytest.mark.xfail(raises=FileNotFoundError, strict=True)
  46. def test_exec_hook_04():
  47. spec = 'ls'
  48. exec_hook(spec, '/non-existent-directory/wcj20fy103', 'test-exec-hook')
  49. def test_exec_hook_05():
  50. spec = 'ls'
  51. oldwd = os.getcwd()
  52. with TemporaryDirectory() as d:
  53. exec_hook(spec, d, 'test-exec-hook')
  54. assert os.getcwd() == oldwd
  55. @pytest.mark.xfail(raises=RuntimeError, strict=True)
  56. def test_exec_hook_06():
  57. spec = '/non-existent-command/hf249h'
  58. with TemporaryDirectory() as d:
  59. exec_hook(spec, d, 'test-exec-hook')
  60. def test_exec_hook_07():
  61. os.chdir('/')
  62. spec = 'flock --nonblock /var/lock/focker.lock -c ls'
  63. focker_lock()
  64. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  65. with TemporaryDirectory() as d:
  66. exec_hook(spec, d, 'test-exec-hook')
  67. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  68. focker_unlock()
  69. def _test_simple_forward(fun, fwd_fun_name='exec_hook'):
  70. src = inspect.getsource(fun)
  71. mod = ast.parse(src)
  72. assert isinstance(mod.body[0], ast.FunctionDef)
  73. assert isinstance(mod.body[0].body[0], ast.Return)
  74. assert isinstance(mod.body[0].body[0].value, ast.Call)
  75. assert mod.body[0].body[0].value.func.id == fwd_fun_name
  76. def test_exec_prebuild():
  77. _test_simple_forward(exec_prebuild)
  78. def test_exec_postbuild():
  79. _test_simple_forward(exec_postbuild)
  80. def test_build_volumes():
  81. subprocess.check_output(['focker', 'volume', 'remove', '--force', 'test-build-volumes'])
  82. err = False
  83. try:
  84. name, _ = zfs_find('test-build-volumes', focker_type='volume')
  85. except:
  86. err = True
  87. assert err
  88. spec = {
  89. 'test-build-volumes': {
  90. 'chown': '65534:65534',
  91. 'chmod': 0o123,
  92. 'zfs': {
  93. 'quota': '1G',
  94. 'readonly': 'on'
  95. }
  96. }
  97. }
  98. build_volumes(spec)
  99. name, _ = zfs_find('test-build-volumes', focker_type='volume')
  100. st = os.stat(zfs_mountpoint(name))
  101. assert st.st_uid == 65534
  102. assert st.st_gid == 65534
  103. assert ('%o' % st.st_mode)[-3:] == '123'
  104. zst = zfs_parse_output(['zfs', 'get', '-H', 'quota,readonly', name])
  105. assert zst[0][2] == '1G'
  106. assert zst[1][2] == 'on'
  107. subprocess.check_output(['zfs', 'destroy', '-r', '-f', name])