예제 #1
0
    def setUp(self):
        self.ep1 = pkg_resources.EntryPoint("ep1",
                                            "p1.ep1",
                                            dist=mock.MagicMock(key="p1"))
        self.ep1prim = pkg_resources.EntryPoint("ep1",
                                                "p2.ep2",
                                                dist=mock.MagicMock(key="p2"))
        # nested
        self.ep2 = pkg_resources.EntryPoint("ep2",
                                            "p2.foo.ep2",
                                            dist=mock.MagicMock(key="p2"))
        # project name != top-level package name
        self.ep3 = pkg_resources.EntryPoint("ep3",
                                            "a.ep3",
                                            dist=mock.MagicMock(key="p3"))

        from certbot.plugins.disco import PluginEntryPoint
        self.plugin_ep = PluginEntryPoint(EP_SA)
예제 #2
0
    def test_entry_point_to_plugin_name(self):
        from certbot.plugins.disco import PluginEntryPoint

        names = {
            self.ep1: "p1:ep1",
            self.ep1prim: "p2:ep1",
            self.ep2: "p2:ep2",
            self.ep3: "p3:ep3",
            EP_SA: "sa",
        }

        for entry_point, name in six.iteritems(names):
            self.assertEqual(
                name, PluginEntryPoint.entry_point_to_plugin_name(entry_point))
예제 #3
0
    def setUp(self):
        self.ep1 = pkg_resources.EntryPoint(
            "ep1", "p1.ep1", dist=mock.MagicMock(key="p1"))
        self.ep1prim = pkg_resources.EntryPoint(
            "ep1", "p2.ep2", dist=mock.MagicMock(key="p2"))
        # nested
        self.ep2 = pkg_resources.EntryPoint(
            "ep2", "p2.foo.ep2", dist=mock.MagicMock(key="p2"))
        # project name != top-level package name
        self.ep3 = pkg_resources.EntryPoint(
            "ep3", "a.ep3", dist=mock.MagicMock(key="p3"))

        from certbot.plugins.disco import PluginEntryPoint
        self.plugin_ep = PluginEntryPoint(EP_SA)
예제 #4
0
    def test_entry_point_to_plugin_name(self):
        from certbot.plugins.disco import PluginEntryPoint

        names = {
            self.ep1: "p1:ep1",
            self.ep1prim: "p2:ep1",
            self.ep2: "p2:ep2",
            self.ep3: "p3:ep3",
            EP_SA: "sa",
        }

        for entry_point, name in six.iteritems(names):
            self.assertEqual(
                name, PluginEntryPoint.entry_point_to_plugin_name(entry_point))
