test_daklib_fstransactions.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #! /usr/bin/env python
  2. #
  3. # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. from base_test import DakTestCase
  19. from daklib.fstransactions import FilesystemTransaction
  20. from unittest import main
  21. import os
  22. import shutil
  23. import tempfile
  24. class TemporaryDirectory:
  25. def __init__(self):
  26. self.directory = None
  27. def __str__(self):
  28. return self.directory
  29. def filename(self, suffix):
  30. return os.path.join(self.directory, suffix)
  31. def __enter__(self):
  32. self.directory = tempfile.mkdtemp()
  33. return self
  34. def __exit__(self, *args):
  35. if self.directory is not None:
  36. shutil.rmtree(self.directory)
  37. self.directory = None
  38. return None
  39. class FilesystemTransactionTestCase(DakTestCase):
  40. def _copy_a_b(self, tmp, fs, **kwargs):
  41. fs.copy(tmp.filename('a'), tmp.filename('b'), **kwargs)
  42. def _write_to_a(self, tmp):
  43. with open(tmp.filename('a'), 'w') as fh:
  44. print >>fh, 'a'
  45. def test_copy_non_existing(self):
  46. def copy():
  47. with TemporaryDirectory() as t:
  48. with FilesystemTransaction() as fs:
  49. self._copy_a_b(t, fs)
  50. self.assertRaises(IOError, copy)
  51. def test_copy_existing_and_commit(self):
  52. with TemporaryDirectory() as t:
  53. self._write_to_a(t)
  54. with FilesystemTransaction() as fs:
  55. self._copy_a_b(t, fs)
  56. self.assert_(os.path.exists(t.filename('a')))
  57. self.assert_(os.path.exists(t.filename('b')))
  58. self.assert_(os.path.exists(t.filename('a')))
  59. self.assert_(os.path.exists(t.filename('b')))
  60. def test_copy_existing_and_rollback(self):
  61. with TemporaryDirectory() as t:
  62. self._write_to_a(t)
  63. class TestException(Exception):
  64. pass
  65. try:
  66. with FilesystemTransaction() as fs:
  67. self._copy_a_b(t, fs)
  68. self.assert_(os.path.exists(t.filename('a')))
  69. self.assert_(os.path.exists(t.filename('b')))
  70. raise TestException()
  71. except TestException:
  72. pass
  73. self.assert_(os.path.exists(t.filename('a')))
  74. self.assert_(not os.path.exists(t.filename('b')))
  75. def test_unlink_and_commit(self):
  76. with TemporaryDirectory() as t:
  77. self._write_to_a(t)
  78. a = t.filename('a')
  79. with FilesystemTransaction() as fs:
  80. self.assert_(os.path.exists(a))
  81. fs.unlink(a)
  82. self.assert_(not os.path.exists(a))
  83. self.assert_(not os.path.exists(a))
  84. def test_unlink_and_rollback(self):
  85. with TemporaryDirectory() as t:
  86. self._write_to_a(t)
  87. a = t.filename('a')
  88. class TestException(Exception):
  89. pass
  90. try:
  91. with FilesystemTransaction() as fs:
  92. self.assert_(os.path.exists(a))
  93. fs.unlink(a)
  94. self.assert_(not os.path.exists(a))
  95. raise TestException()
  96. except TestException:
  97. pass
  98. self.assert_(os.path.exists(a))
  99. if __name__ == '__main__':
  100. main()