test_collect.py 7.9 KB

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