mirror of
https://github.com/borgmatic-collective/borgmatic.git
synced 2026-07-24 10:53:02 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
634d9e4946 | ||
|
|
54933ebef5 | ||
|
|
157e59ac88 | ||
|
|
666f0dd751 | ||
|
|
8b179e4647 | ||
|
|
865eff7d98 | ||
|
|
b9741f4d0b | ||
|
|
02781662f8 | ||
|
|
32a1043468 | ||
|
|
3e4aeec649 | ||
|
|
b98b827594 | ||
|
|
255cc6ec23 | ||
|
|
51fc37d57a | ||
|
|
1921f55a9d | ||
|
|
fbd381fcc1 | ||
|
|
cd88f9f2ea | ||
|
|
788281cfb9 | ||
|
|
cd234b689d | ||
|
|
92354a77ee | ||
|
|
48ff3e70d1 | ||
|
|
7e9adfb899 | ||
|
|
e238e256f7 | ||
|
|
3ecb92a8d2 | ||
|
|
d58d450628 | ||
|
|
dee9c6e293 | ||
|
|
897c4487de | ||
|
|
48b50b5209 | ||
|
|
13bae8c23b | ||
|
|
6f3accf691 |
@@ -1,3 +1,28 @@
|
||||
1.6.1
|
||||
* #294: Add Healthchecks monitoring hook "ping_body_limit" option to configure how many bytes of
|
||||
logs to send to the Healthchecks server.
|
||||
* #402: Remove the error when "archive_name_format" is specified but a retention prefix isn't.
|
||||
* #420: Warn when an unsupported variable is used in a hook command.
|
||||
* #439: Change connection failures for monitoring hooks (Healthchecks, Cronitor, PagerDuty, and
|
||||
Cronhub) to be warnings instead of errors. This way, the monitoring system failing does not block
|
||||
backups.
|
||||
* #460: Add Healthchecks monitoring hook "send_logs" option to enable/disable sending borgmatic
|
||||
logs to the Healthchecks server.
|
||||
* #525: Add Healthchecks monitoring hook "states" option to only enable pinging for particular
|
||||
monitoring states (start, finish, fail).
|
||||
* #528: Improve the error message when a configuration override contains an invalid value.
|
||||
* #531: BREAKING: When deep merging common configuration, merge colliding list values by appending
|
||||
them. Previously, one list replaced the other.
|
||||
* #532: When a configuration include is a relative path, load it from either the current working
|
||||
directory or from the directory containing the file doing the including. Previously, only the
|
||||
working directory was used.
|
||||
* Add a randomized delay to the sample systemd timer to spread out the load on a server.
|
||||
* Change the configuration format for borgmatic monitoring hooks (Healthchecks, Cronitor,
|
||||
PagerDuty, and Cronhub) to specify the ping URL / integration key as a named option. The intent
|
||||
is to support additional options (some in this release). This change is backwards-compatible.
|
||||
* Add emojis to documentation table of contents to make it easier to find particular how-to and
|
||||
reference guides at a glance.
|
||||
|
||||
1.6.0
|
||||
* #381: BREAKING: Greatly simplify configuration file reuse by deep merging when including common
|
||||
configuration. See the documentation for more information:
|
||||
@@ -8,7 +33,8 @@
|
||||
timing-sensitive tasks like pausing containers. Side effect: before/after command hooks now run
|
||||
once for each configured repository instead of once per configuration file. Additionally, the
|
||||
"repositories" interpolated variable has been changed to "repository", containing the path to the
|
||||
current repository for the hook.
|
||||
current repository for the hook. See the documentation for more information:
|
||||
https://torsion.org/borgmatic/docs/how-to/add-preparation-and-cleanup-steps-to-backups/
|
||||
* #513: Add mention of sudo's "secure_path" option to borgmatic installation documentation.
|
||||
* #515: Fix "borgmatic borg key ..." to pass parameters to Borg in the correct order.
|
||||
* #516: Fix handling of TERM signal to exit borgmatic, not just forward the signal to Borg.
|
||||
|
||||
@@ -6,6 +6,19 @@ import ruamel.yaml
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Yaml_with_loader_stream(ruamel.yaml.YAML):
|
||||
'''
|
||||
A derived class of ruamel.yaml.YAML that simply tacks the loaded stream (file object) onto the
|
||||
loader class so that it's available anywhere that's passed a loader (in this case,
|
||||
include_configuration() below).
|
||||
'''
|
||||
|
||||
def get_constructor_parser(self, stream):
|
||||
constructor, parser = super(Yaml_with_loader_stream, self).get_constructor_parser(stream)
|
||||
constructor.loader.stream = stream
|
||||
return constructor, parser
|
||||
|
||||
|
||||
def load_configuration(filename):
|
||||
'''
|
||||
Load the given configuration file and return its contents as a data structure of nested dicts
|
||||
@@ -14,7 +27,7 @@ def load_configuration(filename):
|
||||
Raise ruamel.yaml.error.YAMLError if something goes wrong parsing the YAML, or RecursionError
|
||||
if there are too many recursive includes.
|
||||
'''
|
||||
yaml = ruamel.yaml.YAML(typ='safe')
|
||||
yaml = Yaml_with_loader_stream(typ='safe')
|
||||
yaml.Constructor = Include_constructor
|
||||
|
||||
return yaml.load(open(filename))
|
||||
@@ -22,10 +35,31 @@ def load_configuration(filename):
|
||||
|
||||
def include_configuration(loader, filename_node):
|
||||
'''
|
||||
Load the given YAML filename (ignoring the given loader so we can use our own), and return its
|
||||
contents as a data structure of nested dicts and lists.
|
||||
Load the given YAML filename (ignoring the given loader so we can use our own) and return its
|
||||
contents as a data structure of nested dicts and lists. If the filename is relative, probe for
|
||||
it within 1. the current working directory and 2. the directory containing the YAML file doing
|
||||
the including.
|
||||
|
||||
Raise FileNotFoundError if an included file was not found.
|
||||
'''
|
||||
return load_configuration(os.path.expanduser(filename_node.value))
|
||||
include_directories = [os.getcwd(), os.path.abspath(os.path.dirname(loader.stream.name))]
|
||||
include_filename = os.path.expanduser(filename_node.value)
|
||||
|
||||
if not os.path.isabs(include_filename):
|
||||
candidate_filenames = [
|
||||
os.path.join(directory, include_filename) for directory in include_directories
|
||||
]
|
||||
|
||||
for candidate_filename in candidate_filenames:
|
||||
if os.path.exists(candidate_filename):
|
||||
include_filename = candidate_filename
|
||||
break
|
||||
else:
|
||||
raise FileNotFoundError(
|
||||
f'Could not find include {filename_node.value} at {" or ".join(candidate_filenames)}'
|
||||
)
|
||||
|
||||
return load_configuration(include_filename)
|
||||
|
||||
|
||||
DELETED_NODE = object()
|
||||
@@ -123,6 +157,20 @@ def deep_merge_nodes(nodes):
|
||||
anchor=b_value.anchor,
|
||||
),
|
||||
)
|
||||
# If we're dealing with SequenceNodes, merge by appending one sequence to the other.
|
||||
elif isinstance(b_value, ruamel.yaml.nodes.SequenceNode):
|
||||
replaced_nodes[(b_key, b_value)] = (
|
||||
b_key,
|
||||
ruamel.yaml.nodes.SequenceNode(
|
||||
tag=b_value.tag,
|
||||
value=a_value.value + b_value.value,
|
||||
start_mark=b_value.start_mark,
|
||||
end_mark=b_value.end_mark,
|
||||
flow_style=b_value.flow_style,
|
||||
comment=b_value.comment,
|
||||
anchor=b_value.anchor,
|
||||
),
|
||||
)
|
||||
|
||||
return [
|
||||
replaced_nodes.get(node, node) for node in nodes if replaced_nodes.get(node) != DELETED_NODE
|
||||
|
||||
@@ -3,8 +3,24 @@ def normalize(config):
|
||||
Given a configuration dict, apply particular hard-coded rules to normalize its contents to
|
||||
adhere to the configuration schema.
|
||||
'''
|
||||
# Upgrade exclude_if_present from a string to a list.
|
||||
exclude_if_present = config.get('location', {}).get('exclude_if_present')
|
||||
|
||||
# "Upgrade" exclude_if_present from a string to a list.
|
||||
if isinstance(exclude_if_present, str):
|
||||
config['location']['exclude_if_present'] = [exclude_if_present]
|
||||
|
||||
# Upgrade various monitoring hooks from a string to a dict.
|
||||
healthchecks = config.get('hooks', {}).get('healthchecks')
|
||||
if isinstance(healthchecks, str):
|
||||
config['hooks']['healthchecks'] = {'ping_url': healthchecks}
|
||||
|
||||
cronitor = config.get('hooks', {}).get('cronitor')
|
||||
if isinstance(cronitor, str):
|
||||
config['hooks']['cronitor'] = {'ping_url': cronitor}
|
||||
|
||||
pagerduty = config.get('hooks', {}).get('pagerduty')
|
||||
if isinstance(pagerduty, str):
|
||||
config['hooks']['pagerduty'] = {'integration_key': pagerduty}
|
||||
|
||||
cronhub = config.get('hooks', {}).get('cronhub')
|
||||
if isinstance(cronhub, str):
|
||||
config['hooks']['cronhub'] = {'ping_url': cronhub}
|
||||
|
||||
@@ -52,16 +52,20 @@ def parse_overrides(raw_overrides):
|
||||
if not raw_overrides:
|
||||
return ()
|
||||
|
||||
try:
|
||||
return tuple(
|
||||
(tuple(raw_keys.split('.')), convert_value_type(value))
|
||||
for raw_override in raw_overrides
|
||||
for raw_keys, value in (raw_override.split('=', 1),)
|
||||
)
|
||||
except ValueError:
|
||||
raise ValueError('Invalid override. Make sure you use the form: SECTION.OPTION=VALUE')
|
||||
except ruamel.yaml.error.YAMLError as error:
|
||||
raise ValueError(f'Invalid override value: {error}')
|
||||
parsed_overrides = []
|
||||
|
||||
for raw_override in raw_overrides:
|
||||
try:
|
||||
raw_keys, value = raw_override.split('=', 1)
|
||||
parsed_overrides.append((tuple(raw_keys.split('.')), convert_value_type(value),))
|
||||
except ValueError:
|
||||
raise ValueError(
|
||||
f"Invalid override '{raw_override}'. Make sure you use the form: SECTION.OPTION=VALUE"
|
||||
)
|
||||
except ruamel.yaml.error.YAMLError as error:
|
||||
raise ValueError(f"Invalid override '{raw_override}': {error.problem}")
|
||||
|
||||
return tuple(parsed_overrides)
|
||||
|
||||
|
||||
def apply_overrides(config, raw_overrides):
|
||||
|
||||
@@ -332,10 +332,10 @@ properties:
|
||||
Name of the archive. Borg placeholders can be used. See the
|
||||
output of "borg help placeholders" for details. Defaults to
|
||||
"{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}". If you specify this
|
||||
option, you must also specify a prefix in the retention
|
||||
section to avoid accidental pruning of archives with a
|
||||
different archive name format. And you should also specify a
|
||||
prefix in the consistency section as well.
|
||||
option, consider also specifying a prefix in the retention
|
||||
and consistency sections to avoid accidental
|
||||
pruning/checking of archives with different archive name
|
||||
formats.
|
||||
example: "{hostname}-documents-{now}"
|
||||
relocated_repo_access_is_ok:
|
||||
type: boolean
|
||||
@@ -882,41 +882,99 @@ properties:
|
||||
https://docs.mongodb.com/database-tools/mongorestore/ for
|
||||
details.
|
||||
healthchecks:
|
||||
type: string
|
||||
type: object
|
||||
required: ['ping_url']
|
||||
additionalProperties: false
|
||||
properties:
|
||||
ping_url:
|
||||
type: string
|
||||
description: |
|
||||
Healthchecks ping URL or UUID to notify when a
|
||||
backup begins, ends, or errors.
|
||||
example: https://hc-ping.com/your-uuid-here
|
||||
send_logs:
|
||||
type: boolean
|
||||
description: |
|
||||
Send borgmatic logs to Healthchecks as part the
|
||||
"finish" state. Defaults to true.
|
||||
example: false
|
||||
ping_body_limit:
|
||||
type: integer
|
||||
description: |
|
||||
Number of bytes of borgmatic logs to send to
|
||||
Healthchecks, ideally the same as PING_BODY_LIMIT
|
||||
configured on the Healthchecks server. Set to 0 to
|
||||
send all logs and disable this truncation. Defaults
|
||||
to 100000.
|
||||
example: 200000
|
||||
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 all states.
|
||||
example:
|
||||
- finish
|
||||
description: |
|
||||
Healthchecks ping URL or UUID to notify when a backup
|
||||
begins, ends, or errors. Create an account at
|
||||
https://healthchecks.io if you'd like to use this service.
|
||||
See borgmatic monitoring documentation for details.
|
||||
example:
|
||||
https://hc-ping.com/your-uuid-here
|
||||
cronitor:
|
||||
type: string
|
||||
description: |
|
||||
Cronitor ping URL to notify when a backup begins, ends, or
|
||||
errors. Create an account at https://cronitor.io if you'd
|
||||
like to use this service. See borgmatic monitoring
|
||||
documentation for details.
|
||||
example:
|
||||
https://cronitor.link/d3x0c1
|
||||
pagerduty:
|
||||
type: string
|
||||
description: |
|
||||
PagerDuty integration key used to notify PagerDuty when a
|
||||
backup errors. Create an account at
|
||||
https://www.pagerduty.com/ if you'd like to use this
|
||||
Configuration for a monitoring integration with
|
||||
Healthchecks. Create an account at https://healthchecks.io
|
||||
(or self-host Healthchecks) if you'd like to use this
|
||||
service. See borgmatic monitoring documentation for details.
|
||||
example:
|
||||
a177cad45bd374409f78906a810a3074
|
||||
cronhub:
|
||||
type: string
|
||||
cronitor:
|
||||
type: object
|
||||
required: ['ping_url']
|
||||
additionalProperties: false
|
||||
properties:
|
||||
ping_url:
|
||||
type: string
|
||||
description: |
|
||||
Cronitor ping URL to notify when a backup begins,
|
||||
ends, or errors.
|
||||
example: https://cronitor.link/d3x0c1
|
||||
description: |
|
||||
Cronhub ping URL to notify when a backup begins, ends, or
|
||||
errors. Create an account at https://cronhub.io if you'd
|
||||
Configuration for a monitoring integration with Cronitor.
|
||||
Create an account at https://cronitor.io if you'd
|
||||
like to use this service. See borgmatic monitoring
|
||||
documentation for details.
|
||||
example:
|
||||
https://cronhub.io/start/1f5e3410-254c-11e8-b61d-55875966d01
|
||||
pagerduty:
|
||||
type: object
|
||||
required: ['integration_key']
|
||||
additionalProperties: false
|
||||
properties:
|
||||
integration_key:
|
||||
type: string
|
||||
description: |
|
||||
PagerDuty integration key used to notify PagerDuty
|
||||
when a backup errors.
|
||||
example: a177cad45bd374409f78906a810a3074
|
||||
description: |
|
||||
Configuration for a monitoring integration with PagerDuty.
|
||||
Create an account at https://www.pagerduty.com/ if you'd
|
||||
like to use this service. See borgmatic monitoring
|
||||
documentation for details.
|
||||
cronhub:
|
||||
type: object
|
||||
required: ['ping_url']
|
||||
additionalProperties: false
|
||||
properties:
|
||||
ping_url:
|
||||
type: string
|
||||
description: |
|
||||
Cronhub ping URL to notify when a backup begins,
|
||||
ends, or errors.
|
||||
example: https://cronhub.io/ping/1f5e3410-254c-5587
|
||||
description: |
|
||||
Configuration for a monitoring integration with Crunhub.
|
||||
Create an account at https://cronhub.io if you'd like to
|
||||
use this service. See borgmatic monitoring documentation
|
||||
for details.
|
||||
umask:
|
||||
type: integer
|
||||
description: |
|
||||
|
||||
@@ -65,15 +65,6 @@ def apply_logical_validation(config_filename, parsed_configuration):
|
||||
below), run through any additional logical validation checks. If there are any such validation
|
||||
problems, raise a Validation_error.
|
||||
'''
|
||||
archive_name_format = parsed_configuration.get('storage', {}).get('archive_name_format')
|
||||
prefix = parsed_configuration.get('retention', {}).get('prefix')
|
||||
|
||||
if archive_name_format and not prefix:
|
||||
raise Validation_error(
|
||||
config_filename,
|
||||
('If you provide an archive_name_format, you must also specify a retention prefix.',),
|
||||
)
|
||||
|
||||
location_repositories = parsed_configuration.get('location', {}).get('repositories')
|
||||
check_repositories = parsed_configuration.get('consistency', {}).get('check_repositories', [])
|
||||
for repository in check_repositories:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
from borgmatic import execute
|
||||
|
||||
@@ -9,14 +10,19 @@ logger = logging.getLogger(__name__)
|
||||
SOFT_FAIL_EXIT_CODE = 75
|
||||
|
||||
|
||||
def interpolate_context(command, context):
|
||||
def interpolate_context(config_filename, hook_description, command, context):
|
||||
'''
|
||||
Given a single hook command and a dict of context names/values, interpolate the values by
|
||||
"{name}" into the command and return the result.
|
||||
Given a config filename, a hook description, a single hook command, and a dict of context
|
||||
names/values, interpolate the values by "{name}" into the command and return the result.
|
||||
'''
|
||||
for name, value in context.items():
|
||||
command = command.replace('{%s}' % name, str(value))
|
||||
|
||||
for unsupported_variable in re.findall(r'{\w+}', command):
|
||||
logger.warning(
|
||||
f"{config_filename}: Variable '{unsupported_variable}' is not supported in {hook_description} hook"
|
||||
)
|
||||
|
||||
return command
|
||||
|
||||
|
||||
@@ -26,8 +32,7 @@ def execute_hook(commands, umask, config_filename, description, dry_run, **conte
|
||||
a hook description, and whether this is a dry run, run the given commands. Or, don't run them
|
||||
if this is a dry run.
|
||||
|
||||
The context contains optional values interpolated by name into the hook commands. Currently,
|
||||
this only applies to the on_error hook.
|
||||
The context contains optional values interpolated by name into the hook commands.
|
||||
|
||||
Raise ValueError if the umask cannot be parsed.
|
||||
Raise subprocesses.CalledProcessError if an error occurs in a hook.
|
||||
@@ -39,7 +44,9 @@ def execute_hook(commands, umask, config_filename, description, dry_run, **conte
|
||||
dry_run_label = ' (dry run; not actually running hooks)' if dry_run else ''
|
||||
|
||||
context['configuration_filename'] = config_filename
|
||||
commands = [interpolate_context(command, context) for command in commands]
|
||||
commands = [
|
||||
interpolate_context(config_filename, description, command, context) for command in commands
|
||||
]
|
||||
|
||||
if len(commands) == 1:
|
||||
logger.info(
|
||||
|
||||
@@ -22,14 +22,18 @@ def initialize_monitor(
|
||||
pass
|
||||
|
||||
|
||||
def ping_monitor(ping_url, config_filename, state, monitoring_log_level, dry_run):
|
||||
def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_run):
|
||||
'''
|
||||
Ping the given Cronhub URL, modified with the monitor.State. Use the given configuration
|
||||
Ping the configured Cronhub URL, modified with the monitor.State. Use the given configuration
|
||||
filename in any log entries. If this is a dry run, then don't actually ping anything.
|
||||
'''
|
||||
dry_run_label = ' (dry run; not actually pinging)' if dry_run else ''
|
||||
formatted_state = '/{}/'.format(MONITOR_STATE_TO_CRONHUB[state])
|
||||
ping_url = ping_url.replace('/start/', formatted_state).replace('/ping/', formatted_state)
|
||||
ping_url = (
|
||||
hook_config['ping_url']
|
||||
.replace('/start/', formatted_state)
|
||||
.replace('/ping/', formatted_state)
|
||||
)
|
||||
|
||||
logger.info(
|
||||
'{}: Pinging Cronhub {}{}'.format(config_filename, state.name.lower(), dry_run_label)
|
||||
@@ -38,7 +42,10 @@ def ping_monitor(ping_url, config_filename, state, monitoring_log_level, dry_run
|
||||
|
||||
if not dry_run:
|
||||
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
||||
requests.get(ping_url)
|
||||
try:
|
||||
requests.get(ping_url)
|
||||
except requests.exceptions.RequestException as error:
|
||||
logger.warning(f'{config_filename}: Cronhub error: {error}')
|
||||
|
||||
|
||||
def destroy_monitor(
|
||||
|
||||
@@ -22,13 +22,13 @@ def initialize_monitor(
|
||||
pass
|
||||
|
||||
|
||||
def ping_monitor(ping_url, config_filename, state, monitoring_log_level, dry_run):
|
||||
def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_run):
|
||||
'''
|
||||
Ping the given Cronitor URL, modified with the monitor.State. Use the given configuration
|
||||
Ping the configured Cronitor URL, modified with the monitor.State. Use the given configuration
|
||||
filename in any log entries. If this is a dry run, then don't actually ping anything.
|
||||
'''
|
||||
dry_run_label = ' (dry run; not actually pinging)' if dry_run else ''
|
||||
ping_url = '{}/{}'.format(ping_url, MONITOR_STATE_TO_CRONITOR[state])
|
||||
ping_url = '{}/{}'.format(hook_config['ping_url'], MONITOR_STATE_TO_CRONITOR[state])
|
||||
|
||||
logger.info(
|
||||
'{}: Pinging Cronitor {}{}'.format(config_filename, state.name.lower(), dry_run_label)
|
||||
@@ -37,7 +37,10 @@ def ping_monitor(ping_url, config_filename, state, monitoring_log_level, dry_run
|
||||
|
||||
if not dry_run:
|
||||
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
||||
requests.get(ping_url)
|
||||
try:
|
||||
requests.get(ping_url)
|
||||
except requests.exceptions.RequestException as error:
|
||||
logger.warning(f'{config_filename}: Cronitor error: {error}')
|
||||
|
||||
|
||||
def destroy_monitor(
|
||||
|
||||
@@ -13,13 +13,14 @@ MONITOR_STATE_TO_HEALTHCHECKS = {
|
||||
}
|
||||
|
||||
PAYLOAD_TRUNCATION_INDICATOR = '...\n'
|
||||
PAYLOAD_LIMIT_BYTES = 100 * 1024 - len(PAYLOAD_TRUNCATION_INDICATOR)
|
||||
DEFAULT_PING_BODY_LIMIT_BYTES = 100000
|
||||
|
||||
|
||||
class Forgetful_buffering_handler(logging.Handler):
|
||||
'''
|
||||
A buffering log handler that stores log messages in memory, and throws away messages (oldest
|
||||
first) once a particular capacity in bytes is reached.
|
||||
first) once a particular capacity in bytes is reached. But if the given byte capacity is zero,
|
||||
don't throw away any messages.
|
||||
'''
|
||||
|
||||
def __init__(self, byte_capacity, log_level):
|
||||
@@ -36,6 +37,9 @@ class Forgetful_buffering_handler(logging.Handler):
|
||||
self.byte_count += len(message)
|
||||
self.buffer.append(message)
|
||||
|
||||
if not self.byte_capacity:
|
||||
return
|
||||
|
||||
while self.byte_count > self.byte_capacity and self.buffer:
|
||||
self.byte_count -= len(self.buffer[0])
|
||||
self.buffer.pop(0)
|
||||
@@ -65,31 +69,45 @@ def format_buffered_logs_for_payload():
|
||||
return payload
|
||||
|
||||
|
||||
def initialize_monitor(
|
||||
ping_url_or_uuid, config_filename, monitoring_log_level, dry_run
|
||||
): # pragma: no cover
|
||||
def initialize_monitor(hook_config, config_filename, monitoring_log_level, dry_run):
|
||||
'''
|
||||
Add a handler to the root logger that stores in memory the most recent logs emitted. That
|
||||
way, we can send them all to Healthchecks upon a finish or failure state.
|
||||
Add a handler to the root logger that stores in memory the most recent logs emitted. That way,
|
||||
we can send them all to Healthchecks upon a finish or failure state. But skip this if the
|
||||
"send_logs" option is false.
|
||||
'''
|
||||
if hook_config.get('send_logs') is False:
|
||||
return
|
||||
|
||||
ping_body_limit = max(
|
||||
hook_config.get('ping_body_limit', DEFAULT_PING_BODY_LIMIT_BYTES)
|
||||
- len(PAYLOAD_TRUNCATION_INDICATOR),
|
||||
0,
|
||||
)
|
||||
|
||||
logging.getLogger().addHandler(
|
||||
Forgetful_buffering_handler(PAYLOAD_LIMIT_BYTES, monitoring_log_level)
|
||||
Forgetful_buffering_handler(ping_body_limit, monitoring_log_level)
|
||||
)
|
||||
|
||||
|
||||
def ping_monitor(ping_url_or_uuid, config_filename, state, monitoring_log_level, dry_run):
|
||||
def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_run):
|
||||
'''
|
||||
Ping the given Healthchecks URL or UUID, modified with the monitor.State. Use the given
|
||||
Ping the configured Healthchecks URL or UUID, modified with the monitor.State. Use the given
|
||||
configuration filename in any log entries, and log to Healthchecks with the giving log level.
|
||||
If this is a dry run, then don't actually ping anything.
|
||||
'''
|
||||
ping_url = (
|
||||
ping_url_or_uuid
|
||||
if ping_url_or_uuid.startswith('http')
|
||||
else 'https://hc-ping.com/{}'.format(ping_url_or_uuid)
|
||||
hook_config['ping_url']
|
||||
if hook_config['ping_url'].startswith('http')
|
||||
else 'https://hc-ping.com/{}'.format(hook_config['ping_url'])
|
||||
)
|
||||
dry_run_label = ' (dry run; not actually pinging)' if dry_run else ''
|
||||
|
||||
if 'states' in hook_config and state.name.lower() not in hook_config['states']:
|
||||
logger.info(
|
||||
f'{config_filename}: Skipping Healthchecks {state.name.lower()} ping due to configured states'
|
||||
)
|
||||
return
|
||||
|
||||
healthchecks_state = MONITOR_STATE_TO_HEALTHCHECKS.get(state)
|
||||
if healthchecks_state:
|
||||
ping_url = '{}/{}'.format(ping_url, healthchecks_state)
|
||||
@@ -106,10 +124,13 @@ def ping_monitor(ping_url_or_uuid, config_filename, state, monitoring_log_level,
|
||||
|
||||
if not dry_run:
|
||||
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
||||
requests.post(ping_url, data=payload.encode('utf-8'))
|
||||
try:
|
||||
requests.post(ping_url, data=payload.encode('utf-8'))
|
||||
except requests.exceptions.RequestException as error:
|
||||
logger.warning(f'{config_filename}: Healthchecks error: {error}')
|
||||
|
||||
|
||||
def destroy_monitor(ping_url_or_uuid, config_filename, monitoring_log_level, dry_run):
|
||||
def destroy_monitor(hook_config, config_filename, monitoring_log_level, dry_run):
|
||||
'''
|
||||
Remove the monitor handler that was added to the root logger. This prevents the handler from
|
||||
getting reused by other instances of this monitor.
|
||||
|
||||
@@ -21,10 +21,10 @@ def initialize_monitor(
|
||||
pass
|
||||
|
||||
|
||||
def ping_monitor(integration_key, config_filename, state, monitoring_log_level, dry_run):
|
||||
def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_run):
|
||||
'''
|
||||
If this is an error state, create a PagerDuty event with the given integration key. Use the
|
||||
given configuration filename in any log entries. If this is a dry run, then don't actually
|
||||
If this is an error state, create a PagerDuty event with the configured integration key. Use
|
||||
the given configuration filename in any log entries. If this is a dry run, then don't actually
|
||||
create an event.
|
||||
'''
|
||||
if state != monitor.State.FAIL:
|
||||
@@ -47,7 +47,7 @@ def ping_monitor(integration_key, config_filename, state, monitoring_log_level,
|
||||
)
|
||||
payload = json.dumps(
|
||||
{
|
||||
'routing_key': integration_key,
|
||||
'routing_key': hook_config['integration_key'],
|
||||
'event_action': 'trigger',
|
||||
'payload': {
|
||||
'summary': 'backup failed on {}'.format(hostname),
|
||||
@@ -68,7 +68,10 @@ def ping_monitor(integration_key, config_filename, state, monitoring_log_level,
|
||||
logger.debug('{}: Using PagerDuty payload: {}'.format(config_filename, payload))
|
||||
|
||||
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
||||
requests.post(EVENTS_API_URL, data=payload.encode('utf-8'))
|
||||
try:
|
||||
requests.post(EVENTS_API_URL, data=payload.encode('utf-8'))
|
||||
except requests.exceptions.RequestException as error:
|
||||
logger.warning(f'{config_filename}: PagerDuty error: {error}')
|
||||
|
||||
|
||||
def destroy_monitor(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to add preparation and cleanup steps to backups
|
||||
eleventyNavigation:
|
||||
key: Add preparation and cleanup steps
|
||||
key: 🧹 Add preparation and cleanup steps
|
||||
parent: How-to guides
|
||||
order: 8
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to backup to a removable drive or an intermittent server
|
||||
eleventyNavigation:
|
||||
key: Backup to a removable drive or server
|
||||
key: 💾 Backup to a removable drive/server
|
||||
parent: How-to guides
|
||||
order: 9
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to backup your databases
|
||||
eleventyNavigation:
|
||||
key: Backup your databases
|
||||
key: 🗄️ Backup your databases
|
||||
parent: How-to guides
|
||||
order: 7
|
||||
---
|
||||
@@ -33,7 +33,8 @@ As part of each backup, borgmatic streams a database dump for each configured
|
||||
database directly to Borg, so it's included in the backup without consuming
|
||||
additional disk space. (The exceptions are the PostgreSQL/MongoDB "directory"
|
||||
dump formats, which can't stream and therefore do consume temporary disk
|
||||
space.)
|
||||
space. Additionally, prior to borgmatic 1.5.3, all database dumps consumed
|
||||
temporary disk space.)
|
||||
|
||||
To support this, borgmatic creates temporary named pipes in `~/.borgmatic` by
|
||||
default. To customize this path, set the `borgmatic_source_directory` option
|
||||
@@ -209,9 +210,16 @@ to exclude are `/dev` and `/run`, but that may not be exhaustive.
|
||||
|
||||
If you prefer to restore a database without the help of borgmatic, first
|
||||
[extract](https://torsion.org/borgmatic/docs/how-to/extract-a-backup/) an
|
||||
archive containing a database dump, and then manually restore the dump file
|
||||
found within the extracted `~/.borgmatic/` path (e.g. with `pg_restore`,
|
||||
`mysql`, or `mongorestore`, commands).
|
||||
archive containing a database dump.
|
||||
|
||||
borgmatic extracts the dump file into the *`username`*`/.borgmatic/` directory
|
||||
within the extraction destination path, where *`username`* is the user that
|
||||
created the backup. For example, if you created the backup with the `root`
|
||||
user and you're extracting to `/tmp`, then the dump will be in
|
||||
`/tmp/root/.borgmatic`.
|
||||
|
||||
After extraction, you can manually restore the dump file using native database
|
||||
commands like `pg_restore`, `mysql`, `mongorestore` or similar.
|
||||
|
||||
|
||||
## Preparation and cleanup hooks
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to deal with very large backups
|
||||
eleventyNavigation:
|
||||
key: Deal with very large backups
|
||||
key: 📏 Deal with very large backups
|
||||
parent: How-to guides
|
||||
order: 3
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to develop on borgmatic
|
||||
eleventyNavigation:
|
||||
key: Develop on borgmatic
|
||||
key: 🏗️ Develop on borgmatic
|
||||
parent: How-to guides
|
||||
order: 12
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to extract a backup
|
||||
eleventyNavigation:
|
||||
key: Extract a backup
|
||||
key: 📤 Extract a backup
|
||||
parent: How-to guides
|
||||
order: 6
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to inspect your backups
|
||||
eleventyNavigation:
|
||||
key: Inspect your backups
|
||||
key: 🔎 Inspect your backups
|
||||
parent: How-to guides
|
||||
order: 4
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to make backups redundant
|
||||
eleventyNavigation:
|
||||
key: Make backups redundant
|
||||
key: ☁️ Make backups redundant
|
||||
parent: How-to guides
|
||||
order: 2
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to make per-application backups
|
||||
eleventyNavigation:
|
||||
key: Make per-application backups
|
||||
key: 🔀 Make per-application backups
|
||||
parent: How-to guides
|
||||
order: 1
|
||||
---
|
||||
@@ -32,10 +32,16 @@ perform any merging of configuration files by default. If you'd like borgmatic
|
||||
to merge your configuration files, see below about configuration includes.
|
||||
|
||||
Additionally, the `~/.config/borgmatic.d/` directory works the same way as
|
||||
`/etc/borgmatic.d`. If you need even more customizability, you can specify
|
||||
alternate configuration paths on the command-line with borgmatic's `--config`
|
||||
flag. See `borgmatic --help` for more information.
|
||||
`/etc/borgmatic.d`.
|
||||
|
||||
If you need even more customizability, you can specify alternate configuration
|
||||
paths on the command-line with borgmatic's `--config` flag. (See `borgmatic
|
||||
--help` for more information.) For instance, if you want to schedule your
|
||||
various borgmatic backups to run at different times, you'll need multiple
|
||||
entries in your [scheduling software of
|
||||
choice](https://torsion.org/borgmatic/docs/how-to/set-up-backups/#autopilot),
|
||||
each entry using borgmatic's `--config` flag instead of relying on
|
||||
`/etc/borgmatic.d`.
|
||||
|
||||
## Configuration includes
|
||||
|
||||
@@ -69,6 +75,10 @@ themselves and complaining that they are not valid configuration files, you
|
||||
should put them in a directory other than `/etc/borgmatic.d/`. (A subdirectory
|
||||
is fine.)
|
||||
|
||||
When a configuration include is a relative path, borgmatic loads it from either
|
||||
the current working directory or from the directory containing the file doing
|
||||
the including.
|
||||
|
||||
Note that this form of include must be a YAML value rather than a key. For
|
||||
example, this will not work:
|
||||
|
||||
@@ -113,15 +123,15 @@ Once this include gets merged in, the resulting configuration would have a
|
||||
`keep_hourly` value of `24` and an overridden `keep_daily` value of `5`.
|
||||
|
||||
When there's an option collision between the local file and the merged
|
||||
include, the local file's option takes precedent. And as of borgmatic 1.6.0,
|
||||
include, the local file's option takes precedence. And as of borgmatic 1.6.0,
|
||||
this feature performs a deep merge, meaning that values are merged at all
|
||||
levels in the two configuration files. This allows you to include common
|
||||
configuration—up to full borgmatic configuration files—while overriding only
|
||||
the parts you want to customize.
|
||||
levels in the two configuration files. Colliding list values are appended
|
||||
together. This allows you to include common configuration—up to full borgmatic
|
||||
configuration files—while overriding only the parts you want to customize.
|
||||
|
||||
Note that this `<<` include merging syntax is only for merging in mappings
|
||||
(keys/values). If you'd like to include other types like scalars or lists
|
||||
directly, please see the section above about standard includes.
|
||||
(configuration options and their values). But if you'd like to include a
|
||||
single value directly, please see the section above about standard includes.
|
||||
|
||||
|
||||
## Configuration overrides
|
||||
@@ -166,7 +176,14 @@ borgmatic create --override location.repositories=[test1.borg,test2.borg]
|
||||
Or even a single list element:
|
||||
|
||||
```bash
|
||||
borgmatic create --override location.repositories=[/root/test1.borg]
|
||||
borgmatic create --override location.repositories=[/root/test.borg]
|
||||
```
|
||||
|
||||
If your override value contains special YAML characters like colons, then
|
||||
you'll need quotes for it to parse correctly:
|
||||
|
||||
```bash
|
||||
borgmatic create --override location.repositories="['user@server:test.borg']"
|
||||
```
|
||||
|
||||
There is not currently a way to override a single element of a list without
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to monitor your backups
|
||||
eleventyNavigation:
|
||||
key: Monitor your backups
|
||||
key: 🚨 Monitor your backups
|
||||
parent: How-to guides
|
||||
order: 5
|
||||
---
|
||||
@@ -136,7 +136,8 @@ URL" for your project. Here's an example:
|
||||
|
||||
```yaml
|
||||
hooks:
|
||||
healthchecks: https://hc-ping.com/addffa72-da17-40ae-be9c-ff591afb942a
|
||||
healthchecks:
|
||||
ping_url: https://hc-ping.com/addffa72-da17-40ae-be9c-ff591afb942a
|
||||
```
|
||||
|
||||
With this hook in place, borgmatic pings your Healthchecks project when a
|
||||
@@ -158,7 +159,10 @@ itself. But the logs are only included for errors that occur when a `prune`,
|
||||
|
||||
You can customize the verbosity of the logs that are sent to Healthchecks with
|
||||
borgmatic's `--monitoring-verbosity` flag. The `--files` and `--stats` flags
|
||||
may also be of use. See `borgmatic --help` for more information.
|
||||
may also be of use. See `borgmatic --help` for more information. Additionally,
|
||||
see the [borgmatic configuration
|
||||
file](https://torsion.org/borgmatic/docs/reference/configuration/) for
|
||||
additional Healthchecks options.
|
||||
|
||||
You can configure Healthchecks to notify you by a [variety of
|
||||
mechanisms](https://healthchecks.io/#welcome-integrations) when backups fail
|
||||
@@ -176,7 +180,8 @@ API URL" for your monitor. Here's an example:
|
||||
|
||||
```yaml
|
||||
hooks:
|
||||
cronitor: https://cronitor.link/d3x0c1
|
||||
cronitor:
|
||||
ping_url: https://cronitor.link/d3x0c1
|
||||
```
|
||||
|
||||
With this hook in place, borgmatic pings your Cronitor monitor when a backup
|
||||
@@ -204,7 +209,8 @@ URL" for your monitor. Here's an example:
|
||||
|
||||
```yaml
|
||||
hooks:
|
||||
cronhub: https://cronhub.io/start/1f5e3410-254c-11e8-b61d-55875966d031
|
||||
cronhub:
|
||||
ping_url: https://cronhub.io/start/1f5e3410-254c-11e8-b61d-55875966d031
|
||||
```
|
||||
|
||||
With this hook in place, borgmatic pings your Cronhub monitor when a backup
|
||||
@@ -246,7 +252,8 @@ Here's an example:
|
||||
|
||||
```yaml
|
||||
hooks:
|
||||
pagerduty: a177cad45bd374409f78906a810a3074
|
||||
pagerduty:
|
||||
integration_key: a177cad45bd374409f78906a810a3074
|
||||
```
|
||||
|
||||
With this hook in place, borgmatic creates a PagerDuty event for your service
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to run arbitrary Borg commands
|
||||
eleventyNavigation:
|
||||
key: Run arbitrary Borg commands
|
||||
key: 🔧 Run arbitrary Borg commands
|
||||
parent: How-to guides
|
||||
order: 10
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to set up backups
|
||||
eleventyNavigation:
|
||||
key: Set up backups
|
||||
key: 📥 Set up backups
|
||||
parent: How-to guides
|
||||
order: 0
|
||||
---
|
||||
@@ -83,7 +83,7 @@ Besides the approaches described above, there are several other options for
|
||||
installing borgmatic:
|
||||
|
||||
* [Docker image with scheduled backups](https://hub.docker.com/r/b3vis/borgmatic/) (+ Docker Compose files)
|
||||
* [Docker base image](https://hub.docker.com/r/monachus/borgmatic/)
|
||||
* [Docker image with multi-arch and Docker CLI support](https://hub.docker.com/r/modem7/borgmatic-docker/)
|
||||
* [Debian](https://tracker.debian.org/pkg/borgmatic)
|
||||
* [Ubuntu](https://launchpad.net/ubuntu/+source/borgmatic)
|
||||
* [Fedora official](https://bodhi.fedoraproject.org/updates/?search=borgmatic)
|
||||
@@ -93,7 +93,6 @@ installing borgmatic:
|
||||
* [OpenBSD](http://ports.su/sysutils/borgmatic)
|
||||
* [openSUSE](https://software.opensuse.org/package/borgmatic)
|
||||
* [Ansible role](https://github.com/borgbase/ansible-role-borgbackup)
|
||||
* [stand-alone binary](https://github.com/cmarquardt/borgmatic-binary)
|
||||
* [virtualenv](https://virtualenv.pypa.io/en/stable/)
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: How to upgrade borgmatic
|
||||
eleventyNavigation:
|
||||
key: Upgrade borgmatic
|
||||
key: 📦 Upgrade borgmatic
|
||||
parent: How-to guides
|
||||
order: 11
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: Command-line reference
|
||||
eleventyNavigation:
|
||||
key: Command-line reference
|
||||
key: ⌨️ Command-line reference
|
||||
parent: Reference guides
|
||||
order: 1
|
||||
---
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: Configuration reference
|
||||
eleventyNavigation:
|
||||
key: Configuration reference
|
||||
key: ⚙️ Configuration reference
|
||||
parent: Reference guides
|
||||
order: 0
|
||||
---
|
||||
|
||||
@@ -4,6 +4,7 @@ Description=Run borgmatic backup
|
||||
[Timer]
|
||||
OnCalendar=daily
|
||||
Persistent=true
|
||||
RandomizedDelaySec=3h
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from setuptools import find_packages, setup
|
||||
|
||||
VERSION = '1.6.0'
|
||||
VERSION = '1.6.1'
|
||||
|
||||
|
||||
setup(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import io
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
@@ -14,49 +15,133 @@ def test_load_configuration_parses_contents():
|
||||
assert module.load_configuration('config.yaml') == {'key': 'value'}
|
||||
|
||||
|
||||
def test_load_configuration_inlines_include():
|
||||
def test_load_configuration_inlines_include_relative_to_current_directory():
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
builtins.should_receive('open').with_args('include.yaml').and_return('value')
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(
|
||||
'key: !include include.yaml'
|
||||
)
|
||||
flexmock(module.os).should_receive('getcwd').and_return('/tmp')
|
||||
flexmock(module.os.path).should_receive('isabs').and_return(False)
|
||||
flexmock(module.os.path).should_receive('exists').and_return(True)
|
||||
include_file = io.StringIO('value')
|
||||
include_file.name = 'include.yaml'
|
||||
builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
|
||||
config_file = io.StringIO('key: !include include.yaml')
|
||||
config_file.name = 'config.yaml'
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
|
||||
|
||||
assert module.load_configuration('config.yaml') == {'key': 'value'}
|
||||
|
||||
|
||||
def test_load_configuration_inlines_include_relative_to_config_parent_directory():
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
flexmock(module.os).should_receive('getcwd').and_return('/tmp')
|
||||
flexmock(module.os.path).should_receive('isabs').with_args('/etc').and_return(True)
|
||||
flexmock(module.os.path).should_receive('isabs').with_args('/etc/config.yaml').and_return(True)
|
||||
flexmock(module.os.path).should_receive('isabs').with_args('include.yaml').and_return(False)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/tmp/include.yaml').and_return(
|
||||
False
|
||||
)
|
||||
flexmock(module.os.path).should_receive('exists').with_args('/etc/include.yaml').and_return(
|
||||
True
|
||||
)
|
||||
include_file = io.StringIO('value')
|
||||
include_file.name = 'include.yaml'
|
||||
builtins.should_receive('open').with_args('/etc/include.yaml').and_return(include_file)
|
||||
config_file = io.StringIO('key: !include include.yaml')
|
||||
config_file.name = '/etc/config.yaml'
|
||||
builtins.should_receive('open').with_args('/etc/config.yaml').and_return(config_file)
|
||||
|
||||
assert module.load_configuration('/etc/config.yaml') == {'key': 'value'}
|
||||
|
||||
|
||||
def test_load_configuration_raises_if_relative_include_does_not_exist():
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
flexmock(module.os).should_receive('getcwd').and_return('/tmp')
|
||||
flexmock(module.os.path).should_receive('isabs').with_args('/etc').and_return(True)
|
||||
flexmock(module.os.path).should_receive('isabs').with_args('/etc/config.yaml').and_return(True)
|
||||
flexmock(module.os.path).should_receive('isabs').with_args('include.yaml').and_return(False)
|
||||
flexmock(module.os.path).should_receive('exists').and_return(False)
|
||||
config_file = io.StringIO('key: !include include.yaml')
|
||||
config_file.name = '/etc/config.yaml'
|
||||
builtins.should_receive('open').with_args('/etc/config.yaml').and_return(config_file)
|
||||
|
||||
with pytest.raises(FileNotFoundError):
|
||||
module.load_configuration('/etc/config.yaml')
|
||||
|
||||
|
||||
def test_load_configuration_inlines_absolute_include():
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
flexmock(module.os).should_receive('getcwd').and_return('/tmp')
|
||||
flexmock(module.os.path).should_receive('isabs').and_return(True)
|
||||
flexmock(module.os.path).should_receive('exists').never()
|
||||
include_file = io.StringIO('value')
|
||||
include_file.name = '/root/include.yaml'
|
||||
builtins.should_receive('open').with_args('/root/include.yaml').and_return(include_file)
|
||||
config_file = io.StringIO('key: !include /root/include.yaml')
|
||||
config_file.name = 'config.yaml'
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
|
||||
|
||||
assert module.load_configuration('config.yaml') == {'key': 'value'}
|
||||
|
||||
|
||||
def test_load_configuration_raises_if_absolute_include_does_not_exist():
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
flexmock(module.os).should_receive('getcwd').and_return('/tmp')
|
||||
flexmock(module.os.path).should_receive('isabs').and_return(True)
|
||||
builtins.should_receive('open').with_args('/root/include.yaml').and_raise(FileNotFoundError)
|
||||
config_file = io.StringIO('key: !include /root/include.yaml')
|
||||
config_file.name = 'config.yaml'
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
|
||||
|
||||
with pytest.raises(FileNotFoundError):
|
||||
assert module.load_configuration('config.yaml')
|
||||
|
||||
|
||||
def test_load_configuration_merges_include():
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
builtins.should_receive('open').with_args('include.yaml').and_return(
|
||||
flexmock(module.os).should_receive('getcwd').and_return('/tmp')
|
||||
flexmock(module.os.path).should_receive('isabs').and_return(False)
|
||||
flexmock(module.os.path).should_receive('exists').and_return(True)
|
||||
include_file = io.StringIO(
|
||||
'''
|
||||
foo: bar
|
||||
baz: quux
|
||||
'''
|
||||
)
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(
|
||||
include_file.name = 'include.yaml'
|
||||
builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
|
||||
config_file = io.StringIO(
|
||||
'''
|
||||
foo: override
|
||||
<<: !include include.yaml
|
||||
'''
|
||||
)
|
||||
config_file.name = 'config.yaml'
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
|
||||
|
||||
assert module.load_configuration('config.yaml') == {'foo': 'override', 'baz': 'quux'}
|
||||
|
||||
|
||||
def test_load_configuration_does_not_merge_include_list():
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
builtins.should_receive('open').with_args('include.yaml').and_return(
|
||||
flexmock(module.os).should_receive('getcwd').and_return('/tmp')
|
||||
flexmock(module.os.path).should_receive('isabs').and_return(False)
|
||||
flexmock(module.os.path).should_receive('exists').and_return(True)
|
||||
include_file = io.StringIO(
|
||||
'''
|
||||
- one
|
||||
- two
|
||||
'''
|
||||
)
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(
|
||||
include_file.name = 'include.yaml'
|
||||
builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
|
||||
config_file = io.StringIO(
|
||||
'''
|
||||
foo: bar
|
||||
repositories:
|
||||
<<: !include include.yaml
|
||||
'''
|
||||
)
|
||||
config_file.name = 'config.yaml'
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
|
||||
|
||||
with pytest.raises(ruamel.yaml.error.YAMLError):
|
||||
assert module.load_configuration('config.yaml')
|
||||
@@ -241,3 +326,49 @@ def test_deep_merge_nodes_keeps_deeply_nested_values():
|
||||
assert nested_options[0][1].value == '--init-option'
|
||||
assert nested_options[1][0].value == 'prune'
|
||||
assert nested_options[1][1].value == '--prune-option'
|
||||
|
||||
|
||||
def test_deep_merge_nodes_appends_colliding_sequence_values():
|
||||
node_values = [
|
||||
(
|
||||
ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
|
||||
ruamel.yaml.nodes.MappingNode(
|
||||
tag='tag:yaml.org,2002:map',
|
||||
value=[
|
||||
(
|
||||
ruamel.yaml.nodes.ScalarNode(
|
||||
tag='tag:yaml.org,2002:str', value='before_backup'
|
||||
),
|
||||
ruamel.yaml.nodes.SequenceNode(
|
||||
tag='tag:yaml.org,2002:int', value=['echo 1', 'echo 2']
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
(
|
||||
ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
|
||||
ruamel.yaml.nodes.MappingNode(
|
||||
tag='tag:yaml.org,2002:map',
|
||||
value=[
|
||||
(
|
||||
ruamel.yaml.nodes.ScalarNode(
|
||||
tag='tag:yaml.org,2002:str', value='before_backup'
|
||||
),
|
||||
ruamel.yaml.nodes.SequenceNode(
|
||||
tag='tag:yaml.org,2002:int', value=['echo 3', 'echo 4']
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
]
|
||||
|
||||
result = module.deep_merge_nodes(node_values)
|
||||
assert len(result) == 1
|
||||
(section_key, section_value) = result[0]
|
||||
assert section_key.value == 'hooks'
|
||||
options = section_value.value
|
||||
assert len(options) == 1
|
||||
assert options[0][0].value == 'before_backup'
|
||||
assert options[0][1].value == ['echo 1', 'echo 2', 'echo 3', 'echo 4']
|
||||
|
||||
@@ -21,14 +21,20 @@ def mock_config_and_schema(config_yaml, schema_yaml=None):
|
||||
when parsing the configuration.
|
||||
'''
|
||||
config_stream = io.StringIO(config_yaml)
|
||||
config_stream.name = 'config.yaml'
|
||||
|
||||
if schema_yaml is None:
|
||||
schema_stream = open(module.schema_filename())
|
||||
else:
|
||||
schema_stream = io.StringIO(schema_yaml)
|
||||
schema_stream.name = 'schema.yaml'
|
||||
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
builtins.should_receive('open').with_args('config.yaml').and_return(config_stream)
|
||||
builtins.should_receive('open').with_args('schema.yaml').and_return(schema_stream)
|
||||
flexmock(module.os).should_receive('getcwd').and_return('/tmp')
|
||||
flexmock(module.os.path).should_receive('isabs').and_return(False)
|
||||
flexmock(module.os.path).should_receive('exists').and_return(True)
|
||||
builtins.should_receive('open').with_args('/tmp/config.yaml').and_return(config_stream)
|
||||
builtins.should_receive('open').with_args('/tmp/schema.yaml').and_return(schema_stream)
|
||||
|
||||
|
||||
def test_parse_configuration_transforms_file_into_mapping():
|
||||
@@ -54,7 +60,7 @@ def test_parse_configuration_transforms_file_into_mapping():
|
||||
'''
|
||||
)
|
||||
|
||||
result = module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
result = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
assert result == {
|
||||
'location': {'source_directories': ['/home', '/etc'], 'repositories': ['hostname.borg']},
|
||||
@@ -79,7 +85,7 @@ def test_parse_configuration_passes_through_quoted_punctuation():
|
||||
)
|
||||
)
|
||||
|
||||
result = module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
result = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
assert result == {
|
||||
'location': {
|
||||
@@ -115,7 +121,7 @@ def test_parse_configuration_with_schema_lacking_examples_does_not_raise():
|
||||
''',
|
||||
)
|
||||
|
||||
module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
|
||||
def test_parse_configuration_inlines_include():
|
||||
@@ -133,14 +139,16 @@ def test_parse_configuration_inlines_include():
|
||||
'''
|
||||
)
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
builtins.should_receive('open').with_args('include.yaml').and_return(
|
||||
include_file = io.StringIO(
|
||||
'''
|
||||
keep_daily: 7
|
||||
keep_hourly: 24
|
||||
'''
|
||||
)
|
||||
include_file.name = 'include.yaml'
|
||||
builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
|
||||
|
||||
result = module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
result = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
assert result == {
|
||||
'location': {'source_directories': ['/home'], 'repositories': ['hostname.borg']},
|
||||
@@ -164,14 +172,16 @@ def test_parse_configuration_merges_include():
|
||||
'''
|
||||
)
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
builtins.should_receive('open').with_args('include.yaml').and_return(
|
||||
include_file = io.StringIO(
|
||||
'''
|
||||
keep_daily: 7
|
||||
keep_hourly: 24
|
||||
'''
|
||||
)
|
||||
include_file.name = 'include.yaml'
|
||||
builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
|
||||
|
||||
result = module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
result = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
assert result == {
|
||||
'location': {'source_directories': ['/home'], 'repositories': ['hostname.borg']},
|
||||
@@ -181,23 +191,23 @@ def test_parse_configuration_merges_include():
|
||||
|
||||
def test_parse_configuration_raises_for_missing_config_file():
|
||||
with pytest.raises(FileNotFoundError):
|
||||
module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
|
||||
def test_parse_configuration_raises_for_missing_schema_file():
|
||||
mock_config_and_schema('')
|
||||
builtins = flexmock(sys.modules['builtins'])
|
||||
builtins.should_receive('open').with_args('schema.yaml').and_raise(FileNotFoundError)
|
||||
builtins.should_receive('open').with_args('/tmp/schema.yaml').and_raise(FileNotFoundError)
|
||||
|
||||
with pytest.raises(FileNotFoundError):
|
||||
module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
|
||||
def test_parse_configuration_raises_for_syntax_error():
|
||||
mock_config_and_schema('foo:\nbar')
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
|
||||
def test_parse_configuration_raises_for_validation_error():
|
||||
@@ -211,7 +221,7 @@ def test_parse_configuration_raises_for_validation_error():
|
||||
)
|
||||
|
||||
with pytest.raises(module.Validation_error):
|
||||
module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
|
||||
def test_parse_configuration_applies_overrides():
|
||||
@@ -229,7 +239,7 @@ def test_parse_configuration_applies_overrides():
|
||||
)
|
||||
|
||||
result = module.parse_configuration(
|
||||
'config.yaml', 'schema.yaml', overrides=['location.local_path=borg2']
|
||||
'/tmp/config.yaml', '/tmp/schema.yaml', overrides=['location.local_path=borg2']
|
||||
)
|
||||
|
||||
assert result == {
|
||||
@@ -255,7 +265,7 @@ def test_parse_configuration_applies_normalization():
|
||||
'''
|
||||
)
|
||||
|
||||
result = module.parse_configuration('config.yaml', 'schema.yaml')
|
||||
result = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
|
||||
|
||||
assert result == {
|
||||
'location': {
|
||||
|
||||
@@ -19,6 +19,22 @@ from borgmatic.config import normalize as module
|
||||
{'location': {'source_directories': ['foo', 'bar']}},
|
||||
),
|
||||
({'storage': {'compression': 'yes_please'}}, {'storage': {'compression': 'yes_please'}}),
|
||||
(
|
||||
{'hooks': {'healthchecks': 'https://example.com'}},
|
||||
{'hooks': {'healthchecks': {'ping_url': 'https://example.com'}}},
|
||||
),
|
||||
(
|
||||
{'hooks': {'cronitor': 'https://example.com'}},
|
||||
{'hooks': {'cronitor': {'ping_url': 'https://example.com'}}},
|
||||
),
|
||||
(
|
||||
{'hooks': {'pagerduty': 'https://example.com'}},
|
||||
{'hooks': {'pagerduty': {'integration_key': 'https://example.com'}}},
|
||||
),
|
||||
(
|
||||
{'hooks': {'cronhub': 'https://example.com'}},
|
||||
{'hooks': {'cronhub': {'ping_url': 'https://example.com'}}},
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_normalize_applies_hard_coded_normalization_to_config(config, expected_config):
|
||||
|
||||
@@ -37,33 +37,6 @@ def test_validation_error_string_contains_errors():
|
||||
assert 'uh oh' in result
|
||||
|
||||
|
||||
def test_apply_logical_validation_raises_if_archive_name_format_present_without_prefix():
|
||||
flexmock(module).format_json_error = lambda error: error.message
|
||||
|
||||
with pytest.raises(module.Validation_error):
|
||||
module.apply_logical_validation(
|
||||
'config.yaml',
|
||||
{
|
||||
'storage': {'archive_name_format': '{hostname}-{now}'},
|
||||
'retention': {'keep_daily': 7},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_apply_logical_validation_raises_if_archive_name_format_present_without_retention_prefix():
|
||||
flexmock(module).format_json_error = lambda error: error.message
|
||||
|
||||
with pytest.raises(module.Validation_error):
|
||||
module.apply_logical_validation(
|
||||
'config.yaml',
|
||||
{
|
||||
'storage': {'archive_name_format': '{hostname}-{now}'},
|
||||
'retention': {'keep_daily': 7},
|
||||
'consistency': {'prefix': '{hostname}-'},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_apply_locical_validation_raises_if_unknown_repository_in_check_repositories():
|
||||
flexmock(module).format_json_error = lambda error: error.message
|
||||
|
||||
|
||||
@@ -7,22 +7,34 @@ from borgmatic.hooks import command as module
|
||||
|
||||
|
||||
def test_interpolate_context_passes_through_command_without_variable():
|
||||
assert module.interpolate_context('ls', {'foo': 'bar'}) == 'ls'
|
||||
assert module.interpolate_context('test.yaml', 'pre-backup', 'ls', {'foo': 'bar'}) == 'ls'
|
||||
|
||||
|
||||
def test_interpolate_context_passes_through_command_with_unknown_variable():
|
||||
assert module.interpolate_context('ls {baz}', {'foo': 'bar'}) == 'ls {baz}'
|
||||
assert (
|
||||
module.interpolate_context('test.yaml', 'pre-backup', 'ls {baz}', {'foo': 'bar'})
|
||||
== 'ls {baz}'
|
||||
)
|
||||
|
||||
|
||||
def test_interpolate_context_interpolates_variables():
|
||||
context = {'foo': 'bar', 'baz': 'quux'}
|
||||
|
||||
assert module.interpolate_context('ls {foo}{baz} {baz}', context) == 'ls barquux quux'
|
||||
assert (
|
||||
module.interpolate_context('test.yaml', 'pre-backup', 'ls {foo}{baz} {baz}', context)
|
||||
== 'ls barquux quux'
|
||||
)
|
||||
|
||||
|
||||
def test_interpolate_context_does_not_touch_unknown_variables():
|
||||
context = {'foo': 'bar', 'baz': 'quux'}
|
||||
|
||||
assert module.interpolate_context('test.yaml', 'pre-backup', 'ls {wtf}', context) == 'ls {wtf}'
|
||||
|
||||
|
||||
def test_execute_hook_invokes_each_command():
|
||||
flexmock(module).should_receive('interpolate_context').replace_with(
|
||||
lambda command, context: command
|
||||
lambda config_file, hook_description, command, context: command
|
||||
)
|
||||
flexmock(module.execute).should_receive('execute_command').with_args(
|
||||
[':'], output_log_level=logging.WARNING, shell=True
|
||||
@@ -33,7 +45,7 @@ def test_execute_hook_invokes_each_command():
|
||||
|
||||
def test_execute_hook_with_multiple_commands_invokes_each_command():
|
||||
flexmock(module).should_receive('interpolate_context').replace_with(
|
||||
lambda command, context: command
|
||||
lambda config_file, hook_description, command, context: command
|
||||
)
|
||||
flexmock(module.execute).should_receive('execute_command').with_args(
|
||||
[':'], output_log_level=logging.WARNING, shell=True
|
||||
@@ -47,7 +59,7 @@ def test_execute_hook_with_multiple_commands_invokes_each_command():
|
||||
|
||||
def test_execute_hook_with_umask_sets_that_umask():
|
||||
flexmock(module).should_receive('interpolate_context').replace_with(
|
||||
lambda command, context: command
|
||||
lambda config_file, hook_description, command, context: command
|
||||
)
|
||||
flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
|
||||
flexmock(module.os).should_receive('umask').with_args(0o22).once()
|
||||
@@ -60,7 +72,7 @@ def test_execute_hook_with_umask_sets_that_umask():
|
||||
|
||||
def test_execute_hook_with_dry_run_skips_commands():
|
||||
flexmock(module).should_receive('interpolate_context').replace_with(
|
||||
lambda command, context: command
|
||||
lambda config_file, hook_description, command, context: command
|
||||
)
|
||||
flexmock(module.execute).should_receive('execute_command').never()
|
||||
|
||||
@@ -73,7 +85,7 @@ def test_execute_hook_with_empty_commands_does_not_raise():
|
||||
|
||||
def test_execute_hook_on_error_logs_as_error():
|
||||
flexmock(module).should_receive('interpolate_context').replace_with(
|
||||
lambda command, context: command
|
||||
lambda config_file, hook_description, command, context: command
|
||||
)
|
||||
flexmock(module.execute).should_receive('execute_command').with_args(
|
||||
[':'], output_log_level=logging.ERROR, shell=True
|
||||
|
||||
@@ -4,45 +4,72 @@ from borgmatic.hooks import cronhub as module
|
||||
|
||||
|
||||
def test_ping_monitor_rewrites_ping_url_for_start_state():
|
||||
ping_url = 'https://example.com/start/abcdef'
|
||||
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
||||
flexmock(module.requests).should_receive('get').with_args('https://example.com/start/abcdef')
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=False
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_rewrites_ping_url_and_state_for_start_state():
|
||||
ping_url = 'https://example.com/ping/abcdef'
|
||||
hook_config = {'ping_url': 'https://example.com/ping/abcdef'}
|
||||
flexmock(module.requests).should_receive('get').with_args('https://example.com/start/abcdef')
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=False
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_rewrites_ping_url_for_finish_state():
|
||||
ping_url = 'https://example.com/start/abcdef'
|
||||
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
||||
flexmock(module.requests).should_receive('get').with_args('https://example.com/finish/abcdef')
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.FINISH, monitoring_log_level=1, dry_run=False
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
module.monitor.State.FINISH,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_rewrites_ping_url_for_fail_state():
|
||||
ping_url = 'https://example.com/start/abcdef'
|
||||
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
||||
flexmock(module.requests).should_receive('get').with_args('https://example.com/fail/abcdef')
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
||||
hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_dry_run_does_not_hit_ping_url():
|
||||
ping_url = 'https://example.com'
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('get').never()
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=True
|
||||
hook_config, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=True
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_with_connection_error_does_not_raise():
|
||||
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
||||
flexmock(module.requests).should_receive('get').and_raise(
|
||||
module.requests.exceptions.ConnectionError
|
||||
)
|
||||
|
||||
module.ping_monitor(
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
@@ -4,36 +4,59 @@ from borgmatic.hooks import cronitor as module
|
||||
|
||||
|
||||
def test_ping_monitor_hits_ping_url_for_start_state():
|
||||
ping_url = 'https://example.com'
|
||||
flexmock(module.requests).should_receive('get').with_args('{}/{}'.format(ping_url, 'run'))
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('get').with_args('https://example.com/run')
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=False
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_ping_url_for_finish_state():
|
||||
ping_url = 'https://example.com'
|
||||
flexmock(module.requests).should_receive('get').with_args('{}/{}'.format(ping_url, 'complete'))
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('get').with_args('https://example.com/complete')
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.FINISH, monitoring_log_level=1, dry_run=False
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
module.monitor.State.FINISH,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_ping_url_for_fail_state():
|
||||
ping_url = 'https://example.com'
|
||||
flexmock(module.requests).should_receive('get').with_args('{}/{}'.format(ping_url, 'fail'))
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('get').with_args('https://example.com/fail')
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
||||
hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_dry_run_does_not_hit_ping_url():
|
||||
ping_url = 'https://example.com'
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('get').never()
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=True
|
||||
hook_config, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=True
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_with_connection_error_does_not_raise():
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('get').and_raise(
|
||||
module.requests.exceptions.ConnectionError
|
||||
)
|
||||
|
||||
module.ping_monitor(
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
@@ -12,6 +12,15 @@ def test_forgetful_buffering_handler_emit_collects_log_records():
|
||||
assert not handler.forgot
|
||||
|
||||
|
||||
def test_forgetful_buffering_handler_emit_collects_log_records_with_zero_byte_capacity():
|
||||
handler = module.Forgetful_buffering_handler(byte_capacity=0, log_level=1)
|
||||
handler.emit(flexmock(getMessage=lambda: 'foo'))
|
||||
handler.emit(flexmock(getMessage=lambda: 'bar'))
|
||||
|
||||
assert handler.buffer == ['foo\n', 'bar\n']
|
||||
assert not handler.forgot
|
||||
|
||||
|
||||
def test_forgetful_buffering_handler_emit_forgets_log_records_when_capacity_reached():
|
||||
handler = module.Forgetful_buffering_handler(byte_capacity=len('foo\nbar\n'), log_level=1)
|
||||
handler.emit(flexmock(getMessage=lambda: 'foo'))
|
||||
@@ -60,15 +69,68 @@ def test_format_buffered_logs_for_payload_without_handler_produces_empty_payload
|
||||
assert payload == ''
|
||||
|
||||
|
||||
def test_initialize_monitor_creates_log_handler_with_ping_body_limit():
|
||||
ping_body_limit = 100
|
||||
monitoring_log_level = 1
|
||||
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler').with_args(
|
||||
ping_body_limit - len(module.PAYLOAD_TRUNCATION_INDICATOR), monitoring_log_level
|
||||
).once()
|
||||
|
||||
module.initialize_monitor(
|
||||
{'ping_body_limit': ping_body_limit}, 'test.yaml', monitoring_log_level, dry_run=False
|
||||
)
|
||||
|
||||
|
||||
def test_initialize_monitor_creates_log_handler_with_default_ping_body_limit():
|
||||
monitoring_log_level = 1
|
||||
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler').with_args(
|
||||
module.DEFAULT_PING_BODY_LIMIT_BYTES - len(module.PAYLOAD_TRUNCATION_INDICATOR),
|
||||
monitoring_log_level,
|
||||
).once()
|
||||
|
||||
module.initialize_monitor({}, 'test.yaml', monitoring_log_level, dry_run=False)
|
||||
|
||||
|
||||
def test_initialize_monitor_creates_log_handler_with_zero_ping_body_limit():
|
||||
ping_body_limit = 0
|
||||
monitoring_log_level = 1
|
||||
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler').with_args(
|
||||
ping_body_limit, monitoring_log_level
|
||||
).once()
|
||||
|
||||
module.initialize_monitor(
|
||||
{'ping_body_limit': ping_body_limit}, 'test.yaml', monitoring_log_level, dry_run=False
|
||||
)
|
||||
|
||||
|
||||
def test_initialize_monitor_creates_log_handler_when_send_logs_true():
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler').once()
|
||||
|
||||
module.initialize_monitor(
|
||||
{'send_logs': True}, 'test.yaml', monitoring_log_level=1, dry_run=False
|
||||
)
|
||||
|
||||
|
||||
def test_initialize_monitor_bails_when_send_logs_false():
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler').never()
|
||||
|
||||
module.initialize_monitor(
|
||||
{'send_logs': False}, 'test.yaml', monitoring_log_level=1, dry_run=False
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_ping_url_for_start_state():
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler')
|
||||
ping_url = 'https://example.com'
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('post').with_args(
|
||||
'{}/{}'.format(ping_url, 'start'), data=''.encode('utf-8')
|
||||
'https://example.com/start', data=''.encode('utf-8')
|
||||
)
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url,
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
state=module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
@@ -77,15 +139,15 @@ def test_ping_monitor_hits_ping_url_for_start_state():
|
||||
|
||||
|
||||
def test_ping_monitor_hits_ping_url_for_finish_state():
|
||||
ping_url = 'https://example.com'
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
payload = 'data'
|
||||
flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
|
||||
flexmock(module.requests).should_receive('post').with_args(
|
||||
ping_url, data=payload.encode('utf-8')
|
||||
'https://example.com', data=payload.encode('utf-8')
|
||||
)
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url,
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
state=module.monitor.State.FINISH,
|
||||
monitoring_log_level=1,
|
||||
@@ -94,15 +156,15 @@ def test_ping_monitor_hits_ping_url_for_finish_state():
|
||||
|
||||
|
||||
def test_ping_monitor_hits_ping_url_for_fail_state():
|
||||
ping_url = 'https://example.com'
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
payload = 'data'
|
||||
flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
|
||||
flexmock(module.requests).should_receive('post').with_args(
|
||||
'{}/{}'.format(ping_url, 'fail'), data=payload.encode('utf')
|
||||
'https://example.com/fail', data=payload.encode('utf')
|
||||
)
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url,
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
state=module.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
@@ -111,15 +173,15 @@ def test_ping_monitor_hits_ping_url_for_fail_state():
|
||||
|
||||
|
||||
def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
|
||||
ping_uuid = 'abcd-efgh-ijkl-mnop'
|
||||
hook_config = {'ping_url': 'abcd-efgh-ijkl-mnop'}
|
||||
payload = 'data'
|
||||
flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
|
||||
flexmock(module.requests).should_receive('post').with_args(
|
||||
'https://hc-ping.com/{}'.format(ping_uuid), data=payload.encode('utf-8')
|
||||
'https://hc-ping.com/{}'.format(hook_config['ping_url']), data=payload.encode('utf-8')
|
||||
)
|
||||
|
||||
module.ping_monitor(
|
||||
ping_uuid,
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
state=module.monitor.State.FINISH,
|
||||
monitoring_log_level=1,
|
||||
@@ -129,13 +191,60 @@ def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
|
||||
|
||||
def test_ping_monitor_dry_run_does_not_hit_ping_url():
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler')
|
||||
ping_url = 'https://example.com'
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('post').never()
|
||||
|
||||
module.ping_monitor(
|
||||
ping_url,
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
state=module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=True,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_does_not_hit_ping_url_when_states_not_matching():
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler')
|
||||
hook_config = {'ping_url': 'https://example.com', 'states': ['finish']}
|
||||
flexmock(module.requests).should_receive('post').never()
|
||||
|
||||
module.ping_monitor(
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
state=module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=True,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_hits_ping_url_when_states_matching():
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler')
|
||||
hook_config = {'ping_url': 'https://example.com', 'states': ['start', 'finish']}
|
||||
flexmock(module.requests).should_receive('post').with_args(
|
||||
'https://example.com/start', data=''.encode('utf-8')
|
||||
)
|
||||
|
||||
module.ping_monitor(
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
state=module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_with_connection_error_does_not_raise():
|
||||
flexmock(module).should_receive('Forgetful_buffering_handler')
|
||||
flexmock(module.logger).should_receive('warning')
|
||||
hook_config = {'ping_url': 'https://example.com'}
|
||||
flexmock(module.requests).should_receive('post').with_args(
|
||||
'https://example.com/start', data=''.encode('utf-8')
|
||||
).and_raise(module.requests.exceptions.ConnectionError)
|
||||
|
||||
module.ping_monitor(
|
||||
hook_config,
|
||||
'config.yaml',
|
||||
state=module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
@@ -7,7 +7,11 @@ def test_ping_monitor_ignores_start_state():
|
||||
flexmock(module.requests).should_receive('post').never()
|
||||
|
||||
module.ping_monitor(
|
||||
'abc123', 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=False
|
||||
{'integration_key': 'abc123'},
|
||||
'config.yaml',
|
||||
module.monitor.State.START,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
@@ -15,7 +19,11 @@ def test_ping_monitor_ignores_finish_state():
|
||||
flexmock(module.requests).should_receive('post').never()
|
||||
|
||||
module.ping_monitor(
|
||||
'abc123', 'config.yaml', module.monitor.State.FINISH, monitoring_log_level=1, dry_run=False
|
||||
{'integration_key': 'abc123'},
|
||||
'config.yaml',
|
||||
module.monitor.State.FINISH,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
@@ -23,7 +31,11 @@ def test_ping_monitor_calls_api_for_fail_state():
|
||||
flexmock(module.requests).should_receive('post')
|
||||
|
||||
module.ping_monitor(
|
||||
'abc123', 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
||||
{'integration_key': 'abc123'},
|
||||
'config.yaml',
|
||||
module.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
|
||||
@@ -31,5 +43,24 @@ def test_ping_monitor_dry_run_does_not_call_api():
|
||||
flexmock(module.requests).should_receive('post').never()
|
||||
|
||||
module.ping_monitor(
|
||||
'abc123', 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=True
|
||||
{'integration_key': 'abc123'},
|
||||
'config.yaml',
|
||||
module.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=True,
|
||||
)
|
||||
|
||||
|
||||
def test_ping_monitor_with_connection_error_does_not_raise():
|
||||
flexmock(module.requests).should_receive('post').and_raise(
|
||||
module.requests.exceptions.ConnectionError
|
||||
)
|
||||
flexmock(module.logger).should_receive('warning')
|
||||
|
||||
module.ping_monitor(
|
||||
{'integration_key': 'abc123'},
|
||||
'config.yaml',
|
||||
module.monitor.State.FAIL,
|
||||
monitoring_log_level=1,
|
||||
dry_run=False,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user