blob: ce36f7eb7e351747d5c1e2771723d495d433f036 [file] [log] [blame]
Mike Frysingera7f08bc2019-08-27 15:16:33 -04001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Gilad Arnold7de05f72014-02-14 13:14:20 -08003# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Unit tests for cherrypy_ext."""
8
Amin Hassanic5af4262019-11-13 13:37:20 -08009from __future__ import print_function
10
Gilad Arnold7de05f72014-02-14 13:14:20 -080011import tempfile
12import unittest
13
Amin Hassanic5af4262019-11-13 13:37:20 -080014import mox # pylint: disable=import-error
15import cherrypy # pylint: disable=import-error
Gilad Arnold7de05f72014-02-14 13:14:20 -080016
17import cherrypy_ext
18
19
20class CherrypyExtTest(mox.MoxTestBase):
21 """Tests for the cherrypy_ext module."""
22
23 def testPortFile(self):
24 """Check that PortFile correctly reports a bound port."""
25 with tempfile.NamedTemporaryFile(delete=False) as f:
26 portfile = f.name
27 bus = self.mox.CreateMock(cherrypy.engine)
28 self.mox.StubOutWithMock(bus, 'log')
29 bus.log(mox.IsA(str)).MultipleTimes()
30
31 cherrypy.server = self.mox.CreateMock(object)
32 cherrypy.server.httpserver = self.mox.CreateMock(object)
33 cherrypy.server.httpserver.socket = self.mox.CreateMock(object)
34 cherrypy.server.httpserver.socket.getsockname = None
35 self.mox.StubOutWithMock(cherrypy.server.httpserver.socket, 'getsockname')
36 cherrypy.server.httpserver.socket.getsockname().AndReturn(None)
37 cherrypy.server.httpserver.socket.getsockname().AndReturn(('', 55555))
38
39 self.mox.ReplayAll()
40
41 plugin = cherrypy_ext.PortFile(bus, portfile)
42 plugin.start() # Signal server start; no socket binding yet.
43 with open(portfile) as f:
44 self.assertEqual('', f.read())
45 plugin.log('foo', 1) # Emit a log signal; socket "bound" at this point.
46 with open(portfile) as f:
47 self.assertEqual('55555', f.read())
48
49 self.mox.VerifyAll()
50
Amin Hassanic5af4262019-11-13 13:37:20 -080051 def testZeroPortPatcherSuccess(self):
52 """Make sure that ZeroPatcher successfully patches CherryPy.
53
54 This merely ensures that the patcher applies cleanly to the CherryPy
55 version available to the test environment, giving us some assurance that
56 it's still compatible with the range of versions that we might be using it
57 with. The actual testing of the arbitrary port binding feature is covered
58 by integration tests.
59 """
60 self.assertIsNone(cherrypy_ext.ZeroPortPatcher.DoPatch(cherrypy))
61
62 def testZeroPortPatcherFailure(self):
63 """Make sure that ZeroPatcher fails with an incompatible CherryPy.
64
65 This ensures that the patcher fails when applied to a CherryPy version that
66 does not have the desired properties.
67 """
68 module = cherrypy.process.servers
69 func_name = 'wait_for_free_port'
70 orig_func = getattr(module, func_name, None)
71 self.assertTrue(orig_func)
72 delattr(module, func_name)
73 self.assertRaises(AttributeError, cherrypy_ext.ZeroPortPatcher.DoPatch,
74 cherrypy)
75 setattr(module, func_name, orig_func)
76
77
Gilad Arnold7de05f72014-02-14 13:14:20 -080078if __name__ == '__main__':
79 unittest.main()