Mike Frysinger | a7f08bc | 2019-08-27 15:16:33 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
Gilad Arnold | 7de05f7 | 2014-02-14 13:14:20 -0800 | [diff] [blame] | 3 | # 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 Hassani | c5af426 | 2019-11-13 13:37:20 -0800 | [diff] [blame^] | 9 | from __future__ import print_function |
| 10 | |
Gilad Arnold | 7de05f7 | 2014-02-14 13:14:20 -0800 | [diff] [blame] | 11 | import tempfile |
| 12 | import unittest |
| 13 | |
Amin Hassani | c5af426 | 2019-11-13 13:37:20 -0800 | [diff] [blame^] | 14 | import mox # pylint: disable=import-error |
| 15 | import cherrypy # pylint: disable=import-error |
Gilad Arnold | 7de05f7 | 2014-02-14 13:14:20 -0800 | [diff] [blame] | 16 | |
| 17 | import cherrypy_ext |
| 18 | |
| 19 | |
| 20 | class 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 Hassani | c5af426 | 2019-11-13 13:37:20 -0800 | [diff] [blame^] | 51 | 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 Arnold | 7de05f7 | 2014-02-14 13:14:20 -0800 | [diff] [blame] | 78 | if __name__ == '__main__': |
| 79 | unittest.main() |