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!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 line
4.6KB

  1. from focker.jail import backup_file, \
  2. jail_fs_create, \
  3. gen_env_command, \
  4. quote, \
  5. jail_create
  6. import tempfile
  7. import os
  8. import subprocess
  9. from focker.zfs import zfs_mountpoint, \
  10. zfs_exists, \
  11. zfs_tag, \
  12. zfs_find
  13. import jailconf
  14. def test_backup_file():
  15. with tempfile.TemporaryDirectory() as d:
  16. fname = os.path.join(d, 'dummy.conf')
  17. with open(fname, 'w') as f:
  18. f.write('init')
  19. nbackups = 10
  20. for i in range(15):
  21. backup_file(fname, nbackups=nbackups, chmod=0o640)
  22. with open(fname, 'w') as f:
  23. f.write(str(i))
  24. fname = os.path.join(d, 'dummy.conf')
  25. with open(fname, 'r') as f:
  26. assert f.read() == '14'
  27. for i in range(nbackups):
  28. fname = os.path.join(d, 'dummy.conf.%d' % i)
  29. assert os.path.exists(fname)
  30. with open(fname, 'r') as f:
  31. if i < 5:
  32. assert f.read() == str(i + 9)
  33. else:
  34. assert f.read() == str(i - 1)
  35. def test_jail_fs_create_01():
  36. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-jail-fs-create-01'])
  37. subprocess.check_output(['focker', 'bootstrap', '--empty', '-t', 'test-jail-fs-create-01'])
  38. name = jail_fs_create('test-jail-fs-create-01')
  39. assert zfs_exists(name)
  40. mountpoint = zfs_mountpoint(name)
  41. assert os.path.exists(mountpoint)
  42. with open(os.path.join(mountpoint, 'test.txt'), 'w') as f:
  43. f.write('test\n')
  44. assert os.path.exists(os.path.join(mountpoint, 'test.txt'))
  45. with open(os.path.join(mountpoint, 'test.txt'), 'r') as f:
  46. assert f.read() == 'test\n'
  47. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-jail-fs-create-01'])
  48. assert not zfs_exists(name)
  49. assert not os.path.exists(mountpoint)
  50. def test_jail_fs_create_02():
  51. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-jail-fs-create-02'])
  52. name = jail_fs_create()
  53. zfs_tag(name, ['test-jail-fs-create-02'])
  54. assert zfs_exists(name)
  55. mountpoint = zfs_mountpoint(name)
  56. assert os.path.exists(mountpoint)
  57. with open(os.path.join(mountpoint, 'test.txt'), 'w') as f:
  58. f.write('test\n')
  59. assert os.path.exists(os.path.join(mountpoint, 'test.txt'))
  60. with open(os.path.join(mountpoint, 'test.txt'), 'r') as f:
  61. assert f.read() == 'test\n'
  62. subprocess.check_output(['focker', 'jail', 'remove', 'test-jail-fs-create-02'])
  63. assert not zfs_exists(name)
  64. assert not os.path.exists(mountpoint)
  65. def test_gen_env_command():
  66. command = gen_env_command('echo $TEST_VARIABLE_1 && echo $TEST_VARIABLE_2',
  67. {'TEST_VARIABLE_1': 'foo', 'TEST_VARIABLE_2': 'foo bar'})
  68. assert command == 'export TEST_VARIABLE_1=foo && export TEST_VARIABLE_2=\'foo bar\' && echo $TEST_VARIABLE_1 && echo $TEST_VARIABLE_2'
  69. def test_quote():
  70. res = quote('foo \\ bar \'baz\'')
  71. assert res == '\'foo \\\\ bar \\\'baz\\\'\''
  72. def test_jail_create():
  73. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-jail-create'])
  74. subprocess.check_output(['focker', 'volume', 'remove', '--force', 'test-jail-create'])
  75. name = jail_fs_create()
  76. zfs_tag(name, ['test-jail-create'])
  77. subprocess.check_output(['focker', 'volume', 'create', '-t', 'test-jail-create'])
  78. mountpoint = zfs_mountpoint(name)
  79. jail_name = jail_create(mountpoint, '/bin/sh /etc/rc', {
  80. 'DUMMY_1': 'foo',
  81. 'DUMMY_2': 'bar'
  82. }, [
  83. ('test-jail-create', '/test-jail-create'),
  84. ('/tmp', '/test-tmp')
  85. ], hostname='test-jail-create', overrides={
  86. 'ip4.addr': '127.1.2.3'
  87. })
  88. assert jail_name == os.path.split(mountpoint)[-1]
  89. assert os.path.exists(mountpoint)
  90. vol_name, _ = zfs_find('test-jail-create', focker_type='volume')
  91. vol_mountpoint = zfs_mountpoint(vol_name)
  92. assert os.path.exists(vol_mountpoint)
  93. conf = jailconf.load('/etc/jail.conf')
  94. assert jail_name in conf
  95. conf = conf[jail_name]
  96. assert conf['path'] == mountpoint
  97. assert conf['exec.start'] == '\'export DUMMY_1=foo && export DUMMY_2=bar && /bin/sh /etc/rc\''
  98. assert conf['exec.prestart'] == f'\'cp /etc/resolv.conf {mountpoint}/etc/resolv.conf && mount -t nullfs {vol_mountpoint} {mountpoint}/test-jail-create && mount -t nullfs /tmp {mountpoint}/test-tmp\''
  99. assert conf['ip4.addr'] == '\'127.1.2.3\''
  100. subprocess.check_output(['focker', 'jail', 'remove', 'test-jail-create'])
  101. subprocess.check_output(['focker', 'volume', 'remove', 'test-jail-create'])