certbot/letsencrypt-apache/letsencrypt_apache/tests/parser_test.py

129 lines
4.3 KiB
Python
Raw Normal View History

2015-05-10 06:47:58 -04:00
"""Tests for letsencrypt_apache.parser."""
2014-12-19 18:49:29 -05:00
import os
import shutil
import sys
import unittest
import augeas
import mock
import zope.component
2014-12-19 18:49:29 -05:00
2015-05-10 08:25:29 -04:00
from letsencrypt import errors
from letsencrypt.display import util as display_util
2014-12-19 18:49:29 -05:00
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.tests import util
2014-12-19 18:49:29 -05:00
2015-01-24 08:12:45 -05:00
class ApacheParserTest(util.ApacheTest):
"""Apache Parser Test."""
2014-12-19 18:49:29 -05:00
2015-01-24 08:12:45 -05:00
def setUp(self):
super(ApacheParserTest, self).setUp()
2014-12-19 18:49:29 -05:00
zope.component.provideUtility(display_util.FileDisplay(sys.stdout))
2014-12-19 18:49:29 -05:00
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.parser import ApacheParser
self.aug = augeas.Augeas(flags=augeas.Augeas.NONE)
self.parser = ApacheParser(self.aug, self.config_path, self.ssl_options)
2014-12-19 18:49:29 -05:00
def tearDown(self):
shutil.rmtree(self.temp_dir)
shutil.rmtree(self.config_dir)
shutil.rmtree(self.work_dir)
def test_root_normalized(self):
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.parser import ApacheParser
path = os.path.join(self.temp_dir, "debian_apache_2_4/////"
"two_vhost_80/../two_vhost_80/apache2")
parser = ApacheParser(self.aug, path, None)
self.assertEqual(parser.root, self.config_path)
def test_root_absolute(self):
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.parser import ApacheParser
parser = ApacheParser(self.aug, os.path.relpath(self.config_path), None)
self.assertEqual(parser.root, self.config_path)
def test_root_no_trailing_slash(self):
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.parser import ApacheParser
parser = ApacheParser(self.aug, self.config_path + os.path.sep, None)
self.assertEqual(parser.root, self.config_path)
2014-12-19 18:49:29 -05:00
def test_parse_file(self):
"""Test parse_file.
letsencrypt.conf is chosen as the test file as it will not be
included during the normal course of execution.
"""
file_path = os.path.join(
self.config_path, "sites-available", "letsencrypt.conf")
2015-01-25 05:23:21 -05:00
self.parser._parse_file(file_path) # pylint: disable=protected-access
2014-12-19 18:49:29 -05:00
# search for the httpd incl
matches = self.parser.aug.match(
"/augeas/load/Httpd/incl [. ='%s']" % file_path)
self.assertTrue(matches)
def test_find_dir(self):
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.parser import case_i
test = self.parser.find_dir(case_i("Listen"), "443")
2014-12-19 18:49:29 -05:00
# This will only look in enabled hosts
test2 = self.parser.find_dir(case_i("documentroot"))
2014-12-19 18:49:29 -05:00
self.assertEqual(len(test), 2)
self.assertEqual(len(test2), 3)
def test_add_dir(self):
aug_default = "/files" + self.parser.loc["default"]
2014-12-20 06:43:39 -05:00
self.parser.add_dir(aug_default, "AddDirective", "test")
2014-12-19 18:49:29 -05:00
self.assertTrue(
self.parser.find_dir("AddDirective", "test", aug_default))
self.parser.add_dir(aug_default, "AddList", ["1", "2", "3", "4"])
matches = self.parser.find_dir("AddList", None, aug_default)
for i, match in enumerate(matches):
self.assertEqual(self.parser.aug.get(match), str(i + 1))
def test_add_dir_to_ifmodssl(self):
"""test add_dir_to_ifmodssl.
Path must be valid before attempting to add to augeas
"""
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.parser import get_aug_path
2014-12-19 18:49:29 -05:00
self.parser.add_dir_to_ifmodssl(
get_aug_path(self.parser.loc["default"]),
2014-12-19 18:49:29 -05:00
"FakeDirective", "123")
matches = self.parser.find_dir("FakeDirective", "123")
self.assertEqual(len(matches), 1)
self.assertTrue("IfModule" in matches[0])
def test_get_aug_path(self):
2015-05-10 06:47:58 -04:00
from letsencrypt_apache.parser import get_aug_path
self.assertEqual("/files/etc/apache", get_aug_path("/etc/apache"))
2014-12-19 18:49:29 -05:00
def test_set_locations(self):
2015-05-10 06:47:58 -04:00
with mock.patch("letsencrypt_apache.parser.os.path") as mock_path:
2014-12-19 18:49:29 -05:00
mock_path.isfile.return_value = False
# pylint: disable=protected-access
2015-06-26 12:29:40 -04:00
self.assertRaises(errors.PluginError,
2014-12-19 18:49:29 -05:00
self.parser._set_locations, self.ssl_options)
mock_path.isfile.side_effect = [True, False, False]
# pylint: disable=protected-access
results = self.parser._set_locations(self.ssl_options)
self.assertEqual(results["default"], results["listen"])
self.assertEqual(results["default"], results["name"])
2015-01-24 08:12:45 -05:00
2015-03-26 20:39:08 -04:00
if __name__ == "__main__":
unittest.main() # pragma: no cover