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.

test_image.py 3.3KB

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