浏览代码

Add missing test coverage and fix incorrect test expectations (#855).

Dan Helfman 4 月之前
父节点
当前提交
1ad6be2077
共有 2 个文件被更改,包括 31 次插入8 次删除
  1. 1 1
      borgmatic/hooks/monitoring/sentry.py
  2. 30 7
      tests/unit/hooks/monitoring/test_sentry.py

+ 1 - 1
borgmatic/hooks/monitoring/sentry.py

@@ -16,7 +16,7 @@ def initialize_monitor(
 
 
 
 
 DATA_SOURCE_NAME_URL_PATTERN = re.compile(
 DATA_SOURCE_NAME_URL_PATTERN = re.compile(
-    '^(?P<protocol>.*)://(?P<username>.*)@(?P<hostname>.*)/(?P<project_id>.*)$'
+    '^(?P<protocol>.+)://(?P<username>.+)@(?P<hostname>.+)/(?P<project_id>.+)$'
 )
 )
 
 
 
 

+ 30 - 7
tests/unit/hooks/monitoring/test_sentry.py

@@ -28,9 +28,9 @@ def test_ping_monitor_constructs_cron_url_and_pings_it(state, configured_states,
     if configured_states:
     if configured_states:
         hook_config['states'] = configured_states
         hook_config['states'] = configured_states
 
 
-    flexmock(module.requests).should_receive('get').with_args(
+    flexmock(module.requests).should_receive('post').with_args(
         f'https://o294220.ingest.us.sentry.io/api/203069/cron/test/5f80ec/?status={expected_status}'
         f'https://o294220.ingest.us.sentry.io/api/203069/cron/test/5f80ec/?status={expected_status}'
-    ).and_return(flexmock(ok=True))
+    ).and_return(flexmock(ok=True)).once()
 
 
     module.ping_monitor(
     module.ping_monitor(
         hook_config,
         hook_config,
@@ -48,7 +48,7 @@ def test_ping_monitor_with_unconfigured_state_bails():
         'monitor_slug': 'test',
         'monitor_slug': 'test',
         'states': ['fail'],
         'states': ['fail'],
     }
     }
-    flexmock(module.requests).should_receive('get').never()
+    flexmock(module.requests).should_receive('post').never()
 
 
     module.ping_monitor(
     module.ping_monitor(
         hook_config,
         hook_config,
@@ -67,7 +67,6 @@ def test_ping_monitor_with_unconfigured_state_bails():
         'https://o294220.ingest.us.sentry.io/203069',
         'https://o294220.ingest.us.sentry.io/203069',
         'https://5f80ec@/203069',
         'https://5f80ec@/203069',
         'https://5f80ec@o294220.ingest.us.sentry.io',
         'https://5f80ec@o294220.ingest.us.sentry.io',
-        'https://5f80ec@o294220.ingest.us.sentry.io/203069/',
     ),
     ),
 )
 )
 def test_ping_monitor_with_invalid_data_source_name_url_bails(data_source_name_url):
 def test_ping_monitor_with_invalid_data_source_name_url_bails(data_source_name_url):
@@ -76,7 +75,7 @@ def test_ping_monitor_with_invalid_data_source_name_url_bails(data_source_name_u
         'monitor_slug': 'test',
         'monitor_slug': 'test',
     }
     }
 
 
-    flexmock(module.requests).should_receive('get').never()
+    flexmock(module.requests).should_receive('post').never()
 
 
     module.ping_monitor(
     module.ping_monitor(
         hook_config,
         hook_config,
@@ -95,7 +94,7 @@ def test_ping_monitor_with_invalid_sentry_state_bails():
         # This should never actually happen in practice, because the config schema prevents it.
         # This should never actually happen in practice, because the config schema prevents it.
         'states': ['log'],
         'states': ['log'],
     }
     }
-    flexmock(module.requests).should_receive('get').never()
+    flexmock(module.requests).should_receive('post').never()
 
 
     module.ping_monitor(
     module.ping_monitor(
         hook_config,
         hook_config,
@@ -112,7 +111,7 @@ def test_ping_monitor_with_dry_run_bails():
         'data_source_name_url': 'https://5f80ec@o294220.ingest.us.sentry.io/203069',
         'data_source_name_url': 'https://5f80ec@o294220.ingest.us.sentry.io/203069',
         'monitor_slug': 'test',
         'monitor_slug': 'test',
     }
     }
-    flexmock(module.requests).should_receive('get').never()
+    flexmock(module.requests).should_receive('post').never()
 
 
     module.ping_monitor(
     module.ping_monitor(
         hook_config,
         hook_config,
@@ -122,3 +121,27 @@ def test_ping_monitor_with_dry_run_bails():
         monitoring_log_level=1,
         monitoring_log_level=1,
         dry_run=True,
         dry_run=True,
     )
     )
+
+
+def test_ping_monitor_with_network_error_does_not_raise():
+    hook_config = {
+        'data_source_name_url': 'https://5f80ec@o294220.ingest.us.sentry.io/203069',
+        'monitor_slug': 'test',
+    }
+
+    response = flexmock(ok=False)
+    response.should_receive('raise_for_status').and_raise(
+        module.requests.exceptions.ConnectionError
+    )
+    flexmock(module.requests).should_receive('post').with_args(
+        'https://o294220.ingest.us.sentry.io/api/203069/cron/test/5f80ec/?status=in_progress'
+    ).and_return(response).once()
+
+    module.ping_monitor(
+        hook_config,
+        {},
+        'config.yaml',
+        borgmatic.hooks.monitoring.monitor.State.START,
+        monitoring_log_level=1,
+        dry_run=False,
+    )