test_environment.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from flexmock import flexmock
  2. from borgmatic.borg import environment as module
  3. def test_make_environment_with_passcommand_should_call_it_and_set_passphrase_file_descriptor_in_environment():
  4. flexmock(module.borgmatic.borg.passcommand).should_receive(
  5. 'get_passphrase_from_passcommand'
  6. ).and_return('passphrase')
  7. flexmock(module.os).should_receive('pipe').and_return((3, 4))
  8. flexmock(module.os).should_receive('write')
  9. flexmock(module.os).should_receive('close')
  10. flexmock(module.os).should_receive('set_inheritable')
  11. environment = module.make_environment({'encryption_passcommand': 'command'})
  12. assert not environment.get('BORG_PASSCOMMAND')
  13. assert environment.get('BORG_PASSPHRASE_FD') == '3'
  14. def test_make_environment_with_passphrase_should_set_environment():
  15. flexmock(module.borgmatic.borg.passcommand).should_receive(
  16. 'get_passphrase_from_passcommand'
  17. ).and_return(None)
  18. flexmock(module.os).should_receive('pipe').never()
  19. flexmock(module.os.environ).should_receive('get').and_return(None)
  20. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  21. 'resolve_credential'
  22. ).replace_with(lambda value, config: value)
  23. environment = module.make_environment({'encryption_passphrase': 'pass'})
  24. assert environment.get('BORG_PASSPHRASE') == 'pass'
  25. def test_make_environment_with_credential_tag_passphrase_should_load_it_and_set_environment():
  26. config = {'encryption_passphrase': '{credential systemd pass}'}
  27. flexmock(module.borgmatic.borg.passcommand).should_receive(
  28. 'get_passphrase_from_passcommand'
  29. ).and_return(None)
  30. flexmock(module.os).should_receive('pipe').never()
  31. flexmock(module.os.environ).should_receive('get').and_return(None)
  32. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  33. 'resolve_credential',
  34. ).with_args('{credential systemd pass}', config).and_return('pass')
  35. environment = module.make_environment(config)
  36. assert environment.get('BORG_PASSPHRASE') == 'pass'
  37. def test_make_environment_with_ssh_command_should_set_environment():
  38. flexmock(module.borgmatic.borg.passcommand).should_receive(
  39. 'get_passphrase_from_passcommand'
  40. ).and_return(None)
  41. flexmock(module.os).should_receive('pipe').never()
  42. flexmock(module.os.environ).should_receive('get').and_return(None)
  43. environment = module.make_environment({'ssh_command': 'ssh -C'})
  44. assert environment.get('BORG_RSH') == 'ssh -C'
  45. def test_make_environment_without_configuration_sets_certain_environment_variables():
  46. flexmock(module.borgmatic.borg.passcommand).should_receive(
  47. 'get_passphrase_from_passcommand'
  48. ).and_return(None)
  49. flexmock(module.os).should_receive('pipe').never()
  50. flexmock(module.os.environ).should_receive('get').and_return(None)
  51. environment = module.make_environment({})
  52. # Default environment variables.
  53. assert environment == {
  54. 'BORG_EXIT_CODES': 'modern',
  55. 'BORG_RELOCATED_REPO_ACCESS_IS_OK': 'no',
  56. 'BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK': 'no',
  57. }
  58. def test_make_environment_without_configuration_does_not_set_certain_environment_variables_if_already_set():
  59. flexmock(module.borgmatic.borg.passcommand).should_receive(
  60. 'get_passphrase_from_passcommand'
  61. ).and_return(None)
  62. flexmock(module.os).should_receive('pipe').never()
  63. flexmock(module.os.environ).should_receive('get').with_args(
  64. 'BORG_RELOCATED_REPO_ACCESS_IS_OK'
  65. ).and_return('yup')
  66. flexmock(module.os.environ).should_receive('get').with_args(
  67. 'BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK'
  68. ).and_return('nah')
  69. environment = module.make_environment({})
  70. assert environment == {'BORG_EXIT_CODES': 'modern'}
  71. def test_make_environment_with_relocated_repo_access_true_should_set_environment_yes():
  72. flexmock(module.borgmatic.borg.passcommand).should_receive(
  73. 'get_passphrase_from_passcommand'
  74. ).and_return(None)
  75. flexmock(module.os).should_receive('pipe').never()
  76. flexmock(module.os.environ).should_receive('get').and_return(None)
  77. environment = module.make_environment({'relocated_repo_access_is_ok': True})
  78. assert environment.get('BORG_RELOCATED_REPO_ACCESS_IS_OK') == 'yes'
  79. def test_make_environment_with_relocated_repo_access_false_should_set_environment_no():
  80. flexmock(module.borgmatic.borg.passcommand).should_receive(
  81. 'get_passphrase_from_passcommand'
  82. ).and_return(None)
  83. flexmock(module.os).should_receive('pipe').never()
  84. flexmock(module.os.environ).should_receive('get').and_return(None)
  85. environment = module.make_environment({'relocated_repo_access_is_ok': False})
  86. assert environment.get('BORG_RELOCATED_REPO_ACCESS_IS_OK') == 'no'
  87. def test_make_environment_check_i_know_what_i_am_doing_true_should_set_environment_YES():
  88. flexmock(module.borgmatic.borg.passcommand).should_receive(
  89. 'get_passphrase_from_passcommand'
  90. ).and_return(None)
  91. flexmock(module.os).should_receive('pipe').never()
  92. flexmock(module.os.environ).should_receive('get').and_return(None)
  93. environment = module.make_environment({'check_i_know_what_i_am_doing': True})
  94. assert environment.get('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING') == 'YES'
  95. def test_make_environment_check_i_know_what_i_am_doing_false_should_set_environment_NO():
  96. flexmock(module.borgmatic.borg.passcommand).should_receive(
  97. 'get_passphrase_from_passcommand'
  98. ).and_return(None)
  99. flexmock(module.os).should_receive('pipe').never()
  100. flexmock(module.os.environ).should_receive('get').and_return(None)
  101. environment = module.make_environment({'check_i_know_what_i_am_doing': False})
  102. assert environment.get('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING') == 'NO'
  103. def test_make_environment_with_integer_variable_value():
  104. flexmock(module.borgmatic.borg.passcommand).should_receive(
  105. 'get_passphrase_from_passcommand'
  106. ).and_return(None)
  107. flexmock(module.os).should_receive('pipe').never()
  108. flexmock(module.os.environ).should_receive('get').and_return(None)
  109. environment = module.make_environment({'borg_files_cache_ttl': 40})
  110. assert environment.get('BORG_FILES_CACHE_TTL') == '40'