예제 #5
0
class PluginEntryPointTest(unittest.TestCase):
    """Tests for certbot.plugins.disco.PluginEntryPoint."""

    def setUp(self):
        self.ep1 = pkg_resources.EntryPoint(
            "ep1", "p1.ep1", dist=mock.MagicMock(key="p1"))
        self.ep1prim = pkg_resources.EntryPoint(
            "ep1", "p2.ep2", dist=mock.MagicMock(key="p2"))
        # nested
        self.ep2 = pkg_resources.EntryPoint(
            "ep2", "p2.foo.ep2", dist=mock.MagicMock(key="p2"))
        # project name != top-level package name
        self.ep3 = pkg_resources.EntryPoint(
            "ep3", "a.ep3", dist=mock.MagicMock(key="p3"))

        from certbot.plugins.disco import PluginEntryPoint
        self.plugin_ep = PluginEntryPoint(EP_SA)

    def test_entry_point_to_plugin_name(self):
        from certbot.plugins.disco import PluginEntryPoint

        names = {
            self.ep1: "p1:ep1",
            self.ep1prim: "p2:ep1",
            self.ep2: "p2:ep2",
            self.ep3: "p3:ep3",
            EP_SA: "sa",
        }

        for entry_point, name in six.iteritems(names):
            self.assertEqual(
                name, PluginEntryPoint.entry_point_to_plugin_name(entry_point))

    def test_description(self):
        self.assertTrue("temporary webserver" in self.plugin_ep.description)

    def test_description_with_name(self):
        self.plugin_ep.plugin_cls = mock.MagicMock(description="Desc")
        self.assertEqual(
            "Desc (sa)", self.plugin_ep.description_with_name)

    def test_long_description(self):
        self.plugin_ep.plugin_cls = mock.MagicMock(
            long_description="Long desc")
        self.assertEqual(
            "Long desc", self.plugin_ep.long_description)

    def test_long_description_nonexistent(self):
        self.plugin_ep.plugin_cls = mock.MagicMock(
            description="Long desc not found", spec=["description"])
        self.assertEqual(
            "Long desc not found", self.plugin_ep.long_description)

    def test_ifaces(self):
        self.assertTrue(self.plugin_ep.ifaces((interfaces.IAuthenticator,)))
        self.assertFalse(self.plugin_ep.ifaces((interfaces.IInstaller,)))
        self.assertFalse(self.plugin_ep.ifaces((
            interfaces.IInstaller, interfaces.IAuthenticator)))

    def test__init__(self):
        self.assertFalse(self.plugin_ep.initialized)
        self.assertFalse(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)
        self.assertFalse(self.plugin_ep.available)
        self.assertTrue(self.plugin_ep.problem is None)
        self.assertTrue(self.plugin_ep.entry_point is EP_SA)
        self.assertEqual("sa", self.plugin_ep.name)

        self.assertTrue(self.plugin_ep.plugin_cls is standalone.Authenticator)

    def test_init(self):
        config = mock.MagicMock()
        plugin = self.plugin_ep.init(config=config)
        self.assertTrue(self.plugin_ep.initialized)
        self.assertTrue(plugin.config is config)
        # memoize!
        self.assertTrue(self.plugin_ep.init() is plugin)
        self.assertTrue(plugin.config is config)
        # try to give different config
        self.assertTrue(self.plugin_ep.init(123) is plugin)
        self.assertTrue(plugin.config is config)

        self.assertFalse(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)
        self.assertFalse(self.plugin_ep.available)

    def test_verify(self):
        iface1 = mock.MagicMock(__name__="iface1")
        iface2 = mock.MagicMock(__name__="iface2")
        iface3 = mock.MagicMock(__name__="iface3")
        # pylint: disable=protected-access
        self.plugin_ep._initialized = plugin = mock.MagicMock()

        exceptions = zope.interface.exceptions
        with mock.patch("certbot.plugins."
                        "disco.zope.interface") as mock_zope:
            mock_zope.exceptions = exceptions

            def verify_object(iface, obj):  # pylint: disable=missing-docstring
                assert obj is plugin
                assert iface is iface1 or iface is iface2 or iface is iface3
                if iface is iface3:
                    raise mock_zope.exceptions.BrokenImplementation(None, None)
            mock_zope.verify.verifyObject.side_effect = verify_object
            self.assertTrue(self.plugin_ep.verify((iface1,)))
            self.assertTrue(self.plugin_ep.verify((iface1, iface2)))
            self.assertFalse(self.plugin_ep.verify((iface3,)))
            self.assertFalse(self.plugin_ep.verify((iface1, iface3)))

    def test_prepare(self):
        config = mock.MagicMock()
        self.plugin_ep.init(config=config)
        self.plugin_ep.prepare()
        self.assertTrue(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)

        # output doesn't matter that much, just test if it runs
        str(self.plugin_ep)

    def test_prepare_misconfigured(self):
        plugin = mock.MagicMock()
        plugin.prepare.side_effect = errors.MisconfigurationError
        # pylint: disable=protected-access
        self.plugin_ep._initialized = plugin
        self.assertTrue(isinstance(self.plugin_ep.prepare(),
                                   errors.MisconfigurationError))
        self.assertTrue(self.plugin_ep.prepared)
        self.assertTrue(self.plugin_ep.misconfigured)
        self.assertTrue(isinstance(self.plugin_ep.problem,
                                   errors.MisconfigurationError))
        self.assertTrue(self.plugin_ep.available)

    def test_prepare_no_installation(self):
        plugin = mock.MagicMock()
        plugin.prepare.side_effect = errors.NoInstallationError
        # pylint: disable=protected-access
        self.plugin_ep._initialized = plugin
        self.assertTrue(isinstance(self.plugin_ep.prepare(),
                                   errors.NoInstallationError))
        self.assertTrue(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)
        self.assertFalse(self.plugin_ep.available)

    def test_prepare_generic_plugin_error(self):
        plugin = mock.MagicMock()
        plugin.prepare.side_effect = errors.PluginError
        # pylint: disable=protected-access
        self.plugin_ep._initialized = plugin
        self.assertTrue(isinstance(self.plugin_ep.prepare(), errors.PluginError))
        self.assertTrue(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)
        self.assertFalse(self.plugin_ep.available)

    def test_repr(self):
        self.assertEqual("PluginEntryPoint#sa", repr(self.plugin_ep))
