Check augas path inconsistencies in initialization

This commit is contained in:
Joona Hoikkala 2019-11-08 15:41:06 +02:00
parent b87191ee7d
commit 2e90a22ea0
No known key found for this signature in database
GPG key ID: D5AA86BBF9B29A5C
4 changed files with 62 additions and 11 deletions

View file

@ -64,6 +64,9 @@ Translates over to:
"/files/etc/apache2/apache2.conf/bLoCk[1]",
]
"""
from acme.magic_typing import Set # pylint: disable=unused-import, no-name-in-module
from certbot import errors
from certbot.compat import os
from certbot_apache import apache_util
from certbot_apache import assertions
@ -71,8 +74,6 @@ from certbot_apache import interfaces
from certbot_apache import parser
from certbot_apache import parsernode_util as util
from certbot.compat import os
from acme.magic_typing import Set # pylint: disable=unused-import, no-name-in-module
class AugeasParserNode(interfaces.ParserNode):
@ -86,6 +87,15 @@ class AugeasParserNode(interfaces.ParserNode):
self.dirty = dirty
self.metadata = metadata
self.parser = self.metadata.get("augeasparser")
try:
if self.metadata["augeaspath"].endswith("/"):
raise errors.PluginError(
"Augeas path: {} has a trailing slash".format(
self.metadata["augeaspath"]
)
)
except KeyError:
raise errors.PluginError("Augeas path is required")
def save(self, msg): # pragma: no cover
pass

View file

@ -2,6 +2,7 @@
import mock
from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module
from certbot import errors
from certbot_apache import assertions
@ -25,7 +26,7 @@ class AugeasParserNodeTest(util.ApacheTest):
name=assertions.PASS,
ancestor=None,
filepath=assertions.PASS,
metadata={"augeasparser": mock.Mock()}
metadata={"augeasparser": mock.Mock(), "augeaspath": "/files/anything"}
)
testcases = {
"/some/path/FirstNode/SecondNode": "SecondNode",
@ -198,3 +199,35 @@ class AugeasParserNodeTest(util.ApacheTest):
new_block = parser.aug.match("{}/VirtualHost[2]".format(root_path))
self.assertEqual(len(new_block), 1)
self.assertTrue(vh.metadata["augeaspath"].endswith("VirtualHost[2]"))
def test_node_init_error_bad_augeaspath(self):
from certbot_apache.augeasparser import AugeasBlockNode
parameters = {
"name": assertions.PASS,
"ancestor": None,
"filepath": assertions.PASS,
"metadata": {
"augeasparser": mock.Mock(),
"augeaspath": "/files/path/endswith/slash/"
}
}
self.assertRaises(
errors.PluginError,
AugeasBlockNode,
**parameters
)
def test_node_init_error_missing_augeaspath(self):
from certbot_apache.augeasparser import AugeasBlockNode
parameters = {
"name": assertions.PASS,
"ancestor": None,
"filepath": assertions.PASS,
"metadata": {
"augeasparser": mock.Mock(),
}
}
self.assertRaises(
errors.PluginError,
AugeasBlockNode,
**parameters
)

View file

@ -78,7 +78,8 @@ class MultipleVhostsTest(util.ApacheTest):
@mock.patch("certbot_apache.parser.ApacheParser")
@mock.patch("certbot_apache.configurator.util.exe_exists")
def _test_prepare_locked(self, unused_parser, unused_exe_exists):
@mock.patch("certbot_apache.configurator.ApacheConfigurator.get_parsernode_root")
def _test_prepare_locked(self, _node, _exists, _parser):
try:
self.config.prepare()
except errors.PluginError as err:

View file

@ -164,10 +164,12 @@ class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public-
def test_find_comments(self):
pri_comments = [augeasparser.AugeasCommentNode(comment="some comment",
ancestor=self.block,
filepath="/path/to/whatever")]
filepath="/path/to/whatever",
metadata=self.metadata)]
sec_comments = [augeasparser.AugeasCommentNode(comment=assertions.PASS,
ancestor=self.block,
filepath=assertions.PASS)]
filepath=assertions.PASS,
metadata=self.metadata)]
find_coms_primary = mock.MagicMock(return_value=pri_comments)
find_coms_secondary = mock.MagicMock(return_value=sec_comments)
self.block.primary.find_comments = find_coms_primary
@ -303,10 +305,12 @@ class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public-
def test_find_coms_second_passing(self):
notpassing = [augeasparser.AugeasCommentNode(comment="notpassing",
ancestor=self.block,
filepath="/path/to/whatever")]
filepath="/path/to/whatever",
metadata=self.metadata)]
passing = [augeasparser.AugeasCommentNode(comment=assertions.PASS,
ancestor=self.block,
filepath=assertions.PASS)]
filepath=assertions.PASS,
metadata=self.metadata)]
find_coms_primary = mock.MagicMock(return_value=notpassing)
find_coms_secondary = mock.MagicMock(return_value=passing)
self.block.primary.find_comments = find_coms_primary
@ -398,13 +402,16 @@ class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public-
def test_parsernode_notequal(self):
ne_block = augeasparser.AugeasBlockNode(name="different",
ancestor=self.block,
filepath="/path/to/whatever")
filepath="/path/to/whatever",
metadata=self.metadata)
ne_directive = augeasparser.AugeasDirectiveNode(name="different",
ancestor=self.block,
filepath="/path/to/whatever")
filepath="/path/to/whatever",
metadata=self.metadata)
ne_comment = augeasparser.AugeasCommentNode(comment="different",
ancestor=self.block,
filepath="/path/to/whatever")
filepath="/path/to/whatever",
metadata=self.metadata)
self.assertFalse(self.block == ne_block)
self.assertFalse(self.directive == ne_directive)
self.assertFalse(self.comment == ne_comment)