forked from borgmatic-collective/borgmatic
Compare commits
12 Commits
c3678f10a6
...
4763c323d0
Author | SHA1 | Date |
---|---|---|
Pim Kunis | 4763c323d0 | |
Pim Kunis | eaa22be3db | |
Pim Kunis | a587e207f9 | |
Pim Kunis | db8079b699 | |
Pim Kunis | 5a989826a1 | |
Pim Kunis | 21f4266273 | |
Pim Kunis | e7252c7545 | |
Pim Kunis | 86011c8418 | |
Pim Kunis | f3295ccb4a | |
Dan Helfman | 06c2154e6a | |
Dan Helfman | ac1e1a9407 | |
Dan Helfman | 10933fd55b |
|
@ -93,5 +93,3 @@ trigger:
|
|||
- borgmatic-collective/borgmatic
|
||||
branch:
|
||||
- main
|
||||
event:
|
||||
- push
|
||||
|
|
6
NEWS
6
NEWS
|
@ -1,4 +1,9 @@
|
|||
1.8.3.dev0
|
||||
* #665: BREAKING: Simplify logging logic as follows: Syslog verbosity is now disabled by
|
||||
default, but setting the "--syslog-verbosity" flag enables it regardless of whether you're at an
|
||||
interactive console. Additionally, "--log-file-verbosity" and "--monitoring-verbosity" now
|
||||
default to 1 (info about steps borgmatic is taking) instead of 0. And both syslog logging and
|
||||
file logging can be enabled simultaneously.
|
||||
* #743: Add a monitoring hook for sending backup status and logs to to Grafana Loki. See the
|
||||
documentation for more information:
|
||||
https://torsion.org/borgmatic/docs/how-to/monitor-your-backups/#loki-hook
|
||||
|
@ -7,6 +12,7 @@
|
|||
* #754: Fix error handling to log command output as one record per line instead of truncating
|
||||
too-long output and swallowing the end of some Borg error messages.
|
||||
* #757: Update documentation so "sudo borgmatic" works for pipx borgmatic installations.
|
||||
* #761: Fix for borgmatic not stopping Borg immediately when the user presses ctrl-C.
|
||||
* Update documentation to recommend installing/upgrading borgmatic with pipx instead of pip. See the
|
||||
documentation for more information:
|
||||
https://torsion.org/borgmatic/docs/how-to/set-up-backups/#installation
|
||||
|
|
|
@ -259,28 +259,28 @@ def make_parsers():
|
|||
type=int,
|
||||
choices=range(-2, 3),
|
||||
default=0,
|
||||
help='Display verbose progress to the console (disabled, errors only, default, some, or lots: -2, -1, 0, 1, or 2)',
|
||||
help='Display verbose progress to the console: -2 (disabled), -1 (errors only), 0 (responses to actions, the default), 1 (info about steps borgmatic is taking), or 2 (debug)',
|
||||
)
|
||||
global_group.add_argument(
|
||||
'--syslog-verbosity',
|
||||
type=int,
|
||||
choices=range(-2, 3),
|
||||
default=0,
|
||||
help='Log verbose progress to syslog (disabled, errors only, default, some, or lots: -2, -1, 0, 1, or 2). Ignored when console is interactive or --log-file is given',
|
||||
default=-2,
|
||||
help='Log verbose progress to syslog: -2 (disabled, the default), -1 (errors only), 0 (responses to actions), 1 (info about steps borgmatic is taking), or 2 (debug)',
|
||||
)
|
||||
global_group.add_argument(
|
||||
'--log-file-verbosity',
|
||||
type=int,
|
||||
choices=range(-2, 3),
|
||||
default=0,
|
||||
help='Log verbose progress to log file (disabled, errors only, default, some, or lots: -2, -1, 0, 1, or 2). Only used when --log-file is given',
|
||||
default=1,
|
||||
help='When --log-file is given, log verbose progress to file: -2 (disabled), -1 (errors only), 0 (responses to actions), 1 (info about steps borgmatic is taking, the default), or 2 (debug)',
|
||||
)
|
||||
global_group.add_argument(
|
||||
'--monitoring-verbosity',
|
||||
type=int,
|
||||
choices=range(-2, 3),
|
||||
default=0,
|
||||
help='Log verbose progress to monitoring integrations that support logging (from disabled, errors only, default, some, or lots: -2, -1, 0, 1, or 2)',
|
||||
default=1,
|
||||
help='When a monitoring integration supporting logging is configured, log verbose progress to it: -2 (disabled), -1 (errors only), responses to actions (0), 1 (info about steps borgmatic is taking, the default), or 2 (debug)',
|
||||
)
|
||||
global_group.add_argument(
|
||||
'--log-file',
|
||||
|
|
|
@ -1306,6 +1306,99 @@ properties:
|
|||
example:
|
||||
- start
|
||||
- finish
|
||||
apprise:
|
||||
type: object
|
||||
required: ['services']
|
||||
additionalProperties: false
|
||||
properties:
|
||||
services:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
required:
|
||||
- url
|
||||
- label
|
||||
properties:
|
||||
url:
|
||||
type: string
|
||||
example: "gotify://hostname/token"
|
||||
label:
|
||||
type: string
|
||||
example: mastodon
|
||||
description: |
|
||||
A list of Apprise services to publish to with URLs
|
||||
and labels. The labels are used for logging.
|
||||
A full list of services and their configuration can be found
|
||||
at https://github.com/caronc/apprise/wiki.
|
||||
example:
|
||||
- url: "kodi://user@hostname"
|
||||
label: kodi
|
||||
- url: "line://Token@User"
|
||||
label: line
|
||||
start:
|
||||
type: object
|
||||
required: ['body']
|
||||
properties:
|
||||
title:
|
||||
type: string
|
||||
description: |
|
||||
Specify the message title. If left unspecified, no
|
||||
title is sent.
|
||||
example: Ping!
|
||||
body:
|
||||
type: string
|
||||
description: |
|
||||
Specify the message body.
|
||||
example: Starting backup process.
|
||||
finish:
|
||||
type: object
|
||||
required: ['body']
|
||||
properties:
|
||||
title:
|
||||
type: string
|
||||
description: |
|
||||
Specify the message title. If left unspecified, no
|
||||
title is sent.
|
||||
example: Ping!
|
||||
body:
|
||||
type: string
|
||||
description: |
|
||||
Specify the message body.
|
||||
example: Backups successfully made.
|
||||
fail:
|
||||
type: object
|
||||
required: ['body']
|
||||
properties:
|
||||
title:
|
||||
type: string
|
||||
description: |
|
||||
Specify the message title. If left unspecified, no
|
||||
title is sent.
|
||||
example: Ping!
|
||||
body:
|
||||
type: string
|
||||
description: |
|
||||
Specify the message body.
|
||||
example: Your backups have failed.
|
||||
states:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
enum:
|
||||
- start
|
||||
- finish
|
||||
- fail
|
||||
uniqueItems: true
|
||||
description: |
|
||||
List of one or more monitoring states to ping for: "start",
|
||||
"finish", and/or "fail". Defaults to pinging for failure
|
||||
only. For each selected state, corresponding configuration
|
||||
for the message title and body should be given. If any is
|
||||
left unspecified, a generic message is emitted instead.
|
||||
example:
|
||||
- start
|
||||
- finish
|
||||
|
||||
healthchecks:
|
||||
type: object
|
||||
required: ['ping_url']
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
import logging
|
||||
import operator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def initialize_monitor(
|
||||
ping_url, config, config_filename, monitoring_log_level, dry_run
|
||||
): # pragma: no cover
|
||||
'''
|
||||
No initialization is necessary for this monitor.
|
||||
'''
|
||||
pass
|
||||
|
||||
|
||||
def ping_monitor(hook_config, config, config_filename, state, monitoring_log_level, dry_run):
|
||||
'''
|
||||
Ping the configured Apprise service URLs. Use the given configuration filename in any log
|
||||
entries. If this is a dry run, then don't actually ping anything.
|
||||
'''
|
||||
try:
|
||||
import apprise
|
||||
from apprise import NotifyFormat, NotifyType
|
||||
except ImportError:
|
||||
logger.warning('Unable to import Apprise in monitoring hook')
|
||||
return
|
||||
|
||||
state_to_notify_type = {
|
||||
'start': NotifyType.INFO,
|
||||
'finish': NotifyType.SUCCESS,
|
||||
'fail': NotifyType.FAILURE,
|
||||
'log': NotifyType.INFO,
|
||||
}
|
||||
|
||||
run_states = hook_config.get('states', ['fail'])
|
||||
|
||||
if state.name.lower() not in run_states:
|
||||
return
|
||||
|
||||
state_config = hook_config.get(
|
||||
state.name.lower(),
|
||||
{
|
||||
'title': f'A borgmatic {state.name} event happened',
|
||||
'body': f'A borgmatic {state.name} event happened',
|
||||
},
|
||||
)
|
||||
|
||||
if not hook_config.get('services'):
|
||||
logger.info(f'{config_filename}: No Apprise services to ping')
|
||||
return
|
||||
|
||||
dry_run_string = ' (dry run; not actually pinging)' if dry_run else ''
|
||||
labels_string = ', '.join(map(operator.itemgetter('label'), hook_config.get('services')))
|
||||
logger.info(f'{config_filename}: Pinging Apprise services: {labels_string}{dry_run_string}')
|
||||
|
||||
apprise_object = apprise.Apprise()
|
||||
apprise_object.add(list(map(operator.itemgetter('url'), hook_config.get('services'))))
|
||||
|
||||
if dry_run:
|
||||
return
|
||||
|
||||
result = apprise_object.notify(
|
||||
title=state_config.get('title', ''),
|
||||
body=state_config.get('body'),
|
||||
body_format=NotifyFormat.TEXT,
|
||||
notify_type=state_to_notify_type[state.name.lower()],
|
||||
)
|
||||
|
||||
if result is False:
|
||||
logger.warning(f'{config_filename}: Error sending some Apprise notifications')
|
||||
|
||||
|
||||
def destroy_monitor(
|
||||
ping_url_or_uuid, config, config_filename, monitoring_log_level, dry_run
|
||||
): # pragma: no cover
|
||||
'''
|
||||
No destruction is necessary for this monitor.
|
||||
'''
|
||||
pass
|
|
@ -1,6 +1,7 @@
|
|||
import logging
|
||||
|
||||
from borgmatic.hooks import (
|
||||
apprise,
|
||||
cronhub,
|
||||
cronitor,
|
||||
healthchecks,
|
||||
|
@ -17,6 +18,7 @@ from borgmatic.hooks import (
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
HOOK_NAME_TO_MODULE = {
|
||||
'apprise': apprise,
|
||||
'cronhub': cronhub,
|
||||
'cronitor': cronitor,
|
||||
'healthchecks': healthchecks,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from enum import Enum
|
||||
|
||||
MONITOR_HOOK_NAMES = ('healthchecks', 'cronitor', 'cronhub', 'pagerduty', 'ntfy', 'loki')
|
||||
MONITOR_HOOK_NAMES = ('apprise', 'healthchecks', 'cronitor', 'cronhub', 'pagerduty', 'ntfy', 'loki')
|
||||
|
||||
|
||||
class State(Enum):
|
||||
|
|
|
@ -167,7 +167,7 @@ def configure_logging(
|
|||
Raise FileNotFoundError or PermissionError if the log file could not be opened for writing.
|
||||
'''
|
||||
if syslog_log_level is None:
|
||||
syslog_log_level = console_log_level
|
||||
syslog_log_level = logging.DISABLED
|
||||
if log_file_log_level is None:
|
||||
log_file_log_level = console_log_level
|
||||
if monitoring_log_level is None:
|
||||
|
@ -194,8 +194,11 @@ def configure_logging(
|
|||
console_handler.setFormatter(Console_color_formatter())
|
||||
console_handler.setLevel(console_log_level)
|
||||
|
||||
syslog_path = None
|
||||
if log_file is None and syslog_log_level != logging.DISABLED:
|
||||
handlers = [console_handler]
|
||||
|
||||
if syslog_log_level != logging.DISABLED:
|
||||
syslog_path = None
|
||||
|
||||
if os.path.exists('/dev/log'):
|
||||
syslog_path = '/dev/log'
|
||||
elif os.path.exists('/var/run/syslog'):
|
||||
|
@ -203,14 +206,15 @@ def configure_logging(
|
|||
elif os.path.exists('/var/run/log'):
|
||||
syslog_path = '/var/run/log'
|
||||
|
||||
if syslog_path and not interactive_console():
|
||||
syslog_handler = logging.handlers.SysLogHandler(address=syslog_path)
|
||||
syslog_handler.setFormatter(
|
||||
logging.Formatter('borgmatic: {levelname} {message}', style='{') # noqa: FS003
|
||||
)
|
||||
syslog_handler.setLevel(syslog_log_level)
|
||||
handlers = (console_handler, syslog_handler)
|
||||
elif log_file and log_file_log_level != logging.DISABLED:
|
||||
if syslog_path:
|
||||
syslog_handler = logging.handlers.SysLogHandler(address=syslog_path)
|
||||
syslog_handler.setFormatter(
|
||||
logging.Formatter('borgmatic: {levelname} {message}', style='{') # noqa: FS003
|
||||
)
|
||||
syslog_handler.setLevel(syslog_log_level)
|
||||
handlers.append(syslog_handler)
|
||||
|
||||
if log_file and log_file_log_level != logging.DISABLED:
|
||||
file_handler = logging.handlers.WatchedFileHandler(log_file)
|
||||
file_handler.setFormatter(
|
||||
logging.Formatter(
|
||||
|
@ -218,11 +222,9 @@ def configure_logging(
|
|||
)
|
||||
)
|
||||
file_handler.setLevel(log_file_log_level)
|
||||
handlers = (console_handler, file_handler)
|
||||
else:
|
||||
handlers = (console_handler,)
|
||||
handlers.append(file_handler)
|
||||
|
||||
logging.basicConfig(
|
||||
level=min(console_log_level, syslog_log_level, log_file_log_level, monitoring_log_level),
|
||||
level=min(handler.level for handler in handlers),
|
||||
handlers=handlers,
|
||||
)
|
||||
|
|
|
@ -23,12 +23,20 @@ def handle_signal(signal_number, frame):
|
|||
if signal_number == signal.SIGTERM:
|
||||
logger.critical('Exiting due to TERM signal')
|
||||
sys.exit(EXIT_CODE_FROM_SIGNAL + signal.SIGTERM)
|
||||
elif signal_number == signal.SIGINT:
|
||||
raise KeyboardInterrupt()
|
||||
|
||||
|
||||
def configure_signals():
|
||||
'''
|
||||
Configure borgmatic's signal handlers to pass relevant signals through to any child processes
|
||||
like Borg. Note that SIGINT gets passed through even without these changes.
|
||||
like Borg.
|
||||
'''
|
||||
for signal_number in (signal.SIGHUP, signal.SIGTERM, signal.SIGUSR1, signal.SIGUSR2):
|
||||
for signal_number in (
|
||||
signal.SIGHUP,
|
||||
signal.SIGINT,
|
||||
signal.SIGTERM,
|
||||
signal.SIGUSR1,
|
||||
signal.SIGUSR2,
|
||||
):
|
||||
signal.signal(signal_number, handle_signal)
|
||||
|
|
|
@ -18,7 +18,7 @@ sudo pipx upgrade borgmatic
|
|||
|
||||
Omit `sudo` if you installed borgmatic as a non-root user. And if you
|
||||
installed borgmatic *both* as root and as a non-root user, you'll need to
|
||||
upgrade each installation indepedently.
|
||||
upgrade each installation independently.
|
||||
|
||||
If you originally installed borgmatic with `sudo pip3 install --user`, you can
|
||||
uninstall it first with `sudo pip3 uninstall borgmatic` and then [install it
|
||||
|
|
1
setup.py
1
setup.py
|
@ -36,6 +36,7 @@ setup(
|
|||
'ruamel.yaml>0.15.0,<0.18.0',
|
||||
'setuptools',
|
||||
),
|
||||
extras_require={"Apprise": ["apprise"]},
|
||||
include_package_data=True,
|
||||
python_requires='>=3.7',
|
||||
)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
appdirs==1.4.4; python_version >= '3.8'
|
||||
apprise==1.3.0
|
||||
attrs==22.2.0; python_version >= '3.8'
|
||||
black==23.3.0; python_version >= '3.8'
|
||||
certifi==2022.9.24
|
||||
chardet==5.1.0
|
||||
click==8.1.3; python_version >= '3.8'
|
||||
codespell==2.2.4
|
||||
|
@ -14,16 +16,18 @@ flexmock==0.11.3
|
|||
idna==3.4
|
||||
importlib_metadata==6.3.0; python_version < '3.8'
|
||||
isort==5.12.0
|
||||
jsonschema==4.17.3
|
||||
Markdown==3.4.1
|
||||
mccabe==0.7.0
|
||||
packaging==23.1
|
||||
pluggy==1.0.0
|
||||
pathspec==0.11.1; python_version >= '3.8'
|
||||
pluggy==1.0.0
|
||||
py==1.11.0
|
||||
pycodestyle==2.10.0
|
||||
pyflakes==3.0.1
|
||||
jsonschema==4.17.3
|
||||
pytest==7.3.0
|
||||
pytest-cov==4.0.0
|
||||
PyYAML==6.0
|
||||
regex; python_version >= '3.8'
|
||||
requests==2.31.0
|
||||
ruamel.yaml>0.15.0,<0.18.0
|
||||
|
|
|
@ -13,8 +13,9 @@ def test_parse_arguments_with_no_arguments_uses_defaults():
|
|||
global_arguments = arguments['global']
|
||||
assert global_arguments.config_paths == config_paths
|
||||
assert global_arguments.verbosity == 0
|
||||
assert global_arguments.syslog_verbosity == 0
|
||||
assert global_arguments.log_file_verbosity == 0
|
||||
assert global_arguments.syslog_verbosity == -2
|
||||
assert global_arguments.log_file_verbosity == 1
|
||||
assert global_arguments.monitoring_verbosity == 1
|
||||
|
||||
|
||||
def test_parse_arguments_with_multiple_config_flags_parses_as_list():
|
||||
|
@ -25,8 +26,9 @@ def test_parse_arguments_with_multiple_config_flags_parses_as_list():
|
|||
global_arguments = arguments['global']
|
||||
assert global_arguments.config_paths == ['myconfig', 'otherconfig']
|
||||
assert global_arguments.verbosity == 0
|
||||
assert global_arguments.syslog_verbosity == 0
|
||||
assert global_arguments.log_file_verbosity == 0
|
||||
assert global_arguments.syslog_verbosity == -2
|
||||
assert global_arguments.log_file_verbosity == 1
|
||||
assert global_arguments.monitoring_verbosity == 1
|
||||
|
||||
|
||||
def test_parse_arguments_with_action_after_config_path_omits_action():
|
||||
|
@ -71,8 +73,9 @@ def test_parse_arguments_with_verbosity_overrides_default():
|
|||
global_arguments = arguments['global']
|
||||
assert global_arguments.config_paths == config_paths
|
||||
assert global_arguments.verbosity == 1
|
||||
assert global_arguments.syslog_verbosity == 0
|
||||
assert global_arguments.log_file_verbosity == 0
|
||||
assert global_arguments.syslog_verbosity == -2
|
||||
assert global_arguments.log_file_verbosity == 1
|
||||
assert global_arguments.monitoring_verbosity == 1
|
||||
|
||||
|
||||
def test_parse_arguments_with_syslog_verbosity_overrides_default():
|
||||
|
@ -85,6 +88,8 @@ def test_parse_arguments_with_syslog_verbosity_overrides_default():
|
|||
assert global_arguments.config_paths == config_paths
|
||||
assert global_arguments.verbosity == 0
|
||||
assert global_arguments.syslog_verbosity == 2
|
||||
assert global_arguments.log_file_verbosity == 1
|
||||
assert global_arguments.monitoring_verbosity == 1
|
||||
|
||||
|
||||
def test_parse_arguments_with_log_file_verbosity_overrides_default():
|
||||
|
@ -96,8 +101,9 @@ def test_parse_arguments_with_log_file_verbosity_overrides_default():
|
|||
global_arguments = arguments['global']
|
||||
assert global_arguments.config_paths == config_paths
|
||||
assert global_arguments.verbosity == 0
|
||||
assert global_arguments.syslog_verbosity == 0
|
||||
assert global_arguments.syslog_verbosity == -2
|
||||
assert global_arguments.log_file_verbosity == -1
|
||||
assert global_arguments.monitoring_verbosity == 1
|
||||
|
||||
|
||||
def test_parse_arguments_with_single_override_parses():
|
||||
|
|
|
@ -0,0 +1,197 @@
|
|||
import apprise
|
||||
from apprise import NotifyFormat, NotifyType
|
||||
from flexmock import flexmock
|
||||
|
||||
import borgmatic.hooks.monitor
|
||||
from borgmatic.hooks import apprise as module
|
||||
|
||||
topic = 'borgmatic-unit-testing'
|
||||
|
||||
|
||||
def test_ping_monitor_adheres_dry_run():
|
||||
flexmock(apprise.Apprise).should_receive('notify').never()
|
||||
|
||||
module.ping_monitor(
|
||||
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}]},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=True,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_does_not_hit_with_no_states():
|
||||
flexmock(apprise.Apprise).should_receive('notify').never()
|
||||
|
||||
module.ping_monitor(
|
||||
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': []},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=True,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_fail_by_default():
|
||||
flexmock(apprise.Apprise).should_receive('notify').once()
|
||||
|
||||
for state in borgmatic.hooks.monitor.State:
|
||||
module.ping_monitor(
|
||||
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}]},
|
||||
{},
|
||||
'config.yaml',
|
||||
state,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_with_finish_default_config():
|
||||
flexmock(apprise.Apprise).should_receive('notify').with_args(
|
||||
title='A borgmatic FINISH event happened',
|
||||
body='A borgmatic FINISH event happened',
|
||||
body_format=NotifyFormat.TEXT,
|
||||
notify_type=NotifyType.SUCCESS,
|
||||
).once()
|
||||
|
||||
module.ping_monitor(
|
||||
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['finish']},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.FINISH,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_with_start_default_config():
|
||||
flexmock(apprise.Apprise).should_receive('notify').with_args(
|
||||
title='A borgmatic START event happened',
|
||||
body='A borgmatic START event happened',
|
||||
body_format=NotifyFormat.TEXT,
|
||||
notify_type=NotifyType.INFO,
|
||||
).once()
|
||||
|
||||
module.ping_monitor(
|
||||
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['start']},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_with_fail_default_config():
|
||||
flexmock(apprise.Apprise).should_receive('notify').with_args(
|
||||
title='A borgmatic FAIL event happened',
|
||||
body='A borgmatic FAIL event happened',
|
||||
body_format=NotifyFormat.TEXT,
|
||||
notify_type=NotifyType.FAILURE,
|
||||
).once()
|
||||
|
||||
module.ping_monitor(
|
||||
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['fail']},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_with_log_default_config():
|
||||
flexmock(apprise.Apprise).should_receive('notify').with_args(
|
||||
title='A borgmatic LOG event happened',
|
||||
body='A borgmatic LOG event happened',
|
||||
body_format=NotifyFormat.TEXT,
|
||||
notify_type=NotifyType.INFO,
|
||||
).once()
|
||||
|
||||
module.ping_monitor(
|
||||
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['log']},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.LOG,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_with_custom_message_title():
|
||||
flexmock(apprise.Apprise).should_receive('notify').with_args(
|
||||
title='foo',
|
||||
body='bar',
|
||||
body_format=NotifyFormat.TEXT,
|
||||
notify_type=NotifyType.FAILURE,
|
||||
).once()
|
||||
|
||||
module.ping_monitor(
|
||||
{
|
||||
'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}],
|
||||
'states': ['fail'],
|
||||
'fail': {'title': 'foo', 'body': 'bar'},
|
||||
},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_with_custom_message_body():
|
||||
flexmock(apprise.Apprise).should_receive('notify').with_args(
|
||||
title='',
|
||||
body='baz',
|
||||
body_format=NotifyFormat.TEXT,
|
||||
notify_type=NotifyType.FAILURE,
|
||||
).once()
|
||||
|
||||
module.ping_monitor(
|
||||
{
|
||||
'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}],
|
||||
'states': ['fail'],
|
||||
'fail': {'body': 'baz'},
|
||||
},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_multiple_services():
|
||||
flexmock(apprise.Apprise).should_receive('add').with_args(
|
||||
[f'ntfys://{topic}', f'ntfy://{topic}']
|
||||
).once()
|
||||
|
||||
module.ping_monitor(
|
||||
{
|
||||
'services': [
|
||||
{'url': f'ntfys://{topic}', 'label': 'ntfys'},
|
||||
{'url': f'ntfy://{topic}', 'label': 'ntfy'},
|
||||
]
|
||||
},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_warning_for_no_services():
|
||||
flexmock(module.logger).should_receive('info').once()
|
||||
|
||||
module.ping_monitor(
|
||||
{'services': []},
|
||||
{},
|
||||
'config.yaml',
|
||||
borgmatic.hooks.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
|
@ -174,16 +174,18 @@ def test_add_logging_level_skips_global_setting_if_already_set():
|
|||
module.add_logging_level('PLAID', 99)
|
||||
|
||||
|
||||
def test_configure_logging_probes_for_log_socket_on_linux():
|
||||
def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_linux():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
flexmock(module).should_receive('Console_color_formatter')
|
||||
flexmock(module).should_receive('interactive_console').and_return(False)
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.INFO, handlers=tuple
|
||||
level=logging.DEBUG, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
|
||||
syslog_handler = logging.handlers.SysLogHandler()
|
||||
|
@ -191,19 +193,21 @@ def test_configure_logging_probes_for_log_socket_on_linux():
|
|||
address='/dev/log'
|
||||
).and_return(syslog_handler).once()
|
||||
|
||||
module.configure_logging(logging.INFO)
|
||||
module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
|
||||
|
||||
|
||||
def test_configure_logging_probes_for_log_socket_on_macos():
|
||||
def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_macos():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
flexmock(module).should_receive('Console_color_formatter')
|
||||
flexmock(module).should_receive('interactive_console').and_return(False)
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.INFO, handlers=tuple
|
||||
level=logging.DEBUG, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(True)
|
||||
|
@ -212,19 +216,21 @@ def test_configure_logging_probes_for_log_socket_on_macos():
|
|||
address='/var/run/syslog'
|
||||
).and_return(syslog_handler).once()
|
||||
|
||||
module.configure_logging(logging.INFO)
|
||||
module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
|
||||
|
||||
|
||||
def test_configure_logging_probes_for_log_socket_on_freebsd():
|
||||
def test_configure_logging_with_syslog_log_level_probes_for_log_socket_on_freebsd():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
flexmock(module).should_receive('Console_color_formatter')
|
||||
flexmock(module).should_receive('interactive_console').and_return(False)
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.INFO, handlers=tuple
|
||||
level=logging.DEBUG, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(False)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/var/run/syslog').and_return(False)
|
||||
|
@ -234,85 +240,56 @@ def test_configure_logging_probes_for_log_socket_on_freebsd():
|
|||
address='/var/run/log'
|
||||
).and_return(syslog_handler).once()
|
||||
|
||||
module.configure_logging(logging.INFO)
|
||||
module.configure_logging(logging.INFO, syslog_log_level=logging.DEBUG)
|
||||
|
||||
|
||||
def test_configure_logging_sets_global_logger_to_most_verbose_log_level():
|
||||
def test_configure_logging_without_syslog_log_level_skips_syslog():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
flexmock(module).should_receive('Console_color_formatter')
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.DEBUG, handlers=tuple
|
||||
).once()
|
||||
flexmock(module.os.path).should_receive('exists').and_return(False)
|
||||
level=logging.INFO, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').never()
|
||||
flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
|
||||
|
||||
module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
|
||||
module.configure_logging(console_log_level=logging.INFO)
|
||||
|
||||
|
||||
def test_configure_logging_skips_syslog_if_not_found():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
flexmock(module).should_receive('Console_color_formatter')
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.INFO, handlers=tuple
|
||||
level=logging.INFO, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').and_return(False)
|
||||
flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
|
||||
|
||||
module.configure_logging(console_log_level=logging.INFO)
|
||||
|
||||
|
||||
def test_configure_logging_skips_syslog_if_interactive_console():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
)
|
||||
flexmock(module).should_receive('Console_color_formatter')
|
||||
flexmock(module).should_receive('interactive_console').and_return(True)
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.INFO, handlers=tuple
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
|
||||
flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
|
||||
|
||||
module.configure_logging(console_log_level=logging.INFO)
|
||||
|
||||
|
||||
def test_configure_logging_skips_syslog_if_syslog_logging_is_disabled():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).DISABLED = module.DISABLED
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
)
|
||||
flexmock(module).should_receive('Console_color_formatter')
|
||||
flexmock(module).should_receive('interactive_console').never()
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.INFO, handlers=tuple
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
|
||||
flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
|
||||
|
||||
module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DISABLED)
|
||||
module.configure_logging(console_log_level=logging.INFO, syslog_log_level=logging.DEBUG)
|
||||
|
||||
|
||||
def test_configure_logging_skips_log_file_if_log_file_logging_is_disabled():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).DISABLED = module.DISABLED
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
|
||||
# syslog skipped in non-interactive console if --log-file argument provided
|
||||
flexmock(module).should_receive('interactive_console').and_return(False)
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.INFO, handlers=tuple
|
||||
level=logging.INFO, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').never()
|
||||
flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
|
||||
|
@ -327,13 +304,13 @@ def test_configure_logging_to_log_file_instead_of_syslog():
|
|||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
|
||||
# syslog skipped in non-interactive console if --log-file argument provided
|
||||
flexmock(module).should_receive('interactive_console').and_return(False)
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.DEBUG, handlers=tuple
|
||||
level=logging.DEBUG, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').never()
|
||||
flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
|
||||
|
@ -343,7 +320,40 @@ def test_configure_logging_to_log_file_instead_of_syslog():
|
|||
).and_return(file_handler).once()
|
||||
|
||||
module.configure_logging(
|
||||
console_log_level=logging.INFO, log_file_log_level=logging.DEBUG, log_file='/tmp/logfile'
|
||||
console_log_level=logging.INFO,
|
||||
syslog_log_level=logging.DISABLED,
|
||||
log_file_log_level=logging.DEBUG,
|
||||
log_file='/tmp/logfile',
|
||||
)
|
||||
|
||||
|
||||
def test_configure_logging_to_both_log_file_and_syslog():
|
||||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.DEBUG, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
|
||||
syslog_handler = logging.handlers.SysLogHandler()
|
||||
flexmock(module.logging.handlers).should_receive('SysLogHandler').with_args(
|
||||
address='/dev/log'
|
||||
).and_return(syslog_handler).once()
|
||||
file_handler = logging.handlers.WatchedFileHandler('/tmp/logfile')
|
||||
flexmock(module.logging.handlers).should_receive('WatchedFileHandler').with_args(
|
||||
'/tmp/logfile'
|
||||
).and_return(file_handler).once()
|
||||
|
||||
module.configure_logging(
|
||||
console_log_level=logging.INFO,
|
||||
syslog_log_level=logging.DEBUG,
|
||||
log_file_log_level=logging.DEBUG,
|
||||
log_file='/tmp/logfile',
|
||||
)
|
||||
|
||||
|
||||
|
@ -354,12 +364,14 @@ def test_configure_logging_to_log_file_formats_with_custom_log_format():
|
|||
'{message}', style='{' # noqa: FS003
|
||||
).once()
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
|
||||
flexmock(module).should_receive('interactive_console').and_return(False)
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.DEBUG, handlers=tuple
|
||||
level=logging.DEBUG, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/dev/log').and_return(True)
|
||||
flexmock(module.logging.handlers).should_receive('SysLogHandler').never()
|
||||
|
@ -380,13 +392,13 @@ def test_configure_logging_skips_log_file_if_argument_is_none():
|
|||
flexmock(module).should_receive('add_custom_log_levels')
|
||||
flexmock(module.logging).ANSWER = module.ANSWER
|
||||
flexmock(module).should_receive('Multi_stream_handler').and_return(
|
||||
flexmock(setFormatter=lambda formatter: None, setLevel=lambda level: None)
|
||||
flexmock(
|
||||
setFormatter=lambda formatter: None, setLevel=lambda level: None, level=logging.INFO
|
||||
)
|
||||
)
|
||||
|
||||
# No WatchedFileHandler added if argument --log-file is None
|
||||
flexmock(module).should_receive('interactive_console').and_return(False)
|
||||
flexmock(module.logging).should_receive('basicConfig').with_args(
|
||||
level=logging.INFO, handlers=tuple
|
||||
level=logging.INFO, handlers=list
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').and_return(False)
|
||||
flexmock(module.logging.handlers).should_receive('WatchedFileHandler').never()
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import pytest
|
||||
from flexmock import flexmock
|
||||
|
||||
from borgmatic import signals as module
|
||||
|
@ -34,6 +35,17 @@ def test_handle_signal_exits_on_sigterm():
|
|||
module.handle_signal(signal_number, frame)
|
||||
|
||||
|
||||
def test_handle_signal_raises_on_sigint():
|
||||
signal_number = module.signal.SIGINT
|
||||
frame = flexmock(f_back=flexmock(f_code=flexmock(co_name='something')))
|
||||
flexmock(module.os).should_receive('getpgrp').and_return(flexmock)
|
||||
flexmock(module.os).should_receive('killpg')
|
||||
flexmock(module.sys).should_receive('exit').never()
|
||||
|
||||
with pytest.raises(KeyboardInterrupt):
|
||||
module.handle_signal(signal_number, frame)
|
||||
|
||||
|
||||
def test_configure_signals_installs_signal_handlers():
|
||||
flexmock(module.signal).should_receive('signal').at_least().once()
|
||||
|
||||
|
|
Loading…
Reference in New Issue