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!
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

81 строка
3.0KB

  1. from focker.jail import backup_file, \
  2. jail_fs_create, \
  3. gen_env_command, \
  4. quote
  5. import tempfile
  6. import os
  7. import subprocess
  8. from focker.zfs import zfs_mountpoint, \
  9. zfs_exists, \
  10. zfs_tag
  11. def test_backup_file():
  12. with tempfile.TemporaryDirectory() as d:
  13. fname = os.path.join(d, 'dummy.conf')
  14. with open(fname, 'w') as f:
  15. f.write('init')
  16. nbackups = 10
  17. for i in range(15):
  18. backup_file(fname, nbackups=nbackups, chmod=0o640)
  19. with open(fname, 'w') as f:
  20. f.write(str(i))
  21. fname = os.path.join(d, 'dummy.conf')
  22. with open(fname, 'r') as f:
  23. assert f.read() == '14'
  24. for i in range(nbackups):
  25. fname = os.path.join(d, 'dummy.conf.%d' % i)
  26. assert os.path.exists(fname)
  27. with open(fname, 'r') as f:
  28. if i < 5:
  29. assert f.read() == str(i + 9)
  30. else:
  31. assert f.read() == str(i - 1)
  32. def test_jail_fs_create_01():
  33. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-jail-fs-create-01'])
  34. subprocess.check_output(['focker', 'bootstrap', '--empty', '-t', 'test-jail-fs-create-01'])
  35. name = jail_fs_create('test-jail-fs-create-01')
  36. assert zfs_exists(name)
  37. mountpoint = zfs_mountpoint(name)
  38. assert os.path.exists(mountpoint)
  39. with open(os.path.join(mountpoint, 'test.txt'), 'w') as f:
  40. f.write('test\n')
  41. assert os.path.exists(os.path.join(mountpoint, 'test.txt'))
  42. with open(os.path.join(mountpoint, 'test.txt'), 'r') as f:
  43. assert f.read() == 'test\n'
  44. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-jail-fs-create-01'])
  45. assert not zfs_exists(name)
  46. assert not os.path.exists(mountpoint)
  47. def test_jail_fs_create_02():
  48. subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-jail-fs-create-02'])
  49. name = jail_fs_create()
  50. zfs_tag(name, ['test-jail-fs-create-02'])
  51. assert zfs_exists(name)
  52. mountpoint = zfs_mountpoint(name)
  53. assert os.path.exists(mountpoint)
  54. with open(os.path.join(mountpoint, 'test.txt'), 'w') as f:
  55. f.write('test\n')
  56. assert os.path.exists(os.path.join(mountpoint, 'test.txt'))
  57. with open(os.path.join(mountpoint, 'test.txt'), 'r') as f:
  58. assert f.read() == 'test\n'
  59. subprocess.check_output(['focker', 'jail', 'remove', 'test-jail-fs-create-02'])
  60. assert not zfs_exists(name)
  61. assert not os.path.exists(mountpoint)
  62. def test_gen_env_command():
  63. command = gen_env_command('echo $TEST_VARIABLE_1 && echo $TEST_VARIABLE_2',
  64. {'TEST_VARIABLE_1': 'foo', 'TEST_VARIABLE_2': 'foo bar'})
  65. assert command == 'export TEST_VARIABLE_1=foo && export TEST_VARIABLE_2=\'foo bar\' && echo $TEST_VARIABLE_1 && echo $TEST_VARIABLE_2'
  66. def test_quote():
  67. res = quote('foo \\ bar \'baz\'')
  68. assert res == '\'foo \\\\ bar \\\'baz\\\'\''