test_snapshot.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from flexmock import flexmock
  2. from borgmatic.borg.pattern import Pattern
  3. from borgmatic.hooks.data_source import snapshot as module
  4. def test_get_contained_patterns_without_candidates_returns_empty():
  5. flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=flexmock()))
  6. flexmock(module.os.path).should_receive('exists').and_return(True)
  7. assert module.get_contained_patterns('/mnt', {}) == ()
  8. def test_get_contained_patterns_with_self_candidate_returns_self():
  9. device = flexmock()
  10. flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
  11. flexmock(module.os.path).should_receive('exists').and_return(True)
  12. candidates = {
  13. Pattern('/foo', device=device),
  14. Pattern('/mnt', device=device),
  15. Pattern('/bar', device=device),
  16. }
  17. assert module.get_contained_patterns('/mnt', candidates) == (Pattern('/mnt', device=device),)
  18. assert candidates == {Pattern('/foo', device=device), Pattern('/bar', device=device)}
  19. def test_get_contained_patterns_with_self_candidate_and_caret_prefix_returns_self():
  20. device = flexmock()
  21. flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
  22. flexmock(module.os.path).should_receive('exists').and_return(True)
  23. candidates = {
  24. Pattern('^/foo', device=device),
  25. Pattern('^/mnt', device=device),
  26. Pattern('^/bar', device=device),
  27. }
  28. assert module.get_contained_patterns('/mnt', candidates) == (Pattern('^/mnt', device=device),)
  29. assert candidates == {Pattern('^/foo', device=device), Pattern('^/bar', device=device)}
  30. def test_get_contained_patterns_with_child_candidate_returns_child():
  31. device = flexmock()
  32. flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
  33. flexmock(module.os.path).should_receive('exists').and_return(True)
  34. candidates = {
  35. Pattern('/foo', device=device),
  36. Pattern('/mnt/subdir', device=device),
  37. Pattern('/bar', device=device),
  38. }
  39. assert module.get_contained_patterns('/mnt', candidates) == (
  40. Pattern('/mnt/subdir', device=device),
  41. )
  42. assert candidates == {Pattern('/foo', device=device), Pattern('/bar', device=device)}
  43. def test_get_contained_patterns_with_grandchild_candidate_returns_child():
  44. device = flexmock()
  45. flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
  46. flexmock(module.os.path).should_receive('exists').and_return(True)
  47. candidates = {
  48. Pattern('/foo', device=device),
  49. Pattern('/mnt/sub/dir', device=device),
  50. Pattern('/bar', device=device),
  51. }
  52. assert module.get_contained_patterns('/mnt', candidates) == (
  53. Pattern('/mnt/sub/dir', device=device),
  54. )
  55. assert candidates == {Pattern('/foo', device=device), Pattern('/bar', device=device)}
  56. def test_get_contained_patterns_ignores_child_candidate_on_another_device():
  57. one_device = flexmock()
  58. another_device = flexmock()
  59. flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=one_device))
  60. flexmock(module.os.path).should_receive('exists').and_return(True)
  61. candidates = {
  62. Pattern('/foo', device=one_device),
  63. Pattern('/mnt/subdir', device=another_device),
  64. Pattern('/bar', device=one_device),
  65. }
  66. assert module.get_contained_patterns('/mnt', candidates) == ()
  67. assert candidates == {
  68. Pattern('/foo', device=one_device),
  69. Pattern('/mnt/subdir', device=another_device),
  70. Pattern('/bar', device=one_device),
  71. }
  72. def test_get_contained_patterns_with_non_existent_parent_directory_ignores_child_candidate():
  73. device = flexmock()
  74. flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
  75. flexmock(module.os.path).should_receive('exists').and_return(False)
  76. candidates = {
  77. Pattern('/foo', device=device),
  78. Pattern('/mnt/subdir', device=device),
  79. Pattern('/bar', device=device),
  80. }
  81. assert module.get_contained_patterns('/mnt', candidates) == ()
  82. assert candidates == {
  83. Pattern('/foo', device=device),
  84. Pattern('/mnt/subdir', device=device),
  85. Pattern('/bar', device=device),
  86. }