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!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_image.py 4.5KB

4 anos atrás
4 anos atrás
4 anos atrás
4 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import pytest
  2. from focker.image import validate_spec, \
  3. build_squeeze, \
  4. build, \
  5. command_image_build
  6. import subprocess
  7. from tempfile import TemporaryDirectory
  8. import focker.image
  9. import os
  10. from focker.zfs import zfs_find, \
  11. zfs_mountpoint, \
  12. zfs_exists_snapshot_sha256
  13. from focker.misc import focker_unlock
  14. import yaml
  15. def test_validate_spec_01():
  16. spec = { 'base': 'base', 'steps': 'steps' }
  17. validate_spec(spec)
  18. def test_validate_spec_02():
  19. spec = { 'steps': 'steps' }
  20. with pytest.raises(ValueError):
  21. validate_spec(spec)
  22. def test_validate_spec_03():
  23. spec = { 'base': 'base' }
  24. with pytest.raises(ValueError):
  25. validate_spec(spec)
  26. def test_validate_spec_04():
  27. spec = {}
  28. with pytest.raises(ValueError):
  29. validate_spec(spec)
  30. def test_build_squeeze(monkeypatch):
  31. focker_unlock()
  32. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-build-squeeze-base'])
  33. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-build-squeeze-base'])
  34. spec = dict(base='test-build-squeeze-base', steps=[
  35. dict(copy=['/etc/localtime', '/etc/localtime']),
  36. dict(copy=['/etc/hosts', '/etc/hosts'])
  37. ])
  38. _, base_sha256 = zfs_find('test-build-squeeze-base', focker_type='image')
  39. def fail(sha256, *args, **kwargs):
  40. if sha256 != base_sha256:
  41. raise RuntimeError('No pre-existing layers expected apart from base')
  42. monkeypatch.setattr(focker.image, 'zfs_snapshot_by_sha256', fail)
  43. with TemporaryDirectory() as d:
  44. args = lambda: 0
  45. args.focker_dir = d
  46. name, _ = build_squeeze(spec, args)
  47. focker_unlock()
  48. mountpoint = zfs_mountpoint(name.split('@')[0])
  49. print('name:', name, 'mountpoint:', mountpoint)
  50. assert os.path.exists(os.path.join(mountpoint, 'etc/localtime'))
  51. assert os.path.exists(os.path.join(mountpoint, 'etc/hosts'))
  52. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-build-squeeze-base'])
  53. assert not os.path.exists(mountpoint)
  54. def test_build(monkeypatch):
  55. focker_unlock()
  56. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-build-squeeze-base'])
  57. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-build-squeeze-base'])
  58. spec = dict(base='test-build-squeeze-base', steps=[
  59. dict(copy=['/etc/localtime', '/etc/localtime']),
  60. dict(copy=['/etc/hosts', '/etc/hosts'])
  61. ])
  62. _, base_sha256 = zfs_find('test-build-squeeze-base', focker_type='image')
  63. counter = 0
  64. def count_calls(*args, **kwargs):
  65. nonlocal counter
  66. counter += 1
  67. return zfs_exists_snapshot_sha256(*args, **kwargs)
  68. monkeypatch.setattr(focker.image, 'zfs_exists_snapshot_sha256', count_calls)
  69. with TemporaryDirectory() as d:
  70. args = lambda: 0
  71. args.focker_dir = d
  72. name, _ = build(spec, args)
  73. assert counter == 2
  74. focker_unlock()
  75. mountpoint = zfs_mountpoint(name.split('@')[0])
  76. print('name:', name, 'mountpoint:', mountpoint)
  77. assert os.path.exists(os.path.join(mountpoint, 'etc/localtime'))
  78. assert os.path.exists(os.path.join(mountpoint, 'etc/hosts'))
  79. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-build-squeeze-base'])
  80. assert not os.path.exists(mountpoint)
  81. def test_command_image_build():
  82. focker_unlock()
  83. subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-command-image-build-base'])
  84. subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-command-image-build-base'])
  85. with TemporaryDirectory() as d:
  86. args = lambda: 0
  87. with open(os.path.join(d, 'Fockerfile'), 'w') as f:
  88. yaml.dump(dict(base='test-command-image-build-base', steps=[
  89. dict(copy=['/etc/localtime', '/etc/localtime']),
  90. dict(copy=['/etc/hosts', '/etc/hosts'])
  91. ]), f)
  92. args.focker_dir = d
  93. args.squeeze = False
  94. args.tags = [ 'test-command-image-build' ]
  95. command_image_build(args)
  96. focker_unlock()
  97. name, _ = zfs_find('test-command-image-build', focker_type='image')
  98. mountpoint = zfs_mountpoint(name)
  99. assert os.path.exists(os.path.join(mountpoint, 'etc/localtime'))
  100. assert os.path.exists(os.path.join(mountpoint, 'etc/hosts'))
  101. subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-command-image-build-base'])
  102. assert not os.path.exists(mountpoint)