test_config.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from subprocess import CalledProcessError
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks.data_source import config as module
  5. def test_resolve_database_option_uses_config_value():
  6. data_source = {'option': 'original_value', 'restore_option': 'restore_value'}
  7. assert module.resolve_database_option('option', data_source) == 'original_value'
  8. def test_resolve_database_option_with_restore_uses_restore_value():
  9. data_source = {'option': 'original_value', 'restore_option': 'restore_value'}
  10. assert module.resolve_database_option('option', data_source, restore=True) == 'restore_value'
  11. def test_resolve_database_option_with_connection_params_uses_connection_params_value():
  12. data_source = {'option': 'original_value', 'restore_option': 'restore_value'}
  13. connection_params = {'option': 'connection_value'}
  14. assert (
  15. module.resolve_database_option('option', data_source, connection_params)
  16. == 'connection_value'
  17. )
  18. def test_resolve_database_option_with_restore_and_connection_params_uses_connection_params_value():
  19. data_source = {'option': 'original_value', 'restore_option': 'restore_value'}
  20. connection_params = {'option': 'connection_value'}
  21. assert (
  22. module.resolve_database_option('option', data_source, connection_params, restore=True)
  23. == 'connection_value'
  24. )
  25. def test_resolve_database_option_with_hostname_uses_hostname_specific_function():
  26. data_source = {'hostname': 'original_value'}
  27. connection_params = {'hostname': 'connection_value'}
  28. flexmock(module).should_receive('get_hostname_from_config').and_return('special_value').once()
  29. assert (
  30. module.resolve_database_option('hostname', data_source, connection_params, restore=True)
  31. == 'special_value'
  32. )
  33. def test_get_hostname_from_config_gets_container_ip():
  34. data_source = {
  35. 'container': 'original_container',
  36. 'hostname': 'original_hostname',
  37. 'restore_container': 'restore_container',
  38. 'restore_hostname': 'restore_hostname',
  39. }
  40. flexmock(module).should_receive('get_ip_from_container').with_args(
  41. 'original_container'
  42. ).and_return('container_ip_1')
  43. assert module.get_hostname_from_config(data_source) == 'container_ip_1'
  44. def test_get_hostname_from_config_gets_connection_params_container_ip():
  45. data_source = {
  46. 'container': 'original_container',
  47. 'hostname': 'original_hostname',
  48. 'restore_container': 'restore_container',
  49. 'restore_hostname': 'restore_hostname',
  50. }
  51. connection_params = {'container': 'connection_container', 'hostname': 'connection_hostname'}
  52. flexmock(module).should_receive('get_ip_from_container').with_args('original_container').never()
  53. flexmock(module).should_receive('get_ip_from_container').with_args(
  54. 'connection_container'
  55. ).and_return('container_ip_2')
  56. assert module.get_hostname_from_config(data_source, connection_params) == 'container_ip_2'
  57. def test_get_hostname_from_config_gets_restore_container_ip():
  58. data_source = {
  59. 'container': 'original_container',
  60. 'hostname': 'original_hostname',
  61. 'restore_container': 'restore_container',
  62. 'restore_hostname': 'restore_hostname',
  63. }
  64. flexmock(module).should_receive('get_ip_from_container').with_args('original_container').never()
  65. flexmock(module).should_receive('get_ip_from_container').with_args(
  66. 'restore_container'
  67. ).and_return('container_ip_3')
  68. assert module.get_hostname_from_config(data_source, restore=True) == 'container_ip_3'
  69. def test_get_ip_from_container_without_engines_errors():
  70. flexmock(module.shutil).should_receive('which').and_return(None).and_return(None)
  71. with pytest.raises(ValueError):
  72. module.get_ip_from_container('yolo')
  73. def test_get_ip_from_container_parses_top_level_ip_address():
  74. flexmock(module.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')
  75. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  76. '{"IPAddress": "1.2.3.4"}'
  77. )
  78. assert module.get_ip_from_container('yolo') == '1.2.3.4'
  79. def test_get_ip_from_container_parses_network_ip_address():
  80. flexmock(module.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')
  81. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  82. '{"Networks": {"my_network": {"IPAddress": "5.6.7.8"}}}'
  83. )
  84. assert module.get_ip_from_container('yolo') == '5.6.7.8'
  85. def test_get_ip_from_container_without_container_errors():
  86. flexmock(module.shutil).should_receive('which').and_return('/usr/bin/podman')
  87. flexmock(module).should_receive('execute_command_and_capture_output').and_raise(
  88. CalledProcessError, 1, ['/usr/bin/podman', 'inspect', 'yolo'], None, 'No such object'
  89. )
  90. with pytest.raises(CalledProcessError):
  91. module.get_ip_from_container('does not exist')
  92. def test_get_ip_from_container_without_network_errors():
  93. flexmock(module.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')
  94. flexmock(module).should_receive('execute_command_and_capture_output').and_return('{}')
  95. with pytest.raises(ValueError) as exc_info:
  96. module.get_ip_from_container('yolo')
  97. assert 'Could not determine ip address for container' in str(exc_info.value)
  98. def test_get_ip_from_container_with_broken_output_errors():
  99. flexmock(module.shutil).should_receive('which').and_return(None).and_return('/usr/bin/podman')
  100. flexmock(module).should_receive('execute_command_and_capture_output').and_return('abc')
  101. with pytest.raises(ValueError) as exc_info:
  102. module.get_ip_from_container('yolo')
  103. assert 'Could not decode JSON output' in str(exc_info.value)