| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 | from flexmock import flexmockimport borgmatic.hooks.monitoring.monitorfrom borgmatic.hooks.monitoring import zabbix as moduleSERVER = 'https://zabbix.com/zabbix/api_jsonrpc.php'ITEMID = 55105USERNAME = 'testuser'PASSWORD = 'fakepassword'API_KEY = 'fakekey'HOST = 'borg-server'KEY = 'borg.status'VALUE = 'fail'DATA_HOST_KEY = {    'jsonrpc': '2.0',    'method': 'history.push',    'params': {'host': HOST, 'key': KEY, 'value': VALUE},    'id': 1,}DATA_HOST_KEY_WITH_KEY_VALUE = {    'jsonrpc': '2.0',    'method': 'history.push',    'params': {'host': HOST, 'key': KEY, 'value': VALUE},    'id': 1,}DATA_ITEMID = {    'jsonrpc': '2.0',    'method': 'history.push',    'params': {'itemid': ITEMID, 'value': VALUE},    'id': 1,}DATA_HOST_KEY_WITH_ITEMID = {    'jsonrpc': '2.0',    'method': 'history.push',    'params': {'itemid': ITEMID, 'value': VALUE},    'id': 1,}DATA_USER_LOGIN = {    'jsonrpc': '2.0',    'method': 'user.login',    'params': {'username': USERNAME, 'password': PASSWORD},    'id': 1,}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}',}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 Nonedef 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 Nonedef 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 Nonedef 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 Nonedef test_ping_monitor_with_non_matching_state_bails():    hook_config = {'api_key': API_KEY}    flexmock(module).should_receive('send_zabbix_request').never()    module.ping_monitor(        hook_config,        {},        'config.yaml',        borgmatic.hooks.monitoring.monitor.State.START,        monitoring_log_level=1,        dry_run=False,    )def test_ping_monitor_config_with_api_key_only_bails():    # This test should exit early since only providing an API KEY is not enough    # for the hook to work    hook_config = {'api_key': API_KEY}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_with_host_only_bails():    # This test should exit early since only providing a HOST is not enough    # for the hook to work    hook_config = {'host': HOST}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_with_key_only_bails():    # This test should exit early since only providing a KEY is not enough    # for the hook to work    hook_config = {'key': KEY}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_with_server_only_bails():    # This test should exit early since only providing a SERVER is not enough    # for the hook to work    hook_config = {'server': SERVER}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_user_password_no_zabbix_data_bails():    # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to    hook_config = {'server': SERVER, 'username': USERNAME, 'password': PASSWORD}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_api_key_no_zabbix_data_bails():    # This test should exit early since there are HOST/KEY or ITEMID provided to publish data to    hook_config = {'server': SERVER, 'api_key': API_KEY}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_itemid_no_auth_data_bails():    # This test should exit early since there is no authentication provided    # and Zabbix requires authentication to use it's API    hook_config = {'server': SERVER, 'itemid': ITEMID}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_host_and_key_no_auth_data_bails():    # This test should exit early since there is no authentication provided    # and Zabbix requires authentication to use it's API    hook_config = {'server': SERVER, 'host': HOST, 'key': KEY}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_host_and_key_with_api_key_auth_data_successful():    # 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,        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,        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_host_and_missing_key_bails():    hook_config = {'server': SERVER, 'host': HOST, 'api_key': API_KEY}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_key_and_missing_host_bails():    hook_config = {'server': SERVER, 'key': KEY, 'api_key': API_KEY}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_host_and_key_with_username_password_auth_data_successful():    # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD    # to authenticate and HOST/KEY to know which item to populate in Zabbix.    hook_config = {        'server': SERVER,        'host': HOST,        'key': KEY,        'username': USERNAME,        'password': PASSWORD,    }    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_LOGIN,        data=DATA_USER_LOGIN,    ).and_return('fakekey').once()    flexmock(module.logger).should_receive('warning').never()    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,        data=DATA_USER_LOGOUT,    ).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_host_and_key_with_username_password_auth_data_and_auth_post_error_bails():    hook_config = {        'server': SERVER,        'host': HOST,        'key': KEY,        'username': USERNAME,        'password': PASSWORD,    }    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_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,        data=DATA_USER_LOGOUT,    ).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_host_and_key_with_username_and_missing_password_bails():    hook_config = {        'server': SERVER,        'host': HOST,        'key': KEY,        'username': USERNAME,    }    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_host_and_key_with_password_and_missing_username_bails():    hook_config = {        'server': SERVER,        'host': HOST,        'key': KEY,        'password': PASSWORD,    }    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).replace_with(lambda value, config: value)    flexmock(module.logger).should_receive('warning').once()    flexmock(module).should_receive('send_zabbix_request').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_itemid_with_api_key_auth_data_successful():    # 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, 'itemid': ITEMID, '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,        data=DATA_ITEMID,    ).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_itemid_with_username_password_auth_data_successful():    # This test should simulate a successful POST to a Zabbix server. This test uses USERNAME/PASSWORD    # to authenticate and HOST/KEY to know which item to populate in Zabbix.    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)    flexmock(module).should_receive('send_zabbix_request').with_args(        f'{SERVER}',        headers=AUTH_HEADERS_LOGIN,        data=DATA_USER_LOGIN,    ).and_return('fakekey').once()    flexmock(module.logger).should_receive('warning').never()    flexmock(module).should_receive('send_zabbix_request').with_args(        f'{SERVER}',        headers=AUTH_HEADERS,        data=DATA_HOST_KEY_WITH_ITEMID,    ).once()    flexmock(module).should_receive('send_zabbix_request').with_args(        f'{SERVER}',        headers=AUTH_HEADERS,        data=DATA_USER_LOGOUT,    ).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_with_credential_error_bails():    hook_config = {'server': SERVER, 'itemid': ITEMID, 'username': USERNAME, 'password': PASSWORD}    flexmock(module.borgmatic.hooks.credential.parse).should_receive(        'resolve_credential'    ).and_raise(ValueError)    flexmock(module).should_receive('send_zabbix_request').never()    flexmock(module.logger).should_receive('warning').once()    module.ping_monitor(        hook_config,        {},        'config.yaml',        borgmatic.hooks.monitoring.monitor.State.FAIL,        monitoring_log_level=1,        dry_run=False,    )
 |