def setUp(self): from certbot_apache._internal.obj import Addr from certbot_apache._internal.obj import VirtualHost self.addr1 = Addr.fromstring("127.0.0.1") self.addr2 = Addr.fromstring("127.0.0.1:443") self.addr_default = Addr.fromstring("_default_:443") self.vhost1 = VirtualHost("filep", "vh_path", set([self.addr1]), False, False, "localhost") self.vhost1b = VirtualHost("filep", "vh_path", set([self.addr1]), False, False, "localhost") self.vhost2 = VirtualHost("fp", "vhp", set([self.addr2]), False, False, "localhost")
def test_conflicts(self): from certbot_apache._internal.obj import Addr from certbot_apache._internal.obj import VirtualHost complex_vh = VirtualHost( "fp", "vhp", set([Addr.fromstring("*:443"), Addr.fromstring("1.2.3.4:443")]), False, False) self.assertTrue(complex_vh.conflicts([self.addr1])) self.assertTrue(complex_vh.conflicts([self.addr2])) self.assertFalse(complex_vh.conflicts([self.addr_default])) self.assertTrue(self.vhost1.conflicts([self.addr2])) self.assertFalse(self.vhost1.conflicts([self.addr_default])) self.assertFalse(self.vhost2.conflicts([self.addr1, self.addr_default]))
def enable_site(self, vhost: VirtualHost) -> None: """Enables an available site, Apache reload required. .. note:: Does not make sure that the site correctly works or that all modules are enabled appropriately. :param vhost: vhost to enable :type vhost: :class:`~certbot_apache._internal.obj.VirtualHost` :raises .errors.NotSupportedError: If filesystem layout is not supported. """ if vhost.enabled: return None enabled_path = ("%s/sites-enabled/%s" % (self.parser.root, os.path.basename(vhost.filep))) if not os.path.isdir(os.path.dirname(enabled_path)): # For some reason, sites-enabled / sites-available do not exist # Call the parent method return super().enable_site(vhost) self.reverter.register_file_creation(False, enabled_path) try: os.symlink(vhost.filep, enabled_path) except OSError as err: if os.path.islink(enabled_path) and filesystem.realpath( enabled_path) == vhost.filep: # Already in shape vhost.enabled = True return None logger.error( "Could not symlink %s to %s, got error: %s", enabled_path, vhost.filep, err.strerror) errstring = ("Encountered error while trying to enable a " + "newly created VirtualHost located at {0} by " + "linking to it from {1}") raise errors.NotSupportedError(errstring.format(vhost.filep, enabled_path)) vhost.enabled = True logger.info("Enabling available site: %s", vhost.filep) self.save_notes += "Enabled site %s\n" % vhost.filep return None
def test_same_server(self): from certbot_apache._internal.obj import VirtualHost no_name1 = VirtualHost("fp", "vhp", {self.addr1}, False, False, None) no_name2 = VirtualHost("fp", "vhp", {self.addr2}, False, False, None) no_name3 = VirtualHost("fp", "vhp", {self.addr_default}, False, False, None) no_name4 = VirtualHost("fp", "vhp", {self.addr2, self.addr_default}, False, False, None) self.assertTrue(self.vhost1.same_server(self.vhost2)) self.assertTrue(no_name1.same_server(no_name2)) self.assertFalse(self.vhost1.same_server(no_name1)) self.assertFalse(no_name1.same_server(no_name3)) self.assertFalse(no_name1.same_server(no_name4))
class VirtualHostTest(unittest.TestCase): """Test the VirtualHost class.""" def setUp(self): from certbot_apache._internal.obj import Addr from certbot_apache._internal.obj import VirtualHost self.addr1 = Addr.fromstring("127.0.0.1") self.addr2 = Addr.fromstring("127.0.0.1:443") self.addr_default = Addr.fromstring("_default_:443") self.vhost1 = VirtualHost("filep", "vh_path", set([self.addr1]), False, False, "localhost") self.vhost1b = VirtualHost("filep", "vh_path", set([self.addr1]), False, False, "localhost") self.vhost2 = VirtualHost("fp", "vhp", set([self.addr2]), False, False, "localhost") def test_repr(self): self.assertEqual( repr(self.addr2), "certbot_apache._internal.obj.Addr(('127.0.0.1', '443'))") def test_eq(self): self.assertTrue(self.vhost1b == self.vhost1) self.assertFalse(self.vhost1 == self.vhost2) self.assertEqual(str(self.vhost1b), str(self.vhost1)) self.assertFalse(self.vhost1b == 1234) def test_ne(self): self.assertTrue(self.vhost1 != self.vhost2) self.assertFalse(self.vhost1 != self.vhost1b) def test_conflicts(self): from certbot_apache._internal.obj import Addr from certbot_apache._internal.obj import VirtualHost complex_vh = VirtualHost( "fp", "vhp", set([Addr.fromstring("*:443"), Addr.fromstring("1.2.3.4:443")]), False, False) self.assertTrue(complex_vh.conflicts([self.addr1])) self.assertTrue(complex_vh.conflicts([self.addr2])) self.assertFalse(complex_vh.conflicts([self.addr_default])) self.assertTrue(self.vhost1.conflicts([self.addr2])) self.assertFalse(self.vhost1.conflicts([self.addr_default])) self.assertFalse(self.vhost2.conflicts([self.addr1, self.addr_default])) def test_same_server(self): from certbot_apache._internal.obj import VirtualHost no_name1 = VirtualHost("fp", "vhp", set([self.addr1]), False, False, None) no_name2 = VirtualHost("fp", "vhp", set([self.addr2]), False, False, None) no_name3 = VirtualHost("fp", "vhp", set([self.addr_default]), False, False, None) no_name4 = VirtualHost("fp", "vhp", set([self.addr2, self.addr_default]), False, False, None) self.assertTrue(self.vhost1.same_server(self.vhost2)) self.assertTrue(no_name1.same_server(no_name2)) self.assertFalse(self.vhost1.same_server(no_name1)) self.assertFalse(no_name1.same_server(no_name3)) self.assertFalse(no_name1.same_server(no_name4))