1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
"""Tests for DualParserNode implementation"""
import unittest
from unittest import mock
from certbot_apache._internal import assertions
from certbot_apache._internal import augeasparser
from certbot_apache._internal import dualparser
import pytest
class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public-methods
"""DualParserNode tests"""
def setUp(self): # pylint: disable=arguments-differ
parser_mock = mock.MagicMock()
parser_mock.aug.match.return_value = []
parser_mock.get_arg.return_value = []
self.metadata = {"augeasparser": parser_mock, "augeaspath": "/invalid", "ac_ast": None}
self.block = dualparser.DualBlockNode(name="block",
ancestor=None,
filepath="/tmp/something",
metadata=self.metadata)
self.block_two = dualparser.DualBlockNode(name="block",
ancestor=self.block,
filepath="/tmp/something",
metadata=self.metadata)
self.directive = dualparser.DualDirectiveNode(name="directive",
ancestor=self.block,
filepath="/tmp/something",
metadata=self.metadata)
self.comment = dualparser.DualCommentNode(comment="comment",
ancestor=self.block,
filepath="/tmp/something",
metadata=self.metadata)
def test_create_with_precreated(self):
cnode = dualparser.DualCommentNode(comment="comment",
ancestor=self.block,
filepath="/tmp/something",
primary=self.comment.secondary,
secondary=self.comment.primary)
dnode = dualparser.DualDirectiveNode(name="directive",
ancestor=self.block,
filepath="/tmp/something",
primary=self.directive.secondary,
secondary=self.directive.primary)
bnode = dualparser.DualBlockNode(name="block",
ancestor=self.block,
filepath="/tmp/something",
primary=self.block.secondary,
secondary=self.block.primary)
# Switched around
assert cnode.primary == self.comment.secondary
assert cnode.secondary == self.comment.primary
assert dnode.primary == self.directive.secondary
assert dnode.secondary == self.directive.primary
assert bnode.primary == self.block.secondary
assert bnode.secondary == self.block.primary
def test_set_params(self):
params = ("first", "second")
self.directive.primary.set_parameters = mock.Mock()
self.directive.secondary.set_parameters = mock.Mock()
self.directive.set_parameters(params)
assert self.directive.primary.set_parameters.called is True
assert self.directive.secondary.set_parameters.called is True
def test_set_parameters(self):
pparams = mock.MagicMock()
sparams = mock.MagicMock()
pparams.parameters = ("a", "b")
sparams.parameters = ("a", "b")
self.directive.primary.set_parameters = pparams
self.directive.secondary.set_parameters = sparams
self.directive.set_parameters(("param", "seq"))
assert pparams.called is True
assert sparams.called is True
def test_delete_child(self):
pdel = mock.MagicMock()
sdel = mock.MagicMock()
self.block.primary.delete_child = pdel
self.block.secondary.delete_child = sdel
self.block.delete_child(self.comment)
assert pdel.called is True
assert sdel.called is True
def test_unsaved_files(self):
puns = mock.MagicMock()
suns = mock.MagicMock()
puns.return_value = assertions.PASS
suns.return_value = assertions.PASS
self.block.primary.unsaved_files = puns
self.block.secondary.unsaved_files = suns
self.block.unsaved_files()
assert puns.called is True
assert suns.called is True
def test_getattr_equality(self):
self.directive.primary.variableexception = "value"
self.directive.secondary.variableexception = "not_value"
with pytest.raises(AssertionError):
_ = self.directive.variableexception
self.directive.primary.variable = "value"
self.directive.secondary.variable = "value"
try:
self.directive.variable
except AssertionError: # pragma: no cover
self.fail("getattr check raised an AssertionError where it shouldn't have")
def test_parsernode_dirty_assert(self):
# disable assertion pass
self.comment.primary.comment = "value"
self.comment.secondary.comment = "value"
self.comment.primary.filepath = "x"
self.comment.secondary.filepath = "x"
self.comment.primary.dirty = False
self.comment.secondary.dirty = True
with pytest.raises(AssertionError):
assertions.assertEqual(self.comment.primary, self.comment.secondary)
def test_parsernode_filepath_assert(self):
# disable assertion pass
self.comment.primary.comment = "value"
self.comment.secondary.comment = "value"
self.comment.primary.filepath = "first"
self.comment.secondary.filepath = "second"
with pytest.raises(AssertionError):
assertions.assertEqual(self.comment.primary, self.comment.secondary)
def test_add_child_block(self):
mock_first = mock.MagicMock(return_value=self.block.primary)
mock_second = mock.MagicMock(return_value=self.block.secondary)
self.block.primary.add_child_block = mock_first
self.block.secondary.add_child_block = mock_second
self.block.add_child_block("Block")
assert mock_first.called is True
assert mock_second.called is True
def test_add_child_directive(self):
mock_first = mock.MagicMock(return_value=self.directive.primary)
mock_second = mock.MagicMock(return_value=self.directive.secondary)
self.block.primary.add_child_directive = mock_first
self.block.secondary.add_child_directive = mock_second
self.block.add_child_directive("Directive")
assert mock_first.called is True
assert mock_second.called is True
def test_add_child_comment(self):
mock_first = mock.MagicMock(return_value=self.comment.primary)
mock_second = mock.MagicMock(return_value=self.comment.secondary)
self.block.primary.add_child_comment = mock_first
self.block.secondary.add_child_comment = mock_second
self.block.add_child_comment("Comment")
assert mock_first.called is True
assert mock_second.called is True
def test_find_comments(self):
pri_comments = [augeasparser.AugeasCommentNode(comment="some comment",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
sec_comments = [augeasparser.AugeasCommentNode(comment=assertions.PASS,
ancestor=self.block,
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
self.block.secondary.find_comments = find_coms_secondary
dcoms = self.block.find_comments("comment")
p_dcoms = [d.primary for d in dcoms]
s_dcoms = [d.secondary for d in dcoms]
p_coms = self.block.primary.find_comments("comment")
s_coms = self.block.secondary.find_comments("comment")
# Check that every comment response is represented in the list of
# DualParserNode instances.
for p in p_dcoms:
assert p in p_coms
for s in s_dcoms:
assert s in s_coms
def test_find_blocks_first_passing(self):
youshallnotpass = [augeasparser.AugeasBlockNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
youshallpass = [augeasparser.AugeasBlockNode(name=assertions.PASS,
ancestor=self.block,
filepath=assertions.PASS,
metadata=self.metadata)]
find_blocks_primary = mock.MagicMock(return_value=youshallpass)
find_blocks_secondary = mock.MagicMock(return_value=youshallnotpass)
self.block.primary.find_blocks = find_blocks_primary
self.block.secondary.find_blocks = find_blocks_secondary
blocks = self.block.find_blocks("something")
for block in blocks:
try:
assertions.assertEqual(block.primary, block.secondary)
except AssertionError: # pragma: no cover
self.fail("Assertion should have passed")
assert assertions.isPassDirective(block.primary) is True
assert assertions.isPassDirective(block.secondary) is False
def test_find_blocks_second_passing(self):
youshallnotpass = [augeasparser.AugeasBlockNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
youshallpass = [augeasparser.AugeasBlockNode(name=assertions.PASS,
ancestor=self.block,
filepath=assertions.PASS,
metadata=self.metadata)]
find_blocks_primary = mock.MagicMock(return_value=youshallnotpass)
find_blocks_secondary = mock.MagicMock(return_value=youshallpass)
self.block.primary.find_blocks = find_blocks_primary
self.block.secondary.find_blocks = find_blocks_secondary
blocks = self.block.find_blocks("something")
for block in blocks:
try:
assertions.assertEqual(block.primary, block.secondary)
except AssertionError: # pragma: no cover
self.fail("Assertion should have passed")
assert assertions.isPassDirective(block.primary) is False
assert assertions.isPassDirective(block.secondary) is True
def test_find_dirs_first_passing(self):
notpassing = [augeasparser.AugeasDirectiveNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
passing = [augeasparser.AugeasDirectiveNode(name=assertions.PASS,
ancestor=self.block,
filepath=assertions.PASS,
metadata=self.metadata)]
find_dirs_primary = mock.MagicMock(return_value=passing)
find_dirs_secondary = mock.MagicMock(return_value=notpassing)
self.block.primary.find_directives = find_dirs_primary
self.block.secondary.find_directives = find_dirs_secondary
directives = self.block.find_directives("something")
for directive in directives:
try:
assertions.assertEqual(directive.primary, directive.secondary)
except AssertionError: # pragma: no cover
self.fail("Assertion should have passed")
assert assertions.isPassDirective(directive.primary) is True
assert assertions.isPassDirective(directive.secondary) is False
def test_find_dirs_second_passing(self):
notpassing = [augeasparser.AugeasDirectiveNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
passing = [augeasparser.AugeasDirectiveNode(name=assertions.PASS,
ancestor=self.block,
filepath=assertions.PASS,
metadata=self.metadata)]
find_dirs_primary = mock.MagicMock(return_value=notpassing)
find_dirs_secondary = mock.MagicMock(return_value=passing)
self.block.primary.find_directives = find_dirs_primary
self.block.secondary.find_directives = find_dirs_secondary
directives = self.block.find_directives("something")
for directive in directives:
try:
assertions.assertEqual(directive.primary, directive.secondary)
except AssertionError: # pragma: no cover
self.fail("Assertion should have passed")
assert assertions.isPassDirective(directive.primary) is False
assert assertions.isPassDirective(directive.secondary) is True
def test_find_coms_first_passing(self):
notpassing = [augeasparser.AugeasCommentNode(comment="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
passing = [augeasparser.AugeasCommentNode(comment=assertions.PASS,
ancestor=self.block,
filepath=assertions.PASS,
metadata=self.metadata)]
find_coms_primary = mock.MagicMock(return_value=passing)
find_coms_secondary = mock.MagicMock(return_value=notpassing)
self.block.primary.find_comments = find_coms_primary
self.block.secondary.find_comments = find_coms_secondary
comments = self.block.find_comments("something")
for comment in comments:
try:
assertions.assertEqual(comment.primary, comment.secondary)
except AssertionError: # pragma: no cover
self.fail("Assertion should have passed")
assert assertions.isPassComment(comment.primary) is True
assert assertions.isPassComment(comment.secondary) is False
def test_find_coms_second_passing(self):
notpassing = [augeasparser.AugeasCommentNode(comment="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
passing = [augeasparser.AugeasCommentNode(comment=assertions.PASS,
ancestor=self.block,
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
self.block.secondary.find_comments = find_coms_secondary
comments = self.block.find_comments("something")
for comment in comments:
try:
assertions.assertEqual(comment.primary, comment.secondary)
except AssertionError: # pragma: no cover
self.fail("Assertion should have passed")
assert assertions.isPassComment(comment.primary) is False
assert assertions.isPassComment(comment.secondary) is True
def test_find_blocks_no_pass_equal(self):
notpassing1 = [augeasparser.AugeasBlockNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
notpassing2 = [augeasparser.AugeasBlockNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
find_blocks_primary = mock.MagicMock(return_value=notpassing1)
find_blocks_secondary = mock.MagicMock(return_value=notpassing2)
self.block.primary.find_blocks = find_blocks_primary
self.block.secondary.find_blocks = find_blocks_secondary
blocks = self.block.find_blocks("anything")
for block in blocks:
with self.subTest(block=block):
assert block.primary == block.secondary
assert block.primary is not block.secondary
def test_find_dirs_no_pass_equal(self):
notpassing1 = [augeasparser.AugeasDirectiveNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
notpassing2 = [augeasparser.AugeasDirectiveNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
find_dirs_primary = mock.MagicMock(return_value=notpassing1)
find_dirs_secondary = mock.MagicMock(return_value=notpassing2)
self.block.primary.find_directives = find_dirs_primary
self.block.secondary.find_directives = find_dirs_secondary
directives = self.block.find_directives("anything")
for directive in directives:
with self.subTest(directive=directive):
assert directive.primary == directive.secondary
assert directive.primary is not directive.secondary
def test_find_comments_no_pass_equal(self):
notpassing1 = [augeasparser.AugeasCommentNode(comment="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
notpassing2 = [augeasparser.AugeasCommentNode(comment="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
find_coms_primary = mock.MagicMock(return_value=notpassing1)
find_coms_secondary = mock.MagicMock(return_value=notpassing2)
self.block.primary.find_comments = find_coms_primary
self.block.secondary.find_comments = find_coms_secondary
comments = self.block.find_comments("anything")
for comment in comments:
with self.subTest(comment=comment):
assert comment.primary == comment.secondary
assert comment.primary is not comment.secondary
def test_find_blocks_no_pass_notequal(self):
notpassing1 = [augeasparser.AugeasBlockNode(name="notpassing",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
notpassing2 = [augeasparser.AugeasBlockNode(name="different",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)]
find_blocks_primary = mock.MagicMock(return_value=notpassing1)
find_blocks_secondary = mock.MagicMock(return_value=notpassing2)
self.block.primary.find_blocks = find_blocks_primary
self.block.secondary.find_blocks = find_blocks_secondary
with pytest.raises(AssertionError):
_ = self.block.find_blocks("anything")
def test_parsernode_notequal(self):
ne_block = augeasparser.AugeasBlockNode(name="different",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)
ne_directive = augeasparser.AugeasDirectiveNode(name="different",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)
ne_comment = augeasparser.AugeasCommentNode(comment="different",
ancestor=self.block,
filepath="/path/to/whatever",
metadata=self.metadata)
assert self.block != ne_block
assert self.directive != ne_directive
assert self.comment != ne_comment
def test_parsed_paths(self):
mock_p = mock.MagicMock(return_value=['/path/file.conf',
'/another/path',
'/path/other.conf'])
mock_s = mock.MagicMock(return_value=['/path/*.conf', '/another/path'])
self.block.primary.parsed_paths = mock_p
self.block.secondary.parsed_paths = mock_s
self.block.parsed_paths()
assert mock_p.called is True
assert mock_s.called is True
def test_parsed_paths_error(self):
mock_p = mock.MagicMock(return_value=['/path/file.conf'])
mock_s = mock.MagicMock(return_value=['/path/*.conf', '/another/path'])
self.block.primary.parsed_paths = mock_p
self.block.secondary.parsed_paths = mock_s
with pytest.raises(AssertionError):
self.block.parsed_paths()
def test_find_ancestors(self):
primarymock = mock.MagicMock(return_value=[])
secondarymock = mock.MagicMock(return_value=[])
self.block.primary.find_ancestors = primarymock
self.block.secondary.find_ancestors = secondarymock
self.block.find_ancestors("anything")
assert primarymock.called is True
assert secondarymock.called is True
|