예제 #6
0
class PluginEntryPointTest(unittest.TestCase):
    """Tests for certbot.plugins.disco.PluginEntryPoint."""
    def setUp(self):
        self.ep1 = pkg_resources.EntryPoint("ep1",
                                            "p1.ep1",
                                            dist=mock.MagicMock(key="p1"))
        self.ep1prim = pkg_resources.EntryPoint("ep1",
                                                "p2.ep2",
                                                dist=mock.MagicMock(key="p2"))
        # nested
        self.ep2 = pkg_resources.EntryPoint("ep2",
                                            "p2.foo.ep2",
                                            dist=mock.MagicMock(key="p2"))
        # project name != top-level package name
        self.ep3 = pkg_resources.EntryPoint("ep3",
                                            "a.ep3",
                                            dist=mock.MagicMock(key="p3"))

        from certbot.plugins.disco import PluginEntryPoint
        self.plugin_ep = PluginEntryPoint(EP_SA)

    def test_entry_point_to_plugin_name(self):
        from certbot.plugins.disco import PluginEntryPoint

        names = {
            self.ep1: "p1:ep1",
            self.ep1prim: "p2:ep1",
            self.ep2: "p2:ep2",
            self.ep3: "p3:ep3",
            EP_SA: "sa",
        }

        for entry_point, name in six.iteritems(names):
            self.assertEqual(
                name, PluginEntryPoint.entry_point_to_plugin_name(entry_point))

    def test_description(self):
        self.assertTrue("temporary webserver" in self.plugin_ep.description)

    def test_description_with_name(self):
        self.plugin_ep.plugin_cls = mock.MagicMock(description="Desc")
        self.assertEqual("Desc (sa)", self.plugin_ep.description_with_name)

    def test_long_description(self):
        self.plugin_ep.plugin_cls = mock.MagicMock(
            long_description="Long desc")
        self.assertEqual("Long desc", self.plugin_ep.long_description)

    def test_long_description_nonexistent(self):
        self.plugin_ep.plugin_cls = mock.MagicMock(
            description="Long desc not found", spec=["description"])
        self.assertEqual("Long desc not found",
                         self.plugin_ep.long_description)

    def test_ifaces(self):
        self.assertTrue(self.plugin_ep.ifaces((interfaces.IAuthenticator, )))
        self.assertFalse(self.plugin_ep.ifaces((interfaces.IInstaller, )))
        self.assertFalse(
            self.plugin_ep.ifaces(
                (interfaces.IInstaller, interfaces.IAuthenticator)))

    def test__init__(self):
        self.assertFalse(self.plugin_ep.initialized)
        self.assertFalse(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)
        self.assertFalse(self.plugin_ep.available)
        self.assertTrue(self.plugin_ep.problem is None)
        self.assertTrue(self.plugin_ep.entry_point is EP_SA)
        self.assertEqual("sa", self.plugin_ep.name)

        self.assertTrue(self.plugin_ep.plugin_cls is standalone.Authenticator)

    def test_init(self):
        config = mock.MagicMock()
        plugin = self.plugin_ep.init(config=config)
        self.assertTrue(self.plugin_ep.initialized)
        self.assertTrue(plugin.config is config)
        # memoize!
        self.assertTrue(self.plugin_ep.init() is plugin)
        self.assertTrue(plugin.config is config)
        # try to give different config
        self.assertTrue(self.plugin_ep.init(123) is plugin)
        self.assertTrue(plugin.config is config)

        self.assertFalse(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)
        self.assertFalse(self.plugin_ep.available)

    def test_verify(self):
        iface1 = mock.MagicMock(__name__="iface1")
        iface2 = mock.MagicMock(__name__="iface2")
        iface3 = mock.MagicMock(__name__="iface3")
        # pylint: disable=protected-access
        self.plugin_ep._initialized = plugin = mock.MagicMock()

        exceptions = zope.interface.exceptions
        with mock.patch("certbot.plugins."
                        "disco.zope.interface") as mock_zope:
            mock_zope.exceptions = exceptions

            def verify_object(iface, obj):  # pylint: disable=missing-docstring
                assert obj is plugin
                assert iface is iface1 or iface is iface2 or iface is iface3
                if iface is iface3:
                    raise mock_zope.exceptions.BrokenImplementation(None, None)

            mock_zope.verify.verifyObject.side_effect = verify_object
            self.assertTrue(self.plugin_ep.verify((iface1, )))
            self.assertTrue(self.plugin_ep.verify((iface1, iface2)))
            self.assertFalse(self.plugin_ep.verify((iface3, )))
            self.assertFalse(self.plugin_ep.verify((iface1, iface3)))

    def test_prepare(self):
        config = mock.MagicMock()
        self.plugin_ep.init(config=config)
        self.plugin_ep.prepare()
        self.assertTrue(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)

        # output doesn't matter that much, just test if it runs
        str(self.plugin_ep)

    def test_prepare_misconfigured(self):
        plugin = mock.MagicMock()
        plugin.prepare.side_effect = errors.MisconfigurationError
        # pylint: disable=protected-access
        self.plugin_ep._initialized = plugin
        self.assertTrue(
            isinstance(self.plugin_ep.prepare(), errors.MisconfigurationError))
        self.assertTrue(self.plugin_ep.prepared)
        self.assertTrue(self.plugin_ep.misconfigured)
        self.assertTrue(
            isinstance(self.plugin_ep.problem, errors.MisconfigurationError))
        self.assertTrue(self.plugin_ep.available)

    def test_prepare_no_installation(self):
        plugin = mock.MagicMock()
        plugin.prepare.side_effect = errors.NoInstallationError
        # pylint: disable=protected-access
        self.plugin_ep._initialized = plugin
        self.assertTrue(
            isinstance(self.plugin_ep.prepare(), errors.NoInstallationError))
        self.assertTrue(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)
        self.assertFalse(self.plugin_ep.available)

    def test_prepare_generic_plugin_error(self):
        plugin = mock.MagicMock()
        plugin.prepare.side_effect = errors.PluginError
        # pylint: disable=protected-access
        self.plugin_ep._initialized = plugin
        self.assertTrue(
            isinstance(self.plugin_ep.prepare(), errors.PluginError))
        self.assertTrue(self.plugin_ep.prepared)
        self.assertFalse(self.plugin_ep.misconfigured)
        self.assertFalse(self.plugin_ep.available)

    def test_repr(self):
        self.assertEqual("PluginEntryPoint#sa", repr(self.plugin_ep))