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.

75 lines
2.8KB

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