|
@@ -24,7 +24,6 @@ DATA_HOST_KEY_WITH_KEY_VALUE = {
|
|
|
'method': 'history.push',
|
|
|
'params': {'host': HOST, 'key': KEY, 'value': VALUE},
|
|
|
'id': 1,
|
|
|
- 'auth': '3fe6ed01a69ebd79907a120bcd04e494',
|
|
|
}
|
|
|
|
|
|
DATA_ITEMID = {
|
|
@@ -39,7 +38,6 @@ DATA_HOST_KEY_WITH_ITEMID = {
|
|
|
'method': 'history.push',
|
|
|
'params': {'itemid': ITEMID, 'value': VALUE},
|
|
|
'id': 1,
|
|
|
- 'auth': '3fe6ed01a69ebd79907a120bcd04e494',
|
|
|
}
|
|
|
|
|
|
DATA_USER_LOGIN = {
|
|
@@ -49,17 +47,99 @@ DATA_USER_LOGIN = {
|
|
|
'id': 1,
|
|
|
}
|
|
|
|
|
|
-AUTH_HEADERS_API_KEY = {
|
|
|
+DATA_USER_LOGOUT = {
|
|
|
+ 'jsonrpc': '2.0',
|
|
|
+ 'method': 'user.logout',
|
|
|
+ 'params': [],
|
|
|
+ 'id': 1,
|
|
|
+}
|
|
|
+
|
|
|
+AUTH_HEADERS_LOGIN = {
|
|
|
+ 'Content-Type': 'application/json-rpc',
|
|
|
+}
|
|
|
+
|
|
|
+AUTH_HEADERS = {
|
|
|
'Content-Type': 'application/json-rpc',
|
|
|
'Authorization': f'Bearer {API_KEY}',
|
|
|
}
|
|
|
|
|
|
-AUTH_HEADERS_USERNAME_PASSWORD = {'Content-Type': 'application/json-rpc'}
|
|
|
+
|
|
|
+def test_send_zabbix_request_with_post_error_bails():
|
|
|
+ server = flexmock()
|
|
|
+ headers = flexmock()
|
|
|
+ data = {'method': 'do.stuff'}
|
|
|
+ response = flexmock(ok=False)
|
|
|
+ response.should_receive('raise_for_status').and_raise(
|
|
|
+ module.requests.exceptions.RequestException
|
|
|
+ )
|
|
|
+
|
|
|
+ flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ server, headers=headers, json=data
|
|
|
+ ).and_return(response)
|
|
|
+
|
|
|
+ assert module.send_zabbix_request(server, headers, data) is None
|
|
|
+
|
|
|
+
|
|
|
+def test_send_zabbix_request_with_invalid_json_response_bails():
|
|
|
+ server = flexmock()
|
|
|
+ headers = flexmock()
|
|
|
+ data = {'method': 'do.stuff'}
|
|
|
+ flexmock(module.requests.exceptions.JSONDecodeError).should_receive('__init__')
|
|
|
+ response = flexmock(ok=True)
|
|
|
+ response.should_receive('json').and_raise(module.requests.exceptions.JSONDecodeError)
|
|
|
+
|
|
|
+ flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ server, headers=headers, json=data
|
|
|
+ ).and_return(response)
|
|
|
+
|
|
|
+ assert module.send_zabbix_request(server, headers, data) is None
|
|
|
+
|
|
|
+
|
|
|
+def test_send_zabbix_request_with_success_returns_response_result():
|
|
|
+ server = flexmock()
|
|
|
+ headers = flexmock()
|
|
|
+ data = {'method': 'do.stuff'}
|
|
|
+ response = flexmock(ok=True)
|
|
|
+ response.should_receive('json').and_return({'result': {'foo': 'bar'}})
|
|
|
+
|
|
|
+ flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ server, headers=headers, json=data
|
|
|
+ ).and_return(response)
|
|
|
+
|
|
|
+ assert module.send_zabbix_request(server, headers, data) == {'foo': 'bar'}
|
|
|
+
|
|
|
+
|
|
|
+def test_send_zabbix_request_with_success_passes_through_missing_result():
|
|
|
+ server = flexmock()
|
|
|
+ headers = flexmock()
|
|
|
+ data = {'method': 'do.stuff'}
|
|
|
+ response = flexmock(ok=True)
|
|
|
+ response.should_receive('json').and_return({})
|
|
|
+
|
|
|
+ flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ server, headers=headers, json=data
|
|
|
+ ).and_return(response)
|
|
|
+
|
|
|
+ assert module.send_zabbix_request(server, headers, data) is None
|
|
|
+
|
|
|
+
|
|
|
+def test_send_zabbix_request_with_error_bails():
|
|
|
+ server = flexmock()
|
|
|
+ headers = flexmock()
|
|
|
+ data = {'method': 'do.stuff'}
|
|
|
+ response = flexmock(ok=True)
|
|
|
+ response.should_receive('json').and_return({'result': {'data': [{'error': 'oops'}]}})
|
|
|
+
|
|
|
+ flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ server, headers=headers, json=data
|
|
|
+ ).and_return(response)
|
|
|
+
|
|
|
+ assert module.send_zabbix_request(server, headers, data) is None
|
|
|
|
|
|
|
|
|
def test_ping_monitor_with_non_matching_state_bails():
|
|
|
hook_config = {'api_key': API_KEY}
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -79,7 +159,7 @@ def test_ping_monitor_config_with_api_key_only_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -99,7 +179,7 @@ def test_ping_monitor_config_with_host_only_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -119,7 +199,7 @@ def test_ping_monitor_config_with_key_only_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -139,7 +219,7 @@ def test_ping_monitor_config_with_server_only_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -158,7 +238,7 @@ def test_ping_monitor_config_user_password_no_zabbix_data_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -177,7 +257,7 @@ def test_ping_monitor_config_api_key_no_zabbix_data_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -197,7 +277,7 @@ def test_ping_monitor_config_itemid_no_auth_data_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -217,7 +297,7 @@ def test_ping_monitor_config_host_and_key_no_auth_data_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -236,11 +316,35 @@ def test_ping_monitor_config_host_and_key_with_api_key_auth_data_successful():
|
|
|
flexmock(module.borgmatic.hooks.credential.parse).should_receive(
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
+ f'{SERVER}',
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_HOST_KEY,
|
|
|
+ ).once()
|
|
|
+ flexmock(module.logger).should_receive('warning').never()
|
|
|
+
|
|
|
+ module.ping_monitor(
|
|
|
+ hook_config,
|
|
|
+ {},
|
|
|
+ 'config.yaml',
|
|
|
+ borgmatic.hooks.monitoring.monitor.State.FAIL,
|
|
|
+ monitoring_log_level=1,
|
|
|
+ dry_run=False,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def test_ping_monitor_config_adds_missing_api_endpoint_to_server_url():
|
|
|
+ # This test should simulate a successful POST to a Zabbix server. This test uses API_KEY
|
|
|
+ # to authenticate and HOST/KEY to know which item to populate in Zabbix.
|
|
|
+ hook_config = {'server': SERVER, 'host': HOST, 'key': KEY, 'api_key': API_KEY}
|
|
|
+ flexmock(module.borgmatic.hooks.credential.parse).should_receive(
|
|
|
+ 'resolve_credential'
|
|
|
+ ).replace_with(lambda value, config: value)
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_API_KEY,
|
|
|
- json=DATA_HOST_KEY,
|
|
|
- ).and_return(flexmock(ok=True)).once()
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_HOST_KEY,
|
|
|
+ ).once()
|
|
|
flexmock(module.logger).should_receive('warning').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
@@ -259,7 +363,7 @@ def test_ping_monitor_config_host_and_missing_key_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -277,7 +381,7 @@ def test_ping_monitor_config_key_and_missing_host_bails():
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -303,24 +407,26 @@ def test_ping_monitor_config_host_and_key_with_username_password_auth_data_succe
|
|
|
flexmock(module.borgmatic.hooks.credential.parse).should_receive(
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
- auth_response = flexmock(ok=True)
|
|
|
- auth_response.should_receive('json').and_return(
|
|
|
- {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
|
|
|
- )
|
|
|
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_USERNAME_PASSWORD,
|
|
|
- json=DATA_USER_LOGIN,
|
|
|
- ).and_return(auth_response).once()
|
|
|
+ headers=AUTH_HEADERS_LOGIN,
|
|
|
+ data=DATA_USER_LOGIN,
|
|
|
+ ).and_return('fakekey').once()
|
|
|
|
|
|
flexmock(module.logger).should_receive('warning').never()
|
|
|
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
+ f'{SERVER}',
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_HOST_KEY_WITH_KEY_VALUE,
|
|
|
+ ).once()
|
|
|
+
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_USERNAME_PASSWORD,
|
|
|
- json=DATA_HOST_KEY_WITH_KEY_VALUE,
|
|
|
- ).and_return(flexmock(ok=True)).once()
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_USER_LOGOUT,
|
|
|
+ ).once()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -344,24 +450,22 @@ def test_ping_monitor_config_host_and_key_with_username_password_auth_data_and_a
|
|
|
flexmock(module.borgmatic.hooks.credential.parse).should_receive(
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
- auth_response = flexmock(ok=False)
|
|
|
- auth_response.should_receive('json').and_return(
|
|
|
- {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
|
|
|
- )
|
|
|
- auth_response.should_receive('raise_for_status').and_raise(
|
|
|
- module.requests.ConnectionError
|
|
|
- ).once()
|
|
|
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_USERNAME_PASSWORD,
|
|
|
- json=DATA_USER_LOGIN,
|
|
|
- ).and_return(auth_response).once()
|
|
|
- flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ headers=AUTH_HEADERS_LOGIN,
|
|
|
+ data=DATA_USER_LOGIN,
|
|
|
+ ).and_return(None).once()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
+ f'{SERVER}',
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_HOST_KEY_WITH_KEY_VALUE,
|
|
|
+ ).never()
|
|
|
+
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_USERNAME_PASSWORD,
|
|
|
- json=DATA_HOST_KEY_WITH_KEY_VALUE,
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_USER_LOGOUT,
|
|
|
).never()
|
|
|
|
|
|
module.ping_monitor(
|
|
@@ -386,7 +490,7 @@ def test_ping_monitor_config_host_and_key_with_username_and_missing_password_bai
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -410,7 +514,7 @@ def test_ping_monitor_config_host_and_key_with_password_and_missing_username_bai
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -429,11 +533,11 @@ def test_ping_monitor_config_itemid_with_api_key_auth_data_successful():
|
|
|
flexmock(module.borgmatic.hooks.credential.parse).should_receive(
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_API_KEY,
|
|
|
- json=DATA_ITEMID,
|
|
|
- ).and_return(flexmock(ok=True)).once()
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_ITEMID,
|
|
|
+ ).once()
|
|
|
flexmock(module.logger).should_receive('warning').never()
|
|
|
|
|
|
module.ping_monitor(
|
|
@@ -454,63 +558,26 @@ def test_ping_monitor_config_itemid_with_username_password_auth_data_successful(
|
|
|
flexmock(module.borgmatic.hooks.credential.parse).should_receive(
|
|
|
'resolve_credential'
|
|
|
).replace_with(lambda value, config: value)
|
|
|
- auth_response = flexmock(ok=True)
|
|
|
- auth_response.should_receive('json').and_return(
|
|
|
- {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
|
|
|
- )
|
|
|
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_USERNAME_PASSWORD,
|
|
|
- json=DATA_USER_LOGIN,
|
|
|
- ).and_return(auth_response).once()
|
|
|
+ headers=AUTH_HEADERS_LOGIN,
|
|
|
+ data=DATA_USER_LOGIN,
|
|
|
+ ).and_return('fakekey').once()
|
|
|
|
|
|
flexmock(module.logger).should_receive('warning').never()
|
|
|
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_USERNAME_PASSWORD,
|
|
|
- json=DATA_HOST_KEY_WITH_ITEMID,
|
|
|
- ).and_return(flexmock(ok=True)).once()
|
|
|
-
|
|
|
- module.ping_monitor(
|
|
|
- hook_config,
|
|
|
- {},
|
|
|
- 'config.yaml',
|
|
|
- borgmatic.hooks.monitoring.monitor.State.FAIL,
|
|
|
- monitoring_log_level=1,
|
|
|
- dry_run=False,
|
|
|
- )
|
|
|
-
|
|
|
-
|
|
|
-def test_ping_monitor_config_itemid_with_username_password_auth_data_and_push_post_error_bails():
|
|
|
- hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}
|
|
|
-
|
|
|
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
|
|
|
- 'resolve_credential'
|
|
|
- ).replace_with(lambda value, config: value)
|
|
|
- auth_response = flexmock(ok=True)
|
|
|
- auth_response.should_receive('json').and_return(
|
|
|
- {'jsonrpc': '2.0', 'result': '3fe6ed01a69ebd79907a120bcd04e494', 'id': 1}
|
|
|
- )
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_HOST_KEY_WITH_ITEMID,
|
|
|
+ ).once()
|
|
|
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').with_args(
|
|
|
f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_USERNAME_PASSWORD,
|
|
|
- json=DATA_USER_LOGIN,
|
|
|
- ).and_return(auth_response).once()
|
|
|
-
|
|
|
- push_response = flexmock(ok=False)
|
|
|
- push_response.should_receive('raise_for_status').and_raise(
|
|
|
- module.requests.ConnectionError
|
|
|
+ headers=AUTH_HEADERS,
|
|
|
+ data=DATA_USER_LOGOUT,
|
|
|
).once()
|
|
|
- flexmock(module.requests).should_receive('post').with_args(
|
|
|
- f'{SERVER}',
|
|
|
- headers=AUTH_HEADERS_USERNAME_PASSWORD,
|
|
|
- json=DATA_HOST_KEY_WITH_ITEMID,
|
|
|
- ).and_return(push_response).once()
|
|
|
-
|
|
|
- flexmock(module.logger).should_receive('warning').once()
|
|
|
|
|
|
module.ping_monitor(
|
|
|
hook_config,
|
|
@@ -528,7 +595,7 @@ def test_ping_monitor_with_credential_error_bails():
|
|
|
flexmock(module.borgmatic.hooks.credential.parse).should_receive(
|
|
|
'resolve_credential'
|
|
|
).and_raise(ValueError)
|
|
|
- flexmock(module.requests).should_receive('post').never()
|
|
|
+ flexmock(module).should_receive('send_zabbix_request').never()
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
|
|
|
module.ping_monitor(
|