test_collect.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import sys
  2. from flexmock import flexmock
  3. from borgmatic.config import collect as module
  4. def test_get_default_config_paths_includes_absolute_user_config_path():
  5. flexmock(module.os, environ={'XDG_CONFIG_HOME': None, 'HOME': '/home/user'})
  6. config_paths = module.get_default_config_paths()
  7. assert '/home/user/.config/borgmatic/config.yaml' in config_paths
  8. def test_get_default_config_paths_prefers_xdg_config_home_for_user_config_path():
  9. flexmock(module.os, environ={'XDG_CONFIG_HOME': '/home/user/.etc', 'HOME': '/home/user'})
  10. config_paths = module.get_default_config_paths()
  11. assert '/home/user/.etc/borgmatic/config.yaml' in config_paths
  12. def test_collect_config_filenames_collects_yml_file_endings():
  13. config_paths = ('config.yaml', '/etc/borgmatic.d')
  14. mock_path = flexmock(module.os.path)
  15. mock_path.should_receive('exists').and_return(True)
  16. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  17. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  18. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yml').and_return(False)
  19. mock_path.should_receive('abspath').replace_with(lambda path: module.os.path.join('/', path))
  20. flexmock(module.os).should_receive('access').and_return(True)
  21. flexmock(module.os).should_receive('listdir')
  22. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(['foo.yml'])
  23. config_filenames = tuple(module.collect_config_filenames(config_paths))
  24. assert config_filenames == ('/config.yaml', '/etc/borgmatic.d/foo.yml')
  25. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_sub_directories():
  26. config_paths = ('config.yaml', '/etc/borgmatic.d')
  27. mock_path = flexmock(module.os.path)
  28. mock_path.should_receive('exists').and_return(True)
  29. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  30. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  31. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  32. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar').and_return(True)
  33. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.yaml').and_return(False)
  34. mock_path.should_receive('abspath').replace_with(lambda path: module.os.path.join('/', path))
  35. flexmock(module.os).should_receive('access').and_return(True)
  36. flexmock(module.os).should_receive('listdir')
  37. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
  38. ['foo.yaml', 'bar', 'baz.yaml'],
  39. )
  40. config_filenames = tuple(module.collect_config_filenames(config_paths))
  41. assert config_filenames == (
  42. '/config.yaml',
  43. '/etc/borgmatic.d/foo.yaml',
  44. '/etc/borgmatic.d/baz.yaml',
  45. )
  46. def test_collect_config_filenames_collects_files_from_given_directories_and_ignores_non_yaml_filenames():
  47. config_paths = ('/etc/borgmatic.d',)
  48. mock_path = flexmock(module.os.path)
  49. mock_path.should_receive('exists').and_return(True)
  50. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  51. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/foo.yaml').and_return(False)
  52. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/bar.yaml~').and_return(False)
  53. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d/baz.txt').and_return(False)
  54. mock_path.should_receive('abspath').replace_with(lambda path: module.os.path.join('/', path))
  55. flexmock(module.os).should_receive('access').and_return(True)
  56. flexmock(module.os).should_receive('listdir')
  57. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(
  58. ['foo.yaml', 'bar.yaml~', 'baz.txt'],
  59. )
  60. config_filenames = tuple(module.collect_config_filenames(config_paths))
  61. assert config_filenames == ('/etc/borgmatic.d/foo.yaml',)
  62. def test_collect_config_filenames_skips_permission_denied_directories():
  63. config_paths = ('config.yaml', '/etc/borgmatic.d')
  64. mock_path = flexmock(module.os.path)
  65. mock_path.should_receive('exists').and_return(True)
  66. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  67. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  68. mock_path.should_receive('abspath').replace_with(lambda path: module.os.path.join('/', path))
  69. flexmock(module.os).should_receive('access').and_return(False)
  70. flexmock(module.os).should_receive('listdir')
  71. flexmock(sys.modules['builtins']).should_receive('sorted').and_return(['config.yaml'])
  72. config_filenames = tuple(module.collect_config_filenames(config_paths))
  73. assert config_filenames == ('/config.yaml',)
  74. def test_collect_config_filenames_skips_etc_borgmatic_config_dot_yaml_if_it_does_not_exist():
  75. config_paths = ('config.yaml', '/etc/borgmatic/config.yaml')
  76. mock_path = flexmock(module.os.path)
  77. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  78. mock_path.should_receive('exists').with_args('/etc/borgmatic/config.yaml').and_return(False)
  79. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  80. mock_path.should_receive('isdir').with_args('/etc/borgmatic/config.yaml').and_return(True)
  81. mock_path.should_receive('abspath').replace_with(lambda path: module.os.path.join('/', path))
  82. config_filenames = tuple(module.collect_config_filenames(config_paths))
  83. assert config_filenames == ('/config.yaml',)
  84. def test_collect_config_filenames_skips_etc_borgmatic_dot_d_if_it_does_not_exist():
  85. config_paths = ('config.yaml', '/etc/borgmatic.d')
  86. mock_path = flexmock(module.os.path)
  87. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  88. mock_path.should_receive('exists').with_args('/etc/borgmatic.d').and_return(False)
  89. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  90. mock_path.should_receive('isdir').with_args('/etc/borgmatic.d').and_return(True)
  91. mock_path.should_receive('abspath').replace_with(lambda path: module.os.path.join('/', path))
  92. config_filenames = tuple(module.collect_config_filenames(config_paths))
  93. assert config_filenames == ('/config.yaml',)
  94. def test_collect_config_filenames_skips_non_canonical_etc_borgmatic_dot_d_if_it_does_not_exist():
  95. config_paths = ('config.yaml', '/etc/../etc/borgmatic.d')
  96. mock_path = flexmock(module.os.path)
  97. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  98. mock_path.should_receive('exists').with_args('/etc/../etc/borgmatic.d').and_return(False)
  99. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  100. mock_path.should_receive('isdir').with_args('/etc/../etc/borgmatic.d').and_return(True)
  101. mock_path.should_receive('abspath').replace_with(lambda path: module.os.path.join('/', path))
  102. config_filenames = tuple(module.collect_config_filenames(config_paths))
  103. assert config_filenames == ('/config.yaml',)
  104. def test_collect_config_filenames_includes_other_directory_if_it_does_not_exist():
  105. config_paths = ('config.yaml', '/my/directory')
  106. mock_path = flexmock(module.os.path)
  107. mock_path.should_receive('exists').with_args('config.yaml').and_return(True)
  108. mock_path.should_receive('exists').with_args('/my/directory').and_return(False)
  109. mock_path.should_receive('isdir').with_args('config.yaml').and_return(False)
  110. mock_path.should_receive('isdir').with_args('/my/directory').and_return(True)
  111. mock_path.should_receive('abspath').replace_with(lambda path: module.os.path.join('/', path))
  112. config_filenames = tuple(module.collect_config_filenames(config_paths))
  113. assert config_filenames == ('/config.yaml', '/my/directory')