当前位置: 首页>>代码示例>>Python>>正文


Python StandaloneAuthenticator.already_listening方法代码示例

本文整理汇总了Python中letsencrypt.plugins.standalone.authenticator.StandaloneAuthenticator.already_listening方法的典型用法代码示例。如果您正苦于以下问题:Python StandaloneAuthenticator.already_listening方法的具体用法?Python StandaloneAuthenticator.already_listening怎么用?Python StandaloneAuthenticator.already_listening使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在letsencrypt.plugins.standalone.authenticator.StandaloneAuthenticator的用法示例。


在下文中一共展示了StandaloneAuthenticator.already_listening方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: AlreadyListeningTest

# 需要导入模块: from letsencrypt.plugins.standalone.authenticator import StandaloneAuthenticator [as 别名]
# 或者: from letsencrypt.plugins.standalone.authenticator.StandaloneAuthenticator import already_listening [as 别名]
class AlreadyListeningTest(unittest.TestCase):
    """Tests for already_listening() method."""
    def setUp(self):
        from letsencrypt.plugins.standalone.authenticator import \
            StandaloneAuthenticator
        self.authenticator = StandaloneAuthenticator(config=CONFIG, name=None)

    @mock.patch("letsencrypt.plugins.standalone.authenticator.psutil."
                "net_connections")
    @mock.patch("letsencrypt.plugins.standalone.authenticator.psutil.Process")
    @mock.patch("letsencrypt.plugins.standalone.authenticator."
                "zope.component.getUtility")
    def test_race_condition(self, mock_get_utility, mock_process, mock_net):
        # This tests a race condition, or permission problem, or OS
        # incompatibility in which, for some reason, no process name can be
        # found to match the identified listening PID.
        from psutil._common import sconn
        conns = [
            sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 30),
                  raddr=(), status="LISTEN", pid=None),
            sconn(fd=3, family=2, type=1, laddr=("192.168.5.10", 32783),
                  raddr=("20.40.60.80", 22), status="ESTABLISHED", pid=1234),
            sconn(fd=-1, family=10, type=1, laddr=("::1", 54321),
                  raddr=("::1", 111), status="CLOSE_WAIT", pid=None),
            sconn(fd=3, family=2, type=1, laddr=("0.0.0.0", 17),
                  raddr=(), status="LISTEN", pid=4416)]
        mock_net.return_value = conns
        mock_process.side_effect = psutil.NoSuchProcess("No such PID")
        # We simulate being unable to find the process name of PID 4416,
        # which results in returning False.
        self.assertFalse(self.authenticator.already_listening(17))
        self.assertEqual(mock_get_utility.generic_notification.call_count, 0)
        mock_process.assert_called_once_with(4416)

    @mock.patch("letsencrypt.plugins.standalone.authenticator.psutil."
                "net_connections")
    @mock.patch("letsencrypt.plugins.standalone.authenticator.psutil.Process")
    @mock.patch("letsencrypt.plugins.standalone.authenticator."
                "zope.component.getUtility")
    def test_not_listening(self, mock_get_utility, mock_process, mock_net):
        from psutil._common import sconn
        conns = [
            sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 30),
                  raddr=(), status="LISTEN", pid=None),
            sconn(fd=3, family=2, type=1, laddr=("192.168.5.10", 32783),
                  raddr=("20.40.60.80", 22), status="ESTABLISHED", pid=1234),
            sconn(fd=-1, family=10, type=1, laddr=("::1", 54321),
                  raddr=("::1", 111), status="CLOSE_WAIT", pid=None)]
        mock_net.return_value = conns
        mock_process.name.return_value = "inetd"
        self.assertFalse(self.authenticator.already_listening(17))
        self.assertEqual(mock_get_utility.generic_notification.call_count, 0)
        self.assertEqual(mock_process.call_count, 0)

    @mock.patch("letsencrypt.plugins.standalone.authenticator.psutil."
                "net_connections")
    @mock.patch("letsencrypt.plugins.standalone.authenticator.psutil.Process")
    @mock.patch("letsencrypt.plugins.standalone.authenticator."
                "zope.component.getUtility")
    def test_listening_ipv4(self, mock_get_utility, mock_process, mock_net):
        from psutil._common import sconn
        conns = [
            sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 30),
                  raddr=(), status="LISTEN", pid=None),
            sconn(fd=3, family=2, type=1, laddr=("192.168.5.10", 32783),
                  raddr=("20.40.60.80", 22), status="ESTABLISHED", pid=1234),
            sconn(fd=-1, family=10, type=1, laddr=("::1", 54321),
                  raddr=("::1", 111), status="CLOSE_WAIT", pid=None),
            sconn(fd=3, family=2, type=1, laddr=("0.0.0.0", 17),
                  raddr=(), status="LISTEN", pid=4416)]
        mock_net.return_value = conns
        mock_process.name.return_value = "inetd"
        result = self.authenticator.already_listening(17)
        self.assertTrue(result)
        self.assertEqual(mock_get_utility.call_count, 1)
        mock_process.assert_called_once_with(4416)

    @mock.patch("letsencrypt.plugins.standalone.authenticator.psutil."
                "net_connections")
    @mock.patch("letsencrypt.plugins.standalone.authenticator.psutil.Process")
    @mock.patch("letsencrypt.plugins.standalone.authenticator."
                "zope.component.getUtility")
    def test_listening_ipv6(self, mock_get_utility, mock_process, mock_net):
        from psutil._common import sconn
        conns = [
            sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 30),
                  raddr=(), status="LISTEN", pid=None),
            sconn(fd=3, family=2, type=1, laddr=("192.168.5.10", 32783),
                  raddr=("20.40.60.80", 22), status="ESTABLISHED", pid=1234),
            sconn(fd=-1, family=10, type=1, laddr=("::1", 54321),
                  raddr=("::1", 111), status="CLOSE_WAIT", pid=None),
            sconn(fd=3, family=10, type=1, laddr=("::", 12345), raddr=(),
                  status="LISTEN", pid=4420),
            sconn(fd=3, family=2, type=1, laddr=("0.0.0.0", 17),
                  raddr=(), status="LISTEN", pid=4416)]
        mock_net.return_value = conns
        mock_process.name.return_value = "inetd"
        result = self.authenticator.already_listening(12345)
        self.assertTrue(result)
        self.assertEqual(mock_get_utility.call_count, 1)
#.........这里部分代码省略.........
开发者ID:mommel,项目名称:lets-encrypt-preview,代码行数:103,代码来源:authenticator_test.py


注:本文中的letsencrypt.plugins.standalone.authenticator.StandaloneAuthenticator.already_listening方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。