"""Test Command and Configuration Reference.
The declarations are written out here rather than read from the repository, since what needs testing is how an option is rendered
rather than what the project happens to declare. The rendered documents are checked as fragments of the xml that comes out.
An option that can be set in the configuration file is described in the configuration reference and linked to from the command
reference, so what is checked there is the link. An option that can only be given on the command line is described in the command
reference, where an example is a command line rather than the configuration file line the configuration reference shows."""
####################################################################################################################################
import os
import tempfile
import xml.etree.ElementTree as etree
from harness.test import *
from command.build.reference import *
from common.storage import file_write
from config.parse import bld_cfg_parse
from help.parse import bld_hlp_parse
# Where the help declaration lives, which the tools tell the parser rather than the parser assuming
PATH_HELP = "doc/xml/reference.xml"
CONFIG = """command:
backup: {}
expire:
internal: true
help: {}
restore: {}
version: {}
optionGroup:
repo: {}
option:
archive-copy:
section: global
type: boolean
default: false
cmd:
section: global
type: string
default-type: dynamic
default: bin
compress-level:
section: global
type: integer
allow-range:
- none: [0, 0]
- gz: [0, 9]
depend:
option: compress-type
compress-type:
section: global
type: string-id
default: gz
delta:
type: boolean
command:
restore: {}
force:
type: boolean
internal: true
online:
section: global
type: boolean
default: true
process-max:
section: global
type: integer
default: 1
allow-range: [1, 999]
repo-path:
section: global
group: repo
type: path
default: /var/lib/pgbackrest
deprecate:
repo-path: {}
db-path: {}
repo-cipher-pass:
section: global
group: repo
type: string
secure: true
format: 6
deprecate:
repo-cipher-pass: {}
repo-storage-port:
section: global
group: repo
type: integer
default:
- s3: 443
- azure: 443
depend:
option: compress-type
set:
type: string
beta: true
command:
restore: {}
spool-path:
section: global
type: path
command:
backup:
internal: true
restore: {}
stanza:
type: string
target:
type: string
command-role:
local: {}
command:
restore: {}
"""
####################################################################################################################################
def _option(id, name, section=None, summary="Summary.", example=None, tag="option"):
"""Help for one option.
A configuration section documents an option as a config-key, so the tag varies with where the help is written."""
return '<%s id="%s" name="%s"%s>%s
Description of %s.
%s%s>' % (
tag,
id,
name,
"" if section is None else ' section="%s"' % section,
summary,
id,
"" if example is None else "".join("%s" % value for value in example),
tag,
)
####################################################################################################################################
HELP = """Configuration description.
%s'
% (id, name, name, name, option)
for id, name, option in (
("backup", "Backup", ""),
("expire", "Expire", ""),
("help", "Help", ""),
("restore", "Restore", "%s" % _option("set", "Set", example=["latest"])),
("version", "Version", ""),
)
),
)
####################################################################################################################################
def _render():
"""Render both references and return the xml of each."""
with tempfile.TemporaryDirectory() as path:
file_write(os.path.join(path, "build/config.yaml"), CONFIG)
file_write(os.path.join(path, PATH_HELP), HELP)
bld_cfg = bld_cfg_parse(path)
bld_hlp = bld_hlp_parse(os.path.join(path, PATH_HELP), bld_cfg, True)
return (
etree.tostring(reference_command_render(bld_cfg, bld_hlp), encoding="unicode"),
etree.tostring(reference_configuration_render(bld_cfg, bld_hlp), encoding="unicode"),
)
####################################################################################################################################
def test_reference_document():
"""A reference says what it is and introduces itself before anything else."""
command, configuration = _render()
for document, subtitle, description in (
(command, "Command Reference", "Command description."),
(configuration, "Configuration Reference", "Configuration description."),
):
assert_in('' % subtitle, document)
assert_in("%s" % description, document)
assert_in('Introduction
', document)
####################################################################################################################################
def test_reference_configuration():
"""The configuration reference documents every option that can be set in the configuration file, under its section."""
_, configuration = _render()
# A section holds the options documented under it, and the introduction it was written with
assert_in('General Options
General introduction.
', configuration)
assert_in('Repository Options', configuration)
# An option is titled with its name and described with the help it was written with
assert_in(
''
"Process Max (--process-max)"
"
Summary.
Description of process-max.
",
configuration,
)
# An option that cannot be set in the configuration file at all is not here, nor is one that is internal. An option holding a
# secret is, since a reader still has to set it -- it is the command reference that leaves it out.
assert_not_in("option-delta", configuration)
assert_not_in("option-force", configuration)
assert_in("option-repo-cipher-pass", configuration)
assert_not_in("option-repo-cipher-pass", _render()[0])
# An example is a configuration file line, one per line, and an option in a group is shown at the first index
assert_in("example: repo1-path=/backup\nexample: repo1-path=/other", configuration)
####################################################################################################################################
def test_reference_command():
"""The command reference documents every command and, under it, the options that command takes."""
command, _ = _render()
assert_in(
'Backup (backup)' "
Backup description.
",
command,
)
# A command a reader has no use for is not here
assert_not_in("command-expire", command)
# Options are grouped by what they are about, and a section that means nothing outside the command is grouped under one heading
assert_in('General Options', command)
assert_in('Repository Options', command)
assert_in('Command Options', command)
# Help a command documents itself wins over the help the option has of its own, and is grouped as a command option
assert_in("
Description of set.
", command)
# An option a command does not take is not under it, nor is one the command marks internal, nor one no user can give
assert_not_in("option-delta", command.split('id="command-backup"')[1].split('id="command-help"')[0])
# An option that can be set in the configuration file is a link to where the configuration reference describes it, named the way
# a heading names it
assert_in(
'Repository Options'
'Repo Path (--repo-path)'
"",
command,
)
# An option that can only be given on the command line is a link to the general options of this document, wherever the command
# groups it
assert_in('Stanza (--stanza)', command)
####################################################################################################################################
def test_reference_command_general():
"""The command reference describes the options that can only be given on the command line before it gets to the commands."""
command, _ = _render()
# The options are described in one place rather than under every command that takes them, and are left out of the contents since
# the contents list the commands
assert_in('General OptionsStanza (--stanza)', command)
# An option that can be set in the configuration file is described in the configuration reference instead, as is one a reader
# has no use for or should not be told about
for name in ("option-process-max", "option-force", "option-repo-cipher-pass"):
assert_not_in(name, command.split('id="section-general"')[1].split('id="command-backup"')[0])
# An example is a command line, with the options given one after another
assert_in("example: --stanza=main", command)
# A boolean is given by naming it, so turning it off is naming the negation of it
assert_in("example: --delta --no-delta", command)
####################################################################################################################################
def test_reference_option_detail():
"""What a reader needs to use an option is rendered below the description of it."""
command, configuration = _render()
# A default, written the way it would be given rather than the way it is declared
assert_in("default: 1", configuration)
assert_in("default: y\n", configuration)
assert_in("default: n", configuration)
assert_in("default: /var/lib/pgbackrest", configuration)
# A default that is worked out when the command runs
assert_in("default: [path of executed pgbackrest binary]", configuration)
# A default per value of the option it depends on
assert_in("default (depending on compress-type):\n s3 - 443\n azure - 443", configuration)
# A range, and a range per value of the option it depends on
assert_in("allowed: [1, 999]", configuration)
assert_in("allow range (depending on compress-type):\n none - [0, 0]\n gz - [0, 9]", configuration)
# The minimum repository format, which is only rendered for an option that has one
assert_in("minimum repository format: 6", configuration)
assert_equal(configuration.count("minimum repository format:"), 1)
# An option that is not ready to be relied on says so
assert_in("
FOR BETA TESTING ONLY. DO NOT USE IN PRODUCTION.
", command)
# Names the option can still be given by, less the one that is the option name itself
assert_in("
Deprecated Names: db-path
", configuration)
# An option whose only deprecation is its own name has no deprecated name to report
assert_not_in("Deprecated Name:", configuration)