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.

test_compose.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from focker.compose import exec_hook, \
  2. exec_prebuild, \
  3. exec_postbuild, \
  4. build_volumes, \
  5. build_images
  6. from tempfile import TemporaryDirectory
  7. import os
  8. import pytest
  9. import fcntl
  10. from focker.misc import focker_lock, \
  11. focker_unlock
  12. import inspect
  13. import ast
  14. import stat
  15. from focker.zfs import zfs_find, \
  16. zfs_mountpoint, \
  17. zfs_parse_output
  18. import subprocess
  19. import yaml
  20. def test_exec_hook_01():
  21. spec = [
  22. 'touch test-exec-hook-01',
  23. 'touch test-exec-hook-02'
  24. ]
  25. with TemporaryDirectory() as d:
  26. exec_hook(spec, d, 'test-exec-hook')
  27. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  28. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  29. assert not os.path.exists(d)
  30. def test_exec_hook_02():
  31. spec = 'touch test-exec-hook-01 && touch test-exec-hook-02'
  32. with TemporaryDirectory() as d:
  33. exec_hook(spec, d, 'test-exec-hook')
  34. assert os.path.exists(os.path.join(d, 'test-exec-hook-01'))
  35. assert os.path.exists(os.path.join(d, 'test-exec-hook-02'))
  36. assert not os.path.exists(d)
  37. @pytest.mark.xfail(raises=ValueError, strict=True)
  38. def test_exec_hook_03a():
  39. spec = 1
  40. with TemporaryDirectory() as d:
  41. exec_hook(spec, d, 'test-exec-hook')
  42. @pytest.mark.xfail(raises=TypeError, strict=True)
  43. def test_exec_hook_03b():
  44. spec = [1]
  45. with TemporaryDirectory() as d:
  46. exec_hook(spec, d, 'test-exec-hook')
  47. @pytest.mark.xfail(raises=FileNotFoundError, strict=True)
  48. def test_exec_hook_04():
  49. spec = 'ls'
  50. exec_hook(spec, '/non-existent-directory/wcj20fy103', 'test-exec-hook')
  51. def test_exec_hook_05():
  52. spec = 'ls'
  53. oldwd = os.getcwd()
  54. with TemporaryDirectory() as d:
  55. exec_hook(spec, d, 'test-exec-hook')
  56. assert os.getcwd() == oldwd
  57. @pytest.mark.xfail(raises=RuntimeError, strict=True)
  58. def test_exec_hook_06():
  59. spec = '/non-existent-command/hf249h'
  60. with TemporaryDirectory() as d:
  61. exec_hook(spec, d, 'test-exec-hook')
  62. def test_exec_hook_07():
  63. os.chdir('/')
  64. spec = 'flock --nonblock /var/lock/focker.lock -c ls'
  65. focker_lock()
  66. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  67. with TemporaryDirectory() as d:
  68. exec_hook(spec, d, 'test-exec-hook')
  69. assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
  70. focker_unlock()
  71. def _test_simple_forward(fun, fwd_fun_name='exec_hook'):
  72. src = inspect.getsource(fun)
  73. mod = ast.parse(src)
  74. assert isinstance(mod.body[0], ast.FunctionDef)
  75. assert isinstance(mod.body[0].body[0], ast.Return)
  76. assert isinstance(mod.body[0].body[0].value, ast.Call)
  77. assert mod.body[0].body[0].value.func.id == fwd_fun_name
  78. def test_exec_prebuild():
  79. _test_simple_forward(exec_prebuild)
  80. def test_exec_postbuild():
  81. _test_simple_forward(exec_postbuild)
  82. def test_build_volumes():
  83. subprocess.check_output(['focker', 'volume', 'remove', '--force', 'test-build-volumes'])
  84. err = False
  85. try:
  86. name, _ = zfs_find('test-build-volumes', focker_type='volume')
  87. except:
  88. err = True
  89. assert err
  90. spec = {
  91. 'test-build-volumes': {
  92. 'chown': '65534:65534',
  93. 'chmod': 0o123,
  94. 'zfs': {
  95. 'quota': '1G',
  96. 'readonly': 'on'
  97. }
  98. }
  99. }
  100. build_volumes(spec)
  101. name, _ = zfs_find('test-build-volumes', focker_type='volume')
  102. st = os.stat(zfs_mountpoint(name))
  103. assert st.st_uid == 65534
  104. assert st.st_gid == 65534
  105. assert ('%o' % st.st_mode)[-3:] == '123'
  106. zst = zfs_parse_output(['zfs', 'get', '-H', 'quota,readonly', name])
  107. assert zst[0][2] == '1G'
  108. assert zst[1][2] == 'on'
  109. subprocess.check_output(['zfs', 'destroy', '-r', '-f', name])
  110. def test_build_images():
  111. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
  112. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '--tags', 'test-focker-bootstrap'])
  113. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-build-images'])
  114. with TemporaryDirectory() as d:
  115. with open(os.path.join(d, 'Fockerfile'), 'w') as f:
  116. yaml.dump({
  117. 'base': 'test-focker-bootstrap',
  118. 'steps': [
  119. { 'copy': [
  120. [ '/bin/sh', '/bin/sh', { 'chmod': 0o777 } ],
  121. [ '/lib/libedit.so.7', '/lib/libedit.so.7' ],
  122. [ '/lib/libncursesw.so.8', '/lib/libncursesw.so.8' ],
  123. [ '/lib/libc.so.7', '/lib/libc.so.7' ],
  124. [ '/usr/bin/touch', '/usr/bin/touch', { 'chmod': 0o777 } ],
  125. [ '/libexec/ld-elf.so.1', '/libexec/ld-elf.so.1', { 'chmod': 0o555 } ]
  126. ] },
  127. { 'run': 'touch /test-build-images' }
  128. ]
  129. }, f)
  130. args = lambda: 0
  131. args.squeeze = False
  132. build_images({
  133. 'test-build-images': '.'
  134. }, d, args)
  135. focker_unlock()
  136. name, _ = zfs_find('test-build-images', focker_type='image')
  137. assert os.path.exists(os.path.join(zfs_mountpoint(name), 'test-build-images'))
  138. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-build-images'])
  139. subprocess.check_output(['focker', 'image', 'prune'])
  140. subